Browse Source

Bug fixes for webhook API (#650)

for-closed-social
Ethan Koenig 7 years ago
committed by Lunny Xiao
parent
commit
27fcf8d30a
4 changed files with 54 additions and 7 deletions
  1. +3
    -1
      models/webhook.go
  2. +41
    -3
      routers/api/v1/api.go
  3. +5
    -1
      routers/api/v1/org/hook.go
  4. +5
    -2
      routers/api/v1/repo/hook.go

+ 3
- 1
models/webhook.go View File

@ -258,8 +258,10 @@ func deleteWebhook(bean *Webhook) (err error) {
return err return err
} }
if _, err = sess.Delete(bean); err != nil {
if count, err := sess.Delete(bean); err != nil {
return err return err
} else if count == 0 {
return ErrWebhookNotExist{ID: bean.ID}
} else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil { } else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
return err return err
} }

+ 41
- 3
routers/api/v1/api.go View File

@ -119,6 +119,44 @@ func reqRepoWriter() macaron.Handler {
} }
} }
func reqOrgMembership() macaron.Handler {
return func(ctx *context.APIContext) {
var orgID int64
if ctx.Org.Organization != nil {
orgID = ctx.Org.Organization.ID
} else if ctx.Org.Team != nil {
orgID = ctx.Org.Team.OrgID
} else {
ctx.Error(500, "", "reqOrgMembership: unprepared context")
return
}
if !models.IsOrganizationMember(orgID, ctx.User.ID) {
ctx.Error(403, "", "Must be an organization member")
return
}
}
}
func reqOrgOwnership() macaron.Handler {
return func(ctx *context.APIContext) {
var orgID int64
if ctx.Org.Organization != nil {
orgID = ctx.Org.Organization.ID
} else if ctx.Org.Team != nil {
orgID = ctx.Org.Team.OrgID
} else {
ctx.Error(500, "", "reqOrgOwnership: unprepared context")
return
}
if !models.IsOrganizationOwner(orgID, ctx.User.ID) {
ctx.Error(403, "", "Must be an organization member")
return
}
}
}
func orgAssignment(args ...bool) macaron.Handler { func orgAssignment(args ...bool) macaron.Handler {
var ( var (
assignOrg bool assignOrg bool
@ -362,9 +400,9 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Combo("").Get(org.ListHooks). m.Combo("").Get(org.ListHooks).
Post(bind(api.CreateHookOption{}), org.CreateHook) Post(bind(api.CreateHookOption{}), org.CreateHook)
m.Combo("/:id").Get(org.GetHook). m.Combo("/:id").Get(org.GetHook).
Patch(bind(api.EditHookOption{}), org.EditHook).
Delete(org.DeleteHook)
})
Patch(reqOrgOwnership(), bind(api.EditHookOption{}), org.EditHook).
Delete(reqOrgOwnership(), org.DeleteHook)
}, reqOrgMembership())
}, orgAssignment(true)) }, orgAssignment(true))
m.Group("/teams/:teamid", func() { m.Group("/teams/:teamid", func() {
m.Get("", org.GetTeam) m.Get("", org.GetTeam)

+ 5
- 1
routers/api/v1/org/hook.go View File

@ -58,7 +58,11 @@ func DeleteHook(ctx *context.APIContext) {
org := ctx.Org.Organization org := ctx.Org.Organization
hookID := ctx.ParamsInt64(":id") hookID := ctx.ParamsInt64(":id")
if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil { if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
ctx.Error(500, "DeleteWebhookByOrgID", err)
if models.IsErrWebhookNotExist(err) {
ctx.Status(404)
} else {
ctx.Error(500, "DeleteWebhookByOrgID", err)
}
return return
} }
ctx.Status(204) ctx.Status(204)

+ 5
- 2
routers/api/v1/repo/hook.go View File

@ -59,9 +59,12 @@ func EditHook(ctx *context.APIContext, form api.EditHookOption) {
// DeleteHook delete a hook of a repository // DeleteHook delete a hook of a repository
func DeleteHook(ctx *context.APIContext) { func DeleteHook(ctx *context.APIContext) {
if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
ctx.Error(500, "DeleteWebhookByRepoID", err)
if models.IsErrWebhookNotExist(err) {
ctx.Status(404)
} else {
ctx.Error(500, "DeleteWebhookByRepoID", err)
}
return return
} }
ctx.Status(204) ctx.Status(204)
} }

Loading…
Cancel
Save