]> source.dussan.org Git - gitea.git/commitdiff
Display total commit count in hook message (#21400) (#21481)
authorKN4CK3R <admin@oldschoolhack.me>
Mon, 17 Oct 2022 14:37:44 +0000 (16:37 +0200)
committerGitHub <noreply@github.com>
Mon, 17 Oct 2022 14:37:44 +0000 (10:37 -0400)
Backport of #21400

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
13 files changed:
modules/notification/webhook/webhook.go
modules/structs/hook.go
routers/api/v1/repo/hook.go
routers/web/repo/webhook.go
services/webhook/dingtalk.go
services/webhook/dingtalk_test.go
services/webhook/discord.go
services/webhook/general_test.go
services/webhook/matrix.go
services/webhook/msteams.go
services/webhook/slack.go
services/webhook/telegram.go
services/webhook/wechatwork.go

index be71d18fdad3d7e0a67ff42a301497cbd1718808..e61929b4cf2779c59cc5ef1d2b8c29902c38e9a9 100644 (file)
@@ -608,15 +608,16 @@ func (m *webhookNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_
        }
 
        if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventPush, &api.PushPayload{
-               Ref:        opts.RefFullName,
-               Before:     opts.OldCommitID,
-               After:      opts.NewCommitID,
-               CompareURL: setting.AppURL + commits.CompareURL,
-               Commits:    apiCommits,
-               HeadCommit: apiHeadCommit,
-               Repo:       convert.ToRepo(repo, perm.AccessModeOwner),
-               Pusher:     apiPusher,
-               Sender:     apiPusher,
+               Ref:          opts.RefFullName,
+               Before:       opts.OldCommitID,
+               After:        opts.NewCommitID,
+               CompareURL:   setting.AppURL + commits.CompareURL,
+               Commits:      apiCommits,
+               TotalCommits: commits.Len,
+               HeadCommit:   apiHeadCommit,
+               Repo:         convert.ToRepo(repo, perm.AccessModeOwner),
+               Pusher:       apiPusher,
+               Sender:       apiPusher,
        }); err != nil {
                log.Error("PrepareWebhooks: %v", err)
        }
@@ -838,15 +839,16 @@ func (m *webhookNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *r
        }
 
        if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventPush, &api.PushPayload{
-               Ref:        opts.RefFullName,
-               Before:     opts.OldCommitID,
-               After:      opts.NewCommitID,
-               CompareURL: setting.AppURL + commits.CompareURL,
-               Commits:    apiCommits,
-               HeadCommit: apiHeadCommit,
-               Repo:       convert.ToRepo(repo, perm.AccessModeOwner),
-               Pusher:     apiPusher,
-               Sender:     apiPusher,
+               Ref:          opts.RefFullName,
+               Before:       opts.OldCommitID,
+               After:        opts.NewCommitID,
+               CompareURL:   setting.AppURL + commits.CompareURL,
+               Commits:      apiCommits,
+               TotalCommits: commits.Len,
+               HeadCommit:   apiHeadCommit,
+               Repo:         convert.ToRepo(repo, perm.AccessModeOwner),
+               Pusher:       apiPusher,
+               Sender:       apiPusher,
        }); err != nil {
                log.Error("PrepareWebhooks: %v", err)
        }
index 07d51915dea9f62ed1222b5a83b9cbafbe443654..4d186c2ca2ca0af1b68d379845134e095b302df7 100644 (file)
@@ -267,15 +267,16 @@ func (p *ReleasePayload) JSONPayload() ([]byte, error) {
 
 // PushPayload represents a payload information of push event.
 type PushPayload struct {
-       Ref        string           `json:"ref"`
-       Before     string           `json:"before"`
-       After      string           `json:"after"`
-       CompareURL string           `json:"compare_url"`
-       Commits    []*PayloadCommit `json:"commits"`
-       HeadCommit *PayloadCommit   `json:"head_commit"`
-       Repo       *Repository      `json:"repository"`
-       Pusher     *User            `json:"pusher"`
-       Sender     *User            `json:"sender"`
+       Ref          string           `json:"ref"`
+       Before       string           `json:"before"`
+       After        string           `json:"after"`
+       CompareURL   string           `json:"compare_url"`
+       Commits      []*PayloadCommit `json:"commits"`
+       TotalCommits int              `json:"total_commits"`
+       HeadCommit   *PayloadCommit   `json:"head_commit"`
+       Repo         *Repository      `json:"repository"`
+       Pusher       *User            `json:"pusher"`
+       Sender       *User            `json:"sender"`
 }
 
 // JSONPayload FIXME
index b17142c0f647fc1c71a5600c3505b5f517b89748..86361817cb4642873b959406bd3dae4ad315fbee 100644 (file)
@@ -169,15 +169,16 @@ func TestHook(ctx *context.APIContext) {
 
        commitID := ctx.Repo.Commit.ID.String()
        if err := webhook_service.PrepareWebhook(hook, ctx.Repo.Repository, webhook.HookEventPush, &api.PushPayload{
-               Ref:        ref,
-               Before:     commitID,
-               After:      commitID,
-               CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
-               Commits:    []*api.PayloadCommit{commit},
-               HeadCommit: commit,
-               Repo:       convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
-               Pusher:     convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
-               Sender:     convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
+               Ref:          ref,
+               Before:       commitID,
+               After:        commitID,
+               CompareURL:   setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
+               Commits:      []*api.PayloadCommit{commit},
+               TotalCommits: 1,
+               HeadCommit:   commit,
+               Repo:         convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
+               Pusher:       convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
+               Sender:       convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
        }); err != nil {
                ctx.Error(http.StatusInternalServerError, "PrepareWebhook: ", err)
                return
index 2b9f78ab018e024256273df02f991f532989666d..78e2b65a96de9168c448349a0767510865d3aead 100644 (file)
@@ -1273,15 +1273,16 @@ func TestWebhook(ctx *context.Context) {
 
        commitID := commit.ID.String()
        p := &api.PushPayload{
-               Ref:        git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
-               Before:     commitID,
-               After:      commitID,
-               CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
-               Commits:    []*api.PayloadCommit{apiCommit},
-               HeadCommit: apiCommit,
-               Repo:       convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
-               Pusher:     apiUser,
-               Sender:     apiUser,
+               Ref:          git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
+               Before:       commitID,
+               After:        commitID,
+               CompareURL:   setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
+               Commits:      []*api.PayloadCommit{apiCommit},
+               TotalCommits: 1,
+               HeadCommit:   apiCommit,
+               Repo:         convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
+               Pusher:       apiUser,
+               Sender:       apiUser,
        }
        if err := webhook_service.PrepareWebhook(w, ctx.Repo.Repository, webhook.HookEventPush, p); err != nil {
                ctx.Flash.Error("PrepareWebhook: " + err.Error())
index 642cf6f2fda161c0e815e34dfc84dcb0fb5da2d7..0fc61ce8c13941a66ef11e0583128c65f11e26fd 100644 (file)
@@ -67,14 +67,14 @@ func (d *DingtalkPayload) Push(p *api.PushPayload) (api.Payloader, error) {
        )
 
        var titleLink, linkText string
-       if len(p.Commits) == 1 {
+       if p.TotalCommits == 1 {
                commitDesc = "1 new commit"
                titleLink = p.Commits[0].URL
-               linkText = fmt.Sprintf("view commit %s", p.Commits[0].ID[:7])
+               linkText = "view commit"
        } else {
-               commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
+               commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
                titleLink = p.CompareURL
-               linkText = fmt.Sprintf("view commit %s...%s", p.Commits[0].ID[:7], p.Commits[len(p.Commits)-1].ID[:7])
+               linkText = "view commits"
        }
        if titleLink == "" {
                titleLink = p.Repo.HTMLURL + "/src/" + util.PathEscapeSegments(branchName)
index b66b5e43a824d1acba51b861dc5deff5c42438ac..e411043364ede1fbd6bcd890963a8b14397c5800 100644 (file)
@@ -82,7 +82,7 @@ func TestDingTalkPayload(t *testing.T) {
 
                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)
                assert.Equal(t, "[test/repo:test] 2 new commits", pl.(*DingtalkPayload).ActionCard.Title)
-               assert.Equal(t, "view commit 2020558...2020558", pl.(*DingtalkPayload).ActionCard.SingleTitle)
+               assert.Equal(t, "view commits", pl.(*DingtalkPayload).ActionCard.SingleTitle)
                assert.Equal(t, "http://localhost:3000/test/repo/src/test", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
        })
 
index ae5460b9a7a1b54f65fe8edb21cb1a1ff0e94b76..f65a8648c435ac0ef956aa950b7aec9c03fd1bf6 100644 (file)
@@ -141,11 +141,11 @@ func (d *DiscordPayload) Push(p *api.PushPayload) (api.Payloader, error) {
        )
 
        var titleLink string
-       if len(p.Commits) == 1 {
+       if p.TotalCommits == 1 {
                commitDesc = "1 new commit"
                titleLink = p.Commits[0].URL
        } else {
-               commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
+               commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
                titleLink = p.CompareURL
        }
        if titleLink == "" {
index 4d73afe060a6818f63cd6bc99867d3c0c4956f48..a2606e4aca734e4411f13be92010ad4631cb1e69 100644 (file)
@@ -82,12 +82,13 @@ func pushTestPayload() *api.PushPayload {
        }
 
        return &api.PushPayload{
-               Ref:        "refs/heads/test",
-               Before:     "2020558fe2e34debb818a514715839cabd25e777",
-               After:      "2020558fe2e34debb818a514715839cabd25e778",
-               CompareURL: "",
-               HeadCommit: commit,
-               Commits:    []*api.PayloadCommit{commit, commit},
+               Ref:          "refs/heads/test",
+               Before:       "2020558fe2e34debb818a514715839cabd25e777",
+               After:        "2020558fe2e34debb818a514715839cabd25e778",
+               CompareURL:   "",
+               HeadCommit:   commit,
+               Commits:      []*api.PayloadCommit{commit, commit},
+               TotalCommits: 2,
                Repo: &api.Repository{
                        HTMLURL:  "http://localhost:3000/test/repo",
                        Name:     "repo",
index a42ab2a93e0633f1edcf08973dfeb9b86c6badd5..37608f22bef2d7dd3f6ec4605b863405707e1035 100644 (file)
@@ -154,10 +154,10 @@ func (m *MatrixPayloadUnsafe) Release(p *api.ReleasePayload) (api.Payloader, err
 func (m *MatrixPayloadUnsafe) Push(p *api.PushPayload) (api.Payloader, error) {
        var commitDesc string
 
-       if len(p.Commits) == 1 {
+       if p.TotalCommits == 1 {
                commitDesc = "1 commit"
        } else {
-               commitDesc = fmt.Sprintf("%d commits", len(p.Commits))
+               commitDesc = fmt.Sprintf("%d commits", p.TotalCommits)
        }
 
        repoLink := MatrixLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
index 59e2e934939829f57a8b9bf8574b7b9399b29be6..856e31b4195bb456eb98d9944a1f89bb249bbe57 100644 (file)
@@ -124,11 +124,11 @@ func (m *MSTeamsPayload) Push(p *api.PushPayload) (api.Payloader, error) {
        )
 
        var titleLink string
-       if len(p.Commits) == 1 {
+       if p.TotalCommits == 1 {
                commitDesc = "1 new commit"
                titleLink = p.Commits[0].URL
        } else {
-               commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
+               commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
                titleLink = p.CompareURL
        }
        if titleLink == "" {
@@ -155,7 +155,7 @@ func (m *MSTeamsPayload) Push(p *api.PushPayload) (api.Payloader, error) {
                text,
                titleLink,
                greenColor,
-               &MSTeamsFact{"Commit count:", fmt.Sprintf("%d", len(p.Commits))},
+               &MSTeamsFact{"Commit count:", fmt.Sprintf("%d", p.TotalCommits)},
        ), nil
 }
 
index 11e1d3c081c28cac597cb22c1bd8d2892aa99a83..0975bba1edcc8191eed349ccc04332f83e75c2f1 100644 (file)
@@ -171,10 +171,10 @@ func (s *SlackPayload) Push(p *api.PushPayload) (api.Payloader, error) {
                commitString string
        )
 
-       if len(p.Commits) == 1 {
+       if p.TotalCommits == 1 {
                commitDesc = "1 new commit"
        } else {
-               commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
+               commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
        }
        if len(p.CompareURL) > 0 {
                commitString = SlackLinkFormatter(p.CompareURL, commitDesc)
index 64211493ec62c60603656e3db4fd08fae265a9a1..97b8a4636e56e630e5717871d93cb0ef9898bec0 100644 (file)
@@ -89,11 +89,11 @@ func (t *TelegramPayload) Push(p *api.PushPayload) (api.Payloader, error) {
        )
 
        var titleLink string
-       if len(p.Commits) == 1 {
+       if p.TotalCommits == 1 {
                commitDesc = "1 new commit"
                titleLink = p.Commits[0].URL
        } else {
-               commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
+               commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
                titleLink = p.CompareURL
        }
        if titleLink == "" {
index de8b777066576931c7f889f9a776a04f21ac4ba5..29d866695a4fb4088992224137902d8c5a22fbc4 100644 (file)
@@ -93,7 +93,7 @@ func (f *WechatworkPayload) Push(p *api.PushPayload) (api.Payloader, error) {
        for i, commit := range p.Commits {
                var authorName string
                if commit.Author != nil {
-                       authorName = "Author" + commit.Author.Name
+                       authorName = "Author" + commit.Author.Name
                }
 
                message := strings.ReplaceAll(commit.Message, "\n\n", "\r\n")