Skip to content

Commit 7e7c45f

Browse files
authored
format(sarif): Add tflint-errors rules for errors (#2373)
1 parent fdd2b5e commit 7e7c45f

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

formatter/sarif.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ func (f *Formatter) sarifAddErrors(errRun *sarif.Run, err error) {
9595
var diags hcl.Diagnostics
9696
if errors.As(err, &diags) {
9797
for _, diag := range diags {
98+
rule := errRun.AddRule(diag.Summary).WithDescription("")
99+
98100
location := sarif.NewPhysicalLocation().
99101
WithArtifactLocation(sarif.NewSimpleArtifactLocation(filepath.ToSlash(diag.Subject.Filename))).
100102
WithRegion(
@@ -107,15 +109,17 @@ func (f *Formatter) sarifAddErrors(errRun *sarif.Run, err error) {
107109
WithEndColumn(diag.Subject.End.Column),
108110
)
109111

110-
errRun.CreateResultForRule(diag.Summary).
112+
errRun.CreateResultForRule(rule.ID).
111113
WithLevel(fromHclSeverity(diag.Severity)).
112114
WithMessage(sarif.NewTextMessage(diag.Detail)).
113115
AddLocation(sarif.NewLocationWithPhysicalLocation(location))
114116
}
115117
return
116118
}
117119

118-
errRun.CreateResultForRule("application_error").
120+
rule := errRun.AddRule("application_error").WithDescription("")
121+
122+
errRun.CreateResultForRule(rule.ID).
119123
WithLevel("error").
120124
WithMessage(sarif.NewTextMessage(err.Error()))
121125
}

formatter/sarif_test.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,21 @@ func Test_sarifPrint(t *testing.T) {
407407
"driver": {
408408
"informationUri": "https://github.com/terraform-linters/tflint",
409409
"name": "tflint-errors",
410-
"rules": [],
410+
"rules": [
411+
{
412+
"id": "application_error",
413+
"shortDescription": {
414+
"text": ""
415+
}
416+
}
417+
],
411418
"version": "%s"
412419
}
413420
},
414421
"results": [
415422
{
416423
"ruleId": "application_error",
417-
"ruleIndex": 18446744073709551615,
424+
"ruleIndex": 0,
418425
"level": "error",
419426
"message": {
420427
"text": "Failed to work; I don't feel like working"
@@ -462,14 +469,21 @@ func Test_sarifPrint(t *testing.T) {
462469
"driver": {
463470
"informationUri": "https://github.com/terraform-linters/tflint",
464471
"name": "tflint-errors",
465-
"rules": [],
472+
"rules": [
473+
{
474+
"id": "summary",
475+
"shortDescription": {
476+
"text": ""
477+
}
478+
}
479+
],
466480
"version": "%s"
467481
}
468482
},
469483
"results": [
470484
{
471485
"ruleId": "summary",
472-
"ruleIndex": 18446744073709551615,
486+
"ruleIndex": 0,
473487
"level": "warning",
474488
"message": {
475489
"text": "detail"
@@ -535,30 +549,43 @@ func Test_sarifPrint(t *testing.T) {
535549
"driver": {
536550
"informationUri": "https://github.com/terraform-linters/tflint",
537551
"name": "tflint-errors",
538-
"rules": [],
552+
"rules": [
553+
{
554+
"id": "application_error",
555+
"shortDescription": {
556+
"text": ""
557+
}
558+
},
559+
{
560+
"id": "summary",
561+
"shortDescription": {
562+
"text": ""
563+
}
564+
}
565+
],
539566
"version": "%s"
540567
}
541568
},
542569
"results": [
543570
{
544571
"ruleId": "application_error",
545-
"ruleIndex": 18446744073709551615,
572+
"ruleIndex": 0,
546573
"level": "error",
547574
"message": {
548575
"text": "an error occurred"
549576
}
550577
},
551578
{
552579
"ruleId": "application_error",
553-
"ruleIndex": 18446744073709551615,
580+
"ruleIndex": 0,
554581
"level": "error",
555582
"message": {
556583
"text": "failed"
557584
}
558585
},
559586
{
560587
"ruleId": "summary",
561-
"ruleIndex": 18446744073709551615,
588+
"ruleIndex": 1,
562589
"level": "warning",
563590
"message": {
564591
"text": "detail"

0 commit comments

Comments
 (0)