1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package main
-
- import (
- "io/ioutil"
- "strings"
-
- "config"
- puller "puller"
-
- "gopkg.in/go-playground/webhooks.v3"
- "gopkg.in/go-playground/webhooks.v3/gitlab"
- )
-
- func SetupWebhook(cfg *config.Config) error {
- hook := gitlab.New(&gitlab.Config{
- Secret: cfg.Webhook.Secret,
- })
- hook.RegisterEvents(HandlePush, gitlab.PushEvents)
-
- return webhooks.Run(
- hook,
- cfg.Webhook.Interface+":"+cfg.Webhook.Port,
- cfg.Webhook.Path,
- )
- }
-
- func HandlePush(payload interface{}, header webhooks.Header) {
- pl := payload.(gitlab.PushEventPayload)
-
- var err error
- for _, commit := range pl.Commits {
- // We don't want to process commits made by the puller
- if commit.Author.Email == "grafana@cozycloud.cc" {
- continue
- }
-
- for _, addedFile := range commit.Added {
- if err = pushFile(addedFile); err != nil {
- panic(err)
- }
- }
-
- for _, modifiedFile := range commit.Modified {
- if err = pushFile(modifiedFile); err != nil {
- panic(err)
- }
- }
-
- // TODO: Remove a dashboard when its file gets deleted?
- }
-
- // Grafana will auto-update the version number after we pushed the new
- // dashboards, so we use the puller mechanic to pull the updated numbers and
- // commit them in the git repo.
- if err = puller.PullGrafanaAndCommit(grafanaClient, cfg); err != nil {
- panic(err)
- }
- }
-
- func pushFile(filename string) error {
- filePath := cfg.Git.ClonePath + "/" + filename
- fileContent, err := ioutil.ReadFile(filePath)
- if err != nil {
- return err
- }
-
- // Remove the .json part
- slug := strings.Split(filename, ".json")[0]
-
- return grafanaClient.UpdateDashboard(slug, fileContent)
- }
|