Skip to content

Commit

Permalink
Add access checker (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmenezes authored Aug 24, 2020
1 parent 3c7ba68 commit 29b3c04
Show file tree
Hide file tree
Showing 19 changed files with 862 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.testTags": "unit"
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Simply calling `codeownerslint` will kick off the cli on the current directory.
| ------------- | ------------- | ------------------------------------------------------------------------------ |
| d | . | Directory: specifies the directory you want to use to lint the CODEOWNERS file |
| f | | Format: specifies the format you want to return lint results |

| t | | Token: specifies the Github's token you want to use |
| tt | bearer | Token Type: specifies the Github's token type you want to use |

##### Exit Codes

| Exit Code | Description |
Expand Down
4 changes: 4 additions & 0 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func RegisterChecker(name string, checker Checker) error {
type ValidatorOptions struct {
Directory string
CodeownersFileLocation string
GithubTokenType string
GithubToken string
}

// Checker provides tools for validating CODEOWNER file contents
Expand Down Expand Up @@ -132,6 +134,8 @@ func Check(options CheckOptions) ([]CheckResult, error) {
validators[checker] = c.NewValidator(ValidatorOptions{
Directory: options.Directory,
CodeownersFileLocation: fileLocation,
GithubToken: options.GithubToken,
GithubTokenType: options.GithubTokenType,
})
}

Expand Down
74 changes: 74 additions & 0 deletions checkers/access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package checkers

import (
"fmt"
"strings"

"github.com/fmenezes/codeowners"
)

const accessCheckerName string = "Access"

func init() {
codeowners.RegisterChecker(accessCheckerName, Access{})
}

// Access represents checker to validate if an owner has access to repo
type Access struct{}

// NewValidator returns validating capabilities for this checker
func (c Access) NewValidator(options codeowners.ValidatorOptions) codeowners.Validator {
return accessValidator{
options: options,
accessMemo: make(map[string]bool),
}
}

type accessValidator struct {
options codeowners.ValidatorOptions
accessMemo map[string]bool
}

// ValidateLine runs this NoOwner's check against each line
func (v accessValidator) ValidateLine(lineNo int, line string) []codeowners.CheckResult {
results := []codeowners.CheckResult{}

_, owners := codeowners.ParseLine(line)

if len(owners) == 0 {
return nil
}

for _, owner := range owners {
if !ownerValid(owner) {
continue
}
writeAccess, found := v.accessMemo[owner]
if !found {
writeAccess, _ = ownerHasWriteAccess(v.options, owner)
v.accessMemo[owner] = writeAccess
}
if !writeAccess {
result := codeowners.CheckResult{
Position: codeowners.Position{
FilePath: v.options.CodeownersFileLocation,
StartLine: lineNo,
EndLine: lineNo,
StartColumn: strings.Index(line, owner) + 1,
},
Message: fmt.Sprintf("Owner '%s' has no write access", owner),
Severity: codeowners.Error,
CheckName: accessCheckerName,
}
result.Position.EndColumn = result.Position.StartColumn + len(owner)

results = append(results, result)
}
}

if len(results) > 0 {
return results
}

return nil
}
Loading

0 comments on commit 29b3c04

Please sign in to comment.