Skip to content

Commit 3ad22bc

Browse files
authored
Add support for apps webhook config endpoints (#2096)
Fixes: #2095.
1 parent eefa002 commit 3ad22bc

File tree

6 files changed

+193
-0
lines changed

6 files changed

+193
-0
lines changed

github/apps_hooks.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2021 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+
)
11+
12+
// GetHookConfig returns the webhook configuration for a GitHub App.
13+
// The underlying transport must be authenticated as an app.
14+
//
15+
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps#get-a-webhook-configuration-for-an-app
16+
func (s *AppsService) GetHookConfig(ctx context.Context) (*HookConfig, *Response, error) {
17+
req, err := s.client.NewRequest("GET", "app/hook/config", nil)
18+
if err != nil {
19+
return nil, nil, err
20+
}
21+
22+
config := new(HookConfig)
23+
resp, err := s.client.Do(ctx, req, &config)
24+
if err != nil {
25+
return nil, resp, err
26+
}
27+
28+
return config, resp, nil
29+
}
30+
31+
// UpdateHookConfig updates the webhook configuration for a GitHub App.
32+
// The underlying transport must be authenticated as an app.
33+
//
34+
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps#update-a-webhook-configuration-for-an-app
35+
func (s *AppsService) UpdateHookConfig(ctx context.Context, config *HookConfig) (*HookConfig, *Response, error) {
36+
req, err := s.client.NewRequest("PATCH", "app/hook/config", config)
37+
if err != nil {
38+
return nil, nil, err
39+
}
40+
41+
c := new(HookConfig)
42+
resp, err := s.client.Do(ctx, req, c)
43+
if err != nil {
44+
return nil, resp, err
45+
}
46+
47+
return c, resp, nil
48+
}

github/apps_hooks_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2021 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+
"net/http"
12+
"testing"
13+
14+
"github.com/google/go-cmp/cmp"
15+
)
16+
17+
func TestAppsService_GetHookConfig(t *testing.T) {
18+
client, mux, _, teardown := setup()
19+
defer teardown()
20+
21+
mux.HandleFunc("/app/hook/config", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, "GET")
23+
fmt.Fprint(w, `{
24+
"content_type": "json",
25+
"insecure_ssl": "0",
26+
"secret": "********",
27+
"url": "https://example.com/webhook"
28+
}`)
29+
})
30+
31+
ctx := context.Background()
32+
config, _, err := client.Apps.GetHookConfig(ctx)
33+
if err != nil {
34+
t.Errorf("Apps.GetHookConfig returned error: %v", err)
35+
}
36+
37+
want := &HookConfig{
38+
ContentType: String("json"),
39+
InsecureSSL: String("0"),
40+
Secret: String("********"),
41+
URL: String("https://example.com/webhook"),
42+
}
43+
if !cmp.Equal(config, want) {
44+
t.Errorf("Apps.GetHookConfig returned %+v, want %+v", config, want)
45+
}
46+
47+
const methodName = "GetHookConfig"
48+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
49+
got, resp, err := client.Apps.GetHookConfig(ctx)
50+
if got != nil {
51+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
52+
}
53+
return resp, err
54+
})
55+
}
56+
57+
func TestAppsService_UpdateHookConfig(t *testing.T) {
58+
client, mux, _, teardown := setup()
59+
defer teardown()
60+
61+
input := &HookConfig{
62+
ContentType: String("json"),
63+
InsecureSSL: String("1"),
64+
Secret: String("s"),
65+
URL: String("u"),
66+
}
67+
68+
mux.HandleFunc("/app/hook/config", func(w http.ResponseWriter, r *http.Request) {
69+
testMethod(t, r, "PATCH")
70+
testBody(t, r, `{"content_type":"json","insecure_ssl":"1","url":"u","secret":"s"}`+"\n")
71+
fmt.Fprint(w, `{
72+
"content_type": "json",
73+
"insecure_ssl": "1",
74+
"secret": "********",
75+
"url": "u"
76+
}`)
77+
})
78+
79+
ctx := context.Background()
80+
config, _, err := client.Apps.UpdateHookConfig(ctx, input)
81+
if err != nil {
82+
t.Errorf("Apps.UpdateHookConfig returned error: %v", err)
83+
}
84+
85+
want := &HookConfig{
86+
ContentType: String("json"),
87+
InsecureSSL: String("1"),
88+
Secret: String("********"),
89+
URL: String("u"),
90+
}
91+
if !cmp.Equal(config, want) {
92+
t.Errorf("Apps.UpdateHookConfig returned %+v, want %+v", config, want)
93+
}
94+
95+
const methodName = "UpdateHookConfig"
96+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
97+
got, resp, err := client.Apps.UpdateHookConfig(ctx, input)
98+
if got != nil {
99+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
100+
}
101+
return resp, err
102+
})
103+
}

github/doc.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ BasicAuthTransport.
7373
7474
GitHub Apps authentication can be provided by the
7575
https://github.com/bradleyfalzon/ghinstallation package.
76+
It supports both authentication as an installation, using an installation access token,
77+
and as an app, using a JWT.
78+
79+
To authenticate as an installation:
7680
7781
import "github.com/bradleyfalzon/ghinstallation"
7882
@@ -89,6 +93,23 @@ https://github.com/bradleyfalzon/ghinstallation package.
8993
// Use client...
9094
}
9195
96+
To authenticate as an app, using a JWT:
97+
98+
import "github.com/bradleyfalzon/ghinstallation"
99+
100+
func main() {
101+
// Wrap the shared transport for use with the application ID 1.
102+
atr, err := ghinstallation.NewAppsTransportKeyFromFile(http.DefaultTransport, 1, "2016-10-19.private-key.pem")
103+
if err != nil {
104+
// Handle error.
105+
}
106+
107+
// Use app transport with client
108+
client := github.NewClient(&http.Client{Transport: atr})
109+
110+
// Use client...
111+
}
112+
92113
Rate Limiting
93114
94115
GitHub imposes a rate limit on all API clients. Unauthenticated clients are

github/github-accessors.go

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

github/github-accessors_test.go

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

github/orgs_audit_log.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ type HookConfig struct {
2424
ContentType *string `json:"content_type,omitempty"`
2525
InsecureSSL *string `json:"insecure_ssl,omitempty"`
2626
URL *string `json:"url,omitempty"`
27+
28+
// Secret is returned obfuscated by GitHub, but it can be set for outgoing requests.
29+
Secret *string `json:"secret,omitempty"`
2730
}
2831

2932
// AuditEntry describes the fields that may be represented by various audit-log "action" entries.

0 commit comments

Comments
 (0)