Skip to content

Commit 6edfa6b

Browse files
lunnyjolheiser
andauthored
Fix broken migration on webhook (#13911)
* Fix broken migration on webhook * Fix lint Co-authored-by: John Olheiser <[email protected]>
1 parent 18e4477 commit 6edfa6b

File tree

8 files changed

+98
-37
lines changed

8 files changed

+98
-37
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ var migrations = []Migration{
267267
NewMigration("Add block on official review requests branch protection", addBlockOnOfficialReviewRequests),
268268
// v161 -> v162
269269
NewMigration("Convert task type from int to string", convertTaskTypeToString),
270+
// v162 -> v163
271+
NewMigration("Convert webhook task type from int to string", convertWebhookTaskTypeToString),
270272
}
271273

272274
// GetCurrentDBVersion returns the current db version

models/migrations/v162.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 convertWebhookTaskTypeToString(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 Webhook struct {
37+
Type string `xorm:"char(16) index"`
38+
}
39+
if err := x.Sync2(new(Webhook)); err != nil {
40+
return err
41+
}
42+
43+
for i, s := range hookTaskTypes {
44+
if _, err := x.Exec("UPDATE webhook set type = ? where hook_task_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, "webhook", "hook_task_type"); err != nil {
55+
return err
56+
}
57+
58+
return sess.Commit()
59+
}

models/repo_generate.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,17 @@ func GenerateWebhooks(ctx DBContext, templateRepo, generateRepo *Repository) err
117117

118118
for _, templateWebhook := range templateWebhooks {
119119
generateWebhook := &Webhook{
120-
RepoID: generateRepo.ID,
121-
URL: templateWebhook.URL,
122-
HTTPMethod: templateWebhook.HTTPMethod,
123-
ContentType: templateWebhook.ContentType,
124-
Secret: templateWebhook.Secret,
125-
HookEvent: templateWebhook.HookEvent,
126-
IsActive: templateWebhook.IsActive,
127-
HookTaskType: templateWebhook.HookTaskType,
128-
OrgID: templateWebhook.OrgID,
129-
Events: templateWebhook.Events,
130-
Meta: templateWebhook.Meta,
120+
RepoID: generateRepo.ID,
121+
URL: templateWebhook.URL,
122+
HTTPMethod: templateWebhook.HTTPMethod,
123+
ContentType: templateWebhook.ContentType,
124+
Secret: templateWebhook.Secret,
125+
HookEvent: templateWebhook.HookEvent,
126+
IsActive: templateWebhook.IsActive,
127+
Type: templateWebhook.Type,
128+
OrgID: templateWebhook.OrgID,
129+
Events: templateWebhook.Events,
130+
Meta: templateWebhook.Meta,
131131
}
132132
if err := createWebhook(ctx.e, generateWebhook); err != nil {
133133
return err

models/webhook.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ type Webhook struct {
110110
Secret string `xorm:"TEXT"`
111111
Events string `xorm:"TEXT"`
112112
*HookEvent `xorm:"-"`
113-
IsSSL bool `xorm:"is_ssl"`
114-
IsActive bool `xorm:"INDEX"`
115-
HookTaskType HookTaskType
116-
Meta string `xorm:"TEXT"` // store hook-specific attributes
117-
LastStatus HookStatus // Last delivery status
113+
IsSSL bool `xorm:"is_ssl"`
114+
IsActive bool `xorm:"INDEX"`
115+
Type HookTaskType `xorm:"char(16) 'type'"`
116+
Meta string `xorm:"TEXT"` // store hook-specific attributes
117+
LastStatus HookStatus // Last delivery status
118118

119119
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
120120
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`

modules/convert/convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func ToHook(repoLink string, w *models.Webhook) *api.Hook {
227227
"url": w.URL,
228228
"content_type": w.ContentType.Name(),
229229
}
230-
if w.HookTaskType == models.SLACK {
230+
if w.Type == models.SLACK {
231231
s := webhook.GetSlackHook(w)
232232
config["channel"] = s.Channel
233233
config["username"] = s.Username
@@ -237,7 +237,7 @@ func ToHook(repoLink string, w *models.Webhook) *api.Hook {
237237

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

routers/api/v1/utils/hook.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
132132
},
133133
BranchFilter: form.BranchFilter,
134134
},
135-
IsActive: form.Active,
136-
HookTaskType: models.HookTaskType(form.Type),
135+
IsActive: form.Active,
136+
Type: models.HookTaskType(form.Type),
137137
}
138-
if w.HookTaskType == models.SLACK {
138+
if w.Type == models.SLACK {
139139
channel, ok := form.Config["channel"]
140140
if !ok {
141141
ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: channel")
@@ -219,7 +219,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho
219219
w.ContentType = models.ToHookContentType(ct)
220220
}
221221

222-
if w.HookTaskType == models.SLACK {
222+
if w.Type == models.SLACK {
223223
if channel, ok := form.Config["channel"]; ok {
224224
meta, err := json.Marshal(&webhook.SlackMeta{
225225
Channel: channel,

routers/repo/webhook.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func GiteaHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
208208
Secret: form.Secret,
209209
HookEvent: ParseHookEvent(form.WebhookForm),
210210
IsActive: form.Active,
211-
HookTaskType: models.GITEA,
211+
Type: models.GITEA,
212212
OrgID: orCtx.OrgID,
213213
IsSystemWebhook: orCtx.IsSystemWebhook,
214214
}
@@ -261,7 +261,7 @@ func newGogsWebhookPost(ctx *context.Context, form auth.NewGogshookForm, kind mo
261261
Secret: form.Secret,
262262
HookEvent: ParseHookEvent(form.WebhookForm),
263263
IsActive: form.Active,
264-
HookTaskType: kind,
264+
Type: kind,
265265
OrgID: orCtx.OrgID,
266266
IsSystemWebhook: orCtx.IsSystemWebhook,
267267
}
@@ -311,7 +311,7 @@ func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
311311
ContentType: models.ContentTypeJSON,
312312
HookEvent: ParseHookEvent(form.WebhookForm),
313313
IsActive: form.Active,
314-
HookTaskType: models.DISCORD,
314+
Type: models.DISCORD,
315315
Meta: string(meta),
316316
OrgID: orCtx.OrgID,
317317
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -353,7 +353,7 @@ func DingtalkHooksNewPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
353353
ContentType: models.ContentTypeJSON,
354354
HookEvent: ParseHookEvent(form.WebhookForm),
355355
IsActive: form.Active,
356-
HookTaskType: models.DINGTALK,
356+
Type: models.DINGTALK,
357357
Meta: "",
358358
OrgID: orCtx.OrgID,
359359
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -404,7 +404,7 @@ func TelegramHooksNewPost(ctx *context.Context, form auth.NewTelegramHookForm) {
404404
ContentType: models.ContentTypeJSON,
405405
HookEvent: ParseHookEvent(form.WebhookForm),
406406
IsActive: form.Active,
407-
HookTaskType: models.TELEGRAM,
407+
Type: models.TELEGRAM,
408408
Meta: string(meta),
409409
OrgID: orCtx.OrgID,
410410
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -458,7 +458,7 @@ func MatrixHooksNewPost(ctx *context.Context, form auth.NewMatrixHookForm) {
458458
HTTPMethod: "PUT",
459459
HookEvent: ParseHookEvent(form.WebhookForm),
460460
IsActive: form.Active,
461-
HookTaskType: models.MATRIX,
461+
Type: models.MATRIX,
462462
Meta: string(meta),
463463
OrgID: orCtx.OrgID,
464464
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -500,7 +500,7 @@ func MSTeamsHooksNewPost(ctx *context.Context, form auth.NewMSTeamsHookForm) {
500500
ContentType: models.ContentTypeJSON,
501501
HookEvent: ParseHookEvent(form.WebhookForm),
502502
IsActive: form.Active,
503-
HookTaskType: models.MSTEAMS,
503+
Type: models.MSTEAMS,
504504
Meta: "",
505505
OrgID: orCtx.OrgID,
506506
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -559,7 +559,7 @@ func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
559559
ContentType: models.ContentTypeJSON,
560560
HookEvent: ParseHookEvent(form.WebhookForm),
561561
IsActive: form.Active,
562-
HookTaskType: models.SLACK,
562+
Type: models.SLACK,
563563
Meta: string(meta),
564564
OrgID: orCtx.OrgID,
565565
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -601,7 +601,7 @@ func FeishuHooksNewPost(ctx *context.Context, form auth.NewFeishuHookForm) {
601601
ContentType: models.ContentTypeJSON,
602602
HookEvent: ParseHookEvent(form.WebhookForm),
603603
IsActive: form.Active,
604-
HookTaskType: models.FEISHU,
604+
Type: models.FEISHU,
605605
Meta: "",
606606
OrgID: orCtx.OrgID,
607607
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -647,8 +647,8 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
647647
return nil, nil
648648
}
649649

650-
ctx.Data["HookType"] = w.HookTaskType
651-
switch w.HookTaskType {
650+
ctx.Data["HookType"] = w.Type
651+
switch w.Type {
652652
case models.SLACK:
653653
ctx.Data["SlackHook"] = webhook.GetSlackHook(w)
654654
case models.DISCORD:

services/webhook/webhook.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo
128128
// Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.).
129129
// Integration webhooks (e.g. drone) still receive the required data.
130130
if pushEvent, ok := p.(*api.PushPayload); ok &&
131-
w.HookTaskType != models.GITEA && w.HookTaskType != models.GOGS &&
131+
w.Type != models.GITEA && w.Type != models.GOGS &&
132132
len(pushEvent.Commits) == 0 {
133133
return nil
134134
}
@@ -144,11 +144,11 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo
144144

145145
var payloader api.Payloader
146146
var err error
147-
webhook, ok := webhooks[w.HookTaskType]
147+
webhook, ok := webhooks[w.Type]
148148
if ok {
149149
payloader, err = webhook.payloadCreator(p, event, w.Meta)
150150
if err != nil {
151-
return fmt.Errorf("create payload for %s[%s]: %v", w.HookTaskType, event, err)
151+
return fmt.Errorf("create payload for %s[%s]: %v", w.Type, event, err)
152152
}
153153
} else {
154154
p.SetSecret(w.Secret)
@@ -172,7 +172,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo
172172
if err = models.CreateHookTask(&models.HookTask{
173173
RepoID: repo.ID,
174174
HookID: w.ID,
175-
Typ: w.HookTaskType,
175+
Typ: w.Type,
176176
URL: w.URL,
177177
Signature: signature,
178178
Payloader: payloader,

0 commit comments

Comments
 (0)