Skip to content

Commit 81e63d0

Browse files
cornelklafrikszeripathtechknowlogick
committed
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 <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 10455a8 commit 81e63d0

10 files changed

+534
-698
lines changed

modules/webhook/dingtalk.go

+21-145
Original file line numberDiff line numberDiff line change
@@ -132,41 +132,14 @@ func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) {
132132
}
133133

134134
func getDingtalkIssuesPayload(p *api.IssuePayload) (*DingtalkPayload, error) {
135-
var text, title string
136-
switch p.Action {
137-
case api.HookIssueOpened:
138-
title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
139-
text = p.Issue.Body
140-
case api.HookIssueClosed:
141-
title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
142-
case api.HookIssueReOpened:
143-
title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
144-
case api.HookIssueEdited:
145-
title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
146-
text = p.Issue.Body
147-
case api.HookIssueAssigned:
148-
title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName,
149-
p.Issue.Assignee.UserName, p.Index, p.Issue.Title)
150-
case api.HookIssueUnassigned:
151-
title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
152-
case api.HookIssueLabelUpdated:
153-
title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
154-
case api.HookIssueLabelCleared:
155-
title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
156-
case api.HookIssueSynchronized:
157-
title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
158-
case api.HookIssueMilestoned:
159-
title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
160-
case api.HookIssueDemilestoned:
161-
title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
162-
}
135+
text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter)
163136

164137
return &DingtalkPayload{
165138
MsgType: "actionCard",
166139
ActionCard: dingtalk.ActionCard{
167-
Text: title + "\r\n\r\n" + text,
140+
Text: text + "\r\n\r\n" + attachmentText,
168141
//Markdown: "# " + title + "\n" + text,
169-
Title: title,
142+
Title: issueTitle,
170143
HideAvatar: "0",
171144
SingleTitle: "view issue",
172145
SingleURL: p.Issue.URL,
@@ -175,93 +148,29 @@ func getDingtalkIssuesPayload(p *api.IssuePayload) (*DingtalkPayload, error) {
175148
}
176149

177150
func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) {
178-
title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title)
179-
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID))
180-
var content string
181-
switch p.Action {
182-
case api.HookIssueCommentCreated:
183-
if p.IsPull {
184-
title = "New comment on pull request " + title
185-
} else {
186-
title = "New comment on issue " + title
187-
}
188-
content = p.Comment.Body
189-
case api.HookIssueCommentEdited:
190-
if p.IsPull {
191-
title = "Comment edited on pull request " + title
192-
} else {
193-
title = "Comment edited on issue " + title
194-
}
195-
content = p.Comment.Body
196-
case api.HookIssueCommentDeleted:
197-
if p.IsPull {
198-
title = "Comment deleted on pull request " + title
199-
} else {
200-
title = "Comment deleted on issue " + title
201-
}
202-
url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
203-
content = p.Comment.Body
204-
}
205-
206-
title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title)
151+
text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter)
207152

208153
return &DingtalkPayload{
209154
MsgType: "actionCard",
210155
ActionCard: dingtalk.ActionCard{
211-
Text: title + "\r\n\r\n" + content,
212-
Title: title,
156+
Text: text + "\r\n\r\n" + p.Comment.Body,
157+
Title: issueTitle,
213158
HideAvatar: "0",
214159
SingleTitle: "view issue comment",
215-
SingleURL: url,
160+
SingleURL: p.Comment.HTMLURL,
216161
},
217162
}, nil
218163
}
219164

220165
func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) {
221-
var text, title string
222-
switch p.Action {
223-
case api.HookIssueOpened:
224-
title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
225-
text = p.PullRequest.Body
226-
case api.HookIssueClosed:
227-
if p.PullRequest.HasMerged {
228-
title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
229-
} else {
230-
title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
231-
}
232-
case api.HookIssueReOpened:
233-
title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
234-
case api.HookIssueEdited:
235-
title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
236-
text = p.PullRequest.Body
237-
case api.HookIssueAssigned:
238-
list := make([]string, len(p.PullRequest.Assignees))
239-
for i, user := range p.PullRequest.Assignees {
240-
list[i] = user.UserName
241-
}
242-
title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName,
243-
strings.Join(list, ", "),
244-
p.Index, p.PullRequest.Title)
245-
case api.HookIssueUnassigned:
246-
title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
247-
case api.HookIssueLabelUpdated:
248-
title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
249-
case api.HookIssueLabelCleared:
250-
title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
251-
case api.HookIssueSynchronized:
252-
title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
253-
case api.HookIssueMilestoned:
254-
title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
255-
case api.HookIssueDemilestoned:
256-
title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
257-
}
166+
text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter)
258167

259168
return &DingtalkPayload{
260169
MsgType: "actionCard",
261170
ActionCard: dingtalk.ActionCard{
262-
Text: title + "\r\n\r\n" + text,
171+
Text: text + "\r\n\r\n" + attachmentText,
263172
//Markdown: "# " + title + "\n" + text,
264-
Title: title,
173+
Title: issueTitle,
265174
HideAvatar: "0",
266175
SingleTitle: "view pull request",
267176
SingleURL: p.PullRequest.HTMLURL,
@@ -327,51 +236,18 @@ func getDingtalkRepositoryPayload(p *api.RepositoryPayload) (*DingtalkPayload, e
327236
}
328237

329238
func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error) {
330-
var title, url string
331-
switch p.Action {
332-
case api.HookReleasePublished:
333-
title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
334-
url = p.Release.URL
335-
return &DingtalkPayload{
336-
MsgType: "actionCard",
337-
ActionCard: dingtalk.ActionCard{
338-
Text: title,
339-
Title: title,
340-
HideAvatar: "0",
341-
SingleTitle: "view release",
342-
SingleURL: url,
343-
},
344-
}, nil
345-
case api.HookReleaseUpdated:
346-
title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
347-
url = p.Release.URL
348-
return &DingtalkPayload{
349-
MsgType: "actionCard",
350-
ActionCard: dingtalk.ActionCard{
351-
Text: title,
352-
Title: title,
353-
HideAvatar: "0",
354-
SingleTitle: "view release",
355-
SingleURL: url,
356-
},
357-
}, nil
239+
text, _ := getReleasePayloadInfo(p, noneLinkFormatter)
358240

359-
case api.HookReleaseDeleted:
360-
title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
361-
url = p.Release.URL
362-
return &DingtalkPayload{
363-
MsgType: "actionCard",
364-
ActionCard: dingtalk.ActionCard{
365-
Text: title,
366-
Title: title,
367-
HideAvatar: "0",
368-
SingleTitle: "view release",
369-
SingleURL: url,
370-
},
371-
}, nil
372-
}
373-
374-
return nil, nil
241+
return &DingtalkPayload{
242+
MsgType: "actionCard",
243+
ActionCard: dingtalk.ActionCard{
244+
Text: text,
245+
Title: text,
246+
HideAvatar: "0",
247+
SingleTitle: "view release",
248+
SingleURL: p.Release.URL,
249+
},
250+
}, nil
375251
}
376252

377253
// GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload

modules/webhook/dingtalk_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package webhook
6+
7+
import (
8+
"testing"
9+
10+
api "code.gitea.io/gitea/modules/structs"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestGetDingTalkIssuesPayload(t *testing.T) {
16+
p := issueTestPayload()
17+
18+
p.Action = api.HookIssueOpened
19+
pl, err := getDingtalkIssuesPayload(p)
20+
require.Nil(t, err)
21+
require.NotNil(t, pl)
22+
assert.Equal(t, "#2 crash", pl.ActionCard.Title)
23+
assert.Equal(t, "[test/repo] Issue opened: #2 crash by user1\r\n\r\n", pl.ActionCard.Text)
24+
25+
p.Action = api.HookIssueClosed
26+
pl, err = getDingtalkIssuesPayload(p)
27+
require.Nil(t, err)
28+
require.NotNil(t, pl)
29+
assert.Equal(t, "#2 crash", pl.ActionCard.Title)
30+
assert.Equal(t, "[test/repo] Issue closed: #2 crash by user1\r\n\r\n", pl.ActionCard.Text)
31+
}

0 commit comments

Comments
 (0)