Skip to content

Commit 5a84558

Browse files
KN4CK3Rwxiaoguanglunny
authored
Display total commit count in hook message (#21400) (#21481)
Backport of #21400 Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 46053c0 commit 5a84558

File tree

13 files changed

+74
-68
lines changed

13 files changed

+74
-68
lines changed

modules/notification/webhook/webhook.go

+20-18
Original file line numberDiff line numberDiff line change
@@ -608,15 +608,16 @@ func (m *webhookNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_
608608
}
609609

610610
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventPush, &api.PushPayload{
611-
Ref: opts.RefFullName,
612-
Before: opts.OldCommitID,
613-
After: opts.NewCommitID,
614-
CompareURL: setting.AppURL + commits.CompareURL,
615-
Commits: apiCommits,
616-
HeadCommit: apiHeadCommit,
617-
Repo: convert.ToRepo(repo, perm.AccessModeOwner),
618-
Pusher: apiPusher,
619-
Sender: apiPusher,
611+
Ref: opts.RefFullName,
612+
Before: opts.OldCommitID,
613+
After: opts.NewCommitID,
614+
CompareURL: setting.AppURL + commits.CompareURL,
615+
Commits: apiCommits,
616+
TotalCommits: commits.Len,
617+
HeadCommit: apiHeadCommit,
618+
Repo: convert.ToRepo(repo, perm.AccessModeOwner),
619+
Pusher: apiPusher,
620+
Sender: apiPusher,
620621
}); err != nil {
621622
log.Error("PrepareWebhooks: %v", err)
622623
}
@@ -838,15 +839,16 @@ func (m *webhookNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *r
838839
}
839840

840841
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventPush, &api.PushPayload{
841-
Ref: opts.RefFullName,
842-
Before: opts.OldCommitID,
843-
After: opts.NewCommitID,
844-
CompareURL: setting.AppURL + commits.CompareURL,
845-
Commits: apiCommits,
846-
HeadCommit: apiHeadCommit,
847-
Repo: convert.ToRepo(repo, perm.AccessModeOwner),
848-
Pusher: apiPusher,
849-
Sender: apiPusher,
842+
Ref: opts.RefFullName,
843+
Before: opts.OldCommitID,
844+
After: opts.NewCommitID,
845+
CompareURL: setting.AppURL + commits.CompareURL,
846+
Commits: apiCommits,
847+
TotalCommits: commits.Len,
848+
HeadCommit: apiHeadCommit,
849+
Repo: convert.ToRepo(repo, perm.AccessModeOwner),
850+
Pusher: apiPusher,
851+
Sender: apiPusher,
850852
}); err != nil {
851853
log.Error("PrepareWebhooks: %v", err)
852854
}

