github.com/go-playground/webhooks/v6@v6.3.0/docker/docker.go (about) 1 package docker 2 3 // this package receives 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 16 // parse errors 17 var ( 18 ErrInvalidHTTPMethod = errors.New("invalid HTTP Method") 19 ErrParsingPayload = errors.New("error parsing payload") 20 ) 21 22 // Event defines a Docker hook event type 23 type Event string 24 25 // Docker hook types (only one for now) 26 const ( 27 BuildEvent Event = "build" 28 ) 29 30 // BuildPayload a docker hub build notice 31 // https://docs.docker.com/docker-hub/webhooks/ 32 type BuildPayload struct { 33 CallbackURL string `json:"callback_url"` 34 PushData struct { 35 Images []string `json:"images"` 36 PushedAt float32 `json:"pushed_at"` 37 Pusher string `json:"pusher"` 38 Tag string `json:"tag"` 39 } `json:"push_data"` 40 Repository struct { 41 CommentCount int `json:"comment_count"` 42 DateCreated float32 `json:"date_created"` 43 Description string `json:"description"` 44 Dockerfile string `json:"dockerfile"` 45 FullDescription string `json:"full_description"` 46 IsOfficial bool `json:"is_official"` 47 IsPrivate bool `json:"is_private"` 48 IsTrusted bool `json:"is_trusted"` 49 Name string `json:"name"` 50 Namespace string `json:"namespace"` 51 Owner string `json:"owner"` 52 RepoName string `json:"repo_name"` 53 RepoURL string `json:"repo_url"` 54 StarCount int `json:"star_count"` 55 Status string `json:"status"` 56 } `json:"repository"` 57 } 58 59 // Webhook instance contains all methods needed to process events 60 type Webhook struct { 61 } 62 63 // New creates and returns a WebHook instance 64 func New() (*Webhook, error) { 65 hook := new(Webhook) 66 return hook, nil 67 } 68 69 // Parse verifies and parses the events specified and returns the payload object or an error 70 func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error) { 71 defer func() { 72 _, _ = io.Copy(ioutil.Discard, r.Body) 73 _ = r.Body.Close() 74 }() 75 76 if r.Method != http.MethodPost { 77 return nil, ErrInvalidHTTPMethod 78 } 79 80 payload, err := ioutil.ReadAll(r.Body) 81 if err != nil || len(payload) == 0 { 82 return nil, ErrParsingPayload 83 } 84 85 var pl BuildPayload 86 err = json.Unmarshal([]byte(payload), &pl) 87 if err != nil { 88 return nil, ErrParsingPayload 89 } 90 return pl, err 91 92 }