|
|
@ -28,8 +28,8 @@ type PushCommit struct { |
|
|
|
|
|
|
|
|
|
|
|
// PushCommits represents list of commits in a push operation.
|
|
|
|
// PushCommits represents list of commits in a push operation.
|
|
|
|
type PushCommits struct { |
|
|
|
type PushCommits struct { |
|
|
|
Len int |
|
|
|
|
|
|
|
Commits []*PushCommit |
|
|
|
Commits []*PushCommit |
|
|
|
|
|
|
|
HeadCommit *PushCommit |
|
|
|
CompareURL string |
|
|
|
CompareURL string |
|
|
|
|
|
|
|
|
|
|
|
avatars map[string]string |
|
|
|
avatars map[string]string |
|
|
@ -44,67 +44,88 @@ func NewPushCommits() *PushCommits { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ToAPIPayloadCommits converts a PushCommits object to
|
|
|
|
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
|
|
|
|
// api.PayloadCommit format.
|
|
|
|
func (pc *PushCommits) toAPIPayloadCommit(repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) { |
|
|
|
func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, error) { |
|
|
|
|
|
|
|
commits := make([]*api.PayloadCommit, len(pc.Commits)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if pc.emailUsers == nil { |
|
|
|
|
|
|
|
pc.emailUsers = make(map[string]*models.User) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var err error |
|
|
|
var err error |
|
|
|
for i, commit := range pc.Commits { |
|
|
|
authorUsername := "" |
|
|
|
authorUsername := "" |
|
|
|
author, ok := pc.emailUsers[commit.AuthorEmail] |
|
|
|
author, ok := pc.emailUsers[commit.AuthorEmail] |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
author, err = models.GetUserByEmail(commit.AuthorEmail) |
|
|
|
author, err = models.GetUserByEmail(commit.AuthorEmail) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
|
|
|
|
authorUsername = author.Name |
|
|
|
|
|
|
|
pc.emailUsers[commit.AuthorEmail] = author |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
authorUsername = author.Name |
|
|
|
authorUsername = author.Name |
|
|
|
|
|
|
|
pc.emailUsers[commit.AuthorEmail] = author |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
authorUsername = author.Name |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
committerUsername := "" |
|
|
|
committerUsername := "" |
|
|
|
committer, ok := pc.emailUsers[commit.CommitterEmail] |
|
|
|
committer, ok := pc.emailUsers[commit.CommitterEmail] |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
committer, err = models.GetUserByEmail(commit.CommitterEmail) |
|
|
|
committer, err = models.GetUserByEmail(commit.CommitterEmail) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
// TODO: check errors other than email not found.
|
|
|
|
// TODO: check errors other than email not found.
|
|
|
|
committerUsername = committer.Name |
|
|
|
|
|
|
|
pc.emailUsers[commit.CommitterEmail] = committer |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
committerUsername = committer.Name |
|
|
|
committerUsername = committer.Name |
|
|
|
|
|
|
|
pc.emailUsers[commit.CommitterEmail] = committer |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
committerUsername = committer.Name |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1) |
|
|
|
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) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err) |
|
|
|
return nil, nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
commits[i] = &api.PayloadCommit{ |
|
|
|
commits[i] = apiCommit |
|
|
|
ID: commit.Sha1, |
|
|
|
if pc.HeadCommit != nil && pc.HeadCommit.Sha1 == commits[i].ID { |
|
|
|
Message: commit.Message, |
|
|
|
headCommit = apiCommit |
|
|
|
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, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return commits, nil |
|
|
|
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 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AvatarLink tries to match user in database with e-mail
|
|
|
|
// AvatarLink tries to match user in database with e-mail
|
|
|
@ -157,13 +178,9 @@ func CommitToPushCommit(commit *git.Commit) *PushCommit { |
|
|
|
// ListToPushCommits transforms a list.List to PushCommits type.
|
|
|
|
// ListToPushCommits transforms a list.List to PushCommits type.
|
|
|
|
func ListToPushCommits(l *list.List) *PushCommits { |
|
|
|
func ListToPushCommits(l *list.List) *PushCommits { |
|
|
|
var commits []*PushCommit |
|
|
|
var commits []*PushCommit |
|
|
|
var actEmail string |
|
|
|
|
|
|
|
for e := l.Front(); e != nil; e = e.Next() { |
|
|
|
for e := l.Front(); e != nil; e = e.Next() { |
|
|
|
commit := e.Value.(*git.Commit) |
|
|
|
commit := CommitToPushCommit(e.Value.(*git.Commit)) |
|
|
|
if actEmail == "" { |
|
|
|
commits = append(commits, commit) |
|
|
|
actEmail = commit.Committer.Email |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
commits = append(commits, CommitToPushCommit(commit)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return &PushCommits{l.Len(), commits, "", make(map[string]string), make(map[string]*models.User)} |
|
|
|
return &PushCommits{commits, nil, "", make(map[string]string), make(map[string]*models.User)} |
|
|
|
} |
|
|
|
} |
|
|
|