Brendan Abolivier 6 lat temu
rodzic
commit
50a388fcbf
Podpisane przez: Brendan Abolivier <contact@brendanabolivier.com> ID klucza GPG: 8EF1500759F70623
1 zmienionych plików z 12 dodań i 0 usunięć
  1. 12
    0
      src/pusher/webhook.go

+ 12
- 0
src/pusher/webhook.go Wyświetl plik

12
 	"gopkg.in/go-playground/webhooks.v3/gitlab"
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
 func SetupWebhook(cfg *config.Config) error {
17
 func SetupWebhook(cfg *config.Config) error {
16
 	hook := gitlab.New(&gitlab.Config{
18
 	hook := gitlab.New(&gitlab.Config{
17
 		Secret: cfg.Webhook.Secret,
19
 		Secret: cfg.Webhook.Secret,
25
 	)
27
 	)
26
 }
28
 }
27
 
29
 
30
+// HandlePush is called each time a push event is sent by GitLab on the webhook.
28
 func HandlePush(payload interface{}, header webhooks.Header) {
31
 func HandlePush(payload interface{}, header webhooks.Header) {
29
 	var err error
32
 	var err error
30
 
33
 
34
+	// Process the payload using the right structure
31
 	pl := payload.(gitlab.PushEventPayload)
35
 	pl := payload.(gitlab.PushEventPayload)
32
 
36
 
37
+	// Clone or pull the repository
33
 	if _, err = git.Sync(cfg.Git); err != nil {
38
 	if _, err = git.Sync(cfg.Git); err != nil {
34
 		panic(err)
39
 		panic(err)
35
 	}
40
 	}
36
 
41
 
42
+	// Iterate over the commits descriptions from the payload
37
 	for _, commit := range pl.Commits {
43
 	for _, commit := range pl.Commits {
38
 		// We don't want to process commits made by the puller
44
 		// We don't want to process commits made by the puller
39
 		if commit.Author.Email == cfg.Git.CommitsAuthor.Email {
45
 		if commit.Author.Email == cfg.Git.CommitsAuthor.Email {
40
 			continue
46
 			continue
41
 		}
47
 		}
42
 
48
 
49
+		// Push all added files
43
 		for _, addedFile := range commit.Added {
50
 		for _, addedFile := range commit.Added {
44
 			if err = pushFile(addedFile); err != nil {
51
 			if err = pushFile(addedFile); err != nil {
45
 				panic(err)
52
 				panic(err)
46
 			}
53
 			}
47
 		}
54
 		}
48
 
55
 
56
+		// Push all modified files
49
 		for _, modifiedFile := range commit.Modified {
57
 		for _, modifiedFile := range commit.Modified {
50
 			if err = pushFile(modifiedFile); err != nil {
58
 			if err = pushFile(modifiedFile); err != nil {
51
 				panic(err)
59
 				panic(err)
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
 func pushFile(filename string) error {
78
 func pushFile(filename string) error {
67
 	filePath := cfg.Git.ClonePath + "/" + filename
79
 	filePath := cfg.Git.ClonePath + "/" + filename
68
 	fileContent, err := ioutil.ReadFile(filePath)
80
 	fileContent, err := ioutil.ReadFile(filePath)