modules/structs/hook.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,16 @@ func (p *ReleasePayload) JSONPayload() ([]byte, error) {
267267

268268
// PushPayload represents a payload information of push event.
269269
type PushPayload struct {
270-
Ref string `json:"ref"`
271-
Before string `json:"before"`
272-
After string `json:"after"`
273-
CompareURL string `json:"compare_url"`
274-
Commits []*PayloadCommit `json:"commits"`
275-
HeadCommit *PayloadCommit `json:"head_commit"`
276-
Repo *Repository `json:"repository"`
277-
Pusher *User `json:"pusher"`
278-
Sender *User `json:"sender"`
270+
Ref string `json:"ref"`
271+
Before string `json:"before"`
272+
After string `json:"after"`
273+
CompareURL string `json:"compare_url"`
274+
Commits []*PayloadCommit `json:"commits"`
275+
TotalCommits int `json:"total_commits"`
276+
HeadCommit *PayloadCommit `json:"head_commit"`
277+
Repo *Repository `json:"repository"`
278+
Pusher *User `json:"pusher"`
279+
Sender *User `json:"sender"`
279280
}
280281

281282
// JSONPayload FIXME

routers/api/v1/repo/hook.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,16 @@ func TestHook(ctx *context.APIContext) {
169169

170170
commitID := ctx.Repo.Commit.ID.String()
171171
if err := webhook_service.PrepareWebhook(hook, ctx.Repo.Repository, webhook.HookEventPush, &api.PushPayload{
172-
Ref: ref,
173-
Before: commitID,
174-
After: commitID,
175-
CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
176-
Commits: []*api.PayloadCommit{commit},
177-
HeadCommit: commit,
178-
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
179-
Pusher: convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
180-
Sender: convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
172+
Ref: ref,
173+
Before: commitID,
174+
After: commitID,
175+
CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
176+
Commits: []*api.PayloadCommit{commit},
177+
TotalCommits: 1,
178+
HeadCommit: commit,
179+
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
180+
Pusher: convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
181+
Sender: convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
181182
}); err != nil {
182183
ctx.Error(http.StatusInternalServerError, "PrepareWebhook: ", err)
183184
return

routers/web/repo/webhook.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -1273,15 +1273,16 @@ func TestWebhook(ctx *context.Context) {
12731273

12741274
commitID := commit.ID.String()
12751275
p := &api.PushPayload{
1276-
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
1277-
Before: commitID,
1278-
After: commitID,
1279-
CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
1280-
Commits: []*api.PayloadCommit{apiCommit},
1281-
HeadCommit: apiCommit,
1282-
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
1283-
Pusher: apiUser,
1284-
Sender: apiUser,
1276+
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
1277+
Before: commitID,
1278+
After: commitID,
1279+
CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
1280+
Commits: []*api.PayloadCommit{apiCommit},
1281+
TotalCommits: 1,
1282+
HeadCommit: apiCommit,
1283+
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
1284+
Pusher: apiUser,
1285+
Sender: apiUser,
12851286
}
12861287
if err := webhook_service.PrepareWebhook(w, ctx.Repo.Repository, webhook.HookEventPush, p); err != nil {
12871288
ctx.Flash.Error("PrepareWebhook: " + err.Error())

services/webhook/dingtalk.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ func (d *DingtalkPayload) Push(p *api.PushPayload) (api.Payloader, error) {
6767
)
6868

6969
var titleLink, linkText string
70-
if len(p.Commits) == 1 {
70+
if p.TotalCommits == 1 {
7171
commitDesc = "1 new commit"
7272
titleLink = p.Commits[0].URL
73-
linkText = fmt.Sprintf("view commit %s", p.Commits[0].ID[:7])
73+
linkText = "view commit"
7474
} else {
75-
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
75+
commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
7676
titleLink = p.CompareURL
77-
linkText = fmt.Sprintf("view commit %s...%s", p.Commits[0].ID[:7], p.Commits[len(p.Commits)-1].ID[:7])
77+
linkText = "view commits"
7878
}
7979
if titleLink == "" {
8080
titleLink = p.Repo.HTMLURL + "/src/" + util.PathEscapeSegments(branchName)

services/webhook/dingtalk_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestDingTalkPayload(t *testing.T) {
8282

8383
assert.Equal(t, "[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1\r\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1", pl.(*DingtalkPayload).ActionCard.Text)
8484
assert.Equal(t, "[test/repo:test] 2 new commits", pl.(*DingtalkPayload).ActionCard.Title)
85-
assert.Equal(t, "view commit 2020558...2020558", pl.(*DingtalkPayload).ActionCard.SingleTitle)
85+
assert.Equal(t, "view commits", pl.(*DingtalkPayload).ActionCard.SingleTitle)
8686
assert.Equal(t, "http://localhost:3000/test/repo/src/test", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
8787
})
8888

services/webhook/discord.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ func (d *DiscordPayload) Push(p *api.PushPayload) (api.Payloader, error) {
141141
)
142142

143143
var titleLink string
144-
if len(p.Commits) == 1 {
144+
if p.TotalCommits == 1 {
145145
commitDesc = "1 new commit"
146146
titleLink = p.Commits[0].URL
147147
} else {
148-
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
148+
commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
149149
titleLink = p.CompareURL
150150
}
151151
if titleLink == "" {

services/webhook/general_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ func pushTestPayload() *api.PushPayload {
8282
}
8383

8484
return &api.PushPayload{
85-
Ref: "refs/heads/test",
86-
Before: "2020558fe2e34debb818a514715839cabd25e777",
87-
After: "2020558fe2e34debb818a514715839cabd25e778",
88-
CompareURL: "",
89-
HeadCommit: commit,
90-
Commits: []*api.PayloadCommit{commit, commit},
85+
Ref: "refs/heads/test",
86+
Before: "2020558fe2e34debb818a514715839cabd25e777",
87+
After: "2020558fe2e34debb818a514715839cabd25e778",
88+
CompareURL: "",
89+
HeadCommit: commit,
90+
Commits: []*api.PayloadCommit{commit, commit},
91+
TotalCommits: 2,
9192
Repo: &api.Repository{
9293
HTMLURL: "http://localhost:3000/test/repo",
9394
Name: "repo",

services/webhook/matrix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ func (m *MatrixPayloadUnsafe) Release(p *api.ReleasePayload) (api.Payloader, err
154154
func (m *MatrixPayloadUnsafe) Push(p *api.PushPayload) (api.Payloader, error) {
155155
var commitDesc string
156156

157-
if len(p.Commits) == 1 {
157+
if p.TotalCommits == 1 {
158158
commitDesc = "1 commit"
159159
} else {
160-
commitDesc = fmt.Sprintf("%d commits", len(p.Commits))
160+
commitDesc = fmt.Sprintf("%d commits", p.TotalCommits)
161161
}
162162

163163
repoLink := MatrixLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)

services/webhook/msteams.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ func (m *MSTeamsPayload) Push(p *api.PushPayload) (api.Payloader, error) {
124124
)
125125

126126
var titleLink string
127-
if len(p.Commits) == 1 {
127+
if p.TotalCommits == 1 {
128128
commitDesc = "1 new commit"
129129
titleLink = p.Commits[0].URL
130130
} else {
131-
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
131+
commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
132132
titleLink = p.CompareURL
133133
}
134134
if titleLink == "" {
@@ -155,7 +155,7 @@ func (m *MSTeamsPayload) Push(p *api.PushPayload) (api.Payloader, error) {
155155
text,
156156
titleLink,
157157
greenColor,
158-
&MSTeamsFact{"Commit count:", fmt.Sprintf("%d", len(p.Commits))},
158+
&MSTeamsFact{"Commit count:", fmt.Sprintf("%d", p.TotalCommits)},
159159
), nil
160160
}
161161

services/webhook/slack.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ func (s *SlackPayload) Push(p *api.PushPayload) (api.Payloader, error) {
171171
commitString string
172172
)
173173

174-
if len(p.Commits) == 1 {
174+
if p.TotalCommits == 1 {
175175
commitDesc = "1 new commit"
176176
} else {
177-
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
177+
commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
178178
}
179179
if len(p.CompareURL) > 0 {
180180
commitString = SlackLinkFormatter(p.CompareURL, commitDesc)

services/webhook/telegram.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ func (t *TelegramPayload) Push(p *api.PushPayload) (api.Payloader, error) {
8989
)
9090

9191
var titleLink string
92-
if len(p.Commits) == 1 {
92+
if p.TotalCommits == 1 {
9393
commitDesc = "1 new commit"
9494
titleLink = p.Commits[0].URL
9595
} else {
96-
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
96+
commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
9797
titleLink = p.CompareURL
9898
}
9999
if titleLink == "" {

services/webhook/wechatwork.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (f *WechatworkPayload) Push(p *api.PushPayload) (api.Payloader, error) {
9393
for i, commit := range p.Commits {
9494
var authorName string
9595
if commit.Author != nil {
96-
authorName = "Author" + commit.Author.Name
96+
authorName = "Author: " + commit.Author.Name
9797
}
9898

9999
message := strings.ReplaceAll(commit.Message, "\n\n", "\r\n")

0 commit comments

Comments
 (0)