|
| 1 | +package docker |
| 2 | + |
| 3 | +// this package recieves the Docker Hub Automated Build webhook |
| 4 | +// https://docs.docker.com/docker-hub/webhooks/ |
| 5 | +// NOT the Docker Trusted Registry webhook |
| 6 | +// https://docs.docker.com/ee/dtr/user/create-and-manage-webhooks/ |
| 7 | + |
| 8 | +import ( |
| 9 | + "encoding/json" |
| 10 | + "errors" |
| 11 | + "io" |
| 12 | + "io/ioutil" |
| 13 | + "net/http" |
| 14 | + |
| 15 | + log "github.com/Sirupsen/logrus" |
| 16 | +) |
| 17 | + |
| 18 | +// parse errors |
| 19 | +var ( |
| 20 | + ErrInvalidHTTPMethod = errors.New("invalid HTTP Method") |
| 21 | + ErrParsingPayload = errors.New("error parsing payload") |
| 22 | +) |
| 23 | + |
| 24 | +// Event defines a GitHub hook event type |
| 25 | +type Event string |
| 26 | + |
| 27 | +// GitHub hook types |
| 28 | +const ( |
| 29 | + BuildEvent Event = "build" |
| 30 | +) |
| 31 | + |
| 32 | +// BuildPayload a docker hub build notice |
| 33 | +// https://docs.docker.com/docker-hub/webhooks/ |
| 34 | +type BuildPayload struct { |
| 35 | + CallbackURL string `json:"callback_url"` |
| 36 | + PushData struct { |
| 37 | + Images []string `json:"images"` |
| 38 | + PushedAt float32 `json:"pushed_at"` |
| 39 | + Pusher string `json:"pusher"` |
| 40 | + Tag string `json:"tag"` |
| 41 | + } `json:"push_data"` |
| 42 | + Repository struct { |
| 43 | + CommentCount int `json:"comment_count"` |
| 44 | + DateCreated float32 `json:"date_created"` |
| 45 | + Description string `json:"description"` |
| 46 | + Dockerfile string `json:"dockerfile"` |
| 47 | + FullDescription string `json:"full_description"` |
| 48 | + IsOfficial bool `json:"is_official"` |
| 49 | + IsPrivate bool `json:"is_private"` |
| 50 | + IsTrusted bool `json:"is_trusted"` |
| 51 | + Name string `json:"name"` |
| 52 | + Namespace string `json:"namespace"` |
| 53 | + Owner string `json:"owner"` |
| 54 | + RepoName string `json:"repo_name"` |
| 55 | + RepoURL string `json:"repo_url"` |
| 56 | + StarCount int `json:"star_count"` |
| 57 | + Status string `json:"status"` |
| 58 | + } `json:"repository"` |
| 59 | +} |
| 60 | + |
| 61 | +// there are no options for docker webhooks |
| 62 | +// however I'm leaving this here for now in anticipation of future support for Docker Trusted Registry |
| 63 | + |
| 64 | +// // Option is a configuration option for the webhook |
| 65 | +// type Option func(*Webhook) error |
| 66 | + |
| 67 | +// // Options is a namespace var for configuration options |
| 68 | +// var Options = WebhookOptions{} |
| 69 | + |
| 70 | +// // WebhookOptions is a namespace for configuration option methods |
| 71 | +// type WebhookOptions struct{} |
| 72 | + |
| 73 | +// // Secret registers the GitHub secret |
| 74 | +// func (WebhookOptions) Secret(secret string) Option { |
| 75 | +// return func(hook *Webhook) error { |
| 76 | +// hook.secret = secret |
| 77 | +// return nil |
| 78 | +// } |
| 79 | +// } |
| 80 | + |
| 81 | +// Webhook instance contains all methods needed to process events |
| 82 | +type Webhook struct { |
| 83 | + secret string |
| 84 | +} |
| 85 | + |
| 86 | +// New creates and returns a WebHook instance denoted by the Provider type |
| 87 | +func New() (*Webhook, error) { |
| 88 | + // func New(options ...Option) (*Webhook, error) { |
| 89 | + hook := new(Webhook) |
| 90 | + // for _, opt := range options { |
| 91 | + // if err := opt(hook); err != nil { |
| 92 | + // return nil, errors.New("Error applying Option") |
| 93 | + // } |
| 94 | + // } |
| 95 | + return hook, nil |
| 96 | +} |
| 97 | + |
| 98 | +// Parse verifies and parses the events specified and returns the payload object or an error |
| 99 | +func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error) { |
| 100 | + defer func() { |
| 101 | + _, _ = io.Copy(ioutil.Discard, r.Body) |
| 102 | + _ = r.Body.Close() |
| 103 | + }() |
| 104 | + |
| 105 | + if r.Method != http.MethodPost { |
| 106 | + return nil, ErrInvalidHTTPMethod |
| 107 | + } |
| 108 | + |
| 109 | + // event := r.Header.Get("X-GitHub-Event") |
| 110 | + // if event == "" { |
| 111 | + // return nil, ErrMissingGithubEventHeader |
| 112 | + // } |
| 113 | + // gitHubEvent := Event(event) |
| 114 | + |
| 115 | + payload, err := ioutil.ReadAll(r.Body) |
| 116 | + if err != nil || len(payload) == 0 { |
| 117 | + log.Error(ErrParsingPayload) |
| 118 | + log.Error(err) |
| 119 | + return nil, ErrParsingPayload |
| 120 | + } |
| 121 | + |
| 122 | + var pl BuildPayload |
| 123 | + err = json.Unmarshal([]byte(payload), &pl) |
| 124 | + if err != nil { |
| 125 | + log.Error(ErrParsingPayload) |
| 126 | + log.Error(err) |
| 127 | + return nil, ErrParsingPayload |
| 128 | + } |
| 129 | + return pl, err |
| 130 | + |
| 131 | +} |
0 commit comments