From bd970b8b27d648b3beff5cdec2f509826df0bb14 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 21 Feb 2017 15:44:35 -0500 Subject: [PATCH] webhook: only trigger specific webhook for test delivery (#3030) --- README.md | 2 +- models/webhook.go | 57 ++++++++++++++--------- routers/api/v1/repo/hook.go | 2 +- routers/repo/webhook.go | 6 +-- templates/repo/settings/hook_history.tmpl | 2 +- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index b2cb3d7ee..804becda2 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Gogs [![Build Status](https://travis-ci.org/gogits/gogs.svg?branch=master)](http ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true) -##### Current tip version: [`.VERSION`](templates/.VERSION) (see [Releases](https://github.com/gogits/gogs/releases) for binary versions ~~or submit a task on [alpha stage automated binary building system](https://build.gogs.io/)~~) +##### Current tip version: [`.VERSION`](templates/.VERSION) (see [Releases](https://github.com/gogits/gogs/releases) for binary versions) | Web | UI | Preview | |:-------------:|:-------:|:-------:| diff --git a/models/webhook.go b/models/webhook.go index e9a4d79b1..639168570 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -197,8 +197,8 @@ func getWebhook(bean *Webhook) (*Webhook, error) { return bean, nil } -// GetWebhookByRepoID returns webhook of repository by given ID. -func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) { +// GetWebhookOfRepoByID returns webhook of repository by given ID. +func GetWebhookOfRepoByID(repoID, id int64) (*Webhook, error) { return getWebhook(&Webhook{ ID: id, RepoID: repoID, @@ -433,29 +433,14 @@ func UpdateHookTask(t *HookTask) error { return err } -// 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) - if err != nil { - return fmt.Errorf("GetActiveWebhooksByRepoID: %v", err) - } - - // check if repo belongs to org and append additional webhooks - if repo.MustOwner().IsOrganization() { - // get hooks for org - orgws, err := GetActiveWebhooksByOrgID(repo.OwnerID) - if err != nil { - return fmt.Errorf("GetActiveWebhooksByOrgID: %v", err) - } - ws = append(ws, orgws...) - } - - if len(ws) == 0 { +// prepareWebhooks adds list of webhooks to task queue. +func prepareWebhooks(repo *Repository, event HookEventType, p api.Payloader, webhooks []*Webhook) (err error) { + if len(webhooks) == 0 { return nil } var payloader api.Payloader - for _, w := range ws { + for _, w := range webhooks { switch event { case HOOK_EVENT_CREATE: if !w.HasCreateEvent() { @@ -504,6 +489,34 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err return nil } +// PrepareWebhooks adds all active webhooks to task queue. +func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error { + webhooks, err := GetActiveWebhooksByRepoID(repo.ID) + if err != nil { + return fmt.Errorf("GetActiveWebhooksByRepoID [%d]: %v", repo.ID, err) + } + + // check if repo belongs to org and append additional webhooks + if repo.MustOwner().IsOrganization() { + // get hooks for org + orgws, err := GetActiveWebhooksByOrgID(repo.OwnerID) + if err != nil { + return fmt.Errorf("GetActiveWebhooksByOrgID [%d]: %v", repo.OwnerID, err) + } + webhooks = append(webhooks, orgws...) + } + return prepareWebhooks(repo, event, p, webhooks) +} + +// TestWebhook adds the test webhook matches the ID to task queue. +func TestWebhook(repo *Repository, event HookEventType, p api.Payloader, webhookID int64) error { + webhook, err := GetWebhookOfRepoByID(repo.ID, webhookID) + if err != nil { + return fmt.Errorf("GetWebhookOfRepoByID [repo_id: %d, id: %d]: %v", repo.ID, webhookID, err) + } + return prepareWebhooks(repo, event, p, []*Webhook{webhook}) +} + func (t *HookTask) deliver() { t.IsDelivered = true @@ -541,7 +554,7 @@ func (t *HookTask) deliver() { } // Update webhook last delivery status. - w, err := GetWebhookByRepoID(t.RepoID, t.HookID) + w, err := GetWebhookOfRepoByID(t.RepoID, t.HookID) if err != nil { log.Error(5, "GetWebhookByID: %v", err) return diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 9857d2ebb..fa66fe471 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -99,7 +99,7 @@ func CreateHook(ctx *context.APIContext, form api.CreateHookOption) { // https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook func EditHook(ctx *context.APIContext, form api.EditHookOption) { - w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) + w, err := models.GetWebhookOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrWebhookNotExist(err) { ctx.Status(404) diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index f033e41bb..542fa6354 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -273,7 +273,7 @@ func checkWebhook(ctx *context.Context) (*OrgRepoCtx, *models.Webhook) { var w *models.Webhook if orCtx.RepoID > 0 { - w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) + w, err = models.GetWebhookOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) } else { w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id")) } @@ -504,8 +504,8 @@ func TestWebhook(ctx *context.Context) { Pusher: apiUser, Sender: apiUser, } - if err := models.PrepareWebhooks(ctx.Repo.Repository, models.HOOK_EVENT_PUSH, p); err != nil { - ctx.Flash.Error("PrepareWebhooks: " + err.Error()) + if err := models.TestWebhook(ctx.Repo.Repository, models.HOOK_EVENT_PUSH, p, ctx.QueryInt64("id")); err != nil { + ctx.Flash.Error("TestWebhook: " + err.Error()) ctx.Status(500) } else { go models.HookQueue.Add(ctx.Repo.Repository.ID) diff --git a/templates/repo/settings/hook_history.tmpl b/templates/repo/settings/hook_history.tmpl index 9ded6a790..26bbef3fb 100644 --- a/templates/repo/settings/hook_history.tmpl +++ b/templates/repo/settings/hook_history.tmpl @@ -4,7 +4,7 @@ {{if .IsRepositoryAdmin}}
+ "{{.i18n.Tr "repo.settings.webhook.test_delivery_desc"}}" data-variation="inverted tiny" data-link="{{.Link}}/test?id={{.Webhook.ID}}" data-redirect="{{.Link}}">{{.i18n.Tr "repo.settings.webhook.test_delivery"}}
{{end}}