Skip to content

Commit a92dd93

Browse files
committed
receive docker hub notification
1 parent 26ca2ef commit a92dd93

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed

docker/docker.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

docker/docker_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package docker
2+
3+
import (
4+
"log"
5+
"net/http"
6+
"net/http/httptest"
7+
"os"
8+
"testing"
9+
10+
"reflect"
11+
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
// NOTES:
16+
// - Run "go test" to run tests
17+
// - Run "gocov test | gocov report" to report on test converage by file
18+
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
19+
//
20+
// or
21+
//
22+
// -- may be a good idea to change to output path to somewherelike /tmp
23+
// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
24+
//
25+
26+
const (
27+
path = "/webhooks"
28+
)
29+
30+
var hook *Webhook
31+
32+
func TestMain(m *testing.M) {
33+
34+
// setup
35+
var err error
36+
hook, err = New()
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
os.Exit(m.Run())
41+
// teardown
42+
}
43+
44+
func newServer(handler http.HandlerFunc) *httptest.Server {
45+
mux := http.NewServeMux()
46+
mux.HandleFunc(path, handler)
47+
return httptest.NewServer(mux)
48+
}
49+
50+
func TestWebhooks(t *testing.T) {
51+
assert := require.New(t)
52+
tests := []struct {
53+
name string
54+
event Event
55+
typ interface{}
56+
filename string
57+
headers http.Header
58+
}{
59+
{
60+
name: "BuildEvent",
61+
event: BuildEvent,
62+
typ: BuildPayload{},
63+
filename: "../testdata/docker/docker_hub_build_notice.json",
64+
headers: http.Header{
65+
"X-Github-Event": []string{"commit_comment"},
66+
},
67+
},
68+
}
69+
70+
for _, tt := range tests {
71+
tc := tt
72+
client := &http.Client{}
73+
t.Run(tt.name, func(t *testing.T) {
74+
t.Parallel()
75+
payload, err := os.Open(tc.filename)
76+
assert.NoError(err)
77+
defer func() {
78+
_ = payload.Close()
79+
}()
80+
81+
var parseError error
82+
var results interface{}
83+
server := newServer(func(w http.ResponseWriter, r *http.Request) {
84+
results, parseError = hook.Parse(r, tc.event)
85+
})
86+
defer server.Close()
87+
req, err := http.NewRequest(http.MethodPost, server.URL+path, payload)
88+
assert.NoError(err)
89+
req.Header = tc.headers
90+
req.Header.Set("Content-Type", "application/json")
91+
92+
resp, err := client.Do(req)
93+
assert.NoError(err)
94+
assert.Equal(http.StatusOK, resp.StatusCode)
95+
assert.NoError(parseError)
96+
assert.Equal(reflect.TypeOf(tc.typ), reflect.TypeOf(results))
97+
})
98+
}
99+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"callback_url": "https://registry.hub.docker.com/u/svendowideit/testhook/hook/2141b5bi5i5b02bec211i4eeih0242eg11000a/",
3+
"push_data": {
4+
"images": [
5+
"27d47432a69bca5f2700e4dff7de0388ed65f9d3fb1ec645e2bc24c223dc1cc3",
6+
"51a9c7c1f8bb2fa19bcd09789a34e63f35abb80044bc10196e304f6634cc582c",
7+
"..."
8+
],
9+
"pushed_at": 1.417566161e+09,
10+
"pusher": "trustedbuilder",
11+
"tag": "latest"
12+
},
13+
"repository": {
14+
"comment_count": 0,
15+
"date_created": 1.417494799e+09,
16+
"description": "",
17+
"dockerfile": "#\n# BUILD\u0009\u0009docker build -t svendowideit/apt-cacher .\n# RUN\u0009\u0009docker run -d -p 3142:3142 -name apt-cacher-run apt-cacher\n#\n# and then you can run containers with:\n# \u0009\u0009docker run -t -i -rm -e http_proxy http://192.168.1.2:3142/ debian bash\n#\nFROM\u0009\u0009ubuntu\n\n\nVOLUME\u0009\u0009[/var/cache/apt-cacher-ng]\nRUN\u0009\u0009apt-get update ; apt-get install -yq apt-cacher-ng\n\nEXPOSE \u0009\u00093142\nCMD\u0009\u0009chmod 777 /var/cache/apt-cacher-ng ; /etc/init.d/apt-cacher-ng start ; tail -f /var/log/apt-cacher-ng/*\n",
18+
"full_description": "Docker Hub based automated build from a GitHub repo",
19+
"is_official": false,
20+
"is_private": true,
21+
"is_trusted": true,
22+
"name": "testhook",
23+
"namespace": "svendowideit",
24+
"owner": "svendowideit",
25+
"repo_name": "svendowideit/testhook",
26+
"repo_url": "https://registry.hub.docker.com/u/svendowideit/testhook/",
27+
"star_count": 0,
28+
"status": "Active"
29+
}
30+
}

0 commit comments

Comments
 (0)