Skip to content

Commit 2d47288

Browse files
authored
Revert "Removing testhook, edithook, and pinghook from the code and the test file (#1111)" (#1115)
This reverts commit cf38b2b. The version will be bumped.
1 parent 2b50c3d commit 2d47288

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

github/repos_hooks.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,24 @@ func (s *RepositoriesService) GetHook(ctx context.Context, owner, repo string, i
169169
return h, resp, nil
170170
}
171171

172+
// EditHook updates a specified Hook.
173+
//
174+
// GitHub API docs: https://developer.github.com/v3/repos/hooks/#edit-a-hook
175+
func (s *RepositoriesService) EditHook(ctx context.Context, owner, repo string, id int64, hook *Hook) (*Hook, *Response, error) {
176+
u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
177+
req, err := s.client.NewRequest("PATCH", u, hook)
178+
if err != nil {
179+
return nil, nil, err
180+
}
181+
h := new(Hook)
182+
resp, err := s.client.Do(ctx, req, h)
183+
if err != nil {
184+
return nil, resp, err
185+
}
186+
187+
return h, resp, nil
188+
}
189+
172190
// DeleteHook deletes a specified Hook.
173191
//
174192
// GitHub API docs: https://developer.github.com/v3/repos/hooks/#delete-a-hook
@@ -180,3 +198,27 @@ func (s *RepositoriesService) DeleteHook(ctx context.Context, owner, repo string
180198
}
181199
return s.client.Do(ctx, req, nil)
182200
}
201+
202+
// PingHook triggers a 'ping' event to be sent to the Hook.
203+
//
204+
// GitHub API docs: https://developer.github.com/v3/repos/hooks/#ping-a-hook
205+
func (s *RepositoriesService) PingHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
206+
u := fmt.Sprintf("repos/%v/%v/hooks/%d/pings", owner, repo, id)
207+
req, err := s.client.NewRequest("POST", u, nil)
208+
if err != nil {
209+
return nil, err
210+
}
211+
return s.client.Do(ctx, req, nil)
212+
}
213+
214+
// TestHook triggers a test Hook by github.
215+
//
216+
// GitHub API docs: https://developer.github.com/v3/repos/hooks/#test-a-push-hook
217+
func (s *RepositoriesService) TestHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
218+
u := fmt.Sprintf("repos/%v/%v/hooks/%d/tests", owner, repo, id)
219+
req, err := s.client.NewRequest("POST", u, nil)
220+
if err != nil {
221+
return nil, err
222+
}
223+
return s.client.Do(ctx, req, nil)
224+
}

github/repos_hooks_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,43 @@ func TestRepositoriesService_GetHook_invalidOwner(t *testing.T) {
103103
testURLParseError(t, err)
104104
}
105105

106+
func TestRepositoriesService_EditHook(t *testing.T) {
107+
client, mux, _, teardown := setup()
108+
defer teardown()
109+
110+
input := &Hook{}
111+
112+
mux.HandleFunc("/repos/o/r/hooks/1", func(w http.ResponseWriter, r *http.Request) {
113+
v := new(Hook)
114+
json.NewDecoder(r.Body).Decode(v)
115+
116+
testMethod(t, r, "PATCH")
117+
if !reflect.DeepEqual(v, input) {
118+
t.Errorf("Request body = %+v, want %+v", v, input)
119+
}
120+
121+
fmt.Fprint(w, `{"id":1}`)
122+
})
123+
124+
hook, _, err := client.Repositories.EditHook(context.Background(), "o", "r", 1, input)
125+
if err != nil {
126+
t.Errorf("Repositories.EditHook returned error: %v", err)
127+
}
128+
129+
want := &Hook{ID: Int64(1)}
130+
if !reflect.DeepEqual(hook, want) {
131+
t.Errorf("Repositories.EditHook returned %+v, want %+v", hook, want)
132+
}
133+
}
134+
135+
func TestRepositoriesService_EditHook_invalidOwner(t *testing.T) {
136+
client, _, _, teardown := setup()
137+
defer teardown()
138+
139+
_, _, err := client.Repositories.EditHook(context.Background(), "%", "%", 1, nil)
140+
testURLParseError(t, err)
141+
}
142+
106143
func TestRepositoriesService_DeleteHook(t *testing.T) {
107144
client, mux, _, teardown := setup()
108145
defer teardown()
@@ -124,3 +161,39 @@ func TestRepositoriesService_DeleteHook_invalidOwner(t *testing.T) {
124161
_, err := client.Repositories.DeleteHook(context.Background(), "%", "%", 1)
125162
testURLParseError(t, err)
126163
}
164+
165+
func TestRepositoriesService_PingHook(t *testing.T) {
166+
client, mux, _, teardown := setup()
167+
defer teardown()
168+
169+
mux.HandleFunc("/repos/o/r/hooks/1/pings", func(w http.ResponseWriter, r *http.Request) {
170+
testMethod(t, r, "POST")
171+
})
172+
173+
_, err := client.Repositories.PingHook(context.Background(), "o", "r", 1)
174+
if err != nil {
175+
t.Errorf("Repositories.PingHook returned error: %v", err)
176+
}
177+
}
178+
179+
func TestRepositoriesService_TestHook(t *testing.T) {
180+
client, mux, _, teardown := setup()
181+
defer teardown()
182+
183+
mux.HandleFunc("/repos/o/r/hooks/1/tests", func(w http.ResponseWriter, r *http.Request) {
184+
testMethod(t, r, "POST")
185+
})
186+
187+
_, err := client.Repositories.TestHook(context.Background(), "o", "r", 1)
188+
if err != nil {
189+
t.Errorf("Repositories.TestHook returned error: %v", err)
190+
}
191+
}
192+
193+
func TestRepositoriesService_TestHook_invalidOwner(t *testing.T) {
194+
client, _, _, teardown := setup()
195+
defer teardown()
196+
197+
_, err := client.Repositories.TestHook(context.Background(), "%", "%", 1)
198+
testURLParseError(t, err)
199+
}

0 commit comments

Comments
 (0)