1
1
mirror of https://github.com/go-gitea/gitea synced 2024-10-31 15:24:25 +00:00

fix webhook test (#2415)

* fix webhook test

* fix error message

* fix typo
This commit is contained in:
Lunny Xiao 2017-08-29 22:55:24 +08:00 committed by GitHub
parent 491f0ebc58
commit 3e6fed368b
2 changed files with 63 additions and 46 deletions

View File

@ -482,44 +482,25 @@ 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
orgHooks, err := GetActiveWebhooksByOrgID(repo.OwnerID)
if err != nil {
return fmt.Errorf("GetActiveWebhooksByOrgID: %v", err)
}
ws = append(ws, orgHooks...)
}
if len(ws) == 0 {
return nil
}
var payloader api.Payloader
for _, w := range ws {
// 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() {
continue
return nil
}
case HookEventPush:
if !w.HasPushEvent() {
continue
return nil
}
case HookEventPullRequest:
if !w.HasPullRequestEvent() {
continue
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:
@ -549,6 +530,34 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
}); 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)
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
orgHooks, err := GetActiveWebhooksByOrgID(repo.OwnerID)
if err != nil {
return fmt.Errorf("GetActiveWebhooksByOrgID: %v", err)
}
ws = append(ws, orgHooks...)
}
if len(ws) == 0 {
return nil
}
for _, w := range ws {
if err = PrepareWebhook(w, repo, event, p); err != nil {
return err
}
}
return nil
}

View File

@ -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)