diff --git a/scm/driver/github/repo.go b/scm/driver/github/repo.go index 7b81c2f35..8daf21dd7 100644 --- a/scm/driver/github/repo.go +++ b/scm/driver/github/repo.go @@ -310,7 +310,26 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input * } func (s *repositoryService) UpdateHook(ctx context.Context, repo string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) { - return nil, nil, scm.ErrNotSupported + path := fmt.Sprintf("repos/%s/hooks/%s", repo, input.Name) + in := new(hook) + in.Active = true + in.Name = "web" + in.Config.Secret = input.Secret + in.Config.ContentType = "json" + in.Config.URL = input.Target + if input.SkipVerify { + in.Config.InsecureSSL = "1" + } else { + in.Config.InsecureSSL = "0" + } + input.NativeEvents = append( + input.NativeEvents, + convertHookEvents(input.Events)..., + ) + in.Events = input.NativeEvents + out := new(hook) + res, err := s.client.do(ctx, "PATCH", path, in, out) + return convertHook(out), res, err } // CreateStatus creates a new commit status. diff --git a/scm/driver/github/repo_test.go b/scm/driver/github/repo_test.go index 8907f121f..5730c6d95 100644 --- a/scm/driver/github/repo_test.go +++ b/scm/driver/github/repo_test.go @@ -417,6 +417,46 @@ func TestRepositoryHookCreate(t *testing.T) { t.Run("Rate", testRate(res)) } +func TestRepositoryHookUpdate(t *testing.T) { + defer gock.Off() + + gock.New("https://api.github.com"). + Patch("/repos/octocat/hello-world/hooks/drone"). + Reply(200). + Type("application/json"). + SetHeaders(mockHeaders). + File("testdata/hook.json") + + in := &scm.HookInput{ + Name: "drone", + Target: "https://example.com", + Secret: "topsecret", + SkipVerify: true, + } + + client := NewDefault() + got, res, err := client.Repositories.UpdateHook(context.Background(), "octocat/hello-world", in) + if err != nil { + t.Error(err) + return + } + + want := new(scm.Hook) + raw, _ := os.ReadFile("testdata/hook.json.golden") + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } + + if diff := cmp.Diff(got, want); diff != "" { + t.Errorf("Unexpected Results") + t.Log(diff) + } + + t.Run("Request", testRequest(res)) + t.Run("Rate", testRate(res)) +} + func TestRepositoryCreate(t *testing.T) { defer gock.Off()