0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 23:33:15 -05:00

Display total commit count in hook message (#21400)

Fixes #21379

The commits are capped by `setting.UI.FeedMaxCommitNum` so
`len(commits)` is not the correct number. So this PR adds a new
`TotalCommits` field.

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
KN4CK3R 2022-10-16 18:22:34 +02:00 committed by GitHub
parent 6f48a36227
commit cad9adeff4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 74 additions and 68 deletions

View file

@ -645,15 +645,16 @@ func (m *webhookNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_
} }
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventPush, &api.PushPayload{ if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventPush, &api.PushPayload{
Ref: opts.RefFullName, Ref: opts.RefFullName,
Before: opts.OldCommitID, Before: opts.OldCommitID,
After: opts.NewCommitID, After: opts.NewCommitID,
CompareURL: setting.AppURL + commits.CompareURL, CompareURL: setting.AppURL + commits.CompareURL,
Commits: apiCommits, Commits: apiCommits,
HeadCommit: apiHeadCommit, TotalCommits: commits.Len,
Repo: convert.ToRepo(repo, perm.AccessModeOwner), HeadCommit: apiHeadCommit,
Pusher: apiPusher, Repo: convert.ToRepo(repo, perm.AccessModeOwner),
Sender: apiPusher, Pusher: apiPusher,
Sender: apiPusher,
}); err != nil { }); err != nil {
log.Error("PrepareWebhooks: %v", err) log.Error("PrepareWebhooks: %v", err)
} }
@ -875,15 +876,16 @@ func (m *webhookNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *r
} }
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventPush, &api.PushPayload{ if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventPush, &api.PushPayload{
Ref: opts.RefFullName, Ref: opts.RefFullName,
Before: opts.OldCommitID, Before: opts.OldCommitID,
After: opts.NewCommitID, After: opts.NewCommitID,
CompareURL: setting.AppURL + commits.CompareURL, CompareURL: setting.AppURL + commits.CompareURL,
Commits: apiCommits, Commits: apiCommits,
HeadCommit: apiHeadCommit, TotalCommits: commits.Len,
Repo: convert.ToRepo(repo, perm.AccessModeOwner), HeadCommit: apiHeadCommit,
Pusher: apiPusher, Repo: convert.ToRepo(repo, perm.AccessModeOwner),
Sender: apiPusher, Pusher: apiPusher,
Sender: apiPusher,
}); err != nil { }); err != nil {
log.Error("PrepareWebhooks: %v", err) log.Error("PrepareWebhooks: %v", err)
} }

View file

@ -267,15 +267,16 @@ func (p *ReleasePayload) JSONPayload() ([]byte, error) {
// PushPayload represents a payload information of push event. // PushPayload represents a payload information of push event.
type PushPayload struct { type PushPayload struct {
Ref string `json:"ref"` Ref string `json:"ref"`
Before string `json:"before"` Before string `json:"before"`
After string `json:"after"` After string `json:"after"`
CompareURL string `json:"compare_url"` CompareURL string `json:"compare_url"`
Commits []*PayloadCommit `json:"commits"` Commits []*PayloadCommit `json:"commits"`
HeadCommit *PayloadCommit `json:"head_commit"` TotalCommits int `json:"total_commits"`
Repo *Repository `json:"repository"` HeadCommit *PayloadCommit `json:"head_commit"`
Pusher *User `json:"pusher"` Repo *Repository `json:"repository"`
Sender *User `json:"sender"` Pusher *User `json:"pusher"`
Sender *User `json:"sender"`
} }
// JSONPayload FIXME // JSONPayload FIXME

View file

@ -169,15 +169,16 @@ func TestHook(ctx *context.APIContext) {
commitID := ctx.Repo.Commit.ID.String() commitID := ctx.Repo.Commit.ID.String()
if err := webhook_service.PrepareWebhook(hook, ctx.Repo.Repository, webhook.HookEventPush, &api.PushPayload{ if err := webhook_service.PrepareWebhook(hook, ctx.Repo.Repository, webhook.HookEventPush, &api.PushPayload{
Ref: ref, Ref: ref,
Before: commitID, Before: commitID,
After: commitID, After: commitID,
CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID), CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
Commits: []*api.PayloadCommit{commit}, Commits: []*api.PayloadCommit{commit},
HeadCommit: commit, TotalCommits: 1,
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone), HeadCommit: commit,
Pusher: convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone), Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
Sender: convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone), Pusher: convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
Sender: convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
}); err != nil { }); err != nil {
ctx.Error(http.StatusInternalServerError, "PrepareWebhook: ", err) ctx.Error(http.StatusInternalServerError, "PrepareWebhook: ", err)
return return

View file

@ -668,15 +668,16 @@ func TestWebhook(ctx *context.Context) {
commitID := commit.ID.String() commitID := commit.ID.String()
p := &api.PushPayload{ p := &api.PushPayload{
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch, Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
Before: commitID, Before: commitID,
After: commitID, After: commitID,
CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID), CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
Commits: []*api.PayloadCommit{apiCommit}, Commits: []*api.PayloadCommit{apiCommit},
HeadCommit: apiCommit, TotalCommits: 1,
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone), HeadCommit: apiCommit,
Pusher: apiUser, Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
Sender: apiUser, Pusher: apiUser,
Sender: apiUser,
} }
if err := webhook_service.PrepareWebhook(w, ctx.Repo.Repository, webhook.HookEventPush, p); err != nil { if err := webhook_service.PrepareWebhook(w, ctx.Repo.Repository, webhook.HookEventPush, p); err != nil {
ctx.Flash.Error("PrepareWebhook: " + err.Error()) ctx.Flash.Error("PrepareWebhook: " + err.Error())

View file

@ -67,14 +67,14 @@ func (d *DingtalkPayload) Push(p *api.PushPayload) (api.Payloader, error) {
) )
var titleLink, linkText string var titleLink, linkText string
if len(p.Commits) == 1 { if p.TotalCommits == 1 {
commitDesc = "1 new commit" commitDesc = "1 new commit"
titleLink = p.Commits[0].URL titleLink = p.Commits[0].URL
linkText = fmt.Sprintf("view commit %s", p.Commits[0].ID[:7]) linkText = "view commit"
} else { } else {
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits)) commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
titleLink = p.CompareURL 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 == "" { if titleLink == "" {
titleLink = p.Repo.HTMLURL + "/src/" + util.PathEscapeSegments(branchName) titleLink = p.Repo.HTMLURL + "/src/" + util.PathEscapeSegments(branchName)

View 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, "[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, "[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)) assert.Equal(t, "http://localhost:3000/test/repo/src/test", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
}) })

View file

