{{.i18n.Tr "repo.settings.add_workwechat_hook_desc" "https://work.weixin.qq.com" | Str2html}}
+ +{{end}} From 2c9193a55d42c1c95f75379fe80c5b798ea4dc94 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Jan 2020 16:27:36 +0100 Subject: [PATCH 2/9] move --- models/webhook_workwechat.go => modules/webhook/workwechat.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/webhook_workwechat.go => modules/webhook/workwechat.go (100%) diff --git a/models/webhook_workwechat.go b/modules/webhook/workwechat.go similarity index 100% rename from models/webhook_workwechat.go rename to modules/webhook/workwechat.go From a39cf7ddf2a83016365f870137f979f62e345b0e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Jan 2020 16:41:55 +0100 Subject: [PATCH 3/9] apply changes of 9422 --- modules/webhook/workwechat.go | 179 +++++----------------------------- 1 file changed, 25 insertions(+), 154 deletions(-) diff --git a/modules/webhook/workwechat.go b/modules/webhook/workwechat.go index 0f57338ca2069..01abdaf871d2d 100644 --- a/modules/webhook/workwechat.go +++ b/modules/webhook/workwechat.go @@ -153,142 +153,46 @@ func getWorkwechatPushPayload(p *api.PushPayload, meta *WorkwechatMeta) (*Workwe } func getWorkwechatIssuesPayload(p *api.IssuePayload, meta *WorkwechatMeta) (*WorkwechatPayload, error) { - var text, title string - 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 - case api.HookIssueClosed: - title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueReOpened: - title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueEdited: - title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - 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) - text = p.Issue.Body - case api.HookIssueUnassigned: - title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueLabelUpdated: - title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueLabelCleared: - title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueSynchronized: - title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueMilestoned: - title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueDemilestoned: - title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - } + text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter) return &WorkwechatPayload{ ChatID: meta.ChatID, MsgType: "textcard", TextCard: TextCard{ - Description: title + "\r\n\r\n" + text, - //Markdown: "# " + title + "\n" + text, - Title: title, - ButtonText: "view issue", - URL: p.Issue.URL, + Description: title + "\r\n\r\n" + attachmentText, + Title: issueTitle, + ButtonText: "view issue", + URL: p.Issue.URL, }, }, nil } func getWorkwechatIssueCommentPayload(p *api.IssueCommentPayload, meta *WorkwechatMeta) (*WorkwechatPayload, 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, CommentHashTag(p.Comment.ID)) - var content string - switch p.Action { - case api.HookIssueCommentCreated: - title = "New comment: " + title - content = p.Comment.Body - case api.HookIssueCommentEdited: - title = "Comment edited: " + title - content = p.Comment.Body - case api.HookIssueCommentDeleted: - title = "Comment deleted: " + title - url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index) - content = p.Comment.Body - } + text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter) return &WorkwechatPayload{ ChatID: meta.ChatID, MsgType: "textcard", TextCard: TextCard{ - Description: title + "\r\n\r\n" + content, - Title: title, + Description: text + "\r\n\r\n" + p.Comment.Body, + Title: issueTitle, ButtonText: "view issue comment", - URL: url, + URL: p.Comment.HTMLURL, }, }, nil } func getWorkwechatPullRequestPayload(p *api.PullRequestPayload, meta *WorkwechatMeta) (*WorkwechatPayload, error) { - var text, title string - 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 - case api.HookIssueClosed: - if p.PullRequest.HasMerged { - title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - } else { - title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - } - text = p.PullRequest.Body - case api.HookIssueReOpened: - title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueEdited: - title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueAssigned: - list, err := MakeAssigneeList(&Issue{ID: p.PullRequest.ID}) - if err != nil { - return &WorkwechatPayload{}, err - } - title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName, - list, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueUnassigned: - title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueLabelUpdated: - title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueLabelCleared: - title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueSynchronized: - title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueMilestoned: - title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueDemilestoned: - title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - } + text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter) return &WorkwechatPayload{ ChatID: meta.ChatID, MsgType: "textcard", TextCard: TextCard{ - Description: title + "\r\n\r\n" + text, - //Markdown: "# " + title + "\n" + text, - Title: title, - ButtonText: "view pull request", - URL: p.PullRequest.HTMLURL, + Description: text + "\r\n\r\n" + attachmentText, + Title: issueTitle, + ButtonText: "view pull request", + URL: p.PullRequest.HTMLURL, }, }, nil } @@ -325,51 +229,18 @@ func getWorkwechatRepositoryPayload(p *api.RepositoryPayload, meta *WorkwechatMe } func getWorkwechatReleasePayload(p *api.ReleasePayload, meta *WorkwechatMeta) (*WorkwechatPayload, error) { - var title, url string - switch p.Action { - case api.HookReleasePublished: - title = fmt.Sprintf("[%s] Release created", p.Release.TagName) - url = p.Release.URL - return &WorkwechatPayload{ - ChatID: meta.ChatID, - MsgType: "textcard", - TextCard: TextCard{ - Description: title, - Title: title, - ButtonText: "view release", - URL: url, - }, - }, nil - case api.HookReleaseUpdated: - title = fmt.Sprintf("[%s] Release updated", p.Release.TagName) - url = p.Release.URL - return &WorkwechatPayload{ - ChatID: meta.ChatID, - MsgType: "textcard", - TextCard: TextCard{ - Description: title, - Title: title, - ButtonText: "view release", - URL: url, - }, - }, nil - - case api.HookReleaseDeleted: - title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName) - url = p.Release.URL - return &WorkwechatPayload{ - ChatID: meta.ChatID, - MsgType: "textcard", - TextCard: TextCard{ - Description: title, - Title: title, - ButtonText: "view release", - URL: url, - }, - }, nil - } + text, _ := getReleasePayloadInfo(p, noneLinkFormatter) - return nil, nil + return &WorkwechatPayload{ + ChatID: meta.ChatID, + MsgType: "textcard", + TextCard: TextCard{ + Description: text, + Title: text, + ButtonText: "view release", + URL: p.Release.URL, + }, + }, nil } // GetWorkwechatPayload converts a work wechat webhook into a WorkwechatPayload From 7eee88ae058d4f9071facb13ff51087f1d2544fb Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Jan 2020 16:44:28 +0100 Subject: [PATCH 4/9] fix package --- modules/webhook/workwechat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/webhook/workwechat.go b/modules/webhook/workwechat.go index 01abdaf871d2d..c86a804827457 100644 --- a/modules/webhook/workwechat.go +++ b/modules/webhook/workwechat.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package models +package webhook import ( "encoding/json" From ebbcac6f760d2ccfd41695ae075f989b5fd31c2f Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Jan 2020 16:56:40 +0100 Subject: [PATCH 5/9] apply changes of 9601 --- modules/webhook/workwechat.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/webhook/workwechat.go b/modules/webhook/workwechat.go index c86a804827457..9e3dadcbb6773 100644 --- a/modules/webhook/workwechat.go +++ b/modules/webhook/workwechat.go @@ -153,13 +153,13 @@ func getWorkwechatPushPayload(p *api.PushPayload, meta *WorkwechatMeta) (*Workwe } func getWorkwechatIssuesPayload(p *api.IssuePayload, meta *WorkwechatMeta) (*WorkwechatPayload, error) { - text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter) + text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true) return &WorkwechatPayload{ ChatID: meta.ChatID, MsgType: "textcard", TextCard: TextCard{ - Description: title + "\r\n\r\n" + attachmentText, + Description: text + "\r\n\r\n" + attachmentText, Title: issueTitle, ButtonText: "view issue", URL: p.Issue.URL, @@ -168,7 +168,7 @@ func getWorkwechatIssuesPayload(p *api.IssuePayload, meta *WorkwechatMeta) (*Wor } func getWorkwechatIssueCommentPayload(p *api.IssueCommentPayload, meta *WorkwechatMeta) (*WorkwechatPayload, error) { - text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter) + text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true) return &WorkwechatPayload{ ChatID: meta.ChatID, @@ -183,7 +183,7 @@ func getWorkwechatIssueCommentPayload(p *api.IssueCommentPayload, meta *Workwech } func getWorkwechatPullRequestPayload(p *api.PullRequestPayload, meta *WorkwechatMeta) (*WorkwechatPayload, error) { - text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter) + text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true) return &WorkwechatPayload{ ChatID: meta.ChatID, @@ -229,7 +229,7 @@ func getWorkwechatRepositoryPayload(p *api.RepositoryPayload, meta *WorkwechatMe } func getWorkwechatReleasePayload(p *api.ReleasePayload, meta *WorkwechatMeta) (*WorkwechatPayload, error) { - text, _ := getReleasePayloadInfo(p, noneLinkFormatter) + text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true) return &WorkwechatPayload{ ChatID: meta.ChatID, @@ -244,7 +244,7 @@ func getWorkwechatReleasePayload(p *api.ReleasePayload, meta *WorkwechatMeta) (* } // GetWorkwechatPayload converts a work wechat webhook into a WorkwechatPayload -func GetWorkwechatPayload(p api.Payloader, event HookEventType, meta string) (*WorkwechatPayload, error) { +func GetWorkwechatPayload(p api.Payloader, event models.HookEventType, meta string) (*WorkwechatPayload, error) { s := new(WorkwechatPayload) workwechatMeta := &WorkwechatMeta{} From 18befa3edafbc5ed866a5a90a0c6741d673546c0 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Jan 2020 17:05:31 +0100 Subject: [PATCH 6/9] fix after code refator .... --- modules/convert/convert.go | 4 ++-- modules/webhook/webhook.go | 2 +- modules/webhook/workwechat.go | 33 ++++++++++++++++++++++----------- routers/repo/webhook.go | 4 ++-- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 18fcb726230a7..ff0db789a0b1a 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -191,8 +191,8 @@ func ToHook(repoLink string, w *models.Webhook) *api.Hook { config["icon_url"] = s.IconURL config["color"] = s.Color } else if w.HookTaskType == models.WORKWECHAT { - s := w.GetWorkwechatHook() - config["chatid"] = s.ChatID + we := webhook.GetWorkwechatHook(w) + config["chatid"] = we.ChatID } return &api.Hook{ diff --git a/modules/webhook/webhook.go b/modules/webhook/webhook.go index 2dc50c992aedd..f531b8be769e6 100644 --- a/modules/webhook/webhook.go +++ b/modules/webhook/webhook.go @@ -114,7 +114,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo if err != nil { return fmt.Errorf("GetMSTeamsPayload: %v", err) } - case WORKWECHAT: + case models.WORKWECHAT: payloader, err = GetWorkwechatPayload(p, event, w.Meta) if err != nil { return fmt.Errorf("GetWorkwechatPayload: %v", err) diff --git a/modules/webhook/workwechat.go b/modules/webhook/workwechat.go index 9e3dadcbb6773..83a6322f6a925 100644 --- a/modules/webhook/workwechat.go +++ b/modules/webhook/workwechat.go @@ -10,8 +10,10 @@ import ( "fmt" "strings" - "code.gitea.io/git" - api "code.gitea.io/sdk/gitea" + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/log" + api "code.gitea.io/gitea/modules/structs" ) type ( @@ -42,6 +44,15 @@ type ( } ) +// GetWorkwechatHook returns workwechat metadata +func GetWorkwechatHook(w *models.Webhook) *WorkwechatMeta { + we := &WorkwechatMeta{} + if err := json.Unmarshal([]byte(w.Meta), we); err != nil { + log.Error("webhook.GetWorkwechatHook(%d): %v", w.ID, err) + } + return we +} + // SetSecret sets the workwechat secret func (p *WorkwechatPayload) SetSecret(_ string) {} @@ -252,23 +263,23 @@ func GetWorkwechatPayload(p api.Payloader, event models.HookEventType, meta stri return s, errors.New("GetWorkwechatPayload meta json:" + err.Error()) } switch event { - case HookEventCreate: + case models.HookEventCreate: return getWorkwechatCreatePayload(p.(*api.CreatePayload), workwechatMeta) - case HookEventDelete: + case models.HookEventDelete: return getWorkwechatDeletePayload(p.(*api.DeletePayload), workwechatMeta) - case HookEventFork: + case models.HookEventFork: return getWorkwechatForkPayload(p.(*api.ForkPayload), workwechatMeta) - case HookEventIssues: + case models.HookEventIssues: return getWorkwechatIssuesPayload(p.(*api.IssuePayload), workwechatMeta) - case HookEventIssueComment: + case models.HookEventIssueComment: return getWorkwechatIssueCommentPayload(p.(*api.IssueCommentPayload), workwechatMeta) - case HookEventPush: + case models.HookEventPush: return getWorkwechatPushPayload(p.(*api.PushPayload), workwechatMeta) - case HookEventPullRequest: + case models.HookEventPullRequest: return getWorkwechatPullRequestPayload(p.(*api.PullRequestPayload), workwechatMeta) - case HookEventRepository: + case models.HookEventRepository: return getWorkwechatRepositoryPayload(p.(*api.RepositoryPayload), workwechatMeta) - case HookEventRelease: + case models.HookEventRelease: return getWorkwechatReleasePayload(p.(*api.ReleasePayload), workwechatMeta) } diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index 113ec489fb1ad..f2ae12486594b 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -506,7 +506,7 @@ func WorkwechatHooksNewPost(ctx *context.Context, form auth.NewWorkwechatHookFor ctx.HTML(200, orCtx.NewTemplate) return } - meta, err := json.Marshal(&models.WorkwechatMeta{ + meta, err := json.Marshal(&webhook.WorkwechatMeta{ ChatID: form.ChatID, }) if err != nil { @@ -887,7 +887,7 @@ func WorkwechatHooksEditPost(ctx *context.Context, form auth.NewWorkwechatHookFo ctx.HTML(200, orCtx.NewTemplate) return } - meta, err := json.Marshal(&models.WorkwechatMeta{ + meta, err := json.Marshal(&webhook.WorkwechatMeta{ ChatID: form.ChatID, }) if err != nil { From 1ce8ab13f81ca367fead1adad6321b90c5488085 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Jan 2020 17:42:27 +0100 Subject: [PATCH 7/9] fix paths --- templates/repo/settings/webhook/list.tmpl | 2 +- templates/repo/settings/webhook/workwechat.tmpl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/repo/settings/webhook/list.tmpl b/templates/repo/settings/webhook/list.tmpl index 20c41d8133684..1cd713286d456 100644 --- a/templates/repo/settings/webhook/list.tmpl +++ b/templates/repo/settings/webhook/list.tmpl @@ -26,7 +26,7 @@{{.i18n.Tr "repo.settings.add_workwechat_hook_desc" "https://work.weixin.qq.com" | Str2html}}
-