Skip to content

Commit 7161710

Browse files
nmiyakegmlewis
authored andcommitted
Add APIs for pre-receive hooks (#945)
Fixes #944.
1 parent 85294ce commit 7161710

File tree

4 files changed

+280
-0
lines changed

4 files changed

+280
-0
lines changed

github/github-accessors.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ const (
116116

117117
// https://developer.github.com/changes/2018-05-07-new-checks-api-public-beta/
118118
mediaTypeCheckRunsPreview = "application/vnd.github.antiope-preview+json"
119+
120+
// https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/
121+
mediaTypePreReceiveHooksPreview = "application/vnd.github.eye-scream-preview"
119122
)
120123

121124
// A Client manages communication with the GitHub API.

github/repos_prereceive_hooks.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2018 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
)
12+
13+
// PreReceiveHook represents a GitHub pre-receive hook for a repository.
14+
type PreReceiveHook struct {
15+
ID *int64 `json:"id,omitempty"`
16+
Name *string `json:"name,omitempty"`
17+
Enforcement *string `json:"enforcement,omitempty"`
18+
ConfigURL *string `json:"configuration_url,omitempty"`
19+
}
20+
21+
func (p PreReceiveHook) String() string {
22+
return Stringify(p)
23+
}
24+
25+
// ListPreReceiveHooks lists all pre-receive hooks for the specified repository.
26+
//
27+
// GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#list-pre-receive-hooks
28+
func (s *RepositoriesService) ListPreReceiveHooks(ctx context.Context, owner, repo string, opt *ListOptions) ([]*PreReceiveHook, *Response, error) {
29+
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks", owner, repo)
30+
u, err := addOptions(u, opt)
31+
if err != nil {
32+
return nil, nil, err
33+
}
34+
35+
req, err := s.client.NewRequest("GET", u, nil)
36+
if err != nil {
37+
return nil, nil, err
38+
}
39+
40+
// TODO: remove custom Accept header when this API fully launches.
41+
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
42+
43+
var hooks []*PreReceiveHook
44+
resp, err := s.client.Do(ctx, req, &hooks)
45+
if err != nil {
46+
return nil, resp, err
47+
}
48+
49+
return hooks, resp, nil
50+
}
51+
52+
// GetPreReceiveHook returns a single specified pre-receive hook.
53+
//
54+
// GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#get-a-single-pre-receive-hook
55+
func (s *RepositoriesService) GetPreReceiveHook(ctx context.Context, owner, repo string, id int64) (*PreReceiveHook, *Response, error) {
56+
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id)
57+
req, err := s.client.NewRequest("GET", u, nil)
58+
if err != nil {
59+
return nil, nil, err
60+
}
61+
62+
// TODO: remove custom Accept header when this API fully launches.
63+
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
64+
65+
h := new(PreReceiveHook)
66+
resp, err := s.client.Do(ctx, req, h)
67+
if err != nil {
68+
return nil, resp, err
69+
}
70+
71+
return h, resp, nil
72+
}
73+
74+
// UpdatePreReceiveHook updates a specified pre-receive hook.
75+
//
76+
// GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#update-pre-receive-hook-enforcement
77+
func (s *RepositoriesService) UpdatePreReceiveHook(ctx context.Context, owner, repo string, id int64, hook *PreReceiveHook) (*PreReceiveHook, *Response, error) {
78+
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id)
79+
req, err := s.client.NewRequest("PATCH", u, hook)
80+
if err != nil {
81+
return nil, nil, err
82+
}
83+
84+
// TODO: remove custom Accept header when this API fully launches.
85+
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
86+
87+
h := new(PreReceiveHook)
88+
resp, err := s.client.Do(ctx, req, h)
89+
if err != nil {
90+
return nil, resp, err
91+
}
92+
93+
return h, resp, nil
94+
}
95+
96+
// DeletePreReceiveHook deletes a specified pre-receive hook.
97+
//
98+
// GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#remove-enforcement-overrides-for-a-pre-receive-hook
99+
func (s *RepositoriesService) DeletePreReceiveHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
100+
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id)
101+
req, err := s.client.NewRequest("DELETE", u, nil)
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
// TODO: remove custom Accept header when this API fully launches.
107+
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
108+
109+
return s.client.Do(ctx, req, nil)
110+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright 2018 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"encoding/json"
11+
"fmt"
12+
"net/http"
13+
"reflect"
14+
"testing"
15+
)
16+
17+
func TestRepositoriesService_ListPreReceiveHooks(t *testing.T) {
18+
client, mux, _, teardown := setup()
19+
defer teardown()
20+
21+
mux.HandleFunc("/repos/o/r/pre-receive-hooks", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, "GET")
23+
testHeader(t, r, "Accept", mediaTypePreReceiveHooksPreview)
24+
testFormValues(t, r, values{"page": "2"})
25+
fmt.Fprint(w, `[{"id":1}, {"id":2}]`)
26+
})
27+
28+
opt := &ListOptions{Page: 2}
29+
30+
hooks, _, err := client.Repositories.ListPreReceiveHooks(context.Background(), "o", "r", opt)
31+
if err != nil {
32+
t.Errorf("Repositories.ListHooks returned error: %v", err)
33+
}
34+
35+
want := []*PreReceiveHook{{ID: Int64(1)}, {ID: Int64(2)}}
36+
if !reflect.DeepEqual(hooks, want) {
37+
t.Errorf("Repositories.ListPreReceiveHooks returned %+v, want %+v", hooks, want)
38+
}
39+
}
40+
41+
func TestRepositoriesService_ListPreReceiveHooks_invalidOwner(t *testing.T) {
42+
client, _, _, teardown := setup()
43+
defer teardown()
44+
45+
_, _, err := client.Repositories.ListPreReceiveHooks(context.Background(), "%", "%", nil)
46+
testURLParseError(t, err)
47+
}
48+
49+
func TestRepositoriesService_GetPreReceiveHook(t *testing.T) {
50+
client, mux, _, teardown := setup()
51+
defer teardown()
52+
53+
mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) {
54+
testMethod(t, r, "GET")
55+
testHeader(t, r, "Accept", mediaTypePreReceiveHooksPreview)
56+
fmt.Fprint(w, `{"id":1}`)
57+
})
58+
59+
hook, _, err := client.Repositories.GetPreReceiveHook(context.Background(), "o", "r", 1)
60+
if err != nil {
61+
t.Errorf("Repositories.GetPreReceiveHook returned error: %v", err)
62+
}
63+
64+
want := &PreReceiveHook{ID: Int64(1)}
65+
if !reflect.DeepEqual(hook, want) {
66+
t.Errorf("Repositories.GetPreReceiveHook returned %+v, want %+v", hook, want)
67+
}
68+
}
69+
70+
func TestRepositoriesService_GetPreReceiveHook_invalidOwner(t *testing.T) {
71+
client, _, _, teardown := setup()
72+
defer teardown()
73+
74+
_, _, err := client.Repositories.GetPreReceiveHook(context.Background(), "%", "%", 1)
75+
testURLParseError(t, err)
76+
}
77+
78+
func TestRepositoriesService_UpdatePreReceiveHook(t *testing.T) {
79+
client, mux, _, teardown := setup()
80+
defer teardown()
81+
82+
input := &PreReceiveHook{Name: String("t")}
83+
84+
mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) {
85+
v := new(PreReceiveHook)
86+
json.NewDecoder(r.Body).Decode(v)
87+
88+
testMethod(t, r, "PATCH")
89+
if !reflect.DeepEqual(v, input) {
90+
t.Errorf("Request body = %+v, want %+v", v, input)
91+
}
92+
93+
fmt.Fprint(w, `{"id":1}`)
94+
})
95+
96+
hook, _, err := client.Repositories.UpdatePreReceiveHook(context.Background(), "o", "r", 1, input)
97+
if err != nil {
98+
t.Errorf("Repositories.UpdatePreReceiveHook returned error: %v", err)
99+
}
100+
101+
want := &PreReceiveHook{ID: Int64(1)}
102+
if !reflect.DeepEqual(hook, want) {
103+
t.Errorf("Repositories.UpdatePreReceiveHook returned %+v, want %+v", hook, want)
104+
}
105+
}
106+
107+
func TestRepositoriesService_PreReceiveHook_invalidOwner(t *testing.T) {
108+
client, _, _, teardown := setup()
109+
defer teardown()
110+
111+
_, _, err := client.Repositories.UpdatePreReceiveHook(context.Background(), "%", "%", 1, nil)
112+
testURLParseError(t, err)
113+
}
114+
115+
func TestRepositoriesService_DeletePreReceiveHook(t *testing.T) {
116+
client, mux, _, teardown := setup()
117+
defer teardown()
118+
119+
mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) {
120+
testMethod(t, r, "DELETE")
121+
})
122+
123+
_, err := client.Repositories.DeletePreReceiveHook(context.Background(), "o", "r", 1)
124+
if err != nil {
125+
t.Errorf("Repositories.DeletePreReceiveHook returned error: %v", err)
126+
}
127+
}
128+
129+
func TestRepositoriesService_DeletePreReceiveHook_invalidOwner(t *testing.T) {
130+
client, _, _, teardown := setup()
131+
defer teardown()
132+
133+
_, err := client.Repositories.DeletePreReceiveHook(context.Background(), "%", "%", 1)
134+
testURLParseError(t, err)
135+
}

0 commit comments

Comments
 (0)