Skip to content

Commit 71d3aa8

Browse files
authored
Merge branch 'main' into add-emoji-codeberg
2 parents 38dd164 + aac663e commit 71d3aa8

File tree

7 files changed

+128
-96
lines changed

7 files changed

+128
-96
lines changed

modules/notification/webhook/webhook.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m
562562

563563
func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
564564
apiPusher := convert.ToUser(pusher, nil)
565-
apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
565+
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
566566
if err != nil {
567567
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
568568
return
@@ -574,6 +574,7 @@ func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Re
574574
After: opts.NewCommitID,
575575
CompareURL: setting.AppURL + commits.CompareURL,
576576
Commits: apiCommits,
577+
HeadCommit: apiHeadCommit,
577578
Repo: convert.ToRepo(repo, models.AccessModeOwner),
578579
Pusher: apiPusher,
579580
Sender: apiPusher,
@@ -790,7 +791,7 @@ func (m *webhookNotifier) NotifyDeleteRelease(doer *models.User, rel *models.Rel
790791

791792
func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
792793
apiPusher := convert.ToUser(pusher, nil)
793-
apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
794+
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
794795
if err != nil {
795796
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
796797
return
@@ -802,6 +803,7 @@ func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *model
802803
After: opts.NewCommitID,
803804
CompareURL: setting.AppURL + commits.CompareURL,
804805
Commits: apiCommits,
806+
HeadCommit: apiHeadCommit,
805807
Repo: convert.ToRepo(repo, models.AccessModeOwner),
806808
Pusher: apiPusher,
807809
Sender: apiPusher,

modules/repository/commits.go

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ type PushCommit struct {
2828

2929
// PushCommits represents list of commits in a push operation.
3030
type PushCommits struct {
31-
Len int
3231
Commits []*PushCommit
32+
HeadCommit *PushCommit
3333
CompareURL string
3434

3535
avatars map[string]string
@@ -44,67 +44,88 @@ func NewPushCommits() *PushCommits {
4444
}
4545
}
4646

47-
// ToAPIPayloadCommits converts a PushCommits object to
48-
// api.PayloadCommit format.
49-
func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, error) {
50-
commits := make([]*api.PayloadCommit, len(pc.Commits))
51-
52-
if pc.emailUsers == nil {
53-
pc.emailUsers = make(map[string]*models.User)
54-
}
47+
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
48+
func (pc *PushCommits) toAPIPayloadCommit(repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) {
5549
var err error
56-
for i, commit := range pc.Commits {
57-
authorUsername := ""
58-
author, ok := pc.emailUsers[commit.AuthorEmail]
59-
if !ok {
60-
author, err = models.GetUserByEmail(commit.AuthorEmail)
61-
if err == nil {
62-
authorUsername = author.Name
63-
pc.emailUsers[commit.AuthorEmail] = author
64-
}
65-
} else {
50+
authorUsername := ""
51+
author, ok := pc.emailUsers[commit.AuthorEmail]
52+
if !ok {
53+
author, err = models.GetUserByEmail(commit.AuthorEmail)
54+
if err == nil {
6655
authorUsername = author.Name
56+
pc.emailUsers[commit.AuthorEmail] = author
6757
}
58+
} else {
59+
authorUsername = author.Name
60+
}
6861

69-
committerUsername := ""
70-
committer, ok := pc.emailUsers[commit.CommitterEmail]
71-
if !ok {
72-
committer, err = models.GetUserByEmail(commit.CommitterEmail)
73-
if err == nil {
74-
// TODO: check errors other than email not found.
75-
committerUsername = committer.Name
76-
pc.emailUsers[commit.CommitterEmail] = committer
77-
}
78-
} else {
62+
committerUsername := ""
63+
committer, ok := pc.emailUsers[commit.CommitterEmail]
64+
if !ok {
65+
committer, err = models.GetUserByEmail(commit.CommitterEmail)
66+
if err == nil {
67+
// TODO: check errors other than email not found.
7968
committerUsername = committer.Name
69+
pc.emailUsers[commit.CommitterEmail] = committer
8070
}
71+
} else {
72+
committerUsername = committer.Name
73+
}
8174

82-
fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1)
75+
fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1)
76+
if err != nil {
77+
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err)
78+
}
79+
80+
return &api.PayloadCommit{
81+
ID: commit.Sha1,
82+
Message: commit.Message,
83+
URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1),
84+
Author: &api.PayloadUser{
85+
Name: commit.AuthorName,
86+
Email: commit.AuthorEmail,
87+
UserName: authorUsername,
88+
},
89+
Committer: &api.PayloadUser{
90+
Name: commit.CommitterName,
91+
Email: commit.CommitterEmail,
92+
UserName: committerUsername,
93+
},
94+
Added: fileStatus.Added,
95+
Removed: fileStatus.Removed,
96+
Modified: fileStatus.Modified,
97+
Timestamp: commit.Timestamp,
98+
}, nil
99+
}
100+
101+
// ToAPIPayloadCommits converts a PushCommits object to api.PayloadCommit format.
102+
// It returns all converted commits and, if provided, the head commit or an error otherwise.
103+
func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, *api.PayloadCommit, error) {
104+
commits := make([]*api.PayloadCommit, len(pc.Commits))
105+
var headCommit *api.PayloadCommit
106+
107+
if pc.emailUsers == nil {
108+
pc.emailUsers = make(map[string]*models.User)
109+
}
110+
for i, commit := range pc.Commits {
111+
apiCommit, err := pc.toAPIPayloadCommit(repoPath, repoLink, commit)
83112
if err != nil {
84-
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err)
113+
return nil, nil, err
85114
}
86115

87-
commits[i] = &api.PayloadCommit{
88-
ID: commit.Sha1,
89-
Message: commit.Message,
90-
URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1),
91-
Author: &api.PayloadUser{
92-
Name: commit.AuthorName,
93-
Email: commit.AuthorEmail,
94-
UserName: authorUsername,
95-
},
96-
Committer: &api.PayloadUser{
97-
Name: commit.CommitterName,
98-
Email: commit.CommitterEmail,
99-
UserName: committerUsername,
100-
},
101-
Added: fileStatus.Added,
102-
Removed: fileStatus.Removed,
103-
Modified: fileStatus.Modified,
104-
Timestamp: commit.Timestamp,
116+
commits[i] = apiCommit
117+
if pc.HeadCommit != nil && pc.HeadCommit.Sha1 == commits[i].ID {
118+
headCommit = apiCommit
105119
}
106120
}
107-
return commits, nil
121+
if pc.HeadCommit != nil && headCommit == nil {
122+
var err error
123+
headCommit, err = pc.toAPIPayloadCommit(repoPath, repoLink, pc.HeadCommit)
124+
if err != nil {
125+
return nil, nil, err
126+
}
127+
}
128+
return commits, headCommit, nil
108129
}
109130

110131
// AvatarLink tries to match user in database with e-mail
@@ -157,13 +178,9 @@ func CommitToPushCommit(commit *git.Commit) *PushCommit {
157178
// ListToPushCommits transforms a list.List to PushCommits type.
158179
func ListToPushCommits(l *list.List) *PushCommits {
159180
var commits []*PushCommit
160-
var actEmail string
161181
for e := l.Front(); e != nil; e = e.Next() {
162-
commit := e.Value.(*git.Commit)
163-
if actEmail == "" {
164-
actEmail = commit.Committer.Email
165-
}
166-
commits = append(commits, CommitToPushCommit(commit))
182+
commit := CommitToPushCommit(e.Value.(*git.Commit))
183+
commits = append(commits, commit)
167184
}
168-
return &PushCommits{l.Len(), commits, "", make(map[string]string), make(map[string]*models.User)}
185+
return &PushCommits{commits, nil, "", make(map[string]string), make(map[string]*models.User)}
169186
}

modules/repository/commits_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
4646
Message: "good signed commit",
4747
},
4848
}
49-
pushCommits.Len = len(pushCommits.Commits)
49+
pushCommits.HeadCommit = &PushCommit{Sha1: "69554a6"}
5050

