diff --git a/api/repo/create.go b/api/repo/create.go index b1abe89de..295fe2b74 100644 --- a/api/repo/create.go +++ b/api/repo/create.go @@ -238,11 +238,15 @@ func CreateRepo(c *gin.Context) { r.SetHash(dbRepo.GetHash()) } - hook := new(library.Hook) + h := new(library.Hook) + if err == nil { + h, _ = database.FromContext(c).LastHookForRepo(dbRepo) + } + // check if we should create the webhook if c.Value("webhookvalidation").(bool) { // send API call to create the webhook - hook, _, err = scm.FromContext(c).Enable(u, r) + h, _, err = scm.FromContext(c).Enable(u, r, h) if err != nil { retErr := fmt.Errorf("unable to create webhook for %s: %w", r.GetFullName(), err) @@ -300,9 +304,9 @@ func CreateRepo(c *gin.Context) { // create init hook in the DB after repo has been added in order to capture its ID if c.Value("webhookvalidation").(bool) { // update initialization hook - hook.SetRepoID(r.GetID()) + h.SetRepoID(r.GetID()) // create first hook for repo in the database - err = database.FromContext(c).CreateHook(hook) + err = database.FromContext(c).CreateHook(h) if err != nil { retErr := fmt.Errorf("unable to create initialization webhook for %s: %w", r.GetFullName(), err) diff --git a/api/repo/repair.go b/api/repo/repair.go index 0a5b7022a..424a7c0c1 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -77,8 +77,17 @@ func RepairRepo(c *gin.Context) { return } + hook, err := database.FromContext(c).LastHookForRepo(r) + if err != nil { + retErr := fmt.Errorf("unable to get last hook for %s: %w", r.GetFullName(), err) + + util.HandleError(c, http.StatusInternalServerError, retErr) + + return + } + // send API call to create the webhook - hook, _, err := scm.FromContext(c).Enable(u, r) + hook, _, err = scm.FromContext(c).Enable(u, r, hook) if err != nil { retErr := fmt.Errorf("unable to create webhook for %s: %w", r.GetFullName(), err) diff --git a/scm/github/repo.go b/scm/github/repo.go index 3f57b1103..7ecd53341 100644 --- a/scm/github/repo.go +++ b/scm/github/repo.go @@ -151,7 +151,7 @@ func (c *client) Disable(u *library.User, org, name string) error { } // Enable activates a repo by creating the webhook. -func (c *client) Enable(u *library.User, r *library.Repo) (*library.Hook, string, error) { +func (c *client) Enable(u *library.User, r *library.Repo, h *library.Hook) (*library.Hook, string, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), @@ -200,7 +200,7 @@ func (c *client) Enable(u *library.User, r *library.Repo) (*library.Hook, string webhook.SetSourceID(r.GetName() + "-" + eventInitialize) webhook.SetCreated(hookInfo.GetCreatedAt().Unix()) webhook.SetEvent(eventInitialize) - webhook.SetNumber(1) + webhook.SetNumber(h.GetNumber() + 1) switch resp.StatusCode { case http.StatusUnprocessableEntity: diff --git a/scm/github/repo_test.go b/scm/github/repo_test.go index 9c30ac5ab..9f95fb12a 100644 --- a/scm/github/repo_test.go +++ b/scm/github/repo_test.go @@ -621,7 +621,7 @@ func TestGithub_Enable(t *testing.T) { client, _ := NewTest(s.URL) // run test - got, _, err := client.Enable(u, r) + got, _, err := client.Enable(u, r, new(library.Hook)) if resp.Code != http.StatusOK { t.Errorf("Enable returned %v, want %v", resp.Code, http.StatusOK) diff --git a/scm/github/webhook.go b/scm/github/webhook.go index d79a11922..11b317b33 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "fmt" + "mime" "net/http" "strconv" "strings" @@ -47,7 +48,13 @@ func (c *client) ProcessWebhook(request *http.Request) (*types.Webhook, error) { h.SetHost(request.Header.Get("X-GitHub-Enterprise-Host")) } - payload, err := github.ValidatePayload(request, nil) + // get content type + contentType, _, err := mime.ParseMediaType(request.Header.Get("Content-Type")) + if err != nil { + return nil, err + } + + payload, err := github.ValidatePayloadFromBody(contentType, request.Body, "", nil) if err != nil { return &types.Webhook{Hook: h}, nil } diff --git a/scm/service.go b/scm/service.go index 5c42105ba..bb0c0e275 100644 --- a/scm/service.go +++ b/scm/service.go @@ -98,7 +98,7 @@ type Service interface { Disable(*library.User, string, string) error // Enable defines a function that activates // a repo by creating the webhook. - Enable(*library.User, *library.Repo) (*library.Hook, string, error) + Enable(*library.User, *library.Repo, *library.Hook) (*library.Hook, string, error) // Update defines a function that updates // a webhook for a specified repo. Update(*library.User, *library.Repo, int64) error