diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f5623d2..092e2f4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,17 +10,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Check out the repo - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Docker Login - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Docker Build and Push id: docker_build - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v6 with: push: true tags: | - target/pull-request-code-coverage:latest - target/pull-request-code-coverage:${{ github.event.release.tag_name }} \ No newline at end of file + pullrequestcc/pull-request-code-coverage:latest + pullrequestcc/pull-request-code-coverage:${{ github.event.release.tag_name }} \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a781a06..ee04ecb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,12 +6,12 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: - go-version: 1.17 + go-version: 1.26.3 - name: Build run: go build -v ./... diff --git a/.golangci.yml b/.golangci.yml index 28a8a3b..99185cd 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,39 +1,35 @@ +version: "2" + linters: - disable-all: true + default: none enable: - bodyclose - - deadcode - - depguard - dogsled - errcheck - gochecknoinits - goconst - gocritic - gocyclo - - gofmt - - goimports - - golint - goprintffuncname - gosec - govet - ineffassign - - interfacer - - maligned - misspell - nakedret - rowserrcheck - - scopelint - staticcheck - - structcheck - - stylecheck - - typecheck - unconvert - unparam - unused - - varcheck -issues: - exclude-rules: - - path: _test\.go - linters: - - scopelint - - funlen \ No newline at end of file + exclusions: + rules: + - path: _test\.go + linters: + - funlen + - goconst + - gosec + +formatters: + enable: + - gofmt + - goimports diff --git a/Dockerfile b/Dockerfile index aa84fe8..5214a90 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.17-alpine as builder +FROM golang:1.26.3-alpine AS builder WORKDIR /go/src/github.com/target/pull-request-code-coverage COPY . . ENV GO111MODULE=on diff --git a/Makefile b/Makefile index e1b823d..0ab7549 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,11 @@ check-gofmt: echo "Success - way to run gofmt!"; \ fi +GOLANGCI_LINT_VERSION=v2.12.2 + bin/golangci-lint: mkdir -p bin - wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.24.0 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b ./bin $(GOLANGCI_LINT_VERSION) .PHONY: lint lint: bin/golangci-lint diff --git a/README.md b/README.md index 4a67ed9..a351c2a 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ Once you have coverage.xml same can be passed as an input to plugin shown below # Development -This project needs go (>= 1.17) to be installed. Make sure you run +This project needs go (>= 1.26.3) to be installed. Make sure you run * make format * make lint diff --git a/go.mod b/go.mod index de194c4..78138dd 100644 --- a/go.mod +++ b/go.mod @@ -1,18 +1,17 @@ module github.com/target/pull-request-code-coverage -go 1.17 +go 1.26.3 require ( github.com/pkg/errors v0.9.1 - github.com/sirupsen/logrus v1.8.1 - github.com/stretchr/testify v1.7.0 - + github.com/sirupsen/logrus v1.9.4 + github.com/stretchr/testify v1.11.1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/objx v0.1.0 // indirect - golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + github.com/stretchr/objx v0.5.3 // indirect + golang.org/x/sys v0.44.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 2fe2451..4c19f8a 100644 --- a/go.sum +++ b/go.sum @@ -1,20 +1,18 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w= +github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g= +github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4= +github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ= +golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/plugin/coverage/cobertura/report.go b/internal/plugin/coverage/cobertura/report.go index 39051df..9d2e397 100644 --- a/internal/plugin/coverage/cobertura/report.go +++ b/internal/plugin/coverage/cobertura/report.go @@ -3,7 +3,6 @@ package cobertura import ( "encoding/xml" "io" - "io/ioutil" "log" "os" @@ -22,7 +21,7 @@ type DefaultLoader struct { func NewReportLoader(sourceDir string) *DefaultLoader { return &DefaultLoader{ - readAllFunc: ioutil.ReadAll, + readAllFunc: io.ReadAll, sourceDir: sourceDir, } } diff --git a/internal/plugin/coverage/jacoco/report.go b/internal/plugin/coverage/jacoco/report.go index a87e471..3d97b36 100644 --- a/internal/plugin/coverage/jacoco/report.go +++ b/internal/plugin/coverage/jacoco/report.go @@ -4,7 +4,6 @@ import ( "encoding/xml" "io" - "io/ioutil" "log" "os" @@ -20,12 +19,12 @@ type DefaultLoader struct { func NewReportLoader() *DefaultLoader { return &DefaultLoader{ - readAllFunc: ioutil.ReadAll, + readAllFunc: io.ReadAll, } } func (l *DefaultLoader) Load(coverageFile string) (coverage.Report, error) { - // nolint: gosec + // nolint: gosec // coverageFile is a user-supplied report path; opening it is the intended behavior xmlFile, openFileErr := os.Open(coverageFile) if openFileErr != nil { @@ -96,7 +95,7 @@ type SourceFile struct { func (f SourceFile) GetCoverageData(lineNumber int) (*domain.CoverageData, bool) { for _, l := range f.Lines { if l.LineNumber == lineNumber { - coverageData := l.CoverageData.toDomain() + coverageData := l.toDomain() return &coverageData, true } } diff --git a/internal/plugin/pluginhttp/client.go b/internal/plugin/pluginhttp/client.go index 260fae0..e024040 100644 --- a/internal/plugin/pluginhttp/client.go +++ b/internal/plugin/pluginhttp/client.go @@ -18,5 +18,6 @@ func (c *DefaultClient) NewRequest(method string, url string, body io.Reader) (* } func (c *DefaultClient) Do(request *http.Request) (*http.Response, error) { + // nolint: gosec // the request targets the user-configured GitHub API URL, which is the intended behavior return http.DefaultClient.Do(request) } diff --git a/internal/plugin/reporter/github_pr.go b/internal/plugin/reporter/github_pr.go index 6e42a4f..addaaee 100644 --- a/internal/plugin/reporter/github_pr.go +++ b/internal/plugin/reporter/github_pr.go @@ -70,6 +70,10 @@ func (s *GithubPullRequest) Write(changedLinesWithCoverage domain.SourceLineCove return errors.Wrap(doErr, "Failed calling github") } + defer func() { + _ = resp.Body.Close() + }() + if resp.StatusCode != HTTPResponseCreated { return errors.Errorf("Failed calling github: bad status code: %v", resp.StatusCode) } @@ -105,7 +109,7 @@ func (s *GithubPullRequest) createCommentBody(changedLinesWithCoverage domain.So result := make([]string, 5) - result[0] = fmt.Sprintf("Code Coverage Summary:\n\n") + result[0] = "Code Coverage Summary:\n\n" result[1] = fmt.Sprintf("Lines Without Coverage Data -> %.f%% (%d)\n", toPercent(safeDiv(float32(linesWithoutDataCount), float32(totalLines), 0)), linesWithoutDataCount) result[2] = fmt.Sprintf("Lines With Coverage Data -> %.f%% (%d)\n", toPercent(safeDiv(float32(linesWithDataCount), float32(totalLines), 1)), linesWithDataCount) result[3] = fmt.Sprintf("Covered Instructions -> **%.f%%** (%d)\n", toPercent(safeDiv(float32(covered), float32(totalInstructions), 1)), covered) diff --git a/internal/plugin/reporter/github_pr_test.go b/internal/plugin/reporter/github_pr_test.go index f1777c1..e342038 100644 --- a/internal/plugin/reporter/github_pr_test.go +++ b/internal/plugin/reporter/github_pr_test.go @@ -1,8 +1,10 @@ package reporter import ( + "io" "net/http" "net/http/httptest" + "strings" "testing" "github.com/pkg/errors" @@ -66,7 +68,7 @@ func TestGithubPullRequest_Write_FailedDo_BadStatus(t *testing.T) { request := httptest.NewRequest("GET", "http://anywhere", nil) mockClient.On("NewRequest", mock.Anything, mock.Anything, mock.Anything).Return(request, nil) - mockClient.On("Do", request).Return(&http.Response{StatusCode: 400}, nil) + mockClient.On("Do", request).Return(&http.Response{StatusCode: 400, Body: io.NopCloser(strings.NewReader(""))}, nil) writer := &GithubPullRequest{ apiBaseURL: "anything", diff --git a/internal/plugin/reporter/simple.go b/internal/plugin/reporter/simple.go index 7984175..55f6e96 100644 --- a/internal/plugin/reporter/simple.go +++ b/internal/plugin/reporter/simple.go @@ -38,7 +38,7 @@ func (s *Simple) Write(changedLinesWithCoverage domain.SourceLineCoverageReport) result := make([]string, 5) - result[0] = fmt.Sprintf("Code Coverage Summary:\n") + result[0] = "Code Coverage Summary:\n" result[1] = fmt.Sprintf("Lines Without Coverage Data -> %.f%% (%d)\n", toPercent(safeDiv(float32(linesWithoutDataCount), float32(totalLines), 0)), linesWithoutDataCount) result[2] = fmt.Sprintf("Lines With Coverage Data -> %.f%% (%d)\n", toPercent(safeDiv(float32(linesWithDataCount), float32(totalLines), 1)), linesWithDataCount) result[3] = fmt.Sprintf("Covered Instructions -> %.f%% (%d)\n", toPercent(safeDiv(float32(covered), float32(totalInstructions), 1)), covered) diff --git a/internal/test/mocks/mock_gh_api.go b/internal/test/mocks/mock_gh_api.go index 0b87bc4..ae99bf2 100644 --- a/internal/test/mocks/mock_gh_api.go +++ b/internal/test/mocks/mock_gh_api.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" "testing" @@ -84,7 +83,7 @@ func mustJSONUnmarshall(bytes []byte, result interface{}) { } func mustReadAll(r io.Reader) []byte { - result, err := ioutil.ReadAll(r) + result, err := io.ReadAll(r) if err != nil { panic(fmt.Sprintf("Unexpected error: %v", err))