forked from forgejo/forgejo
Refactor webhooks to reduce code duplication (#9422)
* Start webhook refactoring to reduce code duplication * More webhook refactoring * Unify webhook release messages * Fix webhook release link * Remove sql import * More webhook refactoring * More webhook refactoring * Webhook tests extended * Fixed issue opened webhook Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: techknowlogick <matti@mdranta.net>
This commit is contained in:
parent
10455a88dc
commit
81e63d0714
10 changed files with 534 additions and 698 deletions
|
@ -266,60 +266,20 @@ func getMSTeamsPushPayload(p *api.PushPayload) (*MSTeamsPayload, error) {
|
|||
}
|
||||
|
||||
func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) {
|
||||
var text, title string
|
||||
var color int
|
||||
url := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
|
||||
switch p.Action {
|
||||
case api.HookIssueOpened:
|
||||
title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
|
||||
text = p.Issue.Body
|
||||
color = orangeColor
|
||||
case api.HookIssueClosed:
|
||||
title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
|
||||
color = redColor
|
||||
case api.HookIssueReOpened:
|
||||
title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueEdited:
|
||||
title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
|
||||
text = p.Issue.Body
|
||||
color = yellowColor
|
||||
case api.HookIssueAssigned:
|
||||
title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName,
|
||||
p.Issue.Assignee.UserName, p.Index, p.Issue.Title)
|
||||
color = greenColor
|
||||
case api.HookIssueUnassigned:
|
||||
title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueLabelUpdated:
|
||||
title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueLabelCleared:
|
||||
title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueSynchronized:
|
||||
title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueMilestoned:
|
||||
title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueDemilestoned:
|
||||
title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
|
||||
color = yellowColor
|
||||
}
|
||||
text, _, attachmentText, color := getIssuesPayloadInfo(p, noneLinkFormatter)
|
||||
|
||||
return &MSTeamsPayload{
|
||||
Type: "MessageCard",
|
||||
Context: "https://schema.org/extensions",
|
||||
ThemeColor: fmt.Sprintf("%x", color),
|
||||
Title: title,
|
||||
Summary: title,
|
||||
Title: text,
|
||||
Summary: text,
|
||||
Sections: []MSTeamsSection{
|
||||
{
|
||||
ActivityTitle: p.Sender.FullName,
|
||||
ActivitySubtitle: p.Sender.UserName,
|
||||
ActivityImage: p.Sender.AvatarURL,
|
||||
Text: text,
|
||||
Text: attachmentText,
|
||||
Facts: []MSTeamsFact{
|
||||
{
|
||||
Name: "Repository:",
|
||||
|
@ -339,7 +299,7 @@ func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) {
|
|||
Targets: []MSTeamsActionTarget{
|
||||
{
|
||||
Os: "default",
|
||||
URI: url,
|
||||
URI: p.Issue.URL,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -348,53 +308,20 @@ func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) {
|
|||
}
|
||||
|
||||
func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload, error) {
|
||||
title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title)
|
||||
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID))
|
||||
content := ""
|
||||
var color int
|
||||
switch p.Action {
|
||||
case api.HookIssueCommentCreated:
|
||||
if p.IsPull {
|
||||
title = "New comment on pull request " + title
|
||||
color = greenColorLight
|
||||
} else {
|
||||
title = "New comment on issue " + title
|
||||
color = orangeColorLight
|
||||
}
|
||||
content = p.Comment.Body
|
||||
case api.HookIssueCommentEdited:
|
||||
if p.IsPull {
|
||||
title = "Comment edited on pull request " + title
|
||||
} else {
|
||||
title = "Comment edited on issue " + title
|
||||
}
|
||||
content = p.Comment.Body
|
||||
color = yellowColor
|
||||
case api.HookIssueCommentDeleted:
|
||||
if p.IsPull {
|
||||
title = "Comment deleted on pull request " + title
|
||||
} else {
|
||||
title = "Comment deleted on issue " + title
|
||||
}
|
||||
url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
|
||||
content = p.Comment.Body
|
||||
color = redColor
|
||||
}
|
||||
|
||||
title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title)
|
||||
text, _, color := getIssueCommentPayloadInfo(p, noneLinkFormatter)
|
||||
|
||||
return &MSTeamsPayload{
|
||||
Type: "MessageCard",
|
||||
Context: "https://schema.org/extensions",
|
||||
ThemeColor: fmt.Sprintf("%x", color),
|
||||
Title: title,
|
||||
Summary: title,
|
||||
Title: text,
|
||||
Summary: text,
|
||||
Sections: []MSTeamsSection{
|
||||
{
|
||||
ActivityTitle: p.Sender.FullName,
|
||||
ActivitySubtitle: p.Sender.UserName,
|
||||
ActivityImage: p.Sender.AvatarURL,
|
||||
Text: content,
|
||||
Text: p.Comment.Body,
|
||||
Facts: []MSTeamsFact{
|
||||
{
|
||||
Name: "Repository:",
|
||||
|
@ -414,7 +341,7 @@ func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload,
|
|||
Targets: []MSTeamsActionTarget{
|
||||
{
|
||||
Os: "default",
|
||||
URI: url,
|
||||
URI: p.Comment.HTMLURL,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -423,69 +350,20 @@ func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload,
|
|||
}
|
||||
|
||||
func getMSTeamsPullRequestPayload(p *api.PullRequestPayload) (*MSTeamsPayload, error) {
|
||||
var text, title string
|
||||
var color int
|
||||
switch p.Action {
|
||||
case api.HookIssueOpened:
|
||||
title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
text = p.PullRequest.Body
|
||||
color = greenColor
|
||||
case api.HookIssueClosed:
|
||||
if p.PullRequest.HasMerged {
|
||||
title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
color = purpleColor
|
||||
} else {
|
||||
title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
color = redColor
|
||||
}
|
||||
case api.HookIssueReOpened:
|
||||
title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueEdited:
|
||||
title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
text = p.PullRequest.Body
|
||||
color = yellowColor
|
||||
case api.HookIssueAssigned:
|
||||
list := make([]string, len(p.PullRequest.Assignees))
|
||||
for i, user := range p.PullRequest.Assignees {
|
||||
list[i] = user.UserName
|
||||
}
|
||||
title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d by %s", p.Repository.FullName,
|
||||
strings.Join(list, ", "),
|
||||
p.Index, p.PullRequest.Title)
|
||||
color = greenColor
|
||||
case api.HookIssueUnassigned:
|
||||
title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueLabelUpdated:
|
||||
title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueLabelCleared:
|
||||
title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueSynchronized:
|
||||
title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueMilestoned:
|
||||
title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
color = yellowColor
|
||||
case api.HookIssueDemilestoned:
|
||||
title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
|
||||
color = yellowColor
|
||||
}
|
||||
text, _, attachmentText, color := getPullRequestPayloadInfo(p, noneLinkFormatter)
|
||||
|
||||
return &MSTeamsPayload{
|
||||
Type: "MessageCard",
|
||||
Context: "https://schema.org/extensions",
|
||||
ThemeColor: fmt.Sprintf("%x", color),
|
||||
Title: title,
|
||||
Summary: title,
|
||||
Title: text,
|
||||
Summary: text,
|
||||
Sections: []MSTeamsSection{
|
||||
{
|
||||
ActivityTitle: p.Sender.FullName,
|
||||
ActivitySubtitle: p.Sender.UserName,
|
||||
ActivityImage: p.Sender.AvatarURL,
|
||||
Text: text,
|
||||
Text: attachmentText,
|
||||
Facts: []MSTeamsFact{
|
||||
{
|
||||
Name: "Repository:",
|
||||
|
@ -625,29 +503,14 @@ func getMSTeamsRepositoryPayload(p *api.RepositoryPayload) (*MSTeamsPayload, err
|
|||
}
|
||||
|
||||
func getMSTeamsReleasePayload(p *api.ReleasePayload) (*MSTeamsPayload, error) {
|
||||
var title, url string
|
||||
var color int
|
||||
switch p.Action {
|
||||
case api.HookReleasePublished:
|
||||
title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
|
||||
url = p.Release.URL
|
||||
color = greenColor
|
||||
case api.HookReleaseUpdated:
|
||||
title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
|
||||
url = p.Release.URL
|
||||
color = greenColor
|
||||
case api.HookReleaseDeleted:
|
||||
title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
|
||||
url = p.Release.URL
|
||||
color = greenColor
|
||||
}
|
||||
text, color := getReleasePayloadInfo(p, noneLinkFormatter)
|
||||
|
||||
return &MSTeamsPayload{
|
||||
Type: "MessageCard",
|
||||
Context: "https://schema.org/extensions",
|
||||
ThemeColor: fmt.Sprintf("%x", color),
|
||||
Title: title,
|
||||
Summary: title,
|
||||
Title: text,
|
||||
Summary: text,
|
||||
Sections: []MSTeamsSection{
|
||||
{
|
||||
ActivityTitle: p.Sender.FullName,
|
||||
|
@ -673,7 +536,7 @@ func getMSTeamsReleasePayload(p *api.ReleasePayload) (*MSTeamsPayload, error) {
|
|||
Targets: []MSTeamsActionTarget{
|
||||
{
|
||||
Os: "default",
|
||||
URI: url,
|
||||
URI: p.Release.URL,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue