|
@@ -57,7 +57,7 @@ func HandlePush(payload interface{}, header webhooks.Header) {
|
57
|
57
|
|
58
|
58
|
|
59
|
59
|
|
60
|
|
- filesToPush := make(map[string]bool)
|
|
60
|
+ filesToPush := make(map[string][]byte)
|
61
|
61
|
|
62
|
62
|
|
63
|
63
|
for _, commit := range pl.Commits {
|
|
@@ -72,56 +72,40 @@ func HandlePush(payload interface{}, header webhooks.Header) {
|
72
|
72
|
continue
|
73
|
73
|
}
|
74
|
74
|
|
75
|
|
-
|
76
|
|
-
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
77
|
78
|
for _, addedFile := range commit.Added {
|
78
|
|
- ignored, err := isIgnored(addedFile)
|
79
|
|
- if err != nil {
|
|
79
|
+ if err = prepareForPush(addedFile, &filesToPush); err != nil {
|
80
|
80
|
logrus.WithFields(logrus.Fields{
|
81
|
81
|
"error": err,
|
82
|
82
|
"filename": addedFile,
|
83
|
|
- }).Error("Failed to check if file is to be ignored")
|
|
83
|
+ }).Error("Failed to prepare file for push")
|
84
|
84
|
|
85
|
85
|
continue
|
86
|
86
|
}
|
87
|
|
-
|
88
|
|
- if !ignored {
|
89
|
|
- logrus.WithFields(logrus.Fields{
|
90
|
|
- "filename": addedFile,
|
91
|
|
- }).Info("Setting file as file to push to Grafana")
|
92
|
|
-
|
93
|
|
- filesToPush[addedFile] = true
|
94
|
|
- }
|
95
|
87
|
}
|
96
|
88
|
|
97
|
|
-
|
98
|
|
-
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
99
|
92
|
for _, modifiedFile := range commit.Modified {
|
100
|
|
- ignored, err := isIgnored(modifiedFile)
|
101
|
|
- if err != nil {
|
|
93
|
+ if err = prepareForPush(modifiedFile, &filesToPush); err != nil {
|
102
|
94
|
logrus.WithFields(logrus.Fields{
|
103
|
95
|
"error": err,
|
104
|
96
|
"filename": modifiedFile,
|
105
|
|
- }).Error("Failed to check if file is to be ignored")
|
|
97
|
+ }).Error("Failed to prepare file for push")
|
106
|
98
|
|
107
|
99
|
continue
|
108
|
100
|
}
|
109
|
|
-
|
110
|
|
- if !ignored {
|
111
|
|
- logrus.WithFields(logrus.Fields{
|
112
|
|
- "filename": modifiedFile,
|
113
|
|
- }).Info("Setting file as file to push to Grafana")
|
114
|
|
-
|
115
|
|
- filesToPush[modifiedFile] = true
|
116
|
|
- }
|
117
|
101
|
}
|
118
|
102
|
|
119
|
103
|
|
120
|
104
|
}
|
121
|
105
|
|
122
|
106
|
|
123
|
|
- for fileToPush := range filesToPush {
|
124
|
|
- if err = pushFile(fileToPush); err != nil {
|
|
107
|
+ for fileToPush, fileContent := range filesToPush {
|
|
108
|
+ if err = grafanaClient.CreateOrUpdateDashboard(fileContent); err != nil {
|
125
|
109
|
logrus.WithFields(logrus.Fields{
|
126
|
110
|
"error": err,
|
127
|
111
|
"filename": fileToPush,
|
|
@@ -143,45 +127,56 @@ func HandlePush(payload interface{}, header webhooks.Header) {
|
143
|
127
|
}
|
144
|
128
|
}
|
145
|
129
|
|
146
|
|
-
|
147
|
|
-
|
148
|
|
-
|
149
|
|
-
|
150
|
|
-func pushFile(filename string) error {
|
151
|
|
- filePath := cfg.Git.ClonePath + "/" + filename
|
152
|
|
- fileContent, err := ioutil.ReadFile(filePath)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+func prepareForPush(
|
|
137
|
+ filename string, filesToPush *map[string][]byte,
|
|
138
|
+) (err error) {
|
|
139
|
+
|
|
140
|
+ if strings.HasSuffix(filename, "versions.json") {
|
|
141
|
+ return
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+ fileContent, err := ioutil.ReadFile(filename)
|
153
|
146
|
if err != nil {
|
154
|
|
- return err
|
|
147
|
+ return
|
155
|
148
|
}
|
156
|
149
|
|
157
|
|
- return grafanaClient.CreateOrUpdateDashboard(fileContent)
|
|
150
|
+
|
|
151
|
+ ignored, err := isIgnored(fileContent)
|
|
152
|
+ if err != nil {
|
|
153
|
+ return
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+ if !ignored {
|
|
158
|
+ logrus.WithFields(logrus.Fields{
|
|
159
|
+ "filename": filename,
|
|
160
|
+ }).Info("Preparing file to be pushed to Grafana")
|
|
161
|
+
|
|
162
|
+ (*filesToPush)[filename] = fileContent
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ return
|
158
|
166
|
}
|
159
|
167
|
|
160
|
168
|
|
161
|
169
|
|
162
|
170
|
|
163
|
171
|
|
164
|
|
-
|
165
|
|
-
|
166
|
|
-func isIgnored(filename string) (bool, error) {
|
167
|
|
-
|
168
|
|
- if strings.HasSuffix(filename, "versions.json") {
|
169
|
|
- return true, nil
|
170
|
|
- }
|
171
|
|
-
|
|
172
|
+func isIgnored(dashboardJSON []byte) (bool, error) {
|
172
|
173
|
|
173
|
174
|
if len(cfg.Grafana.IgnorePrefix) == 0 {
|
174
|
175
|
return false, nil
|
175
|
176
|
}
|
176
|
177
|
|
177
|
|
-
|
178
|
|
- fileContent, err := ioutil.ReadFile(filename)
|
179
|
|
- if err != nil {
|
180
|
|
- return false, err
|
181
|
|
- }
|
182
|
|
-
|
183
|
178
|
|
184
|
|
- slug, err := helpers.GetDashboardSlug(fileContent)
|
|
179
|
+ slug, err := helpers.GetDashboardSlug(dashboardJSON)
|
185
|
180
|
if err != nil {
|
186
|
181
|
return false, err
|
187
|
182
|
}
|