Browse Source

Move pushing to the Grafana API outside of the loop iterating over commits

Brendan Abolivier 6 years ago
parent
commit
7ba24c41f2
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 changed files with 16 additions and 6 deletions
  1. 16
    6
      src/pusher/webhook.go

+ 16
- 6
src/pusher/webhook.go View File

44
 		return
44
 		return
45
 	}
45
 	}
46
 
46
 
47
+	// Files to push are stored in a map before being pushed to the Grafana API.
48
+	// We don't push them in the loop iterating over commits because, in the
49
+	// case a file is successively updated by two commits pushed at the same
50
+	// time, it would push the same file several time, which isn't an optimised
51
+	// behaviour.
52
+	filesToPush := make(map[string]bool)
53
+
47
 	// Iterate over the commits descriptions from the payload
54
 	// Iterate over the commits descriptions from the payload
48
 	for _, commit := range pl.Commits {
55
 	for _, commit := range pl.Commits {
49
 		// We don't want to process commits made by the puller
56
 		// We don't want to process commits made by the puller
53
 
60
 
54
 		// Push all added files
61
 		// Push all added files
55
 		for _, addedFile := range commit.Added {
62
 		for _, addedFile := range commit.Added {
56
-			if err = pushFile(addedFile); err != nil {
57
-				panic(err)
58
-			}
63
+			filesToPush[addedFile] = true
59
 		}
64
 		}
60
 
65
 
61
 		// Push all modified files
66
 		// Push all modified files
62
 		for _, modifiedFile := range commit.Modified {
67
 		for _, modifiedFile := range commit.Modified {
63
-			if err = pushFile(modifiedFile); err != nil {
64
-				panic(err)
65
-			}
68
+			filesToPush[modifiedFile] = true
66
 		}
69
 		}
67
 
70
 
68
 		// TODO: Remove a dashboard when its file gets deleted?
71
 		// TODO: Remove a dashboard when its file gets deleted?
69
 	}
72
 	}
70
 
73
 
74
+	// Push all files to the Grafana API
75
+	for fileToPush := range filesToPush {
76
+		if err = pushFile(fileToPush); err != nil {
77
+			panic(err)
78
+		}
79
+	}
80
+
71
 	// Grafana will auto-update the version number after we pushed the new
81
 	// Grafana will auto-update the version number after we pushed the new
72
 	// dashboards, so we use the puller mechanic to pull the updated numbers and
82
 	// dashboards, so we use the puller mechanic to pull the updated numbers and
73
 	// commit them in the git repo.
83
 	// commit them in the git repo.