@ -142,11 +142,11 @@ func (d *DiscordPayload) Push(p *api.PushPayload) (api.Payloader, error) {
) )
var titleLink string var titleLink string
if len(p.Commits) == 1 { if p.TotalCommits == 1 {
commitDesc = "1 new commit" commitDesc = "1 new commit"
titleLink = p.Commits[0].URL titleLink = p.Commits[0].URL
} else { } else {
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits)) commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
titleLink = p.CompareURL titleLink = p.CompareURL
} }
if titleLink == "" { if titleLink == "" {

View file

@ -82,12 +82,13 @@ func pushTestPayload() *api.PushPayload {
} }
return &api.PushPayload{ return &api.PushPayload{
Ref: "refs/heads/test", Ref: "refs/heads/test",
Before: "2020558fe2e34debb818a514715839cabd25e777", Before: "2020558fe2e34debb818a514715839cabd25e777",
After: "2020558fe2e34debb818a514715839cabd25e778", After: "2020558fe2e34debb818a514715839cabd25e778",
CompareURL: "", CompareURL: "",
HeadCommit: commit, HeadCommit: commit,
Commits: []*api.PayloadCommit{commit, commit}, Commits: []*api.PayloadCommit{commit, commit},
TotalCommits: 2,
Repo: &api.Repository{ Repo: &api.Repository{
HTMLURL: "http://localhost:3000/test/repo", HTMLURL: "http://localhost:3000/test/repo",
Name: "repo", Name: "repo",

View file

@ -161,10 +161,10 @@ func (m *MatrixPayloadUnsafe) Release(p *api.ReleasePayload) (api.Payloader, err
func (m *MatrixPayloadUnsafe) Push(p *api.PushPayload) (api.Payloader, error) { func (m *MatrixPayloadUnsafe) Push(p *api.PushPayload) (api.Payloader, error) {
var commitDesc string var commitDesc string
if len(p.Commits) == 1 { if p.TotalCommits == 1 {
commitDesc = "1 commit" commitDesc = "1 commit"
} else { } else {
commitDesc = fmt.Sprintf("%d commits", len(p.Commits)) commitDesc = fmt.Sprintf("%d commits", p.TotalCommits)
} }
repoLink := MatrixLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName) repoLink := MatrixLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)

View file

@ -125,11 +125,11 @@ func (m *MSTeamsPayload) Push(p *api.PushPayload) (api.Payloader, error) {
) )
var titleLink string var titleLink string
if len(p.Commits) == 1 { if p.TotalCommits == 1 {
commitDesc = "1 new commit" commitDesc = "1 new commit"
titleLink = p.Commits[0].URL titleLink = p.Commits[0].URL
} else { } else {
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits)) commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
titleLink = p.CompareURL titleLink = p.CompareURL
} }
if titleLink == "" { if titleLink == "" {
@ -156,7 +156,7 @@ func (m *MSTeamsPayload) Push(p *api.PushPayload) (api.Payloader, error) {
text, text,
titleLink, titleLink,
greenColor, greenColor,
&MSTeamsFact{"Commit count:", fmt.Sprintf("%d", len(p.Commits))}, &MSTeamsFact{"Commit count:", fmt.Sprintf("%d", p.TotalCommits)},
), nil ), nil
} }

View file

@ -179,10 +179,10 @@ func (s *SlackPayload) Push(p *api.PushPayload) (api.Payloader, error) {
commitString string commitString string
) )
if len(p.Commits) == 1 { if p.TotalCommits == 1 {
commitDesc = "1 new commit" commitDesc = "1 new commit"
} else { } else {
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits)) commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
} }
if len(p.CompareURL) > 0 { if len(p.CompareURL) > 0 {
commitString = SlackLinkFormatter(p.CompareURL, commitDesc) commitString = SlackLinkFormatter(p.CompareURL, commitDesc)

View file

@ -89,11 +89,11 @@ func (t *TelegramPayload) Push(p *api.PushPayload) (api.Payloader, error) {
) )
var titleLink string var titleLink string
if len(p.Commits) == 1 { if p.TotalCommits == 1 {
commitDesc = "1 new commit" commitDesc = "1 new commit"
titleLink = p.Commits[0].URL titleLink = p.Commits[0].URL
} else { } else {
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits)) commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
titleLink = p.CompareURL titleLink = p.CompareURL
} }
if titleLink == "" { if titleLink == "" {

View file

@ -93,7 +93,7 @@ func (f *WechatworkPayload) Push(p *api.PushPayload) (api.Payloader, error) {
for i, commit := range p.Commits { for i, commit := range p.Commits {
var authorName string var authorName string
if commit.Author != nil { if commit.Author != nil {
authorName = "Author" + commit.Author.Name authorName = "Author: " + commit.Author.Name
} }
message := strings.ReplaceAll(commit.Message, "\n\n", "\r\n") message := strings.ReplaceAll(commit.Message, "\n\n", "\r\n")