2019-12-28 14:25:09 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-28 14:25:09 +05:30
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html"
|
2021-11-16 23:48:25 +05:30
|
|
|
"net/url"
|
2019-12-28 14:25:09 +05:30
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-11-16 23:48:25 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-12-28 14:25:09 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type linkFormatter = func(string, string) string
|
|
|
|
|
|
|
|
// noneLinkFormatter does not create a link but just returns the text
|
2021-12-20 10:11:31 +05:30
|
|
|
func noneLinkFormatter(url, text string) string {
|
2019-12-28 14:25:09 +05:30
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// htmlLinkFormatter creates a HTML link
|
2021-12-20 10:11:31 +05:30
|
|
|
func htmlLinkFormatter(url, text string) string {
|
2021-11-16 23:48:25 +05:30
|
|
|
return fmt.Sprintf(`<a href="%s">%s</a>`, html.EscapeString(url), html.EscapeString(text))
|
2019-12-28 14:25:09 +05:30
|
|
|
}
|
|
|
|
|
2020-01-05 03:50:15 +05:30
|
|
|
func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, withSender bool) (string, string, string, int) {
|
2019-12-28 14:25:09 +05:30
|
|
|
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
|
|
|
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
|
|
|
|
titleLink := linkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index), issueTitle)
|
|
|
|
var text string
|
|
|
|
color := yellowColor
|
|
|
|
|
|
|
|
switch p.Action {
|
|
|
|
case api.HookIssueOpened:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Issue opened: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
color = orangeColor
|
|
|
|
case api.HookIssueClosed:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Issue closed: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
color = redColor
|
|
|
|
case api.HookIssueReOpened:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Issue re-opened: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueEdited:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Issue edited: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueAssigned:
|
2021-06-21 07:42:19 +05:30
|
|
|
list := make([]string, len(p.Issue.Assignees))
|
|
|
|
for i, user := range p.Issue.Assignees {
|
2021-11-16 23:48:25 +05:30
|
|
|
list[i] = linkFormatter(setting.AppURL+url.PathEscape(user.UserName), user.UserName)
|
2021-06-21 07:42:19 +05:30
|
|
|
}
|
|
|
|
text = fmt.Sprintf("[%s] Issue assigned to %s: %s", repoLink, strings.Join(list, ", "), titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
color = greenColor
|
|
|
|
case api.HookIssueUnassigned:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Issue unassigned: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueLabelUpdated:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Issue labels updated: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueLabelCleared:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Issue labels cleared: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueSynchronized:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Issue synchronized: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueMilestoned:
|
|
|
|
mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.Issue.Milestone.ID)
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Issue milestoned to %s: %s", repoLink,
|
|
|
|
linkFormatter(mileStoneLink, p.Issue.Milestone.Title), titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueDemilestoned:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Issue milestone cleared: %s", repoLink, titleLink)
|
|
|
|
}
|
|
|
|
if withSender {
|
2021-11-16 23:48:25 +05:30
|
|
|
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
|
2019-12-28 14:25:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var attachmentText string
|
|
|
|
if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited {
|
|
|
|
attachmentText = p.Issue.Body
|
|
|
|
}
|
|
|
|
|
|
|
|
return text, issueTitle, attachmentText, color
|
|
|
|
}
|
|
|
|
|
2020-01-05 03:50:15 +05:30
|
|
|
func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter, withSender bool) (string, string, string, int) {
|
2019-12-28 14:25:09 +05:30
|
|
|
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
|
|
|
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
|
|
|
|
titleLink := linkFormatter(p.PullRequest.URL, issueTitle)
|
|
|
|
var text string
|
2022-11-13 22:27:52 +05:30
|
|
|
var attachmentText string
|
2019-12-28 14:25:09 +05:30
|
|
|
color := yellowColor
|
|
|
|
|
|
|
|
switch p.Action {
|
|
|
|
case api.HookIssueOpened:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request opened: %s", repoLink, titleLink)
|
2022-11-13 22:27:52 +05:30
|
|
|
attachmentText = p.PullRequest.Body
|
2019-12-28 14:25:09 +05:30
|
|
|
color = greenColor
|
|
|
|
case api.HookIssueClosed:
|
|
|
|
if p.PullRequest.HasMerged {
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request merged: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
color = purpleColor
|
|
|
|
} else {
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request closed: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
color = redColor
|
|
|
|
}
|
|
|
|
case api.HookIssueReOpened:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request re-opened: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueEdited:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request edited: %s", repoLink, titleLink)
|
2022-11-13 22:27:52 +05:30
|
|
|
attachmentText = p.PullRequest.Body
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueAssigned:
|
|
|
|
list := make([]string, len(p.PullRequest.Assignees))
|
|
|
|
for i, user := range p.PullRequest.Assignees {
|
|
|
|
list[i] = linkFormatter(setting.AppURL+user.UserName, user.UserName)
|
|
|
|
}
|
2021-06-21 07:42:19 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request assigned to %s: %s", repoLink,
|
2020-01-05 03:50:15 +05:30
|
|
|
strings.Join(list, ", "), titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
color = greenColor
|
|
|
|
case api.HookIssueUnassigned:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request unassigned: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueLabelUpdated:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request labels updated: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueLabelCleared:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request labels cleared: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueSynchronized:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request synchronized: %s", repoLink, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueMilestoned:
|
|
|
|
mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.PullRequest.Milestone.ID)
|
2021-06-21 07:42:19 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request milestoned to %s: %s", repoLink,
|
2020-01-05 03:50:15 +05:30
|
|
|
linkFormatter(mileStoneLink, p.PullRequest.Milestone.Title), titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueDemilestoned:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Pull request milestone cleared: %s", repoLink, titleLink)
|
2020-06-25 09:09:43 +05:30
|
|
|
case api.HookIssueReviewed:
|
|
|
|
text = fmt.Sprintf("[%s] Pull request reviewed: %s", repoLink, titleLink)
|
2022-11-13 22:27:52 +05:30
|
|
|
attachmentText = p.Review.Content
|
2020-01-05 03:50:15 +05:30
|
|
|
}
|
|
|
|
if withSender {
|
|
|
|
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName))
|
2019-12-28 14:25:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return text, issueTitle, attachmentText, color
|
|
|
|
}
|
|
|
|
|
2020-01-05 03:50:15 +05:30
|
|
|
func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter, withSender bool) (text string, color int) {
|
2019-12-28 14:25:09 +05:30
|
|
|
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
2022-05-29 02:14:51 +05:30
|
|
|
refLink := linkFormatter(p.Repository.HTMLURL+"/releases/tag/"+util.PathEscapeSegments(p.Release.TagName), p.Release.TagName)
|
2019-12-28 14:25:09 +05:30
|
|
|
|
|
|
|
switch p.Action {
|
|
|
|
case api.HookReleasePublished:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Release created: %s", repoLink, refLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
color = greenColor
|
|
|
|
case api.HookReleaseUpdated:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Release updated: %s", repoLink, refLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
color = yellowColor
|
|
|
|
case api.HookReleaseDeleted:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Release deleted: %s", repoLink, refLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
color = redColor
|
|
|
|
}
|
2020-01-05 03:50:15 +05:30
|
|
|
if withSender {
|
2021-11-16 23:48:25 +05:30
|
|
|
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
|
2020-01-05 03:50:15 +05:30
|
|
|
}
|
2019-12-28 14:25:09 +05:30
|
|
|
|
|
|
|
return text, color
|
|
|
|
}
|
|
|
|
|
2022-09-05 01:24:23 +05:30
|
|
|
func getWikiPayloadInfo(p *api.WikiPayload, linkFormatter linkFormatter, withSender bool) (string, int, string) {
|
|
|
|
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
|
|
|
pageLink := linkFormatter(p.Repository.HTMLURL+"/wiki/"+url.PathEscape(p.Page), p.Page)
|
|
|
|
|
|
|
|
var text string
|
|
|
|
color := greenColor
|
|
|
|
|
|
|
|
switch p.Action {
|
|
|
|
case api.HookWikiCreated:
|
|
|
|
text = fmt.Sprintf("[%s] New wiki page '%s'", repoLink, pageLink)
|
|
|
|
case api.HookWikiEdited:
|
|
|
|
text = fmt.Sprintf("[%s] Wiki page '%s' edited", repoLink, pageLink)
|
|
|
|
color = yellowColor
|
|
|
|
case api.HookWikiDeleted:
|
|
|
|
text = fmt.Sprintf("[%s] Wiki page '%s' deleted", repoLink, pageLink)
|
|
|
|
color = redColor
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Action != api.HookWikiDeleted && p.Comment != "" {
|
|
|
|
text += fmt.Sprintf(" (%s)", p.Comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
if withSender {
|
|
|
|
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
|
|
|
|
}
|
|
|
|
|
|
|
|
return text, color, pageLink
|
|
|
|
}
|
|
|
|
|
2020-01-05 03:50:15 +05:30
|
|
|
func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFormatter, withSender bool) (string, string, int) {
|
2019-12-28 14:25:09 +05:30
|
|
|
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
|
|
|
issueTitle := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
|
|
|
|
|
|
|
|
var text, typ, titleLink string
|
|
|
|
color := yellowColor
|
|
|
|
|
|
|
|
if p.IsPull {
|
|
|
|
typ = "pull request"
|
|
|
|
titleLink = linkFormatter(p.Comment.PRURL, issueTitle)
|
|
|
|
} else {
|
|
|
|
typ = "issue"
|
|
|
|
titleLink = linkFormatter(p.Comment.IssueURL, issueTitle)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch p.Action {
|
|
|
|
case api.HookIssueCommentCreated:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] New comment on %s %s", repoLink, typ, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
if p.IsPull {
|
|
|
|
color = greenColorLight
|
|
|
|
} else {
|
|
|
|
color = orangeColorLight
|
|
|
|
}
|
|
|
|
case api.HookIssueCommentEdited:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Comment edited on %s %s", repoLink, typ, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
case api.HookIssueCommentDeleted:
|
2020-01-05 03:50:15 +05:30
|
|
|
text = fmt.Sprintf("[%s] Comment deleted on %s %s", repoLink, typ, titleLink)
|
2019-12-28 14:25:09 +05:30
|
|
|
color = redColor
|
|
|
|
}
|
2020-01-05 03:50:15 +05:30
|
|
|
if withSender {
|
2021-11-16 23:48:25 +05:30
|
|
|
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
|
2020-01-05 03:50:15 +05:30
|
|
|
}
|
2019-12-28 14:25:09 +05:30
|
|
|
|
|
|
|
return text, issueTitle, color
|
|
|
|
}
|