diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..6d7b59e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.go] +indent_style = tab +indent_size = 4 + +[Makefile] +indent_style = tab +indent_size = 4 + +[*.mod] +indent_style = tab +indent_size = 4 diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 1072337..c056dbe 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -26,6 +26,11 @@ jobs: with: go-version-file: "go.mod" + - name: Lint + uses: golangci/golangci-lint-action@v7 + with: + version: latest + - name: Test run: go test ./... @@ -59,6 +64,7 @@ jobs: uses: docker/setup-buildx-action@v3 - name: Login to DockerHub + if: github.event_name == 'release' && github.event.action == 'published' uses: docker/login-action@v3 with: username: ${{ vars.DOCKERHUB_USERNAME }} diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..e3ec952 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,26 @@ +version: "2" + +run: + timeout: 10m + modules-download-mode: readonly + +formatters: + enable: + - gofmt + - goimports + settings: + goimports: + local-prefixes: + - github.com/Staffbase/yamllint-action + +linters: + default: none + enable: + - bodyclose + - gosec + - govet + - ineffassign + - noctx + - staticcheck + - unused + - whitespace diff --git a/yamllint-action.go b/yamllint-action.go index ad378c7..01f8041 100644 --- a/yamllint-action.go +++ b/yamllint-action.go @@ -108,7 +108,7 @@ func parseInput(r io.Reader) Report { Severity: severity, } - if _, exist := files[fileName]; exist == false { + if _, exist := files[fileName]; !exist { files[fileName] = &LinterResult{FilePath: fileName} } @@ -156,15 +156,15 @@ func handlePush(ctx context.Context, client *github.Client, report Report) error // find the action's checkrun checkName := os.Getenv("ACTION_NAME") result, _, err := client.Checks.ListCheckRunsForRef(ctx, owner, repoName, head, &github.ListCheckRunsOptions{ - CheckName: github.String(checkName), - Status: github.String("in_progress"), + CheckName: github.Ptr(checkName), + Status: github.Ptr("in_progress"), }) if err != nil { return err } if len(result.CheckRuns) == 0 { - return fmt.Errorf("Unable to find check run for action: %s", checkName) + return fmt.Errorf("unable to find check run for action: %s", checkName) } checkRun := result.CheckRuns[0] @@ -177,12 +177,12 @@ func handlePush(ctx context.Context, client *github.Client, report Report) error if len(t.AssertionResults) > 0 { for _, a := range t.AssertionResults { annotations = append(annotations, &github.CheckRunAnnotation{ - Path: github.String(path), - StartLine: github.Int(a.Line), - EndLine: github.Int(a.Line), - AnnotationLevel: github.String(a.Severity), - Title: github.String(""), - Message: github.String(a.Message), + Path: github.Ptr(path), + StartLine: github.Ptr(a.Line), + EndLine: github.Ptr(a.Line), + AnnotationLevel: github.Ptr(a.Severity), + Title: github.Ptr(""), + Message: github.Ptr(a.Message), }) } } @@ -202,8 +202,8 @@ func handlePush(ctx context.Context, client *github.Client, report Report) error } output := &github.CheckRunOutput{ - Title: github.String("Result"), - Summary: github.String(summary), + Title: github.Ptr("Result"), + Summary: github.Ptr(summary), Annotations: annotations[i:end], } diff --git a/yamllint-action_test.go b/yamllint-action_test.go index 7c05e06..ff1acc3 100644 --- a/yamllint-action_test.go +++ b/yamllint-action_test.go @@ -14,13 +14,13 @@ limitations under the License. package main import ( - "strings" - "testing" + "strings" + "testing" ) func TestParseInput(t *testing.T) { - t.Run("standard case", func(t *testing.T) { - testData := `sources/prod/testapp-deploy.yaml:17:11: [warning] wrong indentation: expected 12 but found 10 (indentation) + t.Run("standard case", func(t *testing.T) { + testData := `sources/prod/testapp-deploy.yaml:17:11: [warning] wrong indentation: expected 12 but found 10 (indentation) sources/prod/testapp2-deploy.yaml:16:11: [warning] wrong indentation: expected 12 but found 10 (indentation) sources/prod/testapp3-deploy.yaml:16:11: [warning] wrong indentation: expected 12 but found 10 (indentation) sources/prod/testapp4-deploy.yaml:17:11: [warning] wrong indentation: expected 12 but found 10 (indentation) @@ -31,21 +31,21 @@ sources/stage/testapp4-deploy.yaml:17:11: [warning] wrong indentation: expected sources/dev/testapp-deploy.yaml:17:11: [warning] wrong indentation: expected 12 but found 10 (indentation) sources/dev/testapp2-deploy.yaml:16:11: [warning] wrong indentation: expected 12 but found 10 (indentation)` - report := parseInput(strings.NewReader(testData)) - if report.NumFailedLines != 10 { - t.Errorf("unexpected amount of failed lines: got %d, wanted %d", report.NumFailedLines, 10) - } + report := parseInput(strings.NewReader(testData)) + if report.NumFailedLines != 10 { + t.Errorf("unexpected amount of failed lines: got %d, wanted %d", report.NumFailedLines, 10) + } - if report.LinterResults[0].AssertionResults[0].Severity != "warning" { - t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[0].AssertionResults[0].Severity, "warning") - } - - if report.LinterResults[6].AssertionResults[0].Severity != "failure" { - t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[4].AssertionResults[0].Severity, "failure") - } + if report.LinterResults[0].AssertionResults[0].Severity != "warning" { + t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[0].AssertionResults[0].Severity, "warning") + } - if report.LinterResults[6].AssertionResults[0].Message != "wrong indentation: expected 12 but found 10 (indentation)" { - t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[4].AssertionResults[0].Message, "wrong indentation: expected 12 but found 10 (indentation)") - } - }) + if report.LinterResults[6].AssertionResults[0].Severity != "failure" { + t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[4].AssertionResults[0].Severity, "failure") + } + + if report.LinterResults[6].AssertionResults[0].Message != "wrong indentation: expected 12 but found 10 (indentation)" { + t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[4].AssertionResults[0].Message, "wrong indentation: expected 12 but found 10 (indentation)") + } + }) }