{{- /* Keep track of our various counts */ -}}
{{- $e := 0 -}}
{{- $w := 0 -}}
{{- $s := 0 -}}
{{- $f := 0 -}}
{{- /* Range over the linted files */ -}}
{{- range .Files}}
{{$table := newTable true}}
{{- $f = add1 $f -}}
{{- .Path | underline | indent 1 -}}
{{- /* Range over the file's alerts */ -}}
{{- range .Alerts -}}
{{- $error := "" -}}
{{- if eq .Severity "error" -}}
{{- $error = .Severity | red -}}
{{- $e = add1 $e -}}
{{- else if eq .Severity "warning" -}}
{{- $error = .Severity | yellow -}}
{{- $w = add1 $w -}}
{{- else -}}
{{- $error = .Severity | blue -}}
{{- $s = add1 $s -}}
{{- end}}
{{- $loc := printf "%d:%d" .Line (index .Span 0) -}}
{{- $row := list $loc $error .Message .Check | toStrings -}}
{{- $table = addRow $table $row -}}
{{end -}}
{{- $table = renderTable $table -}}
{{end}}
{{- $e}} {{"errors" | red}}, {{$w}} {{"warnings" | yellow}} and {{$s}} {{"suggestions" | blue}} in {{$f}} {{$f | int | plural "file" "files"}}.{{- /* Range over the linted files */ -}}
{{- range .Files}}
{{- $path := .Path -}}
{{- /* Range over the file's alerts */ -}}
{{- range .Alerts -}}
{{- $error := "" -}}
{{- if eq .Severity "error" -}}
{{- $error = "ERROR" -}}
{{- else if eq .Severity "warning" -}}
{{- $error = "WARNING" -}}
{{- else -}}
{{- $error = "INFO" -}}
{{- end}}
{{- /* Variables setup */ -}}
{{- $line := printf "%d" .Line -}}
{{- $start := index .Span 0 -}}
{{- $end := add (index .Span 1) 1 -}}
{{- $check := printf "%s" .Check -}}
{{- $message := printf "%s" .Message -}}
{{- /* Output */ -}}
{"message": "{{ $message | jsonEscape }}", "location": {"path": "{{ $path }}", "range": {"start": {"line": {{ $line }}, "column": {{ $start }}}, "end": {"line": {{ $line }}, "column": {{ $end }}}}}, "severity": "{{ $error }}", "code": {"value": "{{ $check | jsonEscape }}"{{ if .Link }}, "url": "{{ .Link | jsonEscape }}"{{ end }}}}
{{end -}}
{{end -}}$ vale --output=sarif.tmpl . > vale.sarif- name: Upload to code scanning
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: vale.sarif{{- /* Collect the rules that fired, so that each is described once. */ -}}
{{- $rules := dict -}}
{{- range .Files -}}
{{- range .Alerts -}}
{{- $_ := set $rules .Check (dict "link" .Link "text" .Description) -}}
{{- end -}}
{{- end -}}
{
"$schema": "https://json.schemastore.org/sarif-2.1.0.json",
"version": "2.1.0",
"runs": [
{
"tool": {
"driver": {
"name": "Vale",
"informationUri": "https://vale.sh",
"rules": [
{{- $first := true -}}
{{- range $id, $rule := $rules }}
{{ if not $first }},{{ end }} {
"id": "{{ $id | jsonEscape }}",
"shortDescription": {"text": "{{ if $rule.text }}{{ $rule.text | jsonEscape }}{{ else }}{{ $id | jsonEscape }}{{ end }}"}
{{- if $rule.link }},
"helpUri": "{{ $rule.link | jsonEscape }}"
{{- end }}
}
{{- $first = false -}}
{{- end }}
]
}
},
"results": [
{{- $first = true -}}
{{- range .Files -}}
{{- $path := .Path -}}
{{- range .Alerts }}
{{ if not $first }},{{ end }} {
"ruleId": "{{ .Check | jsonEscape }}",
"level": "{{ if eq .Severity "error" }}error{{ else if eq .Severity "warning" }}warning{{ else }}note{{ end }}",
"message": {"text": "{{ .Message | jsonEscape }}"},
"locations": [
{
"physicalLocation": {
"artifactLocation": {"uri": "{{ $path | jsonEscape }}"},
"region": {
"startLine": {{ .Line }},
"startColumn": {{ index .Span 0 }},
"endColumn": {{ add (index .Span 1) 1 }},
"snippet": {"text": "{{ .Match | jsonEscape }}"}
}
}
}
]
}
{{- $first = false -}}
{{- end -}}
{{- end }}
]
}
]
}