Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CODEOWNERS checker #16

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

**Linter for k6 extensions**

k6lint is a command line tool and a library for static analysis of the source of k6 extensions. The analysis is done without building a k6 executable with the extension.
k6lint is a command line tool and a library for static analysis of the source of k6 extensions.

The contents of the source directory are used for analysis. If the directory is a git workdir, it also analyzes the git metadata. The analysis is completely local and does not use external APIs (e.g. repository manager API) or services.

Expand All @@ -20,7 +20,8 @@ The detailed result of the checks are described in a [JSON schema](https://grafa
- `git` - checks if the directory is git workdir
- `versions` - checks for semantic versioning git tags
- `build` - checks if k6 can be built with the extension
- `smoke` - checks if the smoke test script exists and runs successfully
- `smoke` - checks if the smoke test script exists and runs successfully (`smoke.js`, `smoke.ts`, `smoke.test.js` or `smoke.test.ts` in the `test`,`tests`, `examples` or the base directory)
- `codeowners` - checks if there is a CODEOWNERS file (for official extensions)

## Install

Expand Down
26 changes: 26 additions & 0 deletions checker_codeowners.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package k6lint

import (
"context"
"path/filepath"
"regexp"
)

var reCODEOWNERS = regexp.MustCompile("^CODEOWNERS$")

func checkerCodeowners(_ context.Context, dir string) *checkResult {
_, shortname, err := findFile(reCODEOWNERS,
dir,
filepath.Join(dir, ".github"),
filepath.Join(dir, "docs"),
)
if err != nil {
return checkError(err)
}

if len(shortname) > 0 {
return checkPassed("found `CODEOWNERS` file")
}

return checkFailed("no CODEOWNERS file found")
}
6 changes: 3 additions & 3 deletions checker_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (mc *moduleChecker) smoke(ctx context.Context, dir string) *checkResult {
return checkFailed("can't build")
}

filename, sortname, err := findFile(reSmoke,
filename, shortname, err := findFile(reSmoke,
dir,
filepath.Join(dir, "test"),
filepath.Join(dir, "tests"),
Expand All @@ -81,7 +81,7 @@ func (mc *moduleChecker) smoke(ctx context.Context, dir string) *checkResult {
return checkError(err)
}

if len(sortname) == 0 {
if len(shortname) == 0 {
return checkFailed("no smoke test file found")
}

Expand All @@ -94,5 +94,5 @@ func (mc *moduleChecker) smoke(ctx context.Context, dir string) *checkResult {
return checkError(err)
}

return checkPassed("`%s` successfully run with k6", sortname)
return checkPassed("`%s` successfully run with k6", shortname)
}
19 changes: 16 additions & 3 deletions checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type checkDefinition struct {
score int
}

func checkDefinitions() []checkDefinition {
func checkDefinitions(official bool) []checkDefinition {
modCheck := newModuleChecker()
gitCheck := newGitChecker()

Expand All @@ -47,11 +47,21 @@ func checkDefinitions() []checkDefinition {
{id: CheckerSmoke, score: 2, fn: modCheck.smoke},
}

if !official {
return defs
}

extra := []checkDefinition{
{id: CheckerCodeowners, score: 2, fn: checkerCodeowners},
}

defs = append(defs, extra...)

return defs
}

func runChecks(ctx context.Context, dir string, opts *Options) ([]Check, int) {
checkDefs := checkDefinitions()
checkDefs := checkDefinitions(opts.Official)
results := make([]Check, 0, len(checkDefs))
passed := passedChecks(opts.Passed)

Expand Down Expand Up @@ -94,7 +104,10 @@ func ParseChecker(val string) (Checker, error) {
CheckerExamples,
CheckerLicense,
CheckerGit,
CheckerVersions:
CheckerVersions,
CheckerBuild,
CheckerSmoke,
CheckerCodeowners:

return v, nil
default:
Expand Down
4 changes: 3 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type options struct {
passing k6lint.Grade
passedStr []string
passed []k6lint.Checker
official bool
}

// New creates new cobra command for exec command.
Expand Down Expand Up @@ -69,6 +70,7 @@ func New() (*cobra.Command, error) {
flags.SortFlags = false

flags.Var(&opts.passing, "passing", "set lowest passing grade")
flags.BoolVar(&opts.official, "official", false, "enable extra checks for official extensions")
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "no output, only validation")
flags.StringVarP(&opts.out, "out", "o", "", "write output to file instead of stdout")
flags.BoolVar(&opts.json, "json", false, "generate JSON output")
Expand Down Expand Up @@ -125,7 +127,7 @@ func run(ctx context.Context, args []string, opts *options) (result error) {
output = file
}

compliance, err := k6lint.Lint(ctx, dir, &k6lint.Options{Passed: opts.passed})
compliance, err := k6lint.Lint(ctx, dir, &k6lint.Options{Passed: opts.passed, Official: opts.official})
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions compliance_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/compliance.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"git",
"versions",
"build",
"smoke"
"smoke",
"codeowners"
]
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/compliance.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ $defs:
- versions
- build
- smoke
- codeowners
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package k6lint
type Options struct {
// Passed contains a list of checkers that have already been marked as successful.
Passed []Checker

// Official can be set true to enable extra checkers (like codeowners) for official extensions.
Official bool
}

func passedChecks(checkers []Checker) map[Checker]Check {
Expand Down
Loading