mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 19:38:23 +00: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:
		| @@ -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) | ||||
|   | ||||
| @@ -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)) | ||||
| 	}) | ||||
|  | ||||
|   | ||||
| @@ -142,11 +142,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 == "" { | ||||
|   | ||||
| @@ -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", | ||||
|   | ||||
| @@ -161,10 +161,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) | ||||
|   | ||||
| @@ -125,11 +125,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 == "" { | ||||
| @@ -156,7 +156,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 | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -179,10 +179,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) | ||||
|   | ||||
| @@ -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 == "" { | ||||
|   | ||||
| @@ -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") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user