summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2017-08-29 22:55:24 +0800
committerGitHub <noreply@github.com>2017-08-29 22:55:24 +0800
commit3e6fed368bbd837854292d3855463d567a689943 (patch)
treedcdff5d6a9ab69f6bd41158ebfee615f208f9c64
parent491f0ebc58854560f72a5360060dac1fee3f1883 (diff)
downloadgitea-3e6fed368bbd837854292d3855463d567a689943.tar.gz
gitea-3e6fed368bbd837854292d3855463d567a689943.zip
fix webhook test (#2415)
* fix webhook test * fix error message * fix typo
-rw-r--r--models/webhook.go97
-rw-r--r--routers/repo/webhook.go12
2 files changed, 63 insertions, 46 deletions
diff --git a/models/webhook.go b/models/webhook.go
index 61840a9811..5ad27ee8f3 100644
--- a/models/webhook.go
+++ b/models/webhook.go
@@ -482,6 +482,57 @@ func UpdateHookTask(t *HookTask) error {
return err
}
+// PrepareWebhook adds special webhook to task queue for given payload.
+func PrepareWebhook(w *Webhook, repo *Repository, event HookEventType, p api.Payloader) error {
+ switch event {
+ case HookEventCreate:
+ if !w.HasCreateEvent() {
+ return nil
+ }
+ case HookEventPush:
+ if !w.HasPushEvent() {
+ return nil
+ }
+ case HookEventPullRequest:
+ if !w.HasPullRequestEvent() {
+ return nil
+ }
+ }
+
+ var payloader api.Payloader
+ var err error
+ // Use separate objects so modifications won't be made on payload on non-Gogs/Gitea type hooks.
+ switch w.HookTaskType {
+ case SLACK:
+ payloader, err = GetSlackPayload(p, event, w.Meta)
+ if err != nil {
+ return fmt.Errorf("GetSlackPayload: %v", err)
+ }
+ case DISCORD:
+ payloader, err = GetDiscordPayload(p, event, w.Meta)
+ if err != nil {
+ return fmt.Errorf("GetDiscordPayload: %v", err)
+ }
+ default:
+ p.SetSecret(w.Secret)
+ payloader = p
+ }
+
+ if err = CreateHookTask(&HookTask{
+ RepoID: repo.ID,
+ HookID: w.ID,
+ Type: w.HookTaskType,
+ URL: w.URL,
+ Payloader: payloader,
+ ContentType: w.ContentType,
+ EventType: event,
+ IsSSL: w.IsSSL,
+ }); err != nil {
+ return fmt.Errorf("CreateHookTask: %v", err)
+ }
+ return nil
+}
+
// PrepareWebhooks adds new webhooks to task queue for given payload.
func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
ws, err := GetActiveWebhooksByRepoID(repo.ID)
@@ -503,51 +554,9 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
return nil
}
- var payloader api.Payloader
for _, w := range ws {
- switch event {
- case HookEventCreate:
- if !w.HasCreateEvent() {
- continue
- }
- case HookEventPush:
- if !w.HasPushEvent() {
- continue
- }
- case HookEventPullRequest:
- if !w.HasPullRequestEvent() {
- continue
- }
- }
-
- // Use separate objects so modifications won't be made on payload on non-Gogs/Gitea type hooks.
- switch w.HookTaskType {
- case SLACK:
- payloader, err = GetSlackPayload(p, event, w.Meta)
- if err != nil {
- return fmt.Errorf("GetSlackPayload: %v", err)
- }
- case DISCORD:
- payloader, err = GetDiscordPayload(p, event, w.Meta)
- if err != nil {
- return fmt.Errorf("GetDiscordPayload: %v", err)
- }
- default:
- p.SetSecret(w.Secret)
- payloader = p
- }
-
- if err = CreateHookTask(&HookTask{
- RepoID: repo.ID,
- HookID: w.ID,
- Type: w.HookTaskType,
- URL: w.URL,
- Payloader: payloader,
- ContentType: w.ContentType,
- EventType: event,
- IsSSL: w.IsSSL,
- }); err != nil {
- return fmt.Errorf("CreateHookTask: %v", err)
+ if err = PrepareWebhook(w, repo, event, p); err != nil {
+ return err
}
}
return nil
diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go
index 58412997df..4011b6f0e6 100644
--- a/routers/repo/webhook.go
+++ b/routers/repo/webhook.go
@@ -545,6 +545,14 @@ func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
// TestWebhook test if web hook is work fine
func TestWebhook(ctx *context.Context) {
+ hookID := ctx.ParamsInt64(":id")
+ w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
+ if err != nil {
+ ctx.Flash.Error("GetWebhookByID: " + err.Error())
+ ctx.Status(500)
+ return
+ }
+
// Grab latest commit or fake one if it's empty repository.
commit := ctx.Repo.Commit
if commit == nil {
@@ -581,8 +589,8 @@ func TestWebhook(ctx *context.Context) {
Pusher: apiUser,
Sender: apiUser,
}
- if err := models.PrepareWebhooks(ctx.Repo.Repository, models.HookEventPush, p); err != nil {
- ctx.Flash.Error("PrepareWebhooks: " + err.Error())
+ if err := models.PrepareWebhook(w, ctx.Repo.Repository, models.HookEventPush, p); err != nil {
+ ctx.Flash.Error("PrepareWebhook: " + err.Error())
ctx.Status(500)
} else {
go models.HookQueue.Add(ctx.Repo.Repository.ID)