|
6 | 6 | "log/slog"
|
7 | 7 | "net/http"
|
8 | 8 | "net/http/httptest"
|
| 9 | + "os" |
| 10 | + "path/filepath" |
9 | 11 | "strconv"
|
10 | 12 | "strings"
|
11 | 13 | "testing"
|
@@ -44,12 +46,16 @@ func TestGitLabReporter(t *testing.T) {
|
44 | 46 | type errorCheck func(err error) error
|
45 | 47 |
|
46 | 48 | p := parser.NewParser(false, parser.PrometheusSchema, model.UTF8Validation)
|
47 |
| - mockFile := p.Parse(strings.NewReader(` |
| 49 | + mockRules := ` |
48 | 50 | - record: target is down
|
49 | 51 | expr: up == 0
|
50 | 52 | - record: sum errors
|
51 | 53 | expr: sum(errors) by (job)
|
52 |
| -`)) |
| 54 | +` |
| 55 | + tmpDir := t.TempDir() |
| 56 | + mockPath := filepath.Join(tmpDir, "foo.txt") |
| 57 | + require.NoError(t, os.WriteFile(mockPath, []byte(mockRules), 0o644)) |
| 58 | + mockFile := p.Parse(strings.NewReader(mockRules)) |
53 | 59 |
|
54 | 60 | fooReport := reporter.Report{
|
55 | 61 | Path: discovery.Path{
|
@@ -157,6 +163,26 @@ func TestGitLabReporter(t *testing.T) {
|
157 | 163 |
|
158 | 164 | :information_source: To see documentation covering this check and instructions on how to resolve it [click here](https://cloudflare.github.io/pint/checks/%s.html).
|
159 | 165 | `, reporter, summary, details, reporter))
|
| 166 | + } |
| 167 | + discBodyWithDiag := func(reporter, summary, details, yml, diag string) *string { |
| 168 | + return gitlab.Ptr(fmt.Sprintf( |
| 169 | + `:stop_sign: **Fatal** reported by [pint](https://cloudflare.github.io/pint/) **%s** check. |
| 170 | +
|
| 171 | +<details> |
| 172 | +<summary>%s</summary> |
| 173 | +
|
| 174 | +%s |
| 175 | +
|
| 176 | +%s |
| 177 | +
|
| 178 | +%s |
| 179 | +
|
| 180 | +</details> |
| 181 | +
|
| 182 | +------ |
| 183 | +
|
| 184 | +:information_source: To see documentation covering this check and instructions on how to resolve it [click here](https://cloudflare.github.io/pint/checks/a.html). |
| 185 | +`, reporter, summary, yml, diag, details)) |
160 | 186 | }
|
161 | 187 | discPosition := func(path string, line int) *gitlab.PositionOptions {
|
162 | 188 | return gitlab.Ptr(gitlab.PositionOptions{
|
@@ -221,6 +247,69 @@ func TestGitLabReporter(t *testing.T) {
|
221 | 247 | s.ExpectGet(apiOpenMergeRequests).ReturnJSON([]gitlab.BasicMergeRequest{})
|
222 | 248 | }),
|
223 | 249 | },
|
| 250 | + { |
| 251 | + description: "single MR with diagnostics", |
| 252 | + timeout: time.Minute, |
| 253 | + maxComments: 1, |
| 254 | + summary: reporter.NewSummary([]reporter.Report{ |
| 255 | + { |
| 256 | + Path: discovery.Path{ |
| 257 | + SymlinkTarget: mockPath, |
| 258 | + Name: mockPath, |
| 259 | + }, |
| 260 | + ModifiedLines: []int{2}, |
| 261 | + Rule: mockFile.Groups[0].Rules[0], |
| 262 | + Problem: checks.Problem{ |
| 263 | + Reporter: "a", |
| 264 | + Summary: "foo error1", |
| 265 | + Details: "foo details", |
| 266 | + Diagnostics: []diags.Diagnostic{ |
| 267 | + { |
| 268 | + Message: "Diagnostic message", |
| 269 | + Pos: diags.PositionRanges{ |
| 270 | + { |
| 271 | + Line: 2, |
| 272 | + FirstColumn: 3, |
| 273 | + LastColumn: 24, |
| 274 | + }, |
| 275 | + { |
| 276 | + Line: 3, |
| 277 | + FirstColumn: 3, |
| 278 | + LastColumn: 15, |
| 279 | + }, |
| 280 | + }, |
| 281 | + FirstColumn: 1, |
| 282 | + LastColumn: 24, |
| 283 | + }, |
| 284 | + }, |
| 285 | + Lines: diags.LineRange{First: 1, Last: 3}, |
| 286 | + Severity: checks.Fatal, |
| 287 | + Anchor: checks.AnchorAfter, |
| 288 | + }, |
| 289 | + }, |
| 290 | + }), |
| 291 | + mock: httpmock.New(func(s *httpmock.Server) { |
| 292 | + s.ExpectGet(apiUser).ReturnJSON(gitlab.User{ID: 123}) |
| 293 | + s.ExpectGet(apiOpenMergeRequests).ReturnJSON([]gitlab.BasicMergeRequest{ |
| 294 | + {IID: 1}, |
| 295 | + }) |
| 296 | + s.ExpectGet(apiVersions(1)).ReturnJSON([]gitlab.MergeRequestDiffVersion{ |
| 297 | + {ID: 2, HeadCommitSHA: "head", BaseCommitSHA: "base", StartCommitSHA: "start"}, |
| 298 | + {ID: 1, HeadCommitSHA: "head", BaseCommitSHA: "base", StartCommitSHA: "start"}, |
| 299 | + }) |
| 300 | + s.ExpectGet(apiDiffs(1)).ReturnJSON([]gitlab.MergeRequestDiff{ |
| 301 | + {OldPath: mockPath, NewPath: mockPath, Diff: fooDiff}, |
| 302 | + }) |
| 303 | + s.ExpectGet(apiDiscussions(1, true)).ReturnJSON([]gitlab.Discussion{}) |
| 304 | + s.ExpectPost(apiDiscussions(1, false)).WithBodyJSON(gitlab.CreateMergeRequestDiscussionOptions{ |
| 305 | + Body: discBodyWithDiag("a", "foo error1", "foo details", "```yaml\n2 | - record: target is down\n3 | expr: up == 0\n ^^ \n```", "Diagnostic message"), |
| 306 | + Position: discPosition(mockPath, 2), |
| 307 | + }).ReturnJSON(gitlab.Response{}) |
| 308 | + }), |
| 309 | + errorHandler: func(err error) error { |
| 310 | + return err |
| 311 | + }, |
| 312 | + }, |
224 | 313 | {
|
225 | 314 | description: "multiple merge requests",
|
226 | 315 | timeout: time.Minute,
|
|
0 commit comments