2020-01-10 15:04:21 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-10 15:04:21 +05:30
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2022-01-20 04:56:57 +05:30
|
|
|
"context"
|
2020-01-10 15:04:21 +05:30
|
|
|
"fmt"
|
2021-11-16 23:48:25 +05:30
|
|
|
"net/url"
|
2020-01-10 15:04:21 +05:30
|
|
|
"time"
|
|
|
|
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 04:55:46 +05:30
|
|
|
"code.gitea.io/gitea/models/avatars"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-10 15:04:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-12-16 07:48:38 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-01-10 15:04:21 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PushCommit represents a commit in a push operation.
|
|
|
|
type PushCommit struct {
|
|
|
|
Sha1 string
|
|
|
|
Message string
|
|
|
|
AuthorEmail string
|
|
|
|
AuthorName string
|
|
|
|
CommitterEmail string
|
|
|
|
CommitterName string
|
|
|
|
Timestamp time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushCommits represents list of commits in a push operation.
|
|
|
|
type PushCommits struct {
|
|
|
|
Commits []*PushCommit
|
2021-06-29 19:04:03 +05:30
|
|
|
HeadCommit *PushCommit
|
2020-01-10 15:04:21 +05:30
|
|
|
CompareURL string
|
2021-08-26 04:34:58 +05:30
|
|
|
Len int
|
2020-01-10 15:04:21 +05:30
|
|
|
|
|
|
|
avatars map[string]string
|
2021-11-24 15:19:20 +05:30
|
|
|
emailUsers map[string]*user_model.User
|
2020-01-10 15:04:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// NewPushCommits creates a new PushCommits object.
|
|
|
|
func NewPushCommits() *PushCommits {
|
|
|
|
return &PushCommits{
|
|
|
|
avatars: make(map[string]string),
|
2021-11-24 15:19:20 +05:30
|
|
|
emailUsers: make(map[string]*user_model.User),
|
2020-01-10 15:04:21 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 19:04:03 +05:30
|
|
|
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
|
2022-01-20 04:56:57 +05:30
|
|
|
func (pc *PushCommits) toAPIPayloadCommit(ctx context.Context, repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) {
|
2020-01-10 15:04:21 +05:30
|
|
|
var err error
|
2021-06-29 19:04:03 +05:30
|
|
|
authorUsername := ""
|
|
|
|
author, ok := pc.emailUsers[commit.AuthorEmail]
|
|
|
|
if !ok {
|
2021-11-24 15:19:20 +05:30
|
|
|
author, err = user_model.GetUserByEmail(commit.AuthorEmail)
|
2021-06-29 19:04:03 +05:30
|
|
|
if err == nil {
|
2020-01-10 15:04:21 +05:30
|
|
|
authorUsername = author.Name
|
2021-06-29 19:04:03 +05:30
|
|
|
pc.emailUsers[commit.AuthorEmail] = author
|
2020-01-10 15:04:21 +05:30
|
|
|
}
|
2021-06-29 19:04:03 +05:30
|
|
|
} else {
|
|
|
|
authorUsername = author.Name
|
|
|
|
}
|
2020-01-10 15:04:21 +05:30
|
|
|
|
2021-06-29 19:04:03 +05:30
|
|
|
committerUsername := ""
|
|
|
|
committer, ok := pc.emailUsers[commit.CommitterEmail]
|
|
|
|
if !ok {
|
2021-11-24 15:19:20 +05:30
|
|
|
committer, err = user_model.GetUserByEmail(commit.CommitterEmail)
|
2021-06-29 19:04:03 +05:30
|
|
|
if err == nil {
|
|
|
|
// TODO: check errors other than email not found.
|
2020-01-10 15:04:21 +05:30
|
|
|
committerUsername = committer.Name
|
2021-06-29 19:04:03 +05:30
|
|
|
pc.emailUsers[commit.CommitterEmail] = committer
|
2020-01-10 15:04:21 +05:30
|
|
|
}
|
2021-06-29 19:04:03 +05:30
|
|
|
} else {
|
|
|
|
committerUsername = committer.Name
|
|
|
|
}
|
2020-01-10 15:04:21 +05:30
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
fileStatus, err := git.GetCommitFileStatus(ctx, repoPath, commit.Sha1)
|
2021-06-29 19:04:03 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %w", commit.Sha1, err)
|
2021-06-29 19:04:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return &api.PayloadCommit{
|
|
|
|
ID: commit.Sha1,
|
|
|
|
Message: commit.Message,
|
2021-11-16 23:48:25 +05:30
|
|
|
URL: fmt.Sprintf("%s/commit/%s", repoLink, url.PathEscape(commit.Sha1)),
|
2021-06-29 19:04:03 +05:30
|
|
|
Author: &api.PayloadUser{
|
|
|
|
Name: commit.AuthorName,
|
|
|
|
Email: commit.AuthorEmail,
|
|
|
|
UserName: authorUsername,
|
|
|
|
},
|
|
|
|
Committer: &api.PayloadUser{
|
|
|
|
Name: commit.CommitterName,
|
|
|
|
Email: commit.CommitterEmail,
|
|
|
|
UserName: committerUsername,
|
|
|
|
},
|
|
|
|
Added: fileStatus.Added,
|
|
|
|
Removed: fileStatus.Removed,
|
|
|
|
Modified: fileStatus.Modified,
|
|
|
|
Timestamp: commit.Timestamp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToAPIPayloadCommits converts a PushCommits object to api.PayloadCommit format.
|
|
|
|
// It returns all converted commits and, if provided, the head commit or an error otherwise.
|
2022-01-20 04:56:57 +05:30
|
|
|
func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLink string) ([]*api.PayloadCommit, *api.PayloadCommit, error) {
|
2021-06-29 19:04:03 +05:30
|
|
|
commits := make([]*api.PayloadCommit, len(pc.Commits))
|
|
|
|
var headCommit *api.PayloadCommit
|
|
|
|
|
|
|
|
if pc.emailUsers == nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
pc.emailUsers = make(map[string]*user_model.User)
|
2021-06-29 19:04:03 +05:30
|
|
|
}
|
|
|
|
for i, commit := range pc.Commits {
|
2022-01-20 04:56:57 +05:30
|
|
|
apiCommit, err := pc.toAPIPayloadCommit(ctx, repoPath, repoLink, commit)
|
2020-01-10 15:04:21 +05:30
|
|
|
if err != nil {
|
2021-06-29 19:04:03 +05:30
|
|
|
return nil, nil, err
|
2020-01-10 15:04:21 +05:30
|
|
|
}
|
|
|
|
|
2021-06-29 19:04:03 +05:30
|
|
|
commits[i] = apiCommit
|
|
|
|
if pc.HeadCommit != nil && pc.HeadCommit.Sha1 == commits[i].ID {
|
|
|
|
headCommit = apiCommit
|
2020-01-10 15:04:21 +05:30
|
|
|
}
|
|
|
|
}
|
2021-06-29 19:04:03 +05:30
|
|
|
if pc.HeadCommit != nil && headCommit == nil {
|
|
|
|
var err error
|
2022-01-20 04:56:57 +05:30
|
|
|
headCommit, err = pc.toAPIPayloadCommit(ctx, repoPath, repoLink, pc.HeadCommit)
|
2021-06-29 19:04:03 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return commits, headCommit, nil
|
2020-01-10 15:04:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// AvatarLink tries to match user in database with e-mail
|
|
|
|
// in order to show custom avatar, and falls back to general avatar link.
|
|
|
|
func (pc *PushCommits) AvatarLink(email string) string {
|
|
|
|
if pc.avatars == nil {
|
|
|
|
pc.avatars = make(map[string]string)
|
|
|
|
}
|
|
|
|
avatar, ok := pc.avatars[email]
|
|
|
|
if ok {
|
|
|
|
return avatar
|
|
|
|
}
|
|
|
|
|
2021-12-16 07:48:38 +05:30
|
|
|
size := avatars.DefaultAvatarPixelSize * setting.Avatar.RenderedSizeFactor
|
2020-12-09 05:42:15 +05:30
|
|
|
|
2020-01-10 15:04:21 +05:30
|
|
|
u, ok := pc.emailUsers[email]
|
|
|
|
if !ok {
|
|
|
|
var err error
|
2021-11-24 15:19:20 +05:30
|
|
|
u, err = user_model.GetUserByEmail(email)
|
2020-01-10 15:04:21 +05:30
|
|
|
if err != nil {
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 04:55:46 +05:30
|
|
|
pc.avatars[email] = avatars.GenerateEmailAvatarFastLink(email, size)
|
2021-11-24 15:19:20 +05:30
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2020-01-10 15:04:21 +05:30
|
|
|
log.Error("GetUserByEmail: %v", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pc.emailUsers[email] = u
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if u != nil {
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 04:55:46 +05:30
|
|
|
pc.avatars[email] = u.AvatarLinkWithSize(size)
|
2020-01-10 15:04:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return pc.avatars[email]
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitToPushCommit transforms a git.Commit to PushCommit type.
|
|
|
|
func CommitToPushCommit(commit *git.Commit) *PushCommit {
|
|
|
|
return &PushCommit{
|
|
|
|
Sha1: commit.ID.String(),
|
|
|
|
Message: commit.Message(),
|
|
|
|
AuthorEmail: commit.Author.Email,
|
|
|
|
AuthorName: commit.Author.Name,
|
|
|
|
CommitterEmail: commit.Committer.Email,
|
|
|
|
CommitterName: commit.Committer.Name,
|
|
|
|
Timestamp: commit.Author.When,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 23:38:51 +05:30
|
|
|
// GitToPushCommits transforms a list of git.Commits to PushCommits type.
|
|
|
|
func GitToPushCommits(gitCommits []*git.Commit) *PushCommits {
|
|
|
|
commits := make([]*PushCommit, 0, len(gitCommits))
|
|
|
|
for _, commit := range gitCommits {
|
|
|
|
commits = append(commits, CommitToPushCommit(commit))
|
2020-01-10 15:04:21 +05:30
|
|
|
}
|
2021-08-26 04:34:58 +05:30
|
|
|
return &PushCommits{
|
|
|
|
Commits: commits,
|
|
|
|
HeadCommit: nil,
|
|
|
|
CompareURL: "",
|
|
|
|
Len: len(commits),
|
|
|
|
avatars: make(map[string]string),
|
2021-11-24 15:19:20 +05:30
|
|
|
emailUsers: make(map[string]*user_model.User),
|
2021-08-26 04:34:58 +05:30
|
|
|
}
|
2020-01-10 15:04:21 +05:30
|
|
|
}
|