|
@@ -25,40 +25,54 @@ func PullGrafanaAndCommit(
|
25
|
25
|
client *grafana.Client,
|
26
|
26
|
repoURL string, clonePath string, privateKeyPath string,
|
27
|
27
|
) error {
|
28
|
|
- dv := make(map[string]diffVersion)
|
29
|
|
-
|
30
|
|
- dbVersions, err := getDashboardsVersions()
|
|
28
|
+ // Clone or pull the repo
|
|
29
|
+ repo, err := git.Sync(repoURL, clonePath, privateKeyPath)
|
31
|
30
|
if err != nil {
|
32
|
31
|
return err
|
33
|
32
|
}
|
34
|
33
|
|
35
|
|
- repo, err := git.Sync(repoURL, clonePath, privateKeyPath)
|
|
34
|
+ w, err := repo.Worktree()
|
36
|
35
|
if err != nil {
|
37
|
36
|
return err
|
38
|
37
|
}
|
39
|
38
|
|
40
|
|
- w, err := repo.Worktree()
|
|
39
|
+ // Get URIs for all known dashboards
|
|
40
|
+ uris, err := client.GetDashboardsURIs()
|
41
|
41
|
if err != nil {
|
42
|
42
|
return err
|
43
|
43
|
}
|
44
|
44
|
|
45
|
|
- uris, err := client.GetDashboardsURIs()
|
|
45
|
+ dv := make(map[string]diffVersion)
|
|
46
|
+
|
|
47
|
+ // Load versions
|
|
48
|
+ dbVersions, err := getDashboardsVersions()
|
46
|
49
|
if err != nil {
|
47
|
50
|
return err
|
48
|
51
|
}
|
49
|
52
|
|
|
53
|
+ // Iterate over the dashboards URIs
|
50
|
54
|
for _, uri := range uris {
|
|
55
|
+ // Retrieve the dashboard JSON
|
51
|
56
|
dashboard, err := client.GetDashboard(uri)
|
52
|
57
|
if err != nil {
|
53
|
58
|
return err
|
54
|
59
|
}
|
55
|
60
|
|
|
61
|
+ // Check if there's a version for this dashboard in the data loaded from
|
|
62
|
+ // the "versions.json" file. If there's a version and it's older (lower
|
|
63
|
+ // version number) than the version we just retrieved from the Grafana
|
|
64
|
+ // API, or if there's no known version (ok will be false), write the
|
|
65
|
+ // changes in the repo and add the modified file to the git index.
|
56
|
66
|
version, ok := dbVersions[dashboard.Slug]
|
57
|
67
|
if !ok || dashboard.Version > version {
|
58
|
68
|
if err = addDashboardChangesToRepo(dashboard, w); err != nil {
|
59
|
69
|
return err
|
60
|
70
|
}
|
61
|
71
|
|
|
72
|
+ // We don't need to check for the value of ok because if ok is false
|
|
73
|
+ // version will be initialised to the 0-value of the int type, which
|
|
74
|
+ // is 0, so the previous version number will be considered to be 0,
|
|
75
|
+ // which is the behaviour we want.
|
62
|
76
|
dv[dashboard.Slug] = diffVersion{
|
63
|
77
|
oldVersion: version,
|
64
|
78
|
newVersion: dashboard.Version,
|
|
@@ -71,12 +85,15 @@ func PullGrafanaAndCommit(
|
71
|
85
|
return err
|
72
|
86
|
}
|
73
|
87
|
|
|
88
|
+ // Check if there's uncommited changes, and if that's the case, commit them.
|
74
|
89
|
if !status.IsClean() {
|
75
|
90
|
if err = commitNewVersions(dbVersions, dv, w); err != nil {
|
76
|
91
|
return err
|
77
|
92
|
}
|
78
|
93
|
}
|
79
|
94
|
|
|
95
|
+ // Push the changes (we don't do it in the if clause above in case there are
|
|
96
|
+ // pending commits in the local repo that haven't been pushed yet).
|
80
|
97
|
if err = git.Push(repo, privateKeyPath); err != nil {
|
81
|
98
|
return err
|
82
|
99
|
}
|