Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UpdateHook method to RepositoryService. #71

Merged
merged 1 commit into from
Aug 21, 2020
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
Add support for updating a webhook in all SCMs.
The new `UpdateHook` method in `RepositoryService` would update an existing
SCM webhook and overwrite the list of events to watch.

In this patch, `UpdateHook` is not supported in the GitLab driver. It is
implemented separately in a followup patch.
  • Loading branch information
chhsia0 committed Aug 21, 2020
commit 06c5eded081f02e386d7db5c5d01318c955d5728
29 changes: 27 additions & 2 deletions scm/driver/bitbucket/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input *
in.Description = input.Name
in.Events = append(
input.NativeEvents,
convertHookEvents(input.Events)...,
convertFromHookEvents(input.Events)...,
)
out := new(hook)
res, err := s.client.do(ctx, "POST", path, in, out)
Expand All @@ -163,6 +163,31 @@ func (s *repositoryService) CreateStatus(ctx context.Context, repo, ref string,
return convertStatus(out), res, err
}

// UpdateHook updates a repository webhook.
func (s *repositoryService) UpdateHook(ctx context.Context, repo, id string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
target, err := url.Parse(input.Target)
if err != nil {
return nil, nil, err
}
params := target.Query()
params.Set("secret", input.Secret)
target.RawQuery = params.Encode()

path := fmt.Sprintf("2.0/repositories/%s/hooks/%s", repo, id)
in := new(hookInput)
in.URL = target.String()
in.SkipCertVerification = input.SkipVerify
in.Active = true
in.Description = input.Name
in.Events = append(
input.NativeEvents,
convertFromHookEvents(input.Events)...,
)
out := new(hook)
res, err := s.client.do(ctx, "PUT", path, in, out)
return convertHook(out), res, err
}

// DeleteHook deletes a repository webhook.
func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/hooks/%s", repo, id)
Expand Down Expand Up @@ -255,7 +280,7 @@ func convertHook(from *hook) *scm.Hook {
}
}

func convertHookEvents(from scm.HookEvents) []string {
func convertFromHookEvents(from scm.HookEvents) []string {
var events []string
if from.Push {
events = append(events, "repo:push")
Expand Down
42 changes: 34 additions & 8 deletions scm/driver/bitbucket/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,31 +254,43 @@ func TestRepositoryHookList(t *testing.T) {
}
}

func TestRepositoryHookDelete(t *testing.T) {
func TestRepositoryHookCreate(t *testing.T) {
defer gock.Off()

gock.New("https://api.bitbucket.org").
Delete("/2.0/repositories/atlassian/stash-example-plugin/hooks/{d53603cc-3f67-45ea-b310-aaa5ef6ec061}").
Reply(204).Done()
Post("/2.0/repositories/atlassian/stash-example-plugin/hooks").
Reply(201).
Type("application/json").
File("testdata/hook.json")

client, _ := New("https://api.bitbucket.org")
_, err := client.Repositories.DeleteHook(context.Background(), "atlassian/stash-example-plugin", "{d53603cc-3f67-45ea-b310-aaa5ef6ec061}")
got, _, err := client.Repositories.CreateHook(context.Background(), "atlassian/stash-example-plugin", &scm.HookInput{})
if err != nil {
t.Error(err)
return
}

want := new(scm.Hook)
raw, _ := ioutil.ReadFile("testdata/hook.json.golden")
json.Unmarshal(raw, want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

func TestRepositoryHookCreate(t *testing.T) {
func TestRepositoryHookUpdate(t *testing.T) {
defer gock.Off()

gock.New("https://api.bitbucket.org").
Post("/2.0/repositories/atlassian/stash-example-plugin/hooks").
Reply(201).
Put("/2.0/repositories/atlassian/stash-example-plugin/hooks/{d53603cc-3f67-45ea-b310-aaa5ef6ec061}").
Reply(200).
Type("application/json").
File("testdata/hook.json")

client, _ := New("https://api.bitbucket.org")
got, _, err := client.Repositories.CreateHook(context.Background(), "atlassian/stash-example-plugin", &scm.HookInput{})
got, _, err := client.Repositories.UpdateHook(context.Background(), "atlassian/stash-example-plugin", "{d53603cc-3f67-45ea-b310-aaa5ef6ec061}", &scm.HookInput{})
if err != nil {
t.Error(err)
return
Expand All @@ -294,6 +306,20 @@ func TestRepositoryHookCreate(t *testing.T) {
}
}

func TestRepositoryHookDelete(t *testing.T) {
defer gock.Off()

gock.New("https://api.bitbucket.org").
Delete("/2.0/repositories/atlassian/stash-example-plugin/hooks/{d53603cc-3f67-45ea-b310-aaa5ef6ec061}").
Reply(204).Done()

client, _ := New("https://api.bitbucket.org")
_, err := client.Repositories.DeleteHook(context.Background(), "atlassian/stash-example-plugin", "{d53603cc-3f67-45ea-b310-aaa5ef6ec061}")
if err != nil {
t.Error(err)
}
}

func TestConvertFromState(t *testing.T) {
tests := []struct {
src scm.State
Expand Down
25 changes: 25 additions & 0 deletions scm/driver/gitea/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ func (s *repositoryService) CreateStatus(ctx context.Context, repo string, ref s
return convertStatus(out), res, err
}

func (s *repositoryService) UpdateHook(ctx context.Context, repo, id string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
target, err := url.Parse(input.Target)
if err != nil {
return nil, nil, err
}
params := target.Query()
params.Set("secret", input.Secret)
target.RawQuery = params.Encode()

path := fmt.Sprintf("api/v1/repos/%s/hooks/%s", repo, id)
in := new(hook)
in.Type = "gitea"
in.Active = true
in.Config.Secret = input.Secret
in.Config.ContentType = "json"
in.Config.URL = target.String()
in.Events = append(
input.NativeEvents,
convertHookEvent(input.Events)...,
)
out := new(hook)
res, err := s.client.do(ctx, "PATCH", path, in, out)
return convertHook(out), res, err
}

func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/hooks/%s", repo, id)
return s.client.do(ctx, "DELETE", path, nil, nil)
Expand Down
25 changes: 25 additions & 0 deletions scm/driver/gitea/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,31 @@ func TestHookCreate(t *testing.T) {
}
}

func TestHookUpdate(t *testing.T) {
defer gock.Off()

gock.New("https://try.gitea.io").
Patch("/api/v1/repos/go-gitea/gitea/hooks/20").
Reply(200).
Type("application/json").
File("testdata/hook.json")

client, _ := New("https://try.gitea.io")
got, _, err := client.Repositories.UpdateHook(context.Background(), "go-gitea/gitea", "20", &scm.HookInput{})
if err != nil {
t.Error(err)
}

want := new(scm.Hook)
raw, _ := ioutil.ReadFile("testdata/hook.json.golden")
json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

func TestHookDelete(t *testing.T) {
defer gock.Off()

Expand Down
26 changes: 23 additions & 3 deletions scm/driver/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (s *RepositoryService) CreateHook(ctx context.Context, repo string, input *
}
in.Events = append(
input.NativeEvents,
convertHookEvents(input.Events)...,
convertFromHookEvents(input.Events)...,
)
out := new(hook)
res, err := s.client.do(ctx, "POST", path, in, out)
Expand Down Expand Up @@ -154,8 +154,28 @@ func (s *RepositoryService) CreateDeployStatus(ctx context.Context, repo string,
return convertDeployStatus(out), res, err
}

// UpdateHook updates a repository webhook.
func (s *RepositoryService) UpdateHook(ctx context.Context, repo, id string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/hooks/%s", repo, id)
in := new(hook)
in.Active = true
in.Config.Secret = input.Secret
in.Config.ContentType = "json"
in.Config.URL = input.Target
if input.SkipVerify {
in.Config.InsecureSSL = "1"
}
in.Events = append(
input.NativeEvents,
convertFromHookEvents(input.Events)...,
)
out := new(hook)
res, err := s.client.do(ctx, "PATCH", path, in, out)
return convertHook(out), res, err
}

// DeleteHook deletes a repository webhook.
func (s *RepositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) {
func (s *RepositoryService) DeleteHook(ctx context.Context, repo, id string) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/hooks/%s", repo, id)
return s.client.do(ctx, "DELETE", path, nil, nil)
}
Expand Down Expand Up @@ -210,7 +230,7 @@ func convertHook(from *hook) *scm.Hook {
}
}

func convertHookEvents(from scm.HookEvents) []string {
func convertFromHookEvents(from scm.HookEvents) []string {
var events []string
if from.Push {
events = append(events, "push")
Expand Down
61 changes: 49 additions & 12 deletions scm/driver/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,36 +304,49 @@ func TestRepositoryHookList(t *testing.T) {
t.Run("Page", testPage(res))
}

func TestRepositoryHookDelete(t *testing.T) {
func TestRepositoryHookCreate(t *testing.T) {
defer gock.Off()

gock.New("https://api.github.com").
Delete("/repos/octocat/hello-world/hooks/1").
Reply(204).
Post("/repos/octocat/hello-world/hooks").
Reply(201).
Type("application/json").
SetHeaders(mockHeaders)
SetHeaders(mockHeaders).
File("testdata/hook.json")

in := &scm.HookInput{
Name: "drone",
Target: "https://example.com",
Secret: "topsecret",
SkipVerify: true,
}

client := NewDefault()
res, err := client.Repositories.DeleteHook(context.Background(), "octocat/hello-world", "1")
got, res, err := client.Repositories.CreateHook(context.Background(), "octocat/hello-world", in)
if err != nil {
t.Error(err)
return
}

if got, want := res.Status, 204; got != want {
t.Errorf("Want response status %d, got %d", want, got)
want := new(scm.Hook)
raw, _ := ioutil.ReadFile("testdata/hook.json.golden")
json.Unmarshal(raw, want)

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 TestRepositoryHookCreate(t *testing.T) {
func TestRepositoryHookUpdate(t *testing.T) {
defer gock.Off()

gock.New("https://api.github.com").
Post("/repos/octocat/hello-world/hooks").
Reply(201).
Patch("/repos/octocat/hello-world/hooks/1").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/hook.json")
Expand All @@ -346,7 +359,7 @@ func TestRepositoryHookCreate(t *testing.T) {
}

client := NewDefault()
got, res, err := client.Repositories.CreateHook(context.Background(), "octocat/hello-world", in)
got, res, err := client.Repositories.UpdateHook(context.Background(), "octocat/hello-world", "1", in)
if err != nil {
t.Error(err)
return
Expand All @@ -365,6 +378,30 @@ func TestRepositoryHookCreate(t *testing.T) {
t.Run("Rate", testRate(res))
}

func TestRepositoryHookDelete(t *testing.T) {
defer gock.Off()

gock.New("https://api.github.com").
Delete("/repos/octocat/hello-world/hooks/1").
Reply(204).
Type("application/json").
SetHeaders(mockHeaders)

client := NewDefault()
res, err := client.Repositories.DeleteHook(context.Background(), "octocat/hello-world", "1")
if err != nil {
t.Error(err)
return
}

if got, want := res.Status, 204; got != want {
t.Errorf("Want response status %d, got %d", want, got)
}

t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}

func TestConvertState(t *testing.T) {
tests := []struct {
src string
Expand Down Expand Up @@ -484,7 +521,7 @@ func TestHookEvents(t *testing.T) {
},
}
for i, test := range tests {
got, want := convertHookEvents(test.in), test.out
got, want := convertFromHookEvents(test.in), test.out
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results at index %d", i)
t.Log(diff)
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/gitlab/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func (s *repositoryService) CreateStatus(ctx context.Context, repo, ref string,
return convertStatus(out), res, err
}

func (s *repositoryService) UpdateHook(ctx context.Context, repo string, id string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/hooks/%s", encode(repo), id)
return s.client.do(ctx, "DELETE", path, nil, nil)
Expand Down
17 changes: 17 additions & 0 deletions scm/driver/gogs/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ func (s *repositoryService) CreateStatus(context.Context, string, string, *scm.S
return nil, nil, scm.ErrNotSupported
}

func (s *repositoryService) UpdateHook(ctx context.Context, repo, id string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/hooks/%s", repo, id)
in := new(hook)
in.Type = "gogs"
in.Active = true
in.Config.Secret = input.Secret
in.Config.ContentType = "json"
in.Config.URL = input.Target
in.Events = append(
input.NativeEvents,
convertHookEvent(input.Events)...,
)
out := new(hook)
res, err := s.client.do(ctx, "PATCH", path, in, out)
return convertHook(out), res, err
}

func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/hooks/%s", repo, id)
return s.client.do(ctx, "DELETE", path, nil, nil)
Expand Down
Loading