2020-01-10 02:34:21 -07:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
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-05 16:25:46 -07:00
|
|
|
"code.gitea.io/gitea/models/avatars"
|
2020-01-10 02:34:21 -07:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
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 06:34:03 -07:00
|
|
|
HeadCommit *PushCommit
|
2020-01-10 02:34:21 -07:00
|
|
|
CompareURL string
|
2021-08-25 16:04:58 -07:00
|
|
|
Len int
|
2020-01-10 02:34:21 -07:00
|
|
|
|
|
|
|
avatars map[string]string
|
|
|
|
emailUsers map[string]*models.User
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPushCommits creates a new PushCommits object.
|
|
|
|
func NewPushCommits() *PushCommits {
|
|
|
|
return &PushCommits{
|
|
|
|
avatars: make(map[string]string),
|
|
|
|
emailUsers: make(map[string]*models.User),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:34:03 -07:00
|
|
|
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
|
|
|
|
func (pc *PushCommits) toAPIPayloadCommit(repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) {
|
2020-01-10 02:34:21 -07:00
|
|
|
var err error
|
2021-06-29 06:34:03 -07:00
|
|
|
authorUsername := ""
|
|
|
|
author, ok := pc.emailUsers[commit.AuthorEmail]
|
|
|
|
if !ok {
|
|
|
|
author, err = models.GetUserByEmail(commit.AuthorEmail)
|
|
|
|
if err == nil {
|
2020-01-10 02:34:21 -07:00
|
|
|
authorUsername = author.Name
|
2021-06-29 06:34:03 -07:00
|
|
|
pc.emailUsers[commit.AuthorEmail] = author
|
2020-01-10 02:34:21 -07:00
|
|
|
}
|
2021-06-29 06:34:03 -07:00
|
|
|
} else {
|
|
|
|
authorUsername = author.Name
|
|
|
|
}
|
2020-01-10 02:34:21 -07:00
|
|
|
|
2021-06-29 06:34:03 -07:00
|
|
|
committerUsername := ""
|
|
|
|
committer, ok := pc.emailUsers[commit.CommitterEmail]
|
|
|
|
if !ok {
|
|
|
|
committer, err = models.GetUserByEmail(commit.CommitterEmail)
|
|
|
|
if err == nil {
|
|
|
|
// TODO: check errors other than email not found.
|
2020-01-10 02:34:21 -07:00
|
|
|
committerUsername = committer.Name
|
2021-06-29 06:34:03 -07:00
|
|
|
pc.emailUsers[commit.CommitterEmail] = committer
|
2020-01-10 02:34:21 -07:00
|
|
|
}
|
2021-06-29 06:34:03 -07:00
|
|
|
} else {
|
|
|
|
committerUsername = committer.Name
|
|
|
|
}
|
2020-01-10 02:34:21 -07:00
|
|
|
|
2021-06-29 06:34:03 -07:00
|
|
|
fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.PayloadCommit{
|
|
|
|
ID: commit.Sha1,
|
|
|
|
Message: commit.Message,
|
|
|
|
URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1),
|
|
|
|
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.
|
|
|
|
func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, *api.PayloadCommit, error) {
|
|
|
|
commits := make([]*api.PayloadCommit, len(pc.Commits))
|
|
|
|
var headCommit *api.PayloadCommit
|
|
|
|
|
|
|
|
if pc.emailUsers == nil {
|
|
|
|
pc.emailUsers = make(map[string]*models.User)
|
|
|
|
}
|
|
|
|
for i, commit := range pc.Commits {
|
|
|
|
apiCommit, err := pc.toAPIPayloadCommit(repoPath, repoLink, commit)
|
2020-01-10 02:34:21 -07:00
|
|
|
if err != nil {
|
2021-06-29 06:34:03 -07:00
|
|
|
return nil, nil, err
|
2020-01-10 02:34:21 -07:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:34:03 -07:00
|
|
|
commits[i] = apiCommit
|
|
|
|
if pc.HeadCommit != nil && pc.HeadCommit.Sha1 == commits[i].ID {
|
|
|
|
headCommit = apiCommit
|
2020-01-10 02:34:21 -07:00
|
|
|
}
|
|
|
|
}
|
2021-06-29 06:34:03 -07:00
|
|
|
if pc.HeadCommit != nil && headCommit == nil {
|
|
|
|
var err error
|
|
|
|
headCommit, err = pc.toAPIPayloadCommit(repoPath, repoLink, pc.HeadCommit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return commits, headCommit, nil
|
2020-01-10 02:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
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-05 16:25:46 -07:00
|
|
|
size := avatars.DefaultAvatarPixelSize * avatars.AvatarRenderedSizeFactor
|
2020-12-08 17:12:15 -07:00
|
|
|
|
2020-01-10 02:34:21 -07:00
|
|
|
u, ok := pc.emailUsers[email]
|
|
|
|
if !ok {
|
|
|
|
var err error
|
|
|
|
u, err = models.GetUserByEmail(email)
|
|
|
|
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-05 16:25:46 -07:00
|
|
|
pc.avatars[email] = avatars.GenerateEmailAvatarFastLink(email, size)
|
2020-01-10 02:34:21 -07:00
|
|
|
if !models.IsErrUserNotExist(err) {
|
|
|
|
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-05 16:25:46 -07:00
|
|
|
pc.avatars[email] = u.AvatarLinkWithSize(size)
|
2020-01-10 02:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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 11:08:51 -07:00
|
|
|
// 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 02:34:21 -07:00
|
|
|
}
|
2021-08-25 16:04:58 -07:00
|
|
|
return &PushCommits{
|
|
|
|
Commits: commits,
|
|
|
|
HeadCommit: nil,
|
|
|
|
CompareURL: "",
|
|
|
|
Len: len(commits),
|
|
|
|
avatars: make(map[string]string),
|
|
|
|
emailUsers: make(map[string]*models.User),
|
|
|
|
}
|
2020-01-10 02:34:21 -07:00
|
|
|
}
|