Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply and fix more linters #3864

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ linters:
- bidichk
- bodyclose
- canonicalheader
- containedctx
- contextcheck
- copyloopvar
- decorder
Expand All @@ -33,7 +34,10 @@ linters:
- errcheck
- errchkjson
- errname
- errorlint
- exhaustive
- fatcontext
- forcetypeassert
- gci
- ginkgolinter
- gocheckcompilerdirectives
Expand Down Expand Up @@ -65,6 +69,8 @@ linters:
- misspell
- musttag
- nakedret
- nilerr
- noctx
- nolintlint
- nosprintfhostport
- perfsprint
Expand All @@ -85,6 +91,7 @@ linters:
- tenv
- testableexamples
- testifylint
- tparallel
- typecheck
- unconvert
- unparam
Expand All @@ -93,16 +100,12 @@ linters:
- wastedassign
- whitespace
- zerologlint
# - containedctx
# - cyclop
# - depguard
# - dupword
# - err113
# - errorlint
# - exhaustive
# - exhaustruct
# - forbidigo
# - forcetypeassert
# - funlen
# - gochecknoglobals
# - gochecknoinits
Expand All @@ -114,16 +117,13 @@ linters:
# - maintidx
# - mnd
# - nestif
# - nilerr
# - nilnil
# - nlreturn
# - noctx
# - nonamedreturns
# - paralleltest
# - tagliatelle
# - testpackage
# - thelper
# - tparallel
# - varnamelen
# - wrapcheck
# - wsl
Expand Down
10 changes: 5 additions & 5 deletions cmd/ci-reporter/cmd/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var githubCmd = &cobra.Command{
Long: "CI-Signal reporter that generates only a github report.",
PreRun: setGithubConfig,
RunE: func(cmd *cobra.Command, args []string) error {
return RunReport(cfg, &CIReporters{GithubReporter{}})
return RunReport(cmd.Context(), cfg, &CIReporters{GithubReporter{}})
},
}

Expand Down Expand Up @@ -75,7 +75,7 @@ func (r GithubReporter) GetCIReporterHead() CIReporterInfo {
}

