Skip to content

Commit

Permalink
🌱 enable the golangci-lint bugs preset (#3583)
Browse files Browse the repository at this point in the history
* enable bugs preset

Signed-off-by: Spencer Schrock <[email protected]>

* fix noctx linter

Signed-off-by: Spencer Schrock <[email protected]>

* fix bodyclose linter

Signed-off-by: Spencer Schrock <[email protected]>

* fix contextcheck linter

Signed-off-by: Spencer Schrock <[email protected]>

* This ignores all existing cases of musttag linter complaints.

This analyzer seems useful in the future, but some of this code
is old and I don't want to change it for existing code now.

Signed-off-by: Spencer Schrock <[email protected]>

* ignore existing nilerr lints.

This behavior is from the initial commit, and primarily affects metrics.
Leaving as is, and hope to benefit from the linter in the future.

Signed-off-by: Spencer Schrock <[email protected]>

---------

Signed-off-by: Spencer Schrock <[email protected]>
  • Loading branch information
spencerschrock committed Oct 23, 2023
1 parent 49c0eed commit d0cefa5
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ linters:
- unused
- whitespace
- wrapcheck
presets:
- bugs
linters-settings:
errcheck:
check-type-assertions: true
Expand Down
2 changes: 1 addition & 1 deletion attestor/policy/attestation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
sclog "github.com/ossf/scorecard/v4/log"
)

//nolint:govet
//nolint:govet,musttag // JSON usage is test only
type AttestationPolicy struct {
// PreventBinaryArtifacts : set to true to require that this project's SCM repo is
// free of binary artifacts
Expand Down
2 changes: 2 additions & 0 deletions clients/githubrepo/roundtripper/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (gh *rateLimitTransport) RoundTrip(r *http.Request) (*http.Response, error)
rateLimit := resp.Header.Get("X-RateLimit-Remaining")
remaining, err := strconv.Atoi(rateLimit)
if err != nil {
//nolint:nilerr // just an error in metadata, response may still be useful?
return resp, nil
}
ctx, err := tag.New(r.Context(), tag.Upsert(githubstats.ResourceType, resp.Header.Get("X-RateLimit-Resource")))
Expand All @@ -73,6 +74,7 @@ func (gh *rateLimitTransport) RoundTrip(r *http.Request) (*http.Response, error)
if remaining <= 0 {
reset, err := strconv.Atoi(resp.Header.Get("X-RateLimit-Reset"))
if err != nil {
//nolint:nilerr // just an error in metadata, response may still be useful?
return resp, nil
}

Expand Down
7 changes: 5 additions & 2 deletions clients/githubrepo/roundtripper/rate_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package roundtripper

import (
"context"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestRoundTrip(t *testing.T) {
}

t.Run("Successful response", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, ts.URL+"/success", nil)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, ts.URL+"/success", nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
Expand All @@ -69,13 +70,14 @@ func TestRoundTrip(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode)
}
})

t.Run("Retry-After header set", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, ts.URL+"/retry", nil)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, ts.URL+"/retry", nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
Expand All @@ -84,6 +86,7 @@ func TestRoundTrip(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode)
}
Expand Down
2 changes: 1 addition & 1 deletion clients/gitlabrepo/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (handler *graphqlHandler) init(ctx context.Context, repourl *repoURL) {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITLAB_AUTH_TOKEN")},
)
handler.client = oauth2.NewClient(context.Background(), src)
handler.client = oauth2.NewClient(ctx, src)
handler.graphClient = graphql.NewClient(fmt.Sprintf("%s/api/graphql", repourl.Host()), handler.client)
}

Expand Down
14 changes: 10 additions & 4 deletions clients/ossfuzz/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
)

type client struct {
ctx context.Context
err error
projects map[string]bool
statusURL string
Expand All @@ -54,6 +55,7 @@ type ossFuzzStatus struct {
// CreateOSSFuzzClient returns a client which implements RepoClient interface.
func CreateOSSFuzzClient(ossFuzzStatusURL string) clients.RepoClient {
return &client{
ctx: context.Background(),
statusURL: ossFuzzStatusURL,
projects: map[string]bool{},
}
Expand All @@ -62,6 +64,7 @@ func CreateOSSFuzzClient(ossFuzzStatusURL string) clients.RepoClient {
// CreateOSSFuzzClientEager returns a OSS Fuzz Client which has already fetched and parsed the status file.
func CreateOSSFuzzClientEager(ossFuzzStatusURL string) (clients.RepoClient, error) {
c := client{
ctx: context.Background(),
statusURL: ossFuzzStatusURL,
projects: map[string]bool{},
}
Expand Down Expand Up @@ -91,7 +94,7 @@ func (c *client) Search(request clients.SearchRequest) (clients.SearchResponse,
}

func (c *client) init() {
b, err := fetchStatusFile(c.statusURL)
b, err := fetchStatusFile(c.ctx, c.statusURL)
if err != nil {
c.err = err
return
Expand All @@ -118,9 +121,12 @@ func parseStatusFile(contents []byte, m map[string]bool) error {
return nil
}

func fetchStatusFile(uri string) ([]byte, error) {
//nolint:gosec // URI comes from a constant or a test HTTP server, not user input
resp, err := http.Get(uri)
func fetchStatusFile(ctx context.Context, uri string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("making status file request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("http.Get: %w", err)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/internal/packagemanager/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func Test_GetURI_calls_client_get_with_input(t *testing.T) {
t.Errorf("Test_GetURI_calls_client_get_with_input() error in Get= %v", err)
return
}
defer got.Body.Close()
body, err := io.ReadAll(got.Body)
if err != nil {
t.Errorf("Test_GetURI_calls_client_get_with_input() error in ReadAll= %v", err)
Expand Down Expand Up @@ -118,6 +119,7 @@ func Test_Get_calls_client_get_with_input(t *testing.T) {
t.Errorf("Test_Get_calls_client_get_with_input() error in Get = %v", err)
return
}
defer got.Body.Close()
body, err := io.ReadAll(got.Body)
if err != nil {
t.Errorf("Test_Get_calls_client_get_with_input() error in ReadAll = %v", err)
Expand Down
4 changes: 2 additions & 2 deletions cron/internal/format/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
"github.com/ossf/scorecard/v4/pkg"
)

//nolint
type jsonCheckResult struct {
Name string
Details []string
Confidence int
Pass bool
}

//nolint:musttag
type jsonScorecardResult struct {
Repo string
Date string
Expand All @@ -47,7 +47,7 @@ type jsonCheckDocumentationV2 struct {
// Can be extended if needed.
}

//nolint
//nolint:govet
type jsonCheckResultV2 struct {
Details []string `json:"details"`
Score int `json:"score"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/dependencydiff_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type ScorecardResultWithError struct {
}

// DependencyCheckResult is the dependency structure used in the returned results.
//
//nolint:musttag // functionality is deprecated anyway
type DependencyCheckResult struct {
// ChangeType indicates whether the dependency is added, updated, or removed.
ChangeType *ChangeType
Expand Down
3 changes: 2 additions & 1 deletion pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import (
"github.com/ossf/scorecard/v4/log"
)

// nolint: govet
//nolint:govet
type jsonCheckResult struct {
Name string
Details []string
Confidence int
Pass bool
}

//nolint:musttag
type jsonScorecardResult struct {
Repo string
Date string
Expand Down

0 comments on commit d0cefa5

Please sign in to comment.