/
client_hook.go
224 lines (202 loc) · 6.51 KB
/
client_hook.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package github
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/rockbears/log"
"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/sdk"
)
func (g *githubClient) CreateHook(ctx context.Context, repo string, hook *sdk.VCSHook) error {
url := "/repos/" + repo + "/hooks"
if g.proxyURL != "" {
lastIndexSlash := strings.LastIndex(hook.URL, "/")
if g.proxyURL[len(g.proxyURL)-1] == '/' {
lastIndexSlash++
}
hook.URL = g.proxyURL + hook.URL[lastIndexSlash:]
}
if len(hook.Events) == 0 {
hook.Events = sdk.GitHubEventsDefault
}
r := WebhookCreate{
Name: "web",
Active: true,
Events: hook.Events,
Config: WebHookConfig{
URL: hook.URL,
ContentType: "json",
},
}
b, err := json.Marshal(r)
if err != nil {
return sdk.WrapError(err, "Cannot marshal body %+v", r)
}
res, err := g.post(ctx, url, "application/json", bytes.NewBuffer(b), nil, nil)
if err != nil {
return sdk.WrapError(err, "github.CreateHook")
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return sdk.WrapError(err, "ReadAll")
}
if res.StatusCode != 201 {
if strings.Contains(string(body), "Hook already exists on this repository") {
return nil
}
err := fmt.Errorf("Unable to create webhook on github. Status code : %d - Body: %s. ", res.StatusCode, body)
return sdk.WrapError(err, "github.CreateHook. Data : %s", b)
}
if err := sdk.JSONUnmarshal(body, &r); err != nil {
return sdk.WrapError(err, "Cannot unmarshal response")
}
hook.ID = fmt.Sprintf("%d", r.ID)
return nil
}
func (g *githubClient) UpdateHook(ctx context.Context, repo string, hook *sdk.VCSHook) error {
githubWebHook, err := g.getHookByID(ctx, repo, hook.ID)
if err != nil {
return err
}
url := "/repos/" + repo + "/hooks/" + hook.ID
if g.proxyURL != "" {
lastIndexSlash := strings.LastIndex(hook.URL, "/")
if g.proxyURL[len(g.proxyURL)-1] == '/' {
lastIndexSlash++
}
hook.URL = g.proxyURL + hook.URL[lastIndexSlash:]
}
if len(hook.Events) == 0 {
hook.Events = sdk.GitHubEventsDefault
}
githubWebHook.Events = hook.Events
b, err := json.Marshal(githubWebHook)
if err != nil {
return sdk.WrapError(err, "Cannot marshal body %+v", githubWebHook)
}
res, err := g.patch(ctx, url, "application/json", bytes.NewBuffer(b), nil)
if err != nil {
return sdk.WrapError(err, "github.UpdateHook")
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return sdk.WrapError(err, "ReadAll")
}
if res.StatusCode != 200 {
err := fmt.Errorf("Unable to update webhook on github. Status code : %d - Body: %s. ", res.StatusCode, body)
return sdk.WrapError(err, "github.Update. Data : %s", b)
}
return nil
}
func (g *githubClient) getHooks(ctx context.Context, fullname string) ([]Webhook, error) {
var webhooks = []Webhook{}
cacheKey := cache.Key("vcs", "github", "hooks", sdk.Hash512(g.OAuthToken+g.username), "/repos/"+fullname+"/hooks")
opts := []getArgFunc{withETag}
var nextPage = "/repos/" + fullname + "/hooks"
for nextPage != "" {
if ctx.Err() != nil {
break
}
status, body, headers, err := g.get(ctx, nextPage, opts...)
if err != nil {
log.Warn(ctx, "githubClient.PullRequests> Error %s", err)
return nil, sdk.WithStack(err)
}
if status >= 400 {
return nil, sdk.NewError(sdk.ErrUnknownError, errorAPI(body))
}
opts[0] = withETag
nextHooks := []Webhook{}
//Github may return 304 status because we are using conditional request with ETag based headers
if status == http.StatusNotModified {
//If repos aren't updated, lets get them from cache
find, err := g.Cache.Get(cacheKey, &webhooks)
if err != nil {
log.Error(ctx, "cannot get from cache %s: %v", cacheKey, err)
}
if !find {
opts[0] = withoutETag
log.Error(ctx, "Unable to get getHooks (%s) from the cache", strings.ReplaceAll(cacheKey, g.OAuthToken, ""))
continue
}
break
} else {
if err := sdk.JSONUnmarshal(body, &nextHooks); err != nil {
log.Warn(ctx, "githubClient.getHooks> Unable to parse github hooks: %s", err)
return nil, err
}
}
webhooks = append(webhooks, nextHooks...)
nextPage = getNextPage(headers)
}
//Put the body on cache for one hour and one minute
if err := g.Cache.SetWithTTL(cacheKey, webhooks, 61*60); err != nil {
log.Error(ctx, "cannot SetWithTTL: %s: %v", cacheKey, err)
}
return webhooks, nil
}
func (g *githubClient) GetHook(ctx context.Context, fullname, webhookURL string) (sdk.VCSHook, error) {
hooks, err := g.getHooks(ctx, fullname)
if err != nil {
return sdk.VCSHook{}, sdk.WithStack(err)
}
for _, h := range hooks {
log.Info(ctx, "hooks: %s (expecting: %s)", h.Config.URL, webhookURL)
if h.Config.URL == webhookURL {
return sdk.VCSHook{
Name: h.Name,
Events: h.Events,
URL: h.Config.URL,
ContentType: h.Config.ContentType,
ID: strconv.Itoa(h.ID),
}, nil
}
}
return sdk.VCSHook{}, sdk.WithStack(sdk.ErrNotFound)
}
func (g *githubClient) getHookByID(ctx context.Context, fullname, id string) (Webhook, error) {
var webhook Webhook
url := "/repos/" + fullname + "/hooks/" + id
cacheKey := cache.Key("vcs", "github", "hooks", id, sdk.Hash512(g.OAuthToken+g.username), "/repos/"+fullname+"/hooks/"+id)
opts := []getArgFunc{withETag}
status, body, _, err := g.get(ctx, url, opts...)
if err != nil {
log.Warn(ctx, "githubClient.PullRequests> Error %v", err)
return webhook, sdk.WithStack(err)
}
if status >= 400 {
return webhook, sdk.NewError(sdk.ErrUnknownError, errorAPI(body))
}
opts[0] = withETag
//Github may return 304 status because we are using conditional request with ETag based headers
if status == http.StatusNotModified {
//If repos aren't updated, lets get them from cache
find, err := g.Cache.Get(cacheKey, &webhook)
if err != nil {
log.Error(ctx, "cannot get from cache %s: %v", cacheKey, err)
}
if !find {
return webhook, sdk.WithStack(fmt.Errorf("unable to get getHooks (%s) from the cache", strings.ReplaceAll(cacheKey, g.OAuthToken, "")))
}
} else {
if err := sdk.JSONUnmarshal(body, &webhook); err != nil {
log.Warn(ctx, "githubClient.getHookByID> Unable to parse github hook: %v", err)
return webhook, err
}
}
//Put the body on cache for one hour and one minute
if err := g.Cache.SetWithTTL(cacheKey, webhook, 61*60); err != nil {
log.Error(ctx, "cannot SetWithTTL: %s: %v", cacheKey, err)
}
return webhook, nil
}
func (g *githubClient) DeleteHook(ctx context.Context, repo string, hook sdk.VCSHook) error {
return g.delete(ctx, "/repos/"+repo+"/hooks/"+hook.ID)
}