Skip to content

Commit a966a02

Browse files
lunnyzeripath
authored andcommitted
Move more webhook codes from models to webhook module (#8802)
* Move more webhook codes from models to webhook module
1 parent 491887d commit a966a02

File tree

12 files changed

+145
-135
lines changed

12 files changed

+145
-135
lines changed

models/webhook.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,33 +118,6 @@ func (w *Webhook) AfterLoad() {
118118
}
119119
}
120120

121-
// GetSlackHook returns slack metadata
122-
func (w *Webhook) GetSlackHook() *SlackMeta {
123-
s := &SlackMeta{}
124-
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
125-
log.Error("webhook.GetSlackHook(%d): %v", w.ID, err)
126-
}
127-
return s
128-
}
129-
130-
// GetDiscordHook returns discord metadata
131-
func (w *Webhook) GetDiscordHook() *DiscordMeta {
132-
s := &DiscordMeta{}
133-
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
134-
log.Error("webhook.GetDiscordHook(%d): %v", w.ID, err)
135-
}
136-
return s
137-
}
138-
139-
// GetTelegramHook returns telegram metadata
140-
func (w *Webhook) GetTelegramHook() *TelegramMeta {
141-
s := &TelegramMeta{}
142-
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
143-
log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err)
144-
}
145-
return s
146-
}
147-
148121
// History returns history of webhook by given conditions.
149122
func (w *Webhook) History(page int) ([]*HookTask, error) {
150123
return HookTasks(w.ID, page)

models/webhook_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ func TestIsValidHookContentType(t *testing.T) {
2424
assert.False(t, IsValidHookContentType("invalid"))
2525
}
2626

27-
func TestWebhook_GetSlackHook(t *testing.T) {
28-
w := &Webhook{
29-
Meta: `{"channel": "foo", "username": "username", "color": "blue"}`,
30-
}
31-
slackHook := w.GetSlackHook()
32-
assert.Equal(t, *slackHook, SlackMeta{
33-
Channel: "foo",
34-
Username: "username",
35-
Color: "blue",
36-
})
37-
}
38-
3927
func TestWebhook_History(t *testing.T) {
4028
assert.NoError(t, PrepareTestDatabase())
4129
webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)

models/webhook_dingtalk.go renamed to modules/webhook/dingtalk.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

5-
package models
5+
package webhook
66

77
import (
88
"encoding/json"
99
"fmt"
1010
"strings"
1111

12+
"code.gitea.io/gitea/models"
1213
"code.gitea.io/gitea/modules/git"
1314
api "code.gitea.io/gitea/modules/structs"
1415

@@ -184,7 +185,7 @@ func getDingtalkIssuesPayload(p *api.IssuePayload) (*DingtalkPayload, error) {
184185

185186
func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) {
186187
title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title)
187-
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
188+
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID))
188189
var content string
189190
switch p.Action {
190191
case api.HookIssueCommentCreated:
@@ -286,7 +287,7 @@ func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload,
286287
}, nil
287288
}
288289

289-
func getDingtalkPullRequestApprovalPayload(p *api.PullRequestPayload, event HookEventType) (*DingtalkPayload, error) {
290+
func getDingtalkPullRequestApprovalPayload(p *api.PullRequestPayload, event models.HookEventType) (*DingtalkPayload, error) {
290291
var text, title string
291292
switch p.Action {
292293
case api.HookIssueSynchronized:
@@ -392,29 +393,29 @@ func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error)
392393
}
393394

394395
// GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
395-
func GetDingtalkPayload(p api.Payloader, event HookEventType, meta string) (*DingtalkPayload, error) {
396+
func GetDingtalkPayload(p api.Payloader, event models.HookEventType, meta string) (*DingtalkPayload, error) {
396397
s := new(DingtalkPayload)
397398

398399
switch event {
399-
case HookEventCreate:
400+
case models.HookEventCreate:
400401
return getDingtalkCreatePayload(p.(*api.CreatePayload))
401-
case HookEventDelete:
402+
case models.HookEventDelete:
402403
return getDingtalkDeletePayload(p.(*api.DeletePayload))
403-
case HookEventFork:
404+
case models.HookEventFork:
404405
return getDingtalkForkPayload(p.(*api.ForkPayload))
405-
case HookEventIssues:
406+
case models.HookEventIssues:
406407
return getDingtalkIssuesPayload(p.(*api.IssuePayload))
407-
case HookEventIssueComment:
408+
case models.HookEventIssueComment:
408409
return getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
409-
case HookEventPush:
410+
case models.HookEventPush:
410411
return getDingtalkPushPayload(p.(*api.PushPayload))
411-
case HookEventPullRequest:
412+
case models.HookEventPullRequest:
412413
return getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
413-
case HookEventPullRequestApproved, HookEventPullRequestRejected, HookEventPullRequestComment:
414+
case models.HookEventPullRequestApproved, models.HookEventPullRequestRejected, models.HookEventPullRequestComment:
414415
return getDingtalkPullRequestApprovalPayload(p.(*api.PullRequestPayload), event)
415-
case HookEventRepository:
416+
case models.HookEventRepository:
416417
return getDingtalkRepositoryPayload(p.(*api.RepositoryPayload))
417-
case HookEventRelease:
418+
case models.HookEventRelease:
418419
return getDingtalkReleasePayload(p.(*api.ReleasePayload))
419420
}
420421

models/webhook_discord.go renamed to modules/webhook/discord.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

5-
package models
5+
package webhook
66

77
import (
88
"encoding/json"
@@ -11,7 +11,9 @@ import (
1111
"strconv"
1212
"strings"
1313

14+
"code.gitea.io/gitea/models"
1415
"code.gitea.io/gitea/modules/git"
16+
"code.gitea.io/gitea/modules/log"
1517
"code.gitea.io/gitea/modules/setting"
1618
api "code.gitea.io/gitea/modules/structs"
1719
)
@@ -63,6 +65,15 @@ type (
6365
}
6466
)
6567

68+
// GetDiscordHook returns discord metadata
69+
func GetDiscordHook(w *models.Webhook) *DiscordMeta {
70+
s := &DiscordMeta{}
71+
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
72+
log.Error("webhook.GetDiscordHook(%d): %v", w.ID, err)
73+
}
74+
return s
75+
}
76+
6677
func color(clr string) int {
6778
if clr != "" {
6879
clr = strings.TrimLeft(clr, "#")
@@ -288,7 +299,7 @@ func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPa
288299

289300
func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, discord *DiscordMeta) (*DiscordPayload, error) {
290301
title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title)
291-
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
302+
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID))
292303
content := ""
293304
var color int
294305
switch p.Action {
@@ -421,7 +432,7 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta)
421432
}, nil
422433
}
423434

424-
func getDiscordPullRequestApprovalPayload(p *api.PullRequestPayload, meta *DiscordMeta, event HookEventType) (*DiscordPayload, error) {
435+
func getDiscordPullRequestApprovalPayload(p *api.PullRequestPayload, meta *DiscordMeta, event models.HookEventType) (*DiscordPayload, error) {
425436
var text, title string
426437
var color int
427438
switch p.Action {
@@ -435,11 +446,11 @@ func getDiscordPullRequestApprovalPayload(p *api.PullRequestPayload, meta *Disco
435446
text = p.Review.Content
436447

437448
switch event {
438-
case HookEventPullRequestApproved:
449+
case models.HookEventPullRequestApproved:
439450
color = greenColor
440-
case HookEventPullRequestRejected:
451+
case models.HookEventPullRequestRejected:
441452
color = redColor
442-
case HookEventPullRequestComment:
453+
case models.HookEventPullRequestComment:
443454
color = greyColor
444455
default:
445456
color = yellowColor
@@ -534,7 +545,7 @@ func getDiscordReleasePayload(p *api.ReleasePayload, meta *DiscordMeta) (*Discor
534545
}
535546

536547
// GetDiscordPayload converts a discord webhook into a DiscordPayload
537-
func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (*DiscordPayload, error) {
548+
func GetDiscordPayload(p api.Payloader, event models.HookEventType, meta string) (*DiscordPayload, error) {
538549
s := new(DiscordPayload)
539550

540551
discord := &DiscordMeta{}
@@ -543,40 +554,40 @@ func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (*Disc
543554
}
544555

545556
switch event {
546-
case HookEventCreate:
557+
case models.HookEventCreate:
547558
return getDiscordCreatePayload(p.(*api.CreatePayload), discord)
548-
case HookEventDelete:
559+
case models.HookEventDelete:
549560
return getDiscordDeletePayload(p.(*api.DeletePayload), discord)
550-
case HookEventFork:
561+
case models.HookEventFork:
551562
return getDiscordForkPayload(p.(*api.ForkPayload), discord)
552-
case HookEventIssues:
563+
case models.HookEventIssues:
553564
return getDiscordIssuesPayload(p.(*api.IssuePayload), discord)
554-
case HookEventIssueComment:
565+
case models.HookEventIssueComment:
555566
return getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), discord)
556-
case HookEventPush:
567+
case models.HookEventPush:
557568
return getDiscordPushPayload(p.(*api.PushPayload), discord)
558-
case HookEventPullRequest:
569+
case models.HookEventPullRequest:
559570
return getDiscordPullRequestPayload(p.(*api.PullRequestPayload), discord)
560-
case HookEventPullRequestRejected, HookEventPullRequestApproved, HookEventPullRequestComment:
571+
case models.HookEventPullRequestRejected, models.HookEventPullRequestApproved, models.HookEventPullRequestComment:
561572
return getDiscordPullRequestApprovalPayload(p.(*api.PullRequestPayload), discord, event)
562-
case HookEventRepository:
573+
case models.HookEventRepository:
563574
return getDiscordRepositoryPayload(p.(*api.RepositoryPayload), discord)
564-
case HookEventRelease:
575+
case models.HookEventRelease:
565576
return getDiscordReleasePayload(p.(*api.ReleasePayload), discord)
566577
}
567578

568579
return s, nil
569580
}
570581

571-
func parseHookPullRequestEventType(event HookEventType) (string, error) {
582+
func parseHookPullRequestEventType(event models.HookEventType) (string, error) {
572583

573584
switch event {
574585

575-
case HookEventPullRequestApproved:
586+
case models.HookEventPullRequestApproved:
576587
return "approved", nil
577-
case HookEventPullRequestRejected:
588+
case models.HookEventPullRequestRejected:
578589
return "rejected", nil
579-
case HookEventPullRequestComment:
590+
case models.HookEventPullRequestComment:
580591
return "comment", nil
581592

582593
default:

models/webhook_msteams.go renamed to modules/webhook/msteams.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

5-
package models
5+
package webhook
66

77
import (
88
"encoding/json"
99
"fmt"
1010
"strings"
1111

12+
"code.gitea.io/gitea/models"
1213
"code.gitea.io/gitea/modules/git"
1314
api "code.gitea.io/gitea/modules/structs"
1415
)
@@ -357,7 +358,7 @@ func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) {
357358

358359
func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload, error) {
359360
title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title)
360-
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
361+
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID))
361362
content := ""
362363
var color int
363364
switch p.Action {
@@ -530,7 +531,7 @@ func getMSTeamsPullRequestPayload(p *api.PullRequestPayload) (*MSTeamsPayload, e
530531
}, nil
531532
}
532533

533-
func getMSTeamsPullRequestApprovalPayload(p *api.PullRequestPayload, event HookEventType) (*MSTeamsPayload, error) {
534+
func getMSTeamsPullRequestApprovalPayload(p *api.PullRequestPayload, event models.HookEventType) (*MSTeamsPayload, error) {
534535
var text, title string
535536
var color int
536537
switch p.Action {
@@ -544,11 +545,11 @@ func getMSTeamsPullRequestApprovalPayload(p *api.PullRequestPayload, event HookE
544545
text = p.Review.Content
545546

546547
switch event {
547-
case HookEventPullRequestApproved:
548+
case models.HookEventPullRequestApproved:
548549
color = greenColor
549-
case HookEventPullRequestRejected:
550+
case models.HookEventPullRequestRejected:
550551
color = redColor
551-
case HookEventPullRequestComment:
552+
case models.HookEventPullRequestComment:
552553
color = greyColor
553554
default:
554555
color = yellowColor
@@ -699,29 +700,29 @@ func getMSTeamsReleasePayload(p *api.ReleasePayload) (*MSTeamsPayload, error) {
699700
}
700701

701702
// GetMSTeamsPayload converts a MSTeams webhook into a MSTeamsPayload
702-
func GetMSTeamsPayload(p api.Payloader, event HookEventType, meta string) (*MSTeamsPayload, error) {
703+
func GetMSTeamsPayload(p api.Payloader, event models.HookEventType, meta string) (*MSTeamsPayload, error) {
703704
s := new(MSTeamsPayload)
704705

705706
switch event {
706-
case HookEventCreate:
707+
case models.HookEventCreate:
707708
return getMSTeamsCreatePayload(p.(*api.CreatePayload))
708-
case HookEventDelete:
709+
case models.HookEventDelete:
709710
return getMSTeamsDeletePayload(p.(*api.DeletePayload))
710-
case HookEventFork:
711+
case models.HookEventFork:
711712
return getMSTeamsForkPayload(p.(*api.ForkPayload))
712-
case HookEventIssues:
713+
case models.HookEventIssues:
713714
return getMSTeamsIssuesPayload(p.(*api.IssuePayload))
714-
case HookEventIssueComment:
715+
case models.HookEventIssueComment:
715716
return getMSTeamsIssueCommentPayload(p.(*api.IssueCommentPayload))
716-
case HookEventPush:
717+
case models.HookEventPush:
717718
return getMSTeamsPushPayload(p.(*api.PushPayload))
718-
case HookEventPullRequest:
719+
case models.HookEventPullRequest:
719720
return getMSTeamsPullRequestPayload(p.(*api.PullRequestPayload))
720-
case HookEventPullRequestRejected, HookEventPullRequestApproved, HookEventPullRequestComment:
721+
case models.HookEventPullRequestRejected, models.HookEventPullRequestApproved, models.HookEventPullRequestComment:
721722
return getMSTeamsPullRequestApprovalPayload(p.(*api.PullRequestPayload), event)
722-
case HookEventRepository:
723+
case models.HookEventRepository:
723724
return getMSTeamsRepositoryPayload(p.(*api.RepositoryPayload))
724-
case HookEventRelease:
725+
case models.HookEventRelease:
725726
return getMSTeamsReleasePayload(p.(*api.ReleasePayload))
726727
}
727728

0 commit comments

Comments
 (0)