Skip to content

Commit

Permalink
Move more webhook codes from models to webhook module (#8802)
Browse files Browse the repository at this point in the history
* Move more webhook codes from models to webhook module
  • Loading branch information
lunny authored and zeripath committed Nov 3, 2019
1 parent 491887d commit a966a02
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 135 deletions.
27 changes: 0 additions & 27 deletions models/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,33 +118,6 @@ func (w *Webhook) AfterLoad() {
}
}

// GetSlackHook returns slack metadata
func (w *Webhook) GetSlackHook() *SlackMeta {
s := &SlackMeta{}
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
log.Error("webhook.GetSlackHook(%d): %v", w.ID, err)
}
return s
}

// GetDiscordHook returns discord metadata
func (w *Webhook) GetDiscordHook() *DiscordMeta {
s := &DiscordMeta{}
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
log.Error("webhook.GetDiscordHook(%d): %v", w.ID, err)
}
return s
}

// GetTelegramHook returns telegram metadata
func (w *Webhook) GetTelegramHook() *TelegramMeta {
s := &TelegramMeta{}
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err)
}
return s
}

// History returns history of webhook by given conditions.
func (w *Webhook) History(page int) ([]*HookTask, error) {
return HookTasks(w.ID, page)
Expand Down
12 changes: 0 additions & 12 deletions models/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ func TestIsValidHookContentType(t *testing.T) {
assert.False(t, IsValidHookContentType("invalid"))
}

func TestWebhook_GetSlackHook(t *testing.T) {
w := &Webhook{
Meta: `{"channel": "foo", "username": "username", "color": "blue"}`,
}
slackHook := w.GetSlackHook()
assert.Equal(t, *slackHook, SlackMeta{
Channel: "foo",
Username: "username",
Color: "blue",
})
}

func TestWebhook_History(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
Expand Down
29 changes: 15 additions & 14 deletions models/webhook_dingtalk.go → modules/webhook/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// 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"
"fmt"
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"

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

func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, 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))
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID))
var content string
switch p.Action {
case api.HookIssueCommentCreated:
Expand Down Expand Up @@ -286,7 +287,7 @@ func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload,
}, nil
}

func getDingtalkPullRequestApprovalPayload(p *api.PullRequestPayload, event HookEventType) (*DingtalkPayload, error) {
func getDingtalkPullRequestApprovalPayload(p *api.PullRequestPayload, event models.HookEventType) (*DingtalkPayload, error) {
var text, title string
switch p.Action {
case api.HookIssueSynchronized:
Expand Down Expand Up @@ -392,29 +393,29 @@ func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error)
}

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

switch event {
case HookEventCreate:
case models.HookEventCreate:
return getDingtalkCreatePayload(p.(*api.CreatePayload))
case HookEventDelete:
case models.HookEventDelete:
return getDingtalkDeletePayload(p.(*api.DeletePayload))
case HookEventFork:
case models.HookEventFork:
return getDingtalkForkPayload(p.(*api.ForkPayload))
case HookEventIssues:
case models.HookEventIssues:
return getDingtalkIssuesPayload(p.(*api.IssuePayload))
case HookEventIssueComment:
case models.HookEventIssueComment:
return getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
case HookEventPush:
case models.HookEventPush:
return getDingtalkPushPayload(p.(*api.PushPayload))
case HookEventPullRequest:
case models.HookEventPullRequest:
return getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
case HookEventPullRequestApproved, HookEventPullRequestRejected, HookEventPullRequestComment:
case models.HookEventPullRequestApproved, models.HookEventPullRequestRejected, models.HookEventPullRequestComment:
return getDingtalkPullRequestApprovalPayload(p.(*api.PullRequestPayload), event)
case HookEventRepository:
case models.HookEventRepository:
return getDingtalkRepositoryPayload(p.(*api.RepositoryPayload))
case HookEventRelease:
case models.HookEventRelease:
return getDingtalkReleasePayload(p.(*api.ReleasePayload))
}

Expand Down
53 changes: 32 additions & 21 deletions models/webhook_discord.go → modules/webhook/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -11,7 +11,9 @@ import (
"strconv"
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
)
Expand Down Expand Up @@ -63,6 +65,15 @@ type (
}
)

// GetDiscordHook returns discord metadata
func GetDiscordHook(w *models.Webhook) *DiscordMeta {
s := &DiscordMeta{}
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
log.Error("webhook.GetDiscordHook(%d): %v", w.ID, err)
}
return s
}

func color(clr string) int {
if clr != "" {
clr = strings.TrimLeft(clr, "#")
Expand Down Expand Up @@ -288,7 +299,7 @@ func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPa

func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, discord *DiscordMeta) (*DiscordPayload, 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))
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID))
content := ""
var color int
switch p.Action {
Expand Down Expand Up @@ -421,7 +432,7 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta)
}, nil
}

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

switch event {
case HookEventPullRequestApproved:
case models.HookEventPullRequestApproved:
color = greenColor
case HookEventPullRequestRejected:
case models.HookEventPullRequestRejected:
color = redColor
case HookEventPullRequestComment:
case models.HookEventPullRequestComment:
color = greyColor
default:
color = yellowColor
Expand Down Expand Up @@ -534,7 +545,7 @@ func getDiscordReleasePayload(p *api.ReleasePayload, meta *DiscordMeta) (*Discor
}

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

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

switch event {
case HookEventCreate:
case models.HookEventCreate:
return getDiscordCreatePayload(p.(*api.CreatePayload), discord)
case HookEventDelete:
case models.HookEventDelete:
return getDiscordDeletePayload(p.(*api.DeletePayload), discord)
case HookEventFork:
case models.HookEventFork:
return getDiscordForkPayload(p.(*api.ForkPayload), discord)
case HookEventIssues:
case models.HookEventIssues:
return getDiscordIssuesPayload(p.(*api.IssuePayload), discord)
case HookEventIssueComment:
case models.HookEventIssueComment:
return getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), discord)
case HookEventPush:
case models.HookEventPush:
return getDiscordPushPayload(p.(*api.PushPayload), discord)
case HookEventPullRequest:
case models.HookEventPullRequest:
return getDiscordPullRequestPayload(p.(*api.PullRequestPayload), discord)
case HookEventPullRequestRejected, HookEventPullRequestApproved, HookEventPullRequestComment:
case models.HookEventPullRequestRejected, models.HookEventPullRequestApproved, models.HookEventPullRequestComment:
return getDiscordPullRequestApprovalPayload(p.(*api.PullRequestPayload), discord, event)
case HookEventRepository:
case models.HookEventRepository:
return getDiscordRepositoryPayload(p.(*api.RepositoryPayload), discord)
case HookEventRelease:
case models.HookEventRelease:
return getDiscordReleasePayload(p.(*api.ReleasePayload), discord)
}

return s, nil
}

func parseHookPullRequestEventType(event HookEventType) (string, error) {
func parseHookPullRequestEventType(event models.HookEventType) (string, error) {

switch event {

case HookEventPullRequestApproved:
case models.HookEventPullRequestApproved:
return "approved", nil
case HookEventPullRequestRejected:
case models.HookEventPullRequestRejected:
return "rejected", nil
case HookEventPullRequestComment:
case models.HookEventPullRequestComment:
return "comment", nil

default:
Expand Down
35 changes: 18 additions & 17 deletions models/webhook_msteams.go → modules/webhook/msteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// 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"
"fmt"
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
)
Expand Down Expand Up @@ -357,7 +358,7 @@ func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) {

func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload, 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))
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID))
content := ""
var color int
switch p.Action {
Expand Down Expand Up @@ -530,7 +531,7 @@ func getMSTeamsPullRequestPayload(p *api.PullRequestPayload) (*MSTeamsPayload, e
}, nil
}

func getMSTeamsPullRequestApprovalPayload(p *api.PullRequestPayload, event HookEventType) (*MSTeamsPayload, error) {
func getMSTeamsPullRequestApprovalPayload(p *api.PullRequestPayload, event models.HookEventType) (*MSTeamsPayload, error) {
var text, title string
var color int
switch p.Action {
Expand All @@ -544,11 +545,11 @@ func getMSTeamsPullRequestApprovalPayload(p *api.PullRequestPayload, event HookE
text = p.Review.Content

switch event {
case HookEventPullRequestApproved:
case models.HookEventPullRequestApproved:
color = greenColor
case HookEventPullRequestRejected:
case models.HookEventPullRequestRejected:
color = redColor
case HookEventPullRequestComment:
case models.HookEventPullRequestComment:
color = greyColor
default:
color = yellowColor
Expand Down Expand Up @@ -699,29 +700,29 @@ func getMSTeamsReleasePayload(p *api.ReleasePayload) (*MSTeamsPayload, error) {
}

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

switch event {
case HookEventCreate:
case models.HookEventCreate:
return getMSTeamsCreatePayload(p.(*api.CreatePayload))
case HookEventDelete:
case models.HookEventDelete:
return getMSTeamsDeletePayload(p.(*api.DeletePayload))
case HookEventFork:
case models.HookEventFork:
return getMSTeamsForkPayload(p.(*api.ForkPayload))
case HookEventIssues:
case models.HookEventIssues:
return getMSTeamsIssuesPayload(p.(*api.IssuePayload))
case HookEventIssueComment:
case models.HookEventIssueComment:
return getMSTeamsIssueCommentPayload(p.(*api.IssueCommentPayload))
case HookEventPush:
case models.HookEventPush:
return getMSTeamsPushPayload(p.(*api.PushPayload))
case HookEventPullRequest:
case models.HookEventPullRequest:
return getMSTeamsPullRequestPayload(p.(*api.PullRequestPayload))
case HookEventPullRequestRejected, HookEventPullRequestApproved, HookEventPullRequestComment:
case models.HookEventPullRequestRejected, models.HookEventPullRequestApproved, models.HookEventPullRequestComment:
return getMSTeamsPullRequestApprovalPayload(p.(*api.PullRequestPayload), event)
case HookEventRepository:
case models.HookEventRepository:
return getMSTeamsRepositoryPayload(p.(*api.RepositoryPayload))
case HookEventRelease:
case models.HookEventRelease:
return getMSTeamsReleasePayload(p.(*api.ReleasePayload))
}

Expand Down
Loading

0 comments on commit a966a02

Please sign in to comment.