|
@@ -12,6 +12,8 @@ import (
|
12
|
12
|
"gopkg.in/go-playground/webhooks.v3/gitlab"
|
13
|
13
|
)
|
14
|
14
|
|
|
15
|
+// SetupWebhook creates and exposes a GitLab webhook using a given configuration.
|
|
16
|
+// Returns an error if the webhook couldn't be set up.
|
15
|
17
|
func SetupWebhook(cfg *config.Config) error {
|
16
|
18
|
hook := gitlab.New(&gitlab.Config{
|
17
|
19
|
Secret: cfg.Webhook.Secret,
|
|
@@ -25,27 +27,33 @@ func SetupWebhook(cfg *config.Config) error {
|
25
|
27
|
)
|
26
|
28
|
}
|
27
|
29
|
|
|
30
|
+// HandlePush is called each time a push event is sent by GitLab on the webhook.
|
28
|
31
|
func HandlePush(payload interface{}, header webhooks.Header) {
|
29
|
32
|
var err error
|
30
|
33
|
|
|
34
|
+ // Process the payload using the right structure
|
31
|
35
|
pl := payload.(gitlab.PushEventPayload)
|
32
|
36
|
|
|
37
|
+ // Clone or pull the repository
|
33
|
38
|
if _, err = git.Sync(cfg.Git); err != nil {
|
34
|
39
|
panic(err)
|
35
|
40
|
}
|
36
|
41
|
|
|
42
|
+ // Iterate over the commits descriptions from the payload
|
37
|
43
|
for _, commit := range pl.Commits {
|
38
|
44
|
// We don't want to process commits made by the puller
|
39
|
45
|
if commit.Author.Email == cfg.Git.CommitsAuthor.Email {
|
40
|
46
|
continue
|
41
|
47
|
}
|
42
|
48
|
|
|
49
|
+ // Push all added files
|
43
|
50
|
for _, addedFile := range commit.Added {
|
44
|
51
|
if err = pushFile(addedFile); err != nil {
|
45
|
52
|
panic(err)
|
46
|
53
|
}
|
47
|
54
|
}
|
48
|
55
|
|
|
56
|
+ // Push all modified files
|
49
|
57
|
for _, modifiedFile := range commit.Modified {
|
50
|
58
|
if err = pushFile(modifiedFile); err != nil {
|
51
|
59
|
panic(err)
|
|
@@ -63,6 +71,10 @@ func HandlePush(payload interface{}, header webhooks.Header) {
|
63
|
71
|
}
|
64
|
72
|
}
|
65
|
73
|
|
|
74
|
+// pushFile pushes the content of a given file to the Grafana API in order to
|
|
75
|
+// create or update a dashboard.
|
|
76
|
+// Returns an error if there was an issue reading the file or sending its content
|
|
77
|
+// to the Grafana instance.
|
66
|
78
|
func pushFile(filename string) error {
|
67
|
79
|
filePath := cfg.Git.ClonePath + "/" + filename
|
68
|
80
|
fileContent, err := ioutil.ReadFile(filePath)
|