From 3d93532c87823159dd03e9c64a58c7bafed0fa64 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sat, 16 Jul 2016 01:02:55 +0800 Subject: [PATCH] #3274 fix can't get webhook detail of organization --- models/webhook.go | 22 +++++++++++++++++----- routers/api/v1/repo/hook.go | 2 +- routers/org/setting.go | 2 +- routers/repo/webhook.go | 7 ++++++- templates/repo/home.tmpl | 2 +- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/models/webhook.go b/models/webhook.go index 85590c705..134ac1017 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -174,8 +174,8 @@ func CreateWebhook(w *Webhook) error { return err } -// GetWebhookByID returns webhook of repository by given ID. -func GetWebhookByID(repoID, id int64) (*Webhook, error) { +// GetWebhookByRepoID returns webhook of repository by given ID. +func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) { w := new(Webhook) has, err := x.Id(id).And("repo_id=?", repoID).Get(w) if err != nil { @@ -186,6 +186,18 @@ func GetWebhookByID(repoID, id int64) (*Webhook, error) { return w, nil } +// GetWebhookByOrgID returns webhook of organization by given ID. +func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) { + w := new(Webhook) + has, err := x.Id(id).And("org_id=?", orgID).Get(w) + if err != nil { + return nil, err + } else if !has { + return nil, ErrWebhookNotExist{id} + } + return w, nil +} + // GetActiveWebhooksByRepoID returns all active webhooks of repository. func GetActiveWebhooksByRepoID(repoID int64) (ws []*Webhook, err error) { err = x.Where("repo_id=?", repoID).And("is_active=?", true).Find(&ws) @@ -221,8 +233,8 @@ func DeleteWebhook(id int64) (err error) { return sess.Commit() } -// GetWebhooksByOrgId returns all webhooks for an organization. -func GetWebhooksByOrgId(orgID int64) (ws []*Webhook, err error) { +// GetWebhooksByOrgID returns all webhooks for an organization. +func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) { err = x.Find(&ws, &Webhook{OrgID: orgID}) return ws, err } @@ -548,7 +560,7 @@ func (t *HookTask) deliver() { } // Update webhook last delivery status. - w, err := GetWebhookByID(t.RepoID, t.HookID) + w, err := GetWebhookByRepoID(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 0dac8f7cf..0bf46977d 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -98,7 +98,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.GetWebhookByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) + w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrWebhookNotExist(err) { ctx.Status(404) diff --git a/routers/org/setting.go b/routers/org/setting.go index c50e18d7c..a938581c6 100644 --- a/routers/org/setting.go +++ b/routers/org/setting.go @@ -154,7 +154,7 @@ func Webhooks(ctx *context.Context) { return } - ws, err := models.GetWebhooksByOrgId(ctx.Org.Organization.Id) + ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.Id) if err != nil { ctx.Handle(500, "GetWebhooksByOrgId", err) return diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index d90486235..b2274033b 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -220,7 +220,12 @@ func checkWebhook(ctx *context.Context) (*OrgRepoCtx, *models.Webhook) { } ctx.Data["BaseLink"] = orCtx.Link - w, err := models.GetWebhookByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) + var w *models.Webhook + if orCtx.RepoID > 0 { + w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) + } else { + w, err = models.GetWebhookByOrgID(ctx.Org.Organization.Id, ctx.ParamsInt64(":id")) + } if err != nil { if models.IsErrWebhookNotExist(err) { ctx.Handle(404, "GetWebhookByID", nil) diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index fe5a62163..fe7b9c33b 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -9,7 +9,7 @@