-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tool to filter Vale JSON output using rule precedence (#942)
- Loading branch information
Showing
9 changed files
with
194 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
build.conf | ||
/dist | ||
/vale/Grafana.zip | ||
vale/tools/inhibit-rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: Run Vale | ||
description: | | ||
Lints documentation with Vale. | ||
Note that the action assumes you are running the steps using the grafana/vale container image. | ||
inputs: | ||
directory: | ||
default: docs/sources | ||
description: Directory containing documentation for linting. | ||
required: false | ||
filter: | ||
default: "" | ||
description: | | ||
Vale filter that allows you to report an arbitrary subset of the Grafana style. | ||
For more information, refer to https://vale.sh/docs/filters. | ||
token: | ||
default: ${{ github.token }} | ||
description: Used by reviewdog to comment on pull requests. | ||
required: false | ||
runs: | ||
using: composite | ||
steps: | ||
- name: Vale | ||
env: | ||
DIRECTORY: ${{ inputs.directory }} | ||
FILTER: --filter=${{ inputs.filter }} | ||
REVIEWDOG_GITHUB_API_TOKEN: ${{ inputs.token }} | ||
run: | | ||
(cd /etc/vale && vale sync) | ||
vale ${FILTER} --output=/etc/vale/rdjsonl.tmpl ${DIRECTORY} | \ | ||
inhibit-rules | \ | ||
/bin/reviewdog \ | ||
--conf=/etc/vale/.reviewdog.yml \ | ||
--fail-on-error \ | ||
--f=rdjsonl \ | ||
--name=vale \ | ||
--reporter=github-pr-review | ||
shell: sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/grafana/writers-toolkit/tools/exit" | ||
) | ||
|
||
const command = "inhibit-rules" | ||
|
||
func usage(w io.Writer, fs *flag.FlagSet) { | ||
fmt.Fprintf(w, "Usage of %s:\n", command) | ||
fs.SetOutput(w) | ||
fs.PrintDefaults() | ||
|
||
fmt.Fprintf(w, "\n") | ||
fmt.Fprintln(w, "Reads JSON formatted error messages from standard input and filters them based on rule precedence.") | ||
} | ||
|
||
func main() { | ||
fs := flag.NewFlagSet(command, flag.ExitOnError) | ||
flag.Parse() | ||
|
||
if flag.NArg() > 0 { | ||
usage(os.Stderr, fs) | ||
os.Exit(exit.UsageError) | ||
} | ||
|
||
r := bufio.NewReader(os.Stdin) | ||
scanner := bufio.NewScanner(r) | ||
diags := make([]diagnostic, 0) | ||
|
||
var errs bool | ||
for scanner.Scan() { | ||
diag := diagnostic{} | ||
if err := json.Unmarshal(scanner.Bytes(), &diag); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
errs = true | ||
} | ||
|
||
diags = append(diags, diag) | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
errs = true | ||
} | ||
|
||
diags = inhibit(diags) | ||
for _, d := range diags { | ||
if err := json.NewEncoder(os.Stdout).Encode(d); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
errs = true | ||
} | ||
} | ||
|
||
if errs { | ||
os.Exit(exit.RuntimeError) | ||
} | ||
} | ||
|
||
// {"message": "[{{ $check }}] {{ $message | jsonEscape }}", "location": {"path": "{{ $path }}", "range": {"start": {"line": {{ $line }}, "column": {{ $col }}}}}, "severity": "{{ $error }}"} | ||
type diagnostic struct { | ||
Message string `json:"message"` | ||
Location struct { | ||
Path string `json:"path"` | ||
Range struct { | ||
Start struct { | ||
Line int `json:"line"` | ||
Column int `json:"column"` | ||
} `json:"start"` | ||
} `json:"range"` | ||
} `json:"location"` | ||
Severity string `json:"severity"` | ||
} | ||
|
||
// Each error has the rule name in square brackets at the beginning of the message. | ||
// For example: {"message": "[Grafana.Spelling] ..." ...} | ||
func (d diagnostic) Rule() string { | ||
if len(d.Message) == 0 || d.Message[0] != '[' { | ||
return "" | ||
} | ||
|
||
end := strings.Index(d.Message, "]") | ||
if end == -1 { | ||
return "" | ||
} | ||
|
||
return d.Message[1:end] | ||
} | ||
|
||
func (d diagnostic) LocationKey() string { | ||
return fmt.Sprintf("%s:%d:%d", d.Location.Path, d.Location.Range.Start.Line, d.Location.Range.Start.Column) | ||
} | ||
|
||
func inhibit(in []diagnostic) []diagnostic { | ||
filtered := make(map[string]diagnostic) | ||
precedence := map[string]int{ | ||
"Grafana.ProductPossessives": 0, | ||
"Grafana.WordList": 1, | ||
"Grafana.Spelling": 2, | ||
} | ||
|
||
for _, d := range in { | ||
if _, ok := filtered[d.LocationKey()]; !ok || precedence[d.Rule()] < precedence[filtered[d.LocationKey()].Rule()] { | ||
filtered[d.LocationKey()] = d | ||
} | ||
} | ||
|
||
out := make([]diagnostic, 0, len(filtered)) | ||
for _, d := range filtered { | ||
out = append(out, d) | ||
} | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/grafana/writers-toolkit/vale/tools | ||
|
||
go 1.23.3 | ||
|
||
require github.com/grafana/writers-toolkit/tools v0.0.0-20250108111324-7ed051741521 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/grafana/writers-toolkit/tools v0.0.0-20250108111324-7ed051741521 h1:sJw5cYvUHjysFlOEN5MLaJC9XR0Is3S1rLlUuv3L83s= | ||
github.com/grafana/writers-toolkit/tools v0.0.0-20250108111324-7ed051741521/go.mod h1:f8r529z0g/keWOXl33WHJr4uYcv2bp7iwzFKeG+AukA= |