-
Notifications
You must be signed in to change notification settings - Fork 2
/
webhook.go
80 lines (64 loc) · 1.93 KB
/
webhook.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
package dockerhub
import (
"context"
"fmt"
"net/http"
"time"
)
type WebhookService service
type Webhooks struct {
Name string `json:"name"`
HookURL string `json:"hook_url"`
Created time.Time `json:"created"`
LastUpdated time.Time `json:"last_updated"`
}
type WebhookRequest struct {
Name string `json:"name"`
ExpectFinalCallback bool `json:"expect_final_callback"`
Webhooks []Webhooks `json:"webhooks"`
Registry string `json:"registry"`
}
type Webhook struct {
Name string `json:"name"`
Slug string `json:"slug"`
ExpectFinalCallback bool `json:"expect_final_callback"`
Webhooks []Webhooks `json:"webhooks"`
Created time.Time `json:"created"`
LastUpdated time.Time `json:"last_updated"`
}
func (s WebhookService) buildWebhookSlug(namespace, repo string) string {
return fmt.Sprintf("/repositories/%s/%s/webhook_pipeline/", namespace, repo)
}
func (s *WebhookService) CreateWebhook(ctx context.Context, namespace, repo, name, url string) (*Webhook, error) {
slug := s.buildWebhookSlug(namespace, repo)
hook := &WebhookRequest{
Name: name,
ExpectFinalCallback: false,
Registry: "registry-1.docker.io",
}
hook.Webhooks = append(hook.Webhooks, Webhooks{
Name: name,
HookURL: url,
})
req, err := s.client.NewRequest(http.MethodPost, slug, hook)
if err != nil {
return nil, err
}
res := &Webhook{}
if _, err := s.client.Do(ctx, req, res); err != nil {
return nil, err
}
return res, nil
}
func (s *WebhookService) GetWebhooks(ctx context.Context, namespace, repo string) (*Webhook, error) {
slug := s.buildWebhookSlug(namespace, repo)
req, err := s.client.NewRequest(http.MethodGet, slug, nil)
if err != nil {
return nil, err
}
res := &Webhook{}
if _, err := s.client.Do(ctx, req, res); err != nil {
return nil, err
}
return res, nil
}