Skip to content

Commit

Permalink
chore: add readme linter to CI (#11020)
Browse files Browse the repository at this point in the history
  • Loading branch information
reimda authored and MyaLongmire committed Jul 6, 2022
1 parent c958667 commit 7b5f448
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/readme-linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Lint plugin readmes
on:
# push:
# branches-ignore: master
pull_request:
branches: # Names of target branches, not source branches
- master
jobs:
run-readme-linter:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v18.7
with:
base_sha: ${{ github.event.pull_request.base.sha }}
files: plugins/**/README.md
- name: Run readme linter on changed files
run: go run ./tools/readme_linter ${{ steps.changed-files.outputs.all_changed_files }}
4 changes: 4 additions & 0 deletions tools/readme_linter/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ func (t *T) assertHeadingLevel(expected int, n ast.Node) {

t.printFailedAssertf(n, "expected header level %d, have %d", expected, h.Level)
}

func (t *T) pass() bool {
return t.fails == 0
}
16 changes: 11 additions & 5 deletions tools/readme_linter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ func main() {
flag.Parse()

var err error
pass := true
for _, filename := range flag.Args() {
err = checkFile(filename, guessPluginType(filename), *sourceFlag)
var filePass bool
filePass, err = checkFile(filename, guessPluginType(filename), *sourceFlag)
if err != nil {
panic(err)
}
pass = pass && filePass
}
if !pass {
os.Exit(1)
}
}

Expand Down Expand Up @@ -57,10 +63,10 @@ func init() {
rules[pluginInput] = append(rules[pluginInput], inputRules...)
}

func checkFile(filename string, pluginType plugin, sourceFlag bool) error {
func checkFile(filename string, pluginType plugin, sourceFlag bool) (bool, error) {
md, err := os.ReadFile(filename)
if err != nil {
return err
return false, err
}

// Goldmark returns locations as offsets. We want line
Expand Down Expand Up @@ -107,10 +113,10 @@ func checkFile(filename string, pluginType plugin, sourceFlag bool) error {
for _, rule := range rules {
err = rule(&tester, root)
if err != nil {
return err
return false, err
}
}
tester.printPassFail()

return nil
return tester.pass(), nil
}

0 comments on commit 7b5f448

Please sign in to comment.