|
| 1 | +package checkers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/fmenezes/codeowners" |
| 8 | +) |
| 9 | + |
| 10 | +const accessCheckerName string = "Access" |
| 11 | + |
| 12 | +func init() { |
| 13 | + codeowners.RegisterChecker(accessCheckerName, Access{}) |
| 14 | +} |
| 15 | + |
| 16 | +// Access represents checker to validate if an owner has access to repo |
| 17 | +type Access struct{} |
| 18 | + |
| 19 | +// NewValidator returns validating capabilities for this checker |
| 20 | +func (c Access) NewValidator(options codeowners.ValidatorOptions) codeowners.Validator { |
| 21 | + return accessValidator{ |
| 22 | + options: options, |
| 23 | + accessMemo: make(map[string]bool), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +type accessValidator struct { |
| 28 | + options codeowners.ValidatorOptions |
| 29 | + accessMemo map[string]bool |
| 30 | +} |
| 31 | + |
| 32 | +// ValidateLine runs this NoOwner's check against each line |
| 33 | +func (v accessValidator) ValidateLine(lineNo int, line string) []codeowners.CheckResult { |
| 34 | + results := []codeowners.CheckResult{} |
| 35 | + |
| 36 | + _, owners := codeowners.ParseLine(line) |
| 37 | + |
| 38 | + if len(owners) == 0 { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + for _, owner := range owners { |
| 43 | + if !ownerValid(owner) { |
| 44 | + continue |
| 45 | + } |
| 46 | + writeAccess, found := v.accessMemo[owner] |
| 47 | + if !found { |
| 48 | + writeAccess, _ = ownerHasWriteAccess(v.options, owner) |
| 49 | + v.accessMemo[owner] = writeAccess |
| 50 | + } |
| 51 | + if !writeAccess { |
| 52 | + result := codeowners.CheckResult{ |
| 53 | + Position: codeowners.Position{ |
| 54 | + FilePath: v.options.CodeownersFileLocation, |
| 55 | + StartLine: lineNo, |
| 56 | + EndLine: lineNo, |
| 57 | + StartColumn: strings.Index(line, owner) + 1, |
| 58 | + }, |
| 59 | + Message: fmt.Sprintf("Owner '%s' has no write access", owner), |
| 60 | + Severity: codeowners.Error, |
| 61 | + CheckName: accessCheckerName, |
| 62 | + } |
| 63 | + result.Position.EndColumn = result.Position.StartColumn + len(owner) |
| 64 | + |
| 65 | + results = append(results, result) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if len(results) > 0 { |
| 70 | + return results |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
0 commit comments