Skip to content

Commit

Permalink
Allow comments in pattern line, update golangci-lint (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok authored Mar 11, 2022
1 parent 2f478ec commit d95ed83
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ linters:
- typecheck
- unused
- varcheck
- golint
- revive
- gofmt
- misspell
- gochecknoinits
- unparam
- scopelint
- exportloopref
- gosec
- goimports
- whitespace
Expand Down
2 changes: 1 addition & 1 deletion hack/run-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set -E # needs to be set if we want the ERR trap

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
ROOT_PATH=$( cd "${CURRENT_DIR}/.." && pwd )
GOLANGCI_LINT_VERSION="v1.31.0"
GOLANGCI_LINT_VERSION="v1.44.2"
TMP_DIR=$(mktemp -d)

readonly CURRENT_DIR
Expand Down
2 changes: 1 addition & 1 deletion internal/check/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestAPIBuilder(t *testing.T) {
var bldr *check.OutputBuilder = nil
var bldr *check.OutputBuilder

t.Run("Does not panic on ReportIssue when builder is nil", func(t *testing.T) {
assert.NotPanics(t, func() {
Expand Down
4 changes: 1 addition & 3 deletions internal/check/valid_syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ func (v *ValidSyntax) Check(ctx context.Context, in Input) (Output, error) {
for _, item := range entry.Owners {
switch {
case strings.EqualFold(item, "#"):
msg := "Comment (# sign) is not allowed in line with pattern entry. The correct format is: pattern owner1 ... ownerN"
bldr.ReportIssue(msg, WithEntry(entry))
break ownersLoop // no need to check for the rest items in this line, as the whole line is already marked as invalid
break ownersLoop // no need to check for the rest items in this line, as they are ignored
case strings.HasPrefix(item, "@"):
if !usernameOrTeamRegexp.MatchString(item) {
msg := fmt.Sprintf("Owner '%s' does not look like a GitHub username or team name", item)
Expand Down
7 changes: 1 addition & 6 deletions internal/check/valid_syntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ func TestValidSyntaxChecker(t *testing.T) {
},
},
"Comment in pattern line": {
codeowners: `* @org/hakuna-matata # this should be an error`,
issue: &check.Issue{
Severity: check.Error,
LineNo: ptr.Uint64Ptr(1),
Message: "Comment (# sign) is not allowed in line with pattern entry. The correct format is: pattern owner1 ... ownerN",
},
codeowners: `* @org/hakuna-matata # this is allowed`,
},
}
for tn, tc := range tests {
Expand Down
10 changes: 9 additions & 1 deletion pkg/codeowners/owners.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,17 @@ func ParseCodeowners(r io.Reader) []Entry {
continue
}

n := len(fields)
for idx, x := range fields {
if !strings.HasPrefix(x, "#") {
continue
}
n = idx
}

e = append(e, Entry{
Pattern: fields[0],
Owners: fields[1:],
Owners: fields[1:n],
LineNo: no,
})
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/codeowners/owners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const sampleCodeownerFile = `
src/** @org/hakuna-matata @pico-bello
pkg/github.com/** @myk
tests/** @ghost # some comment
internal/** @ghost #some comment v2
`

Expand All @@ -39,6 +41,16 @@ func TestParseCodeownersSuccess(t *testing.T) {
Pattern: "pkg/github.com/**",
Owners: []string{"@myk"},
},
{
LineNo: 7,
Pattern: "tests/**",
Owners: []string{"@ghost"},
},
{
LineNo: 8,
Pattern: "internal/**",
Owners: []string{"@ghost"},
},
}

tFS := afero.NewMemMapFs()
Expand Down

0 comments on commit d95ed83

Please sign in to comment.