Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions api/repo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
11 changes: 10 additions & 1 deletion api/repo/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions scm/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion scm/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion scm/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"mime"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion scm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down