Skip to content

Commit 505b7ea

Browse files
authored
Add support for organization, repository webhook configuration (#2885)
Fixes: #2884.
1 parent 7ceef94 commit 505b7ea

File tree

4 files changed

+344
-0
lines changed

4 files changed

+344
-0
lines changed

github/orgs_hooks_configuration.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2023 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+
// GetHookConfiguration returns the configuration for the specified organization webhook.
14+
//
15+
// GitHub API docs: https://docs.github.com/en/rest/orgs/webhooks?apiVersion=2022-11-28#get-a-webhook-configuration-for-an-organization
16+
func (s *OrganizationsService) GetHookConfiguration(ctx context.Context, org string, id int64) (*HookConfig, *Response, error) {
17+
u := fmt.Sprintf("orgs/%v/hooks/%v/config", org, id)
18+
req, err := s.client.NewRequest("GET", u, nil)
19+
if err != nil {
20+
return nil, nil, err
21+
}
22+
23+
config := new(HookConfig)
24+
resp, err := s.client.Do(ctx, req, config)
25+
if err != nil {
26+
return nil, resp, err
27+
}
28+
29+
return config, resp, nil
30+
}
31+
32+
// EditHookConfiguration updates the configuration for the specified organization webhook.
33+
//
34+
// GitHub API docs: https://docs.github.com/en/rest/orgs/webhooks?apiVersion=2022-11-28#update-a-webhook-configuration-for-an-organization
35+
func (s *OrganizationsService) EditHookConfiguration(ctx context.Context, org string, id int64, config *HookConfig) (*HookConfig, *Response, error) {
36+
u := fmt.Sprintf("orgs/%v/hooks/%v/config", org, id)
37+
req, err := s.client.NewRequest("PATCH", u, config)
38+
if err != nil {
39+
return nil, nil, err
40+
}
41+
42+
c := new(HookConfig)
43+
resp, err := s.client.Do(ctx, req, c)
44+
if err != nil {
45+
return nil, resp, err
46+
}
47+
48+
return c, resp, nil
49+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright 2023 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+
"testing"
14+
15+
"github.com/google/go-cmp/cmp"
16+
)
17+
18+
func TestOrganizationsService_GetHookConfiguration(t *testing.T) {
19+
client, mux, _, teardown := setup()
20+
defer teardown()
21+
22+
mux.HandleFunc("/orgs/o/hooks/1/config", func(w http.ResponseWriter, r *http.Request) {
23+
testMethod(t, r, "GET")
24+
fmt.Fprint(w, `{"content_type": "json", "insecure_ssl": "0", "secret": "********", "url": "https://example.com/webhook"}`)
25+
})
26+
27+
ctx := context.Background()
28+
config, _, err := client.Organizations.GetHookConfiguration(ctx, "o", 1)
29+
if err != nil {
30+
t.Errorf("Organizations.GetHookConfiguration returned error: %v", err)
31+
}
32+
33+
want := &HookConfig{
34+
ContentType: String("json"),
35+
InsecureSSL: String("0"),
36+
Secret: String("********"),
37+
URL: String("https://example.com/webhook"),
38+
}
39+
if !cmp.Equal(config, want) {
40+
t.Errorf("Organizations.GetHookConfiguration returned %+v, want %+v", config, want)
41+
}
42+
43+
const methodName = "GetHookConfiguration"
44+
testBadOptions(t, methodName, func() (err error) {
45+
_, _, err = client.Organizations.GetHookConfiguration(ctx, "\n", -1)
46+
return err
47+
})
48+
49+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
50+
got, resp, err := client.Organizations.GetHookConfiguration(ctx, "o", 1)
51+
if got != nil {
52+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
53+
}
54+
return resp, err
55+
})
56+
}
57+
58+
func TestOrganizationsService_GetHookConfiguration_invalidOrg(t *testing.T) {
59+
client, _, _, teardown := setup()
60+
defer teardown()
61+
62+
ctx := context.Background()
63+
_, _, err := client.Organizations.GetHookConfiguration(ctx, "%", 1)
64+
testURLParseError(t, err)
65+
}
66+
67+
func TestOrganizationsService_EditHookConfiguration(t *testing.T) {
68+
client, mux, _, teardown := setup()
69+
defer teardown()
70+
71+
input := &HookConfig{}
72+
73+
mux.HandleFunc("/orgs/o/hooks/1/config", func(w http.ResponseWriter, r *http.Request) {
74+
v := new(HookConfig)
75+
json.NewDecoder(r.Body).Decode(v)
76+
77+
testMethod(t, r, "PATCH")
78+
if !cmp.Equal(v, input) {
79+
t.Errorf("Request body = %+v, want %+v", v, input)
80+
}
81+
82+
fmt.Fprint(w, `{"content_type": "json", "insecure_ssl": "0", "secret": "********", "url": "https://example.com/webhook"}`)
83+
})
84+
85+
ctx := context.Background()
86+
config, _, err := client.Organizations.EditHookConfiguration(ctx, "o", 1, input)
87+
if err != nil {
88+
t.Errorf("Organizations.EditHookConfiguration returned error: %v", err)
89+
}
90+
91+
want := &HookConfig{
92+
ContentType: String("json"),
93+
InsecureSSL: String("0"),
94+
Secret: String("********"),
95+
URL: String("https://example.com/webhook"),
96+
}
97+
if !cmp.Equal(config, want) {
98+
t.Errorf("Organizations.EditHookConfiguration returned %+v, want %+v", config, want)
99+
}
100+
101+
const methodName = "EditHookConfiguration"
102+
testBadOptions(t, methodName, func() (err error) {
103+
_, _, err = client.Organizations.EditHookConfiguration(ctx, "\n", -1, input)
104+
return err
105+
})
106+
107+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
108+
got, resp, err := client.Organizations.EditHookConfiguration(ctx, "o", 1, input)
109+
if got != nil {
110+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
111+
}
112+
return resp, err
113+
})
114+
}
115+
116+
func TestOrganizationsService_EditHookConfiguration_invalidOrg(t *testing.T) {
117+
client, _, _, teardown := setup()
118+
defer teardown()
119+
120+
ctx := context.Background()
121+
_, _, err := client.Organizations.EditHookConfiguration(ctx, "%", 1, nil)
122+
testURLParseError(t, err)
123+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2023 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+
// GetHookConfiguration returns the configuration for the specified repository webhook.
14+
//
15+
// GitHub API docs: https://docs.github.com/en/rest/webhooks/repo-config?apiVersion=2022-11-28#get-a-webhook-configuration-for-a-repository
16+
func (s *RepositoriesService) GetHookConfiguration(ctx context.Context, owner, repo string, id int64) (*HookConfig, *Response, error) {
17+
u := fmt.Sprintf("repos/%v/%v/hooks/%v/config", owner, repo, id)
18+
req, err := s.client.NewRequest("GET", u, nil)
19+
if err != nil {
20+
return nil, nil, err
21+
}
22+
23+
config := new(HookConfig)
24+
resp, err := s.client.Do(ctx, req, config)
25+
if err != nil {
26+
return nil, resp, err
27+
}
28+
29+
return config, resp, nil
30+
}
31+
32+
// EditHookConfiguration updates the configuration for the specified repository webhook.
33+
//
34+
// GitHub API docs: https://docs.github.com/en/rest/webhooks/repo-config?apiVersion=2022-11-28#update-a-webhook-configuration-for-a-repository
35+
func (s *RepositoriesService) EditHookConfiguration(ctx context.Context, owner, repo string, id int64, config *HookConfig) (*HookConfig, *Response, error) {
36+
u := fmt.Sprintf("repos/%v/%v/hooks/%v/config", owner, repo, id)
37+
req, err := s.client.NewRequest("PATCH", u, config)
38+
if err != nil {
39+
return nil, nil, err
40+
}
41+
42+
c := new(HookConfig)
43+
resp, err := s.client.Do(ctx, req, c)
44+
if err != nil {
45+
return nil, resp, err
46+
}
47+
48+
return c, resp, nil
49+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright 2023 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+
"testing"
14+
15+
"github.com/google/go-cmp/cmp"
16+
)
17+
18+
func TestRepositoriesService_GetHookConfiguration(t *testing.T) {
19+
client, mux, _, teardown := setup()
20+
defer teardown()
21+
22+
mux.HandleFunc("/repos/o/r/hooks/1/config", func(w http.ResponseWriter, r *http.Request) {
23+
testMethod(t, r, "GET")
24+
fmt.Fprint(w, `{"content_type": "json", "insecure_ssl": "0", "secret": "********", "url": "https://example.com/webhook"}`)
25+
})
26+
27+
ctx := context.Background()
28+
config, _, err := client.Repositories.GetHookConfiguration(ctx, "o", "r", 1)
29+
if err != nil {
30+
t.Errorf("Repositories.GetHookConfiguration returned error: %v", err)
31+
}
32+
33+
want := &HookConfig{
34+
ContentType: String("json"),
35+
InsecureSSL: String("0"),
36+
Secret: String("********"),
37+
URL: String("https://example.com/webhook"),
38+
}
39+
if !cmp.Equal(config, want) {
40+
t.Errorf("Repositories.GetHookConfiguration returned %+v, want %+v", config, want)
41+
}
42+
43+
const methodName = "GetHookConfiguration"
44+
testBadOptions(t, methodName, func() (err error) {
45+
_, _, err = client.Repositories.GetHookConfiguration(ctx, "\n", "\n", -1)
46+
return err
47+
})
48+
49+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
50+
got, resp, err := client.Repositories.GetHookConfiguration(ctx, "o", "r", 1)
51+
if got != nil {
52+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
53+
}
54+
return resp, err
55+
})
56+
}
57+
58+
func TestRepositoriesService_GetHookConfiguration_invalidOrg(t *testing.T) {
59+
client, _, _, teardown := setup()
60+
defer teardown()
61+
62+
ctx := context.Background()
63+
_, _, err := client.Repositories.GetHookConfiguration(ctx, "%", "%", 1)
64+
testURLParseError(t, err)
65+
}
66+
67+
func TestRepositoriesService_EditHookConfiguration(t *testing.T) {
68+
client, mux, _, teardown := setup()
69+
defer teardown()
70+
71+
input := &HookConfig{}
72+
73+
mux.HandleFunc("/repos/o/r/hooks/1/config", func(w http.ResponseWriter, r *http.Request) {
74+
v := new(HookConfig)
75+
json.NewDecoder(r.Body).Decode(v)
76+
77+
testMethod(t, r, "PATCH")
78+
if !cmp.Equal(v, input) {
79+
t.Errorf("Request body = %+v, want %+v", v, input)
80+
}
81+
82+
fmt.Fprint(w, `{"content_type": "json", "insecure_ssl": "0", "secret": "********", "url": "https://example.com/webhook"}`)
83+
})
84+
85+
ctx := context.Background()
86+
config, _, err := client.Repositories.EditHookConfiguration(ctx, "o", "r", 1, input)
87+
if err != nil {
88+
t.Errorf("Repositories.EditHookConfiguration returned error: %v", err)
89+
}
90+
91+
want := &HookConfig{
92+
ContentType: String("json"),
93+
InsecureSSL: String("0"),
94+
Secret: String("********"),
95+
URL: String("https://example.com/webhook"),
96+
}
97+
if !cmp.Equal(config, want) {
98+
t.Errorf("Repositories.EditHookConfiguration returned %+v, want %+v", config, want)
99+
}
100+
101+
const methodName = "EditHookConfiguration"
102+
testBadOptions(t, methodName, func() (err error) {
103+
_, _, err = client.Repositories.EditHookConfiguration(ctx, "\n", "\n", -1, input)
104+
return err
105+
})
106+
107+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
108+
got, resp, err := client.Repositories.EditHookConfiguration(ctx, "o", "r", 1, input)
109+
if got != nil {
110+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
111+
}
112+
return resp, err
113+
})
114+
}
115+
116+
func TestRepositoriesService_EditHookConfiguration_invalidOrg(t *testing.T) {
117+
client, _, _, teardown := setup()
118+
defer teardown()
119+
120+
ctx := context.Background()
121+
_, _, err := client.Repositories.EditHookConfiguration(ctx, "%", "%", 1, nil)
122+
testURLParseError(t, err)
123+
}

0 commit comments

Comments
 (0)