5151
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository)
52-
payloadCommits, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16")
52+
payloadCommits, headCommit, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16")
5353
assert.NoError(t, err)
5454
assert.Len(t, payloadCommits, 3)
55+
assert.NotNil(t, headCommit)
5556

5657
assert.Equal(t, "69554a6", payloadCommits[0].ID)
5758
assert.Equal(t, "not signed commit", payloadCommits[0].Message)
@@ -85,6 +86,17 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
8586
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[2].Added)
8687
assert.EqualValues(t, []string{}, payloadCommits[2].Removed)
8788
assert.EqualValues(t, []string{}, payloadCommits[2].Modified)
89+
90+
assert.Equal(t, "69554a6", headCommit.ID)
91+
assert.Equal(t, "not signed commit", headCommit.Message)
92+
assert.Equal(t, "/user2/repo16/commit/69554a6", headCommit.URL)
93+
assert.Equal(t, "User2", headCommit.Committer.Name)
94+
assert.Equal(t, "user2", headCommit.Committer.UserName)
95+
assert.Equal(t, "User2", headCommit.Author.Name)
96+
assert.Equal(t, "user2", headCommit.Author.UserName)
97+
assert.EqualValues(t, []string{}, headCommit.Added)
98+
assert.EqualValues(t, []string{}, headCommit.Removed)
99+
assert.EqualValues(t, []string{"readme.md"}, headCommit.Modified)
88100
}
89101

