-
Notifications
You must be signed in to change notification settings - Fork 235
/
docker.go
91 lines (78 loc) · 2.39 KB
/
docker.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
package docker
// this package receives the Docker Hub Automated Build webhook
// https://docs.docker.com/docker-hub/webhooks/
// NOT the Docker Trusted Registry webhook
// https://docs.docker.com/ee/dtr/user/create-and-manage-webhooks/
import (
"encoding/json"
"errors"
"io"
"net/http"
)
// parse errors
var (
ErrInvalidHTTPMethod = errors.New("invalid HTTP Method")
ErrParsingPayload = errors.New("error parsing payload")
)
// Event defines a Docker hook event type
type Event string
// Docker hook types (only one for now)
const (
BuildEvent Event = "build"
)
// BuildPayload a docker hub build notice
// https://docs.docker.com/docker-hub/webhooks/
type BuildPayload struct {
CallbackURL string `json:"callback_url"`
PushData struct {
Images []string `json:"images"`
PushedAt float32 `json:"pushed_at"`
Pusher string `json:"pusher"`
Tag string `json:"tag"`
} `json:"push_data"`
Repository struct {
CommentCount int `json:"comment_count"`
DateCreated float32 `json:"date_created"`
Description string `json:"description"`
Dockerfile string `json:"dockerfile"`
FullDescription string `json:"full_description"`
IsOfficial bool `json:"is_official"`
IsPrivate bool `json:"is_private"`
IsTrusted bool `json:"is_trusted"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Owner string `json:"owner"`
RepoName string `json:"repo_name"`
RepoURL string `json:"repo_url"`
StarCount int `json:"star_count"`
Status string `json:"status"`
} `json:"repository"`
}
// Webhook instance contains all methods needed to process events
type Webhook struct {
}
// New creates and returns a WebHook instance
func New() (*Webhook, error) {
hook := new(Webhook)
return hook, nil
}
// Parse verifies and parses the events specified and returns the payload object or an error
func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error) {
defer func() {
_, _ = io.Copy(io.Discard, r.Body)
_ = r.Body.Close()
}()
if r.Method != http.MethodPost {
return nil, ErrInvalidHTTPMethod
}
payload, err := io.ReadAll(r.Body)
if err != nil || len(payload) == 0 {
return nil, ErrParsingPayload
}
var pl BuildPayload
err = json.Unmarshal([]byte(payload), &pl)
if err != nil {
return nil, ErrParsingPayload
}
return pl, err
}