forked from knative/eventing-contrib
/
webhook_client.go
185 lines (153 loc) · 5.88 KB
/
webhook_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package github
import (
"context"
"fmt"
"net/url"
"strconv"
ghclient "github.com/google/go-github/github"
"golang.org/x/oauth2"
"knative.dev/pkg/apis"
"knative.dev/pkg/logging"
)
type webhookOptions struct {
accessToken string
secretToken string
url *apis.URL
owner string
repo string
events []string
}
type webhookClient interface {
Create(ctx context.Context, options *webhookOptions, alternateGitHubAPIURL string) (string, error)
Reconcile(ctx context.Context, options *webhookOptions, hookID, alternateGitHubAPIURL string) error
Delete(ctx context.Context, options *webhookOptions, hookID, alternateGitHubAPIURL string) error
}
type gitHubWebhookClient struct{}
func (client gitHubWebhookClient) Create(ctx context.Context, options *webhookOptions, alternateGitHubAPIURL string) (string, error) {
var err error
logger := logging.FromContext(ctx)
ghClient := client.createGitHubClient(ctx, options)
if alternateGitHubAPIURL != "" {
//This is to support GitHub Enterprise, this might be something like https://github.company.com/api/v3/
ghClient.BaseURL, err = url.Parse(alternateGitHubAPIURL)
if err != nil {
logger.Infof("Failed to create webhook because an error occurred parsing githubAPIURL %q, error was: %v", alternateGitHubAPIURL, err)
return "", fmt.Errorf("error occurred parsing githubAPIURL: %v", err)
}
}
hook := client.hookConfig(ctx, options)
var h *ghclient.Hook
var resp *ghclient.Response
if options.repo != "" {
h, resp, err = ghClient.Repositories.CreateHook(ctx, options.owner, options.repo, &hook)
} else {
h, resp, err = ghClient.Organizations.CreateHook(ctx, options.owner, &hook)
}
if err != nil {
logger.Infof("create webhook error response:\n%+v", resp)
return "", fmt.Errorf("failed to create the webhook: %v", err)
}
logger.Infof("created hook: %+v", h)
return strconv.FormatInt(*h.ID, 10), nil
}
func (client gitHubWebhookClient) Reconcile(ctx context.Context, options *webhookOptions, hookID, alternateGitHubAPIURL string) error {
var err error
logger := logging.FromContext(ctx)
ghClient := client.createGitHubClient(ctx, options)
if alternateGitHubAPIURL != "" {
//This is to support GitHub Enterprise, this might be something like https://github.company.com/api/v3/
ghClient.BaseURL, err = url.Parse(alternateGitHubAPIURL)
if err != nil {
logger.Infof("Failed to reconcile webhook because an error occurred parsing githubAPIURL %q, error was: %v", alternateGitHubAPIURL, err)
return fmt.Errorf("error occurred parsing githubAPIURL: %v", err)
}
}
hook := client.hookConfig(ctx, options)
hookIDInt, err := strconv.ParseInt(hookID, 10, 64)
if err != nil {
return fmt.Errorf("failed to convert webhook %q to int64: %v", hookID, err)
}
// In a perfect world we would GetHook and compare, but given the volume of
// system-generated (CreatedAt) or redacted (secret) properties, this seems
// like an exercise in futility.
var h *ghclient.Hook
var resp *ghclient.Response
if options.repo != "" {
h, resp, err = ghClient.Repositories.EditHook(ctx, options.owner, options.repo, hookIDInt, &hook)
} else {
h, resp, err = ghClient.Organizations.EditHook(ctx, options.owner, hookIDInt, &hook)
}
if err != nil {
logger.Infof("update webhook error response:\n%+v", resp)
return fmt.Errorf("failed to update the webhook: %v", err)
}
logger.Infof("updated hook: %+v", h)
return nil
}
func (client gitHubWebhookClient) Delete(ctx context.Context, options *webhookOptions, hookID, alternateGitHubAPIURL string) error {
logger := logging.FromContext(ctx)
hookIDInt, err := strconv.ParseInt(hookID, 10, 64)
if err != nil {
return fmt.Errorf("failed to convert webhook %q to int64: %v", hookID, err)
}
ghClient := client.createGitHubClient(ctx, options)
if alternateGitHubAPIURL != "" {
ghClient.BaseURL, err = url.Parse(alternateGitHubAPIURL)
if err != nil {
logger.Infof("Failed to delete webhook because an error occurred parsing githubAPIURL %q, error was: %v", alternateGitHubAPIURL, err)
return fmt.Errorf("error occurred parsing githubAPIURL: %v", err)
}
}
hook := client.hookConfig(ctx, options)
var resp *ghclient.Response
if options.repo != "" {
resp, err = ghClient.Repositories.DeleteHook(ctx, options.owner, options.repo, hookIDInt)
} else {
resp, err = ghClient.Organizations.DeleteHook(ctx, options.owner, hookIDInt)
}
if err != nil {
logger.Infof("delete webhook error response:\n%+v", resp)
return fmt.Errorf("failed to delete the webhook: %w", err)
}
logger.Infof("deleted hook: %s", hook.ID)
return nil
}
func (client gitHubWebhookClient) createGitHubClient(ctx context.Context, options *webhookOptions) *ghclient.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: options.accessToken},
)
tc := oauth2.NewClient(ctx, ts)
return ghclient.NewClient(tc)
}
func (client gitHubWebhookClient) hookConfig(ctx context.Context, options *webhookOptions) ghclient.Hook {
active := true
config := make(map[string]interface{})
var pus *string
if options.url != nil {
us := options.url.String()
config["url"] = us
pus = &us
}
config["content_type"] = "json"
config["secret"] = options.secretToken
// GitHub hook names are required to be named "web" or the name of a GitHub service
hook := ghclient.Hook{
URL: pus,
Events: options.events,
Active: &active,
Config: config,
}
return hook
}