90102
func TestPushCommits_AvatarLink(t *testing.T) {
@@ -109,7 +121,6 @@ func TestPushCommits_AvatarLink(t *testing.T) {
109121
Message: "message2",
110122
},
111123
}
112-
pushCommits.Len = len(pushCommits.Commits)
113124

114125
assert.Equal(t,
115126
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon&s=112",
@@ -177,7 +188,6 @@ func TestListToPushCommits(t *testing.T) {
177188
})
178189

179190
pushCommits := ListToPushCommits(l)
180-
assert.Equal(t, 2, pushCommits.Len)
181191
if assert.Len(t, pushCommits.Commits, 2) {
182192
assert.Equal(t, "Message1", pushCommits.Commits[0].Message)
183193
assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1)

routers/api/v1/repo/hook.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,17 @@ func TestHook(ctx *context.APIContext) {
140140
return
141141
}
142142

143+
commit := convert.ToPayloadCommit(ctx.Repo.Repository, ctx.Repo.Commit)
144+
143145
if err := webhook.PrepareWebhook(hook, ctx.Repo.Repository, models.HookEventPush, &api.PushPayload{
144-
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
145-
Before: ctx.Repo.Commit.ID.String(),
146-
After: ctx.Repo.Commit.ID.String(),
147-
Commits: []*api.PayloadCommit{
148-
convert.ToPayloadCommit(ctx.Repo.Repository, ctx.Repo.Commit),
149-
},
150-
Repo: convert.ToRepo(ctx.Repo.Repository, models.AccessModeNone),
151-
Pusher: convert.ToUserWithAccessMode(ctx.User, models.AccessModeNone),
152-
Sender: convert.ToUserWithAccessMode(ctx.User, models.AccessModeNone),
146+
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
147+
Before: ctx.Repo.Commit.ID.String(),
148+
After: ctx.Repo.Commit.ID.String(),
149+
Commits: []*api.PayloadCommit{commit},
150+
HeadCommit: commit,
151+
Repo: convert.ToRepo(ctx.Repo.Repository, models.AccessModeNone),
152+
Pusher: convert.ToUserWithAccessMode(ctx.User, models.AccessModeNone),
153+
Sender: convert.ToUserWithAccessMode(ctx.User, models.AccessModeNone),
153154
}); err != nil {
154155
ctx.Error(http.StatusInternalServerError, "PrepareWebhook: ", err)
155156
return

routers/web/repo/webhook.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,28 +1085,30 @@ func TestWebhook(ctx *context.Context) {
10851085
}
10861086

10871087
apiUser := convert.ToUserWithAccessMode(ctx.User, models.AccessModeNone)
1088-
p := &api.PushPayload{
1089-
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
1090-
Before: commit.ID.String(),
1091-
After: commit.ID.String(),
1092-
Commits: []*api.PayloadCommit{
1093-
{
1094-
ID: commit.ID.String(),
1095-
Message: commit.Message(),
1096-
URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
1097-
Author: &api.PayloadUser{
1098-
Name: commit.Author.Name,
1099-
Email: commit.Author.Email,
1100-
},
1101-
Committer: &api.PayloadUser{
1102-
Name: commit.Committer.Name,
1103-
Email: commit.Committer.Email,
1104-
},
1105-
},
1088+
1089+
apiCommit := &api.PayloadCommit{
1090+
ID: commit.ID.String(),
1091+
Message: commit.Message(),
1092+
URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
1093+
Author: &api.PayloadUser{
1094+
Name: commit.Author.Name,
1095+
Email: commit.Author.Email,
1096+
},
1097+
Committer: &api.PayloadUser{
1098+
Name: commit.Committer.Name,
1099+
Email: commit.Committer.Email,
11061100
},
1107-
Repo: convert.ToRepo(ctx.Repo.Repository, models.AccessModeNone),
1108-
Pusher: apiUser,
1109-
Sender: apiUser,
1101+
}
1102+
1103+
p := &api.PushPayload{
1104+
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
1105+
Before: commit.ID.String(),
1106+
After: commit.ID.String(),
1107+
Commits: []*api.PayloadCommit{apiCommit},
1108+
HeadCommit: apiCommit,
1109+
Repo: convert.ToRepo(ctx.Repo.Repository, models.AccessModeNone),
1110+
Pusher: apiUser,
1111+
Sender: apiUser,
11101112
}
11111113
if err := webhook.PrepareWebhook(w, ctx.Repo.Repository, models.HookEventPush, p); err != nil {
11121114
ctx.Flash.Error("PrepareWebhook: " + err.Error())

services/repository/push.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
9595
if opts.IsNewRef() && opts.IsDelRef() {
9696
return fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
9797
}
98-
var commits = &repo_module.PushCommits{}
9998
if opts.IsTag() { // If is tag reference
10099
if pusher == nil || pusher.ID != opts.PusherID {
101100
var err error
@@ -192,7 +191,8 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
192191
}
193192
}
194193

195-
commits = repo_module.ListToPushCommits(l)
194+
commits := repo_module.ListToPushCommits(l)
195+
commits.HeadCommit = repo_module.CommitToPushCommit(newCommit)
196196

197197
if err := repofiles.UpdateIssuesCommit(pusher, repo, commits.Commits, refName); err != nil {
198198
log.Error("updateIssuesCommit: %v", err)

templates/user/dashboard/feeds.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
</li>
102102
{{end}}
103103
{{end}}
104-
{{if and (gt $push.Len 1) $push.CompareURL}}<li><a href="{{AppSubUrl}}/{{$push.CompareURL}}">{{$.i18n.Tr "action.compare_commits" $push.Len}} »</a></li>{{end}}
104+
{{if and (gt (len $push.Commits) 1) $push.CompareURL}}<li><a href="{{AppSubUrl}}/{{$push.CompareURL}}">{{$.i18n.Tr "action.compare_commits" (len $push.Commits)}} »</a></li>{{end}}
105105
</ul>
106106
</div>
107107
{{else if eq .GetOpType 6}}

0 commit comments

Comments
 (0)