mirror of
https://github.com/go-gitea/gitea.git
synced 2026-07-07 04:56:50 +02:00
feat(webhook): add reviewer name to MS Teams review request notifications (#38289)
Include the requested reviewer's username (along with their full name in parentheses, if available) and render the `Repository` and `Pull request` fields as clickable links in Microsoft Teams webhook notifications. Fixes: https://github.com/go-gitea/gitea/issues/38270 ## Screenshots <img width="1246" height="651" alt="image" src="https://github.com/user-attachments/assets/7299ce10-c6d4-4c89-a05a-a258d72c00e5" /> --------- Signed-off-by: Shudhanshu Singh <sudhanshuwriterblc@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
parent
a46e331637
commit
582217a0da
@ -187,6 +187,18 @@ func (m msteamsConvertor) IssueComment(p *api.IssueCommentPayload) (MSTeamsPaylo
|
||||
func (m msteamsConvertor) PullRequest(p *api.PullRequestPayload) (MSTeamsPayload, error) {
|
||||
title, _, extraMarkdown, color := getPullRequestPayloadInfo(p, noneLinkFormatter, false)
|
||||
|
||||
facts := []*MSTeamsFact{
|
||||
{"Pull request:", fmt.Sprintf("[#%d](%s)", p.PullRequest.Index, p.PullRequest.HTMLURL)},
|
||||
}
|
||||
|
||||
if (p.Action == api.HookIssueReviewRequested || p.Action == api.HookIssueReviewRequestRemoved) && p.RequestedReviewer != nil {
|
||||
reviewerName := p.RequestedReviewer.UserName
|
||||
if p.RequestedReviewer.FullName != "" {
|
||||
reviewerName += " (" + p.RequestedReviewer.FullName + ")"
|
||||
}
|
||||
facts = append(facts, &MSTeamsFact{"Requested Reviewer:", reviewerName})
|
||||
}
|
||||
|
||||
return createMSTeamsPayload(
|
||||
p.Repository,
|
||||
p.Sender,
|
||||
@ -194,7 +206,7 @@ func (m msteamsConvertor) PullRequest(p *api.PullRequestPayload) (MSTeamsPayload
|
||||
extraMarkdown,
|
||||
p.PullRequest.HTMLURL,
|
||||
color,
|
||||
&MSTeamsFact{"Pull request #:", strconv.FormatInt(p.PullRequest.ID, 10)},
|
||||
facts...,
|
||||
), nil
|
||||
}
|
||||
|
||||
@ -231,7 +243,7 @@ func (m msteamsConvertor) Review(p *api.PullRequestPayload, event webhook_module
|
||||
text,
|
||||
p.PullRequest.HTMLURL,
|
||||
color,
|
||||
&MSTeamsFact{"Pull request #:", strconv.FormatInt(p.PullRequest.ID, 10)},
|
||||
&MSTeamsFact{"Pull request:", fmt.Sprintf("[#%d](%s)", p.PullRequest.Index, p.PullRequest.HTMLURL)},
|
||||
), nil
|
||||
}
|
||||
|
||||
@ -271,7 +283,6 @@ func (m msteamsConvertor) Wiki(p *api.WikiPayload) (MSTeamsPayload, error) {
|
||||
"",
|
||||
p.Repository.HTMLURL+"/wiki/"+url.PathEscape(p.Page),
|
||||
color,
|
||||
&MSTeamsFact{"Repository:", p.Repository.FullName},
|
||||
), nil
|
||||
}
|
||||
|
||||
@ -346,16 +357,18 @@ func (msteamsConvertor) WorkflowJob(p *api.WorkflowJobPayload) (MSTeamsPayload,
|
||||
), nil
|
||||
}
|
||||
|
||||
func createMSTeamsPayload(r *api.Repository, s *api.User, title, text, actionTarget string, color int, fact *MSTeamsFact) MSTeamsPayload {
|
||||
facts := make([]MSTeamsFact, 0, 2)
|
||||
func createMSTeamsPayload(r *api.Repository, s *api.User, title, text, actionTarget string, color int, extraFacts ...*MSTeamsFact) MSTeamsPayload {
|
||||
facts := make([]MSTeamsFact, 0, len(extraFacts)+1)
|
||||
if r != nil {
|
||||
facts = append(facts, MSTeamsFact{
|
||||
Name: "Repository:",
|
||||
Value: r.FullName,
|
||||
Value: fmt.Sprintf("[%s](%s)", r.FullName, r.HTMLURL),
|
||||
})
|
||||
}
|
||||
if fact != nil {
|
||||
facts = append(facts, *fact)
|
||||
for _, f := range extraFacts {
|
||||
if f != nil {
|
||||
facts = append(facts, *f)
|
||||
}
|
||||
}
|
||||
|
||||
return MSTeamsPayload{
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
package webhook
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
webhook_model "gitea.dev/models/webhook"
|
||||
@ -31,7 +32,7 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repo.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repo.FullName, p.Repo.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "branch:" {
|
||||
assert.Equal(t, "test", fact.Value)
|
||||
} else {
|
||||
@ -57,7 +58,7 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repo.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repo.FullName, p.Repo.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "branch:" {
|
||||
assert.Equal(t, "test", fact.Value)
|
||||
} else {
|
||||
@ -83,7 +84,7 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repo.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repo.FullName, p.Repo.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Forkee:" {
|
||||
assert.Equal(t, p.Forkee.FullName, fact.Value)
|
||||
} else {
|
||||
@ -109,7 +110,7 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repo.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repo.FullName, p.Repo.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Commit count:" {
|
||||
assert.Equal(t, "2", fact.Value)
|
||||
} else {
|
||||
@ -136,7 +137,7 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Issue #:" {
|
||||
assert.Equal(t, "2", fact.Value)
|
||||
} else {
|
||||
@ -159,7 +160,7 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Issue #:" {
|
||||
assert.Equal(t, "2", fact.Value)
|
||||
} else {
|
||||
@ -185,7 +186,7 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Issue #:" {
|
||||
assert.Equal(t, "2", fact.Value)
|
||||
} else {
|
||||
@ -211,9 +212,9 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
} else if fact.Name == "Pull request #:" {
|
||||
assert.Equal(t, "12", fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Pull request:" {
|
||||
assert.Equal(t, fmt.Sprintf("[#%d](%s)", p.PullRequest.Index, p.PullRequest.HTMLURL), fact.Value)
|
||||
} else {
|
||||
t.Fail()
|
||||
}
|
||||
@ -223,6 +224,34 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Equal(t, "http://localhost:3000/test/repo/pulls/12", pl.PotentialAction[0].Targets[0].URI)
|
||||
})
|
||||
|
||||
t.Run("PullRequestReviewRequest", func(t *testing.T) {
|
||||
p := pullRequestTestPayload()
|
||||
p.Action = api.HookIssueReviewRequested
|
||||
p.RequestedReviewer = &api.User{
|
||||
UserName: "reviewer1",
|
||||
FullName: "Reviewer One",
|
||||
}
|
||||
|
||||
pl, err := mc.PullRequest(p)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Len(t, pl.Sections[0].Facts, 3)
|
||||
var hasReviewer bool
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Pull request:" {
|
||||
assert.Equal(t, fmt.Sprintf("[#%d](%s)", p.PullRequest.Index, p.PullRequest.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Requested Reviewer:" {
|
||||
assert.Equal(t, "reviewer1 (Reviewer One)", fact.Value)
|
||||
hasReviewer = true
|
||||
} else {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
assert.True(t, hasReviewer)
|
||||
})
|
||||
|
||||
t.Run("PullRequestComment", func(t *testing.T) {
|
||||
p := pullRequestCommentTestPayload()
|
||||
|
||||
@ -237,7 +266,7 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Issue #:" {
|
||||
assert.Equal(t, "12", fact.Value)
|
||||
} else {
|
||||
@ -264,9 +293,9 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
} else if fact.Name == "Pull request #:" {
|
||||
assert.Equal(t, "12", fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Pull request:" {
|
||||
assert.Equal(t, fmt.Sprintf("[#%d](%s)", p.PullRequest.Index, p.PullRequest.HTMLURL), fact.Value)
|
||||
} else {
|
||||
t.Fail()
|
||||
}
|
||||
@ -290,7 +319,7 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 1)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else {
|
||||
t.Fail()
|
||||
}
|
||||
@ -336,17 +365,14 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections, 1)
|
||||
assert.Equal(t, "user1", pl.Sections[0].ActivitySubtitle)
|
||||
assert.Empty(t, pl.Sections[0].Text)
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
assert.Len(t, pl.Sections[0].Facts, 1)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
assert.Len(t, pl.PotentialAction, 1)
|
||||
assert.Len(t, pl.PotentialAction[0].Targets, 1)
|
||||
assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.PotentialAction[0].Targets[0].URI)
|
||||
|
||||
p.Action = api.HookWikiEdited
|
||||
pl, err = mc.Wiki(p)
|
||||
@ -357,10 +383,10 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections, 1)
|
||||
assert.Equal(t, "user1", pl.Sections[0].ActivitySubtitle)
|
||||
assert.Empty(t, pl.Sections[0].Text)
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
assert.Len(t, pl.Sections[0].Facts, 1)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else {
|
||||
t.Fail()
|
||||
}
|
||||
@ -378,10 +404,10 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections, 1)
|
||||
assert.Equal(t, "user1", pl.Sections[0].ActivitySubtitle)
|
||||
assert.Empty(t, pl.Sections[0].Text)
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
assert.Len(t, pl.Sections[0].Facts, 1)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else {
|
||||
t.Fail()
|
||||
}
|
||||
@ -405,7 +431,7 @@ func TestMSTeamsPayload(t *testing.T) {
|
||||
assert.Len(t, pl.Sections[0].Facts, 2)
|
||||
for _, fact := range pl.Sections[0].Facts {
|
||||
if fact.Name == "Repository:" {
|
||||
assert.Equal(t, p.Repository.FullName, fact.Value)
|
||||
assert.Equal(t, fmt.Sprintf("[%s](%s)", p.Repository.FullName, p.Repository.HTMLURL), fact.Value)
|
||||
} else if fact.Name == "Tag:" {
|
||||
assert.Equal(t, "v1.0", fact.Value)
|
||||
} else {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user