summaryrefslogtreecommitdiffstats
path: root/services/webhook/general.go
diff options
context:
space:
mode:
author谈笑风生间 <makonike@anyview.fun>2023-08-24 09:00:11 +0800
committerGitHub <noreply@github.com>2023-08-24 09:00:11 +0800
commit5104c887d32a19afd468da7e71f39831607f4c44 (patch)
treee823211a3e8cf09662290699888c9b622c197de9 /services/webhook/general.go
parentf67f57a4c28d1660dec6cadb7fd07bdeda98aa5b (diff)
downloadgitea-5104c887d32a19afd468da7e71f39831607f4c44.tar.gz
gitea-5104c887d32a19afd468da7e71f39831607f4c44.zip
Modify the content format of the Feishu webhook (#25106)
close https://github.com/go-gitea/gitea/issues/24368 ## what my pull request does Since the official documentation states that custom bots do not support hyperlink functionality, simply adding it without making some formatting changes would result in an unappealing output. Therefore, I have modified the formatting of the output. Currently, it is only used for Feishu. --- [docs](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/im-v1/message/create_json#%E8%B6%85%E9%93%BE%E6%8E%A5%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E) <img width="641" alt="image" src="https://github.com/go-gitea/gitea/assets/75628309/360e1c81-ab64-4ef6-851e-aa450d6e85a4"> - Issue <img width="423" alt="image" src="https://github.com/go-gitea/gitea/assets/75628309/519f6fae-81ab-4ec8-89b8-f6a73ff93783"> - Issue Comment <img width="548" alt="image" src="https://github.com/go-gitea/gitea/assets/75628309/749c0d1c-3657-431e-b787-8bf4c23cce83"> - Assign <img width="431" alt="image" src="https://github.com/go-gitea/gitea/assets/75628309/066f99e5-eabb-455d-91fb-a8359cc26dc7"> <img width="457" alt="image" src="https://github.com/go-gitea/gitea/assets/75628309/c6c10f99-db83-46ef-a775-4c91979fa68f"> - Merge <img width="408" alt="image" src="https://github.com/go-gitea/gitea/assets/75628309/e627bf43-5954-45aa-acf6-261ee046802f"> - PullRequest <img width="425" alt="image" src="https://github.com/go-gitea/gitea/assets/75628309/72cfa714-d3fa-4fb9-abdd-e8508d756056">
Diffstat (limited to 'services/webhook/general.go')
-rw-r--r--services/webhook/general.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/services/webhook/general.go b/services/webhook/general.go
index b9cc3dc845..986467bc99 100644
--- a/services/webhook/general.go
+++ b/services/webhook/general.go
@@ -28,6 +28,69 @@ func htmlLinkFormatter(url, text string) string {
return fmt.Sprintf(`<a href="%s">%s</a>`, html.EscapeString(url), html.EscapeString(text))
}
+// getPullRequestInfo gets the information for a pull request
+func getPullRequestInfo(p *api.PullRequestPayload) (title, link, by, operator, operateResult, assignees string) {
+ title = fmt.Sprintf("[PullRequest-%s #%d]: %s\n%s", p.Repository.FullName, p.PullRequest.Index, p.Action, p.PullRequest.Title)
+ assignList := p.PullRequest.Assignees
+ assignStringList := make([]string, len(assignList))
+
+ for i, user := range assignList {
+ assignStringList[i] = user.UserName
+ }
+ if p.Action == api.HookIssueAssigned {
+ operateResult = fmt.Sprintf("%s assign this to %s", p.Sender.UserName, assignList[len(assignList)-1].UserName)
+ } else if p.Action == api.HookIssueUnassigned {
+ operateResult = fmt.Sprintf("%s unassigned this for someone", p.Sender.UserName)
+ } else if p.Action == api.HookIssueMilestoned {
+ operateResult = fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.PullRequest.Milestone.ID)
+ }
+ link = p.PullRequest.HTMLURL
+ by = fmt.Sprintf("PullRequest by %s", p.PullRequest.Poster.UserName)
+ if len(assignStringList) > 0 {
+ assignees = fmt.Sprintf("Assignees: %s", strings.Join(assignStringList, ", "))
+ }
+ operator = fmt.Sprintf("Operator: %s", p.Sender.UserName)
+ return title, link, by, operator, operateResult, assignees
+}
+
+// getIssuesInfo gets the information for an issue
+func getIssuesInfo(p *api.IssuePayload) (issueTitle, link, by, operator, operateResult, assignees string) {
+ issueTitle = fmt.Sprintf("[Issue-%s #%d]: %s\n%s", p.Repository.FullName, p.Issue.Index, p.Action, p.Issue.Title)
+ assignList := p.Issue.Assignees
+ assignStringList := make([]string, len(assignList))
+
+ for i, user := range assignList {
+ assignStringList[i] = user.UserName
+ }
+ if p.Action == api.HookIssueAssigned {
+ operateResult = fmt.Sprintf("%s assign this to %s", p.Sender.UserName, assignList[len(assignList)-1].UserName)
+ } else if p.Action == api.HookIssueUnassigned {
+ operateResult = fmt.Sprintf("%s unassigned this for someone", p.Sender.UserName)
+ } else if p.Action == api.HookIssueMilestoned {
+ operateResult = fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.Issue.Milestone.ID)
+ }
+ link = p.Issue.HTMLURL
+ by = fmt.Sprintf("Issue by %s", p.Issue.Poster.UserName)
+ if len(assignStringList) > 0 {
+ assignees = fmt.Sprintf("Assignees: %s", strings.Join(assignStringList, ", "))
+ }
+ operator = fmt.Sprintf("Operator: %s", p.Sender.UserName)
+ return issueTitle, link, by, operator, operateResult, assignees
+}
+
+// getIssuesCommentInfo gets the information for a comment
+func getIssuesCommentInfo(p *api.IssueCommentPayload) (title, link, by, operator string) {
+ title = fmt.Sprintf("[Comment-%s #%d]: %s\n%s", p.Repository.FullName, p.Issue.Index, p.Action, p.Issue.Title)
+ link = p.Issue.HTMLURL
+ if p.IsPull {
+ by = fmt.Sprintf("PullRequest by %s", p.Issue.Poster.UserName)
+ } else {
+ by = fmt.Sprintf("Issue by %s", p.Issue.Poster.UserName)
+ }
+ operator = fmt.Sprintf("Operator: %s", p.Sender.UserName)
+ return title, link, by, operator
+}
+
func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, withSender bool) (string, string, string, int) {
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)