-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
102 lines (84 loc) · 2.35 KB
/
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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
)
type WebhookProxyClient struct {
}
func (c *WebhookProxyClient) request() {
client := &http.Client{}
req, err := http.NewRequest(method, endpoint, reader)
if err != nil {
return err
}
req.Header = headers
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400:
return nil, errors.New("Not found")
}
}
func (c *WebhookProxyClient) Run() {
log.Printf("Forwarding webhooks from %s to %s", proxyEndpoint, jenkinsEndpoint)
ticker := time.NewTicker(10 * time.Second)
for _ = range ticker.C {
resp, err := http.Get(proxyEndpoint + "/webhook")
if err != nil {
log.Printf("Failed to retrieve webhooks: %s", err.Error())
continue
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Failed to read query: %s", err.Error())
resp.Body.Close()
continue
}
var requests RequestCache
if err := json.Unmarshal(body, &requests.Requests); err != nil {
log.Printf("Failed to unmarshal requests: %s: %s", err.Error(), string(body))
resp.Body.Close()
continue
}
resp.Body.Close()
if len(requests.Requests) == 0 {
continue
}
for _, request := range requests.Requests {
log.Printf("Resending request %+v\n", request)
req, err := c.request(request.Method, jenkinsEndpoint, bytes.NewReader([]byte(request.Body)))
if err != nil {
log.Printf("Failed to create request: %+s", err.Error())
continue
}
req.Header = request.Headers
resp, err := client.Do(req)
if err != nil || resp.StatusCode >= 400 {
log.Printf("Failed to push webhook: %+v %+v", err, resp)
continue
}
resp.Body.Close()
req, err = http.NewRequest("DELETE", proxyEndpoint+"/webhook/"+request.UUID, nil)
if err != nil {
log.Printf("Failed to create request: %+s", err.Error())
continue
}
resp, err = client.Do(req)
if err != nil || resp.StatusCode >= 400 {
log.Printf("Failed to delete webhook %s: %+v, %+v", request.UUID, err, resp)
}
if resp != nil {
resp.Body.Close()
}
}
}
}
func NewWebhookProxyClient() *WebhookProxyClient {
return &WebhookProxyClient{}
}