forked from 0xch4z/dockerhub-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhook.go
129 lines (109 loc) · 3.44 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
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
package dockerhub
import (
"context"
"fmt"
"net/http"
"time"
)
// WebhookService Type
type WebhookService service
// Webhooks Payload
type Webhooks struct {
Name string `json:"name"`
HookURL string `json:"hook_url"`
Created time.Time `json:"created"`
LastUpdated time.Time `json:"last_updated"`
}
// Results Response
type Results struct {
Name string `json:"name"`
Slug string `json:"slug"`
ExpectFinalCallback bool `json:"expect_final_callback"`
Created time.Time `json:"created"`
LastUpdated time.Time `json:"last_updated"`
Webhooks []Webhooks `json:"webhooks"`
}
// WebhookRequest payload
type WebhookRequest struct {
Name string `json:"name"`
ExpectFinalCallback bool `json:"expect_final_callback"`
Webhooks []Webhooks `json:"webhooks"`
Registry string `json:"registry"`
}
// Autogenerated Response of webhook
type Autogenerated struct {
Count int `json:"count"`
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
Results []struct {
Name string `json:"name"`
Slug string `json:"slug"`
ExpectFinalCallback bool `json:"expect_final_callback"`
Webhooks []struct {
Name string `json:"name"`
HookURL string `json:"hook_url"`
Created time.Time `json:"created"`
LastUpdated time.Time `json:"last_updated"`
} `json:"webhooks"`
Created time.Time `json:"created"`
LastUpdated time.Time `json:"last_updated"`
} `json:"results"`
}
// WebhookResponse Response of webhook
type WebhookResponse struct {
Count int `json:"count"`
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
Results []Results `json:"results"`
}
func (s WebhookService) buildWebhookSlug(namespace, repo string) string {
return fmt.Sprintf("/repositories/%s/%s/webhook_pipeline/", namespace, repo)
}
// CreateWebhook create webhook for the triggers
func (s *WebhookService) CreateWebhook(ctx context.Context, namespace, repo, name, url string) (*WebhookResponse, 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 := &WebhookResponse{}
if _, err := s.client.Do(ctx, req, res); err != nil {
return nil, err
}
return res, nil
}
// GetWebhooks Get the related webhooks
func (s *WebhookService) GetWebhooks(ctx context.Context, namespace, repo string) (*WebhookResponse, error) {
slug := s.buildWebhookSlug(namespace, repo)
req, err := s.client.NewRequest(http.MethodGet, slug, nil)
if err != nil {
return nil, err
}
res := &WebhookResponse{}
if _, err := s.client.Do(ctx, req, res); err != nil {
return nil, err
}
return res, nil
}
// DeleteWebhook Delete the existing webhooks
func (s *WebhookService) DeleteWebhook(ctx context.Context, namespace, repo, name string) error {
slug := s.buildWebhookSlug(namespace, repo)
webhookURL := fmt.Sprintf("%s%s/", slug, name)
req, err := s.client.NewRequest(http.MethodDelete, webhookURL, nil)
if err != nil {
return err
}
if _, err := s.client.Do(ctx, req, nil); err != nil {
return err
}
return nil
}