// CollectReportData implementation from CIReporter.
func (r GithubReporter) CollectReportData(cfg *Config) ([]*CIReportRecord, error) {
func (r GithubReporter) CollectReportData(ctx context.Context, cfg *Config) ([]*CIReportRecord, error) {
// set filter configuration
denyListFilter := map[FilteredFieldName][]FilteredListVal{}
allowListFilter := map[FilteredFieldName][]FilteredListVal{
Expand All @@ -88,7 +88,7 @@ func (r GithubReporter) CollectReportData(cfg *Config) ([]*CIReportRecord, error
allowListFilter[FilteredFieldName("K8s Release")] = []FilteredListVal{FilteredListVal(cfg.ReleaseVersion)}
}
// request github projectboard data
githubReportData, err := GetGithubReportData(*cfg, denyListFilter, allowListFilter)
githubReportData, err := GetGithubReportData(ctx, *cfg, denyListFilter, allowListFilter)
if err != nil {
return nil, fmt.Errorf("getting GitHub report data: %w", err)
}
Expand Down Expand Up @@ -211,13 +211,13 @@ type (
)

// GetGithubReportData used to request the raw report data from github.
func GetGithubReportData(cfg Config, denyListFieldFilter, allowListFieldFilter map[FilteredFieldName][]FilteredListVal) ([]*TransformedProjectBoardItem, error) {
func GetGithubReportData(ctx context.Context, cfg Config, denyListFieldFilter, allowListFieldFilter map[FilteredFieldName][]FilteredListVal) ([]*TransformedProjectBoardItem, error) {
// lookup project board information
var queryCiSignalProjectBoard ciSignalProjectBoardGraphQLQuery
variablesProjectBoardFields := map[string]interface{}{
"projectBoardID": githubv4.ID(ciSignalProjectBoardID),
}
if err := cfg.GithubClient.Query(context.Background(), &queryCiSignalProjectBoard, variablesProjectBoardFields); err != nil {
if err := cfg.GithubClient.Query(ctx, &queryCiSignalProjectBoard, variablesProjectBoardFields); err != nil {
return nil, err
}

Expand Down
15 changes: 8 additions & 7 deletions cmd/ci-reporter/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/shurcooL/githubv4"
"github.com/spf13/cobra"
"github.com/tj/go-spin"
"golang.org/x/net/context"
)

var rootCmd = &cobra.Command{
Expand All @@ -39,7 +40,7 @@ var rootCmd = &cobra.Command{
// all available reporters are used by default that are used to generate the report
// CLI sub commands can be used to specify a specific reporter
selectedReporters := AllImplementedReporters
return RunReport(cfg, &selectedReporters)
return RunReport(cmd.Context(), cfg, &selectedReporters)
},
}

Expand Down Expand Up @@ -68,7 +69,7 @@ func init() {
}

// RunReport used to execute.
func RunReport(cfg *Config, reporters *CIReporters) error {
func RunReport(ctx context.Context, cfg *Config, reporters *CIReporters) error {
go func() {
s := spin.New()
for {
Expand All @@ -78,7 +79,7 @@ func RunReport(cfg *Config, reporters *CIReporters) error {
}()

// collect data from filtered reporters
reports, err := reporters.CollectReportDataFromReporters(cfg)
reports, err := reporters.CollectReportDataFromReporters(ctx, cfg)
if err != nil {
return err
}
Expand Down Expand Up @@ -137,7 +138,7 @@ type CIReporter interface {
// GetCIReporterHead sets meta information which is used to differentiate reporters
GetCIReporterHead() CIReporterInfo
// CollectReportData is used to request / collect all report data
CollectReportData(*Config) ([]*CIReportRecord, error)
CollectReportData(context.Context, *Config) ([]*CIReportRecord, error)
}

// CIReporters used to specify multiple CIReports, type gets extended by helper functions to collect and visualize report data.
Expand All @@ -147,7 +148,7 @@ type CIReporters []CIReporter
var AllImplementedReporters = CIReporters{GithubReporter{}, TestgridReporter{}}

// SearchReporter used to filter a implemented reporter by name.
func SearchReporter(reporterName string) (CIReporter, error) {
func SearchReporter(ctx context.Context, reporterName string) (CIReporter, error) {
var reporter CIReporter
reporterFound := false
for _, r := range AllImplementedReporters {
Expand All @@ -164,13 +165,13 @@ func SearchReporter(reporterName string) (CIReporter, error) {
}

// CollectReportDataFromReporters used to collect data for multiple reporters.
func (r *CIReporters) CollectReportDataFromReporters(cfg *Config) (*CIReportDataFields, error) {
func (r *CIReporters) CollectReportDataFromReporters(ctx context.Context, cfg *Config) (*CIReportDataFields, error) {
collectedReports := CIReportDataFields{}
for i := range *r {
reporters := *r
reporter := reporters[i]
reporterHead := reporter.GetCIReporterHead()
reportData, err := reporter.CollectReportData(cfg)
reportData, err := reporter.CollectReportData(ctx, cfg)
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/ci-reporter/cmd/testgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"context"
"errors"
"fmt"
"time"
Expand All @@ -33,7 +34,7 @@ var testgridCmd = &cobra.Command{
Long: "CI-Signal reporter that generates only a testgrid report.",
PreRun: setGithubConfig,
RunE: func(cmd *cobra.Command, args []string) error {
return RunReport(cfg, &CIReporters{TestgridReporter{}})
return RunReport(cmd.Context(), cfg, &CIReporters{TestgridReporter{}})
},
}

Expand All @@ -54,8 +55,8 @@ func (r TestgridReporter) GetCIReporterHead() CIReporterInfo {
}

// CollectReportData implementation from CIReporter.
func (r TestgridReporter) CollectReportData(cfg *Config) ([]*CIReportRecord, error) {
testgridReportData, err := GetTestgridReportData(*cfg)
func (r TestgridReporter) CollectReportData(ctx context.Context, cfg *Config) ([]*CIReportRecord, error) {
testgridReportData, err := GetTestgridReportData(ctx, *cfg)
if err != nil {
return nil, err
}
Expand All @@ -80,7 +81,7 @@ func (r TestgridReporter) CollectReportData(cfg *Config) ([]*CIReportRecord, err
}

// GetTestgridReportData used to request the raw report data from testgrid.
func GetTestgridReportData(cfg Config) (testgrid.DashboardData, error) {
func GetTestgridReportData(ctx context.Context, cfg Config) (testgrid.DashboardData, error) {
testgridDashboardNames := []testgrid.DashboardName{"sig-release-master-blocking", "sig-release-master-informing"}
if cfg.ReleaseVersion != "" {
testgridDashboardNames = append(testgridDashboardNames, []testgrid.DashboardName{
Expand All @@ -90,7 +91,7 @@ func GetTestgridReportData(cfg Config) (testgrid.DashboardData, error) {
}
dashboardData := testgrid.DashboardData{}
for i := range testgridDashboardNames {
d, err := testgrid.ReqTestgridDashboardSummary(testgridDashboardNames[i])
d, err := testgrid.ReqTestgridDashboardSummary(ctx, testgridDashboardNames[i])
if err != nil {
if errors.Is(err, testgrid.ErrDashboardNotFound) {
logrus.Warn(fmt.Sprintf("%v for project board %s", err.Error(), testgridDashboardNames[i]))
Expand Down
2 changes: 2 additions & 0 deletions gcb/gcb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ func TestDirForJobType(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

mock := &gcbfakes.FakeImpl{}
sut := New()
sut.impl = mock
Expand Down
2 changes: 1 addition & 1 deletion pkg/binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (b *Binary) ContainsStrings(s ...string) (match bool, err error) {
// Read each rune from the binary file
r, _, err := in.ReadRune()
if err != nil {
if err != io.EOF {
if !errors.Is(err, io.EOF) {
return match, fmt.Errorf("while reading binary data: %w", err)
}
return false, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

func TestBuildDirFromRepoRoot(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
instance *Instance
Expand Down
1 change: 1 addition & 0 deletions pkg/notes/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ func mapKind(kind notes.Kind) notes.Kind {
}

func prettyKind(kind notes.Kind) string {
//nolint:exhaustive // all cases are covered by default
switch kind {
case notes.KindAPIChange:
return "API Change"
Expand Down
4 changes: 2 additions & 2 deletions pkg/notes/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ type Result struct {

type Gatherer struct {
client github.Client
context context.Context
context context.Context //nolint:containedctx // contained context is intentional
options *options.Options
MapProviders []*MapProvider
}
Expand Down Expand Up @@ -787,7 +787,7 @@ func (g *Gatherer) ReleaseNoteForPullRequest(prNr int) (*ReleaseNote, error) {
func (g *Gatherer) notesForCommit(commit *gogithub.RepositoryCommit) (*Result, error) {
prs, err := g.prsFromCommit(commit)
if err != nil {
if err == errNoPRIDFoundInCommitMessage || err == errNoPRFoundForCommitSHA {
if errors.Is(err, errNoPRIDFoundInCommitMessage) || errors.Is(err, errNoPRFoundForCommitSHA) {
logrus.Debugf(
"No matches found when parsing PR from commit SHA %s",
commit.GetSHA(),
Expand Down
2 changes: 2 additions & 0 deletions pkg/notes/notes_gatherer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestMain(m *testing.M) {
}

func TestListCommits(t *testing.T) {
t.Parallel()
const always = -1

zeroTime := &github.Timestamp{}
Expand Down Expand Up @@ -236,6 +237,7 @@ func TestListCommits(t *testing.T) {
}

func TestGatherNotes(t *testing.T) {
t.Parallel()
type getPullRequestStub func(context.Context, string, string, int) (*github.PullRequest, *github.Response, error)
type listPullRequestsWithCommitStub func(context.Context, string, string, string, *github.ListOptions) ([]*github.PullRequest, *github.Response, error)

Expand Down
2 changes: 1 addition & 1 deletion pkg/notes/notes_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func ParseReleaseNotesMap(mapPath string) (*[]ReleaseNotesMap, error) {

for {
noteMap := ReleaseNotesMap{}
if err := decoder.Decode(&noteMap); err == io.EOF {
if err := decoder.Decode(&noteMap); errors.Is(err, io.EOF) {
break
} else if err != nil {
return nil, fmt.Errorf("decoding note map: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/notes/notes_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (g *Gatherer) listLeftParentCommits(opts *options.Options) ([]*commitPrPair

// Find and collect PR number from commit message
prNums, err := prsNumForCommitFromMessage(commitPointer.Message)
if err == errNoPRIDFoundInCommitMessage {
if errors.Is(err, errNoPRIDFoundInCommitMessage) {
logrus.WithFields(logrus.Fields{
"sha": hashString,
}).Debug("no associated PR found")
Expand Down
1 change: 1 addition & 0 deletions pkg/release/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ func (p *Publisher) VerifyLatestUpdate(
gcsVersion, err := p.client.GSUtilOutput("cat", publishFileDst)
if err != nil {
logrus.Infof("%s does not exist but will be created", publishFileDst)
//nolint:nilerr // returning nil is intentional
return true, nil
}

Expand Down
16 changes: 12 additions & 4 deletions pkg/testgrid/testgrid-scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package testgrid

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -37,14 +38,14 @@ type SummaryLookup struct {

// ReqTestgridDashboardSummaries this function requests multiple testgrid summaries concurrently
// This function implements a concurrency pattern to send http requests concurrently.
func ReqTestgridDashboardSummaries(dashboardNames []DashboardName) (DashboardData, error) {
func ReqTestgridDashboardSummaries(ctx context.Context, dashboardNames []DashboardName) (DashboardData, error) {
// Worker
requestData := func(done <-chan interface{}, dashboardNames ...DashboardName) <-chan SummaryLookup {
summaryLookups := make(chan SummaryLookup)
go func() {
defer close(summaryLookups)
for _, dashboardName := range dashboardNames {
summary, err := ReqTestgridDashboardSummary(dashboardName)
summary, err := ReqTestgridDashboardSummary(ctx, dashboardName)
select {
case <-done:
return
Expand Down Expand Up @@ -80,8 +81,15 @@ type NotFound error
var ErrDashboardNotFound NotFound = errors.New("testgrid dashboard not found")

// ReqTestgridDashboardSummary used to retrieve summary information about a testgrid dashboard.
func ReqTestgridDashboardSummary(dashboardName DashboardName) (JobData, error) {
resp, err := http.Get(fmt.Sprintf("https://testgrid.k8s.io/%s/summary", dashboardName))
func ReqTestgridDashboardSummary(ctx context.Context, dashboardName DashboardName) (JobData, error) {
url := fmt.Sprintf("https://testgrid.k8s.io/%s/summary", dashboardName)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, fmt.Errorf("create new request: %w", err)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request remote content: %w", err)
}
Expand Down
Loading
Loading