Skip to content

Commit 42354df

Browse files
lunny6543zeripath
authored
Move webhook type from int to string (#13664)
* Move webhook type from int to string * rename webhook_services * finish refactor * Fix merge * Ignore unnecessary ci Co-authored-by: 6543 <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 4d66ee1 commit 42354df

30 files changed

+186
-174
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ issues:
101101
- path: cmd/dump.go
102102
linters:
103103
- dupl
104+
- path: services/webhook/webhook.go
105+
linters:
106+
- structcheck
104107
- text: "commentFormatting: put a space between `//` and comment text"
105108
linters:
106109
- gocritic

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ var migrations = []Migration{
265265
NewMigration("update reactions constraint", updateReactionConstraint),
266266
// v160 -> v161
267267
NewMigration("Add block on official review requests branch protection", addBlockOnOfficialReviewRequests),
268+
// v161 -> v162
269+
NewMigration("Convert task type from int to string", convertTaskTypeToString),
268270
}
269271

270272
// GetCurrentDBVersion returns the current db version

models/migrations/v161.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2020 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 migrations
6+
7+
import (
8+
"xorm.io/xorm"
9+
)
10+
11+
func convertTaskTypeToString(x *xorm.Engine) error {
12+
const (
13+
GOGS int = iota + 1
14+
SLACK
15+
GITEA
16+
DISCORD
17+
DINGTALK
18+
TELEGRAM
19+
MSTEAMS
20+
FEISHU
21+
MATRIX
22+
)
23+
24+
var hookTaskTypes = map[int]string{
25+
GITEA: "gitea",
26+
GOGS: "gogs",
27+
SLACK: "slack",
28+
DISCORD: "discord",
29+
DINGTALK: "dingtalk",
30+
TELEGRAM: "telegram",
31+
MSTEAMS: "msteams",
32+
FEISHU: "feishu",
33+
MATRIX: "matrix",
34+
}
35+
36+
type HookTask struct {
37+
Typ string `xorm:"char(16) index"`
38+
}
39+
if err := x.Sync2(new(HookTask)); err != nil {
40+
return err
41+
}
42+
43+
for i, s := range hookTaskTypes {
44+
if _, err := x.Exec("UPDATE hook_task set typ = ? where type=?", s, i); err != nil {
45+
return err
46+
}
47+
}
48+
49+
sess := x.NewSession()
50+
defer sess.Close()
51+
if err := sess.Begin(); err != nil {
52+
return err
53+
}
54+
if err := dropTableColumns(sess, "hook_task", "type"); err != nil {
55+
return err
56+
}
57+
58+
return sess.Commit()
59+
}

models/webhook.go

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -547,69 +547,21 @@ func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
547547
// \/ \/ \/ \/ \/
548548

549549
// HookTaskType is the type of an hook task
550-
type HookTaskType int
550+
type HookTaskType string
551551

552552
// Types of hook tasks
553553
const (
554-
GOGS HookTaskType = iota + 1
555-
SLACK
556-
GITEA
557-
DISCORD
558-
DINGTALK
559-
TELEGRAM
560-
MSTEAMS
561-
FEISHU
562-
MATRIX
554+
GITEA HookTaskType = "gitea"
555+
GOGS HookTaskType = "gogs"
556+
SLACK HookTaskType = "slack"
557+
DISCORD HookTaskType = "discord"
558+
DINGTALK HookTaskType = "dingtalk"
559+
TELEGRAM HookTaskType = "telegram"
560+
MSTEAMS HookTaskType = "msteams"
561+
FEISHU HookTaskType = "feishu"
562+
MATRIX HookTaskType = "matrix"
563563
)
564564

565-
var hookTaskTypes = map[string]HookTaskType{
566-
"gitea": GITEA,
567-
"gogs": GOGS,
568-
"slack": SLACK,
569-
"discord": DISCORD,
570-
"dingtalk": DINGTALK,
571-
"telegram": TELEGRAM,
572-
"msteams": MSTEAMS,
573-
"feishu": FEISHU,
574-
"matrix": MATRIX,
575-
}
576-
577-
// ToHookTaskType returns HookTaskType by given name.
578-
func ToHookTaskType(name string) HookTaskType {
579-
return hookTaskTypes[name]
580-
}
581-
582-
// Name returns the name of an hook task type
583-
func (t HookTaskType) Name() string {
584-
switch t {
585-
case GITEA:
586-
return "gitea"
587-
case GOGS:
588-
return "gogs"
589-
case SLACK:
590-
return "slack"
591-
case DISCORD:
592-
return "discord"
593-
case DINGTALK:
594-
return "dingtalk"
595-
case TELEGRAM:
596-
return "telegram"
597-
case MSTEAMS:
598-
return "msteams"
599-
case FEISHU:
600-
return "feishu"
601-
case MATRIX:
602-
return "matrix"
603-
}
604-
return ""
605-
}
606-
607-
// IsValidHookTaskType returns true if given name is a valid hook task type.
608-
func IsValidHookTaskType(name string) bool {
609-
_, ok := hookTaskTypes[name]
610-
return ok
611-
}
612-
613565
// HookEventType is the type of an hook event
614566
type HookEventType string
615567

@@ -687,7 +639,7 @@ type HookTask struct {
687639
RepoID int64 `xorm:"INDEX"`
688640
HookID int64
689641
UUID string
690-
Type HookTaskType
642+
Typ HookTaskType
691643
URL string `xorm:"TEXT"`
692644
Signature string `xorm:"TEXT"`
693645
api.Payloader `xorm:"-"`

models/webhook_test.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -185,28 +185,6 @@ func TestDeleteWebhookByOrgID(t *testing.T) {
185185
assert.True(t, IsErrWebhookNotExist(err))
186186
}
187187

188-
func TestToHookTaskType(t *testing.T) {
189-
assert.Equal(t, GOGS, ToHookTaskType("gogs"))
190-
assert.Equal(t, SLACK, ToHookTaskType("slack"))
191-
assert.Equal(t, GITEA, ToHookTaskType("gitea"))
192-
assert.Equal(t, TELEGRAM, ToHookTaskType("telegram"))
193-
}
194-
195-
func TestHookTaskType_Name(t *testing.T) {
196-
assert.Equal(t, "gogs", GOGS.Name())
197-
assert.Equal(t, "slack", SLACK.Name())
198-
assert.Equal(t, "gitea", GITEA.Name())
199-
assert.Equal(t, "telegram", TELEGRAM.Name())
200-
}
201-
202-
func TestIsValidHookTaskType(t *testing.T) {
203-
assert.True(t, IsValidHookTaskType("gogs"))
204-
assert.True(t, IsValidHookTaskType("slack"))
205-
assert.True(t, IsValidHookTaskType("gitea"))
206-
assert.True(t, IsValidHookTaskType("telegram"))
207-
assert.False(t, IsValidHookTaskType("invalid"))
208-
}
209-
210188
func TestHookTasks(t *testing.T) {
211189
assert.NoError(t, PrepareTestDatabase())
212190
hookTasks, err := HookTasks(1, 1)
@@ -225,7 +203,7 @@ func TestCreateHookTask(t *testing.T) {
225203
hookTask := &HookTask{
226204
RepoID: 3,
227205
HookID: 3,
228-
Type: GITEA,
206+
Typ: GITEA,
229207
URL: "http://www.example.com/unit_test",
230208
Payloader: &api.PushPayload{},
231209
}

modules/convert/convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/structs"
1717
api "code.gitea.io/gitea/modules/structs"
1818
"code.gitea.io/gitea/modules/util"
19-
"code.gitea.io/gitea/modules/webhook"
19+
"code.gitea.io/gitea/services/webhook"
2020

2121
"github.com/unknwon/com"
2222
)
@@ -237,7 +237,7 @@ func ToHook(repoLink string, w *models.Webhook) *api.Hook {
237237

238238
return &api.Hook{
239239
ID: w.ID,
240-
Type: w.HookTaskType.Name(),
240+
Type: string(w.HookTaskType),
241241
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
242242
Active: w.IsActive,
243243
Config: config,

0 commit comments

Comments
 (0)