Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #944
- Loading branch information
Showing
4 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2018 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// PreReceiveHook represents a GitHub pre-receive hook for a repository. | ||
type PreReceiveHook struct { | ||
ID *int64 `json:"id,omitempty"` | ||
Name *string `json:"name,omitempty"` | ||
Enforcement *string `json:"enforcement,omitempty"` | ||
ConfigURL *string `json:"configuration_url,omitempty"` | ||
} | ||
|
||
func (p PreReceiveHook) String() string { | ||
return Stringify(p) | ||
} | ||
|
||
// ListPreReceiveHooks lists all pre-receive hooks for the specified repository. | ||
// | ||
// GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#list-pre-receive-hooks | ||
func (s *RepositoriesService) ListPreReceiveHooks(ctx context.Context, owner, repo string, opt *ListOptions) ([]*PreReceiveHook, *Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks", owner, repo) | ||
u, err := addOptions(u, opt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// TODO: remove custom Accept header when this API fully launches. | ||
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview) | ||
|
||
var hooks []*PreReceiveHook | ||
resp, err := s.client.Do(ctx, req, &hooks) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return hooks, resp, nil | ||
} | ||
|
||
// GetPreReceiveHook returns a single specified pre-receive hook. | ||
// | ||
// GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#get-a-single-pre-receive-hook | ||
func (s *RepositoriesService) GetPreReceiveHook(ctx context.Context, owner, repo string, id int64) (*PreReceiveHook, *Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id) | ||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// TODO: remove custom Accept header when this API fully launches. | ||
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview) | ||
|
||
hook := new(PreReceiveHook) | ||
resp, err := s.client.Do(ctx, req, hook) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return hook, resp, nil | ||
} | ||
|
||
// UpdatePreReceiveHook updates a specified pre-receive hook. | ||
// | ||
// GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#update-pre-receive-hook-enforcement | ||
func (s *RepositoriesService) UpdatePreReceiveHook(ctx context.Context, owner, repo string, id int64, hook *PreReceiveHook) (*PreReceiveHook, *Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id) | ||
req, err := s.client.NewRequest("PATCH", u, hook) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// TODO: remove custom Accept header when this API fully launches. | ||
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview) | ||
|
||
h := new(PreReceiveHook) | ||
resp, err := s.client.Do(ctx, req, h) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return h, resp, nil | ||
} | ||
|
||
// DeletePreReceiveHook deletes a specified pre-receive hook. | ||
// | ||
// GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#remove-enforcement-overrides-for-a-pre-receive-hook | ||
func (s *RepositoriesService) DeletePreReceiveHook(ctx context.Context, owner, repo string, id int64) (*Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id) | ||
req, err := s.client.NewRequest("DELETE", u, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO: remove custom Accept header when this API fully launches. | ||
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview) | ||
|
||
return s.client.Do(ctx, req, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright 2018 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestRepositoriesService_ListPreReceiveHooks(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/pre-receive-hooks", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testHeader(t, r, "Accept", mediaTypePreReceiveHooksPreview) | ||
testFormValues(t, r, values{"page": "2"}) | ||
fmt.Fprint(w, `[{"id":1}, {"id":2}]`) | ||
}) | ||
|
||
opt := &ListOptions{Page: 2} | ||
|
||
hooks, _, err := client.Repositories.ListPreReceiveHooks(context.Background(), "o", "r", opt) | ||
if err != nil { | ||
t.Errorf("Repositories.ListHooks returned error: %v", err) | ||
} | ||
|
||
want := []*PreReceiveHook{{ID: Int64(1)}, {ID: Int64(2)}} | ||
if !reflect.DeepEqual(hooks, want) { | ||
t.Errorf("Repositories.ListPreReceiveHooks returned %+v, want %+v", hooks, want) | ||
} | ||
} | ||
|
||
func TestRepositoriesService_ListPreReceiveHooks_invalidOwner(t *testing.T) { | ||
client, _, _, teardown := setup() | ||
defer teardown() | ||
|
||
_, _, err := client.Repositories.ListPreReceiveHooks(context.Background(), "%", "%", nil) | ||
testURLParseError(t, err) | ||
} | ||
|
||
func TestRepositoriesService_GetPreReceiveHook(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testHeader(t, r, "Accept", mediaTypePreReceiveHooksPreview) | ||
fmt.Fprint(w, `{"id":1}`) | ||
}) | ||
|
||
hook, _, err := client.Repositories.GetPreReceiveHook(context.Background(), "o", "r", 1) | ||
if err != nil { | ||
t.Errorf("Repositories.GetPreReceiveHook returned error: %v", err) | ||
} | ||
|
||
want := &PreReceiveHook{ID: Int64(1)} | ||
if !reflect.DeepEqual(hook, want) { | ||
t.Errorf("Repositories.GetPreReceiveHook returned %+v, want %+v", hook, want) | ||
} | ||
} | ||
|
||
func TestRepositoriesService_GetPreReceiveHook_invalidOwner(t *testing.T) { | ||
client, _, _, teardown := setup() | ||
defer teardown() | ||
|
||
_, _, err := client.Repositories.GetPreReceiveHook(context.Background(), "%", "%", 1) | ||
testURLParseError(t, err) | ||
} | ||
|
||
func TestRepositoriesService_UpdatePreReceiveHook(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
input := &PreReceiveHook{Name: String("t")} | ||
|
||
mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { | ||
v := new(PreReceiveHook) | ||
json.NewDecoder(r.Body).Decode(v) | ||
|
||
testMethod(t, r, "PATCH") | ||
if !reflect.DeepEqual(v, input) { | ||
t.Errorf("Request body = %+v, want %+v", v, input) | ||
} | ||
|
||
fmt.Fprint(w, `{"id":1}`) | ||
}) | ||
|
||
hook, _, err := client.Repositories.UpdatePreReceiveHook(context.Background(), "o", "r", 1, input) | ||
if err != nil { | ||
t.Errorf("Repositories.UpdatePreReceiveHook returned error: %v", err) | ||
} | ||
|
||
want := &PreReceiveHook{ID: Int64(1)} | ||
if !reflect.DeepEqual(hook, want) { | ||
t.Errorf("Repositories.UpdatePreReceiveHook returned %+v, want %+v", hook, want) | ||
} | ||
} | ||
|
||
func TestRepositoriesService_PreReceiveHook_invalidOwner(t *testing.T) { | ||
client, _, _, teardown := setup() | ||
defer teardown() | ||
|
||
_, _, err := client.Repositories.UpdatePreReceiveHook(context.Background(), "%", "%", 1, nil) | ||
testURLParseError(t, err) | ||
} | ||
|
||
func TestRepositoriesService_DeletePreReceiveHook(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "DELETE") | ||
}) | ||
|
||
_, err := client.Repositories.DeletePreReceiveHook(context.Background(), "o", "r", 1) | ||
if err != nil { | ||
t.Errorf("Repositories.DeletePreReceiveHook returned error: %v", err) | ||
} | ||
} | ||
|
||
func TestRepositoriesService_DeletePreReceiveHook_invalidOwner(t *testing.T) { | ||
client, _, _, teardown := setup() | ||
defer teardown() | ||
|
||
_, err := client.Repositories.DeletePreReceiveHook(context.Background(), "%", "%", 1) | ||
testURLParseError(t, err) | ||
} |