Skip to content

Commit

Permalink
Enable wsl and nlreturn linters
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Jan 28, 2025
1 parent f690a96 commit a38acaf
Show file tree
Hide file tree
Showing 114 changed files with 1,310 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ linters:
- nakedret
- nilerr
- nilnesserr
- nlreturn
- noctx
- nolintlint
- nosprintfhostport
Expand Down Expand Up @@ -102,6 +103,7 @@ linters:
- usetesting
- wastedassign
- whitespace
- wsl
- zerologlint
# - cyclop
# - depguard
Expand All @@ -121,15 +123,13 @@ linters:
# - mnd
# - nestif
# - nilnil
# - nlreturn
# - nonamedreturns
# - paralleltest
# - tagliatelle
# - testpackage
# - thelper
# - varnamelen
# - wrapcheck
# - wsl
linters-settings:
gci:
sections:
Expand Down
19 changes: 19 additions & 0 deletions cmd/ci-reporter/cmd/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ func (r GithubReporter) CollectReportData(ctx context.Context, cfg *Config) ([]*
allowListFilter := map[FilteredFieldName][]FilteredListVal{
FilteredFieldName("view"): {"issue-tracking"},
}

if cfg.ShortReport {
denyListFilter[FilteredFieldName("Status")] = []FilteredListVal{FilteredListVal("RESOLVED"), FilteredListVal("PASSING")}
}

if cfg.ReleaseVersion != "" {
allowListFilter[FilteredFieldName("K8s Release")] = []FilteredListVal{FilteredListVal(cfg.ReleaseVersion)}
}
Expand All @@ -92,6 +94,7 @@ func (r GithubReporter) CollectReportData(ctx context.Context, cfg *Config) ([]*
if err != nil {
return nil, fmt.Errorf("getting GitHub report data: %w", err)
}

records := []*CIReportRecord{}

for _, item := range githubReportData {
Expand All @@ -100,6 +103,7 @@ func (r GithubReporter) CollectReportData(ctx context.Context, cfg *Config) ([]*
if issueURL, ok := item.Fields[fieldName(IssueURLKey)]; ok {
URL = string(issueURL)
}

if prURL, ok := item.Fields[fieldName(PullRequestURLKey)]; ok {
URL = string(prURL)
}
Expand All @@ -114,6 +118,7 @@ func (r GithubReporter) CollectReportData(ctx context.Context, cfg *Config) ([]*
UpdatedTimestamp: string(item.Fields[fieldName(UpdatedAtKey)]),
})
}

return records, nil
}

Expand Down Expand Up @@ -214,6 +219,7 @@ type (
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),
}
Expand All @@ -234,22 +240,26 @@ func GetGithubReportData(ctx context.Context, cfg Config, denyListFieldFilter, a
projectBoardFieldID string
projectBoardFieldName string
)

projectBoardFieldIDs := map[projectBoardFieldID]projectBoardFieldName{}

for _, field := range queryCiSignalProjectBoard.Node.ProjectNext.Fields.Nodes {
var fieldSettings GitHubProjectBoardFieldSettings
if err := json.Unmarshal([]byte(field.Settings), &fieldSettings); err != nil {
return nil, err
}

for _, option := range fieldSettings.Options {
projectBoardFieldIDs[projectBoardFieldID(option.ID)] = projectBoardFieldName(option.Name)
}
}

transformedProjectBoardItems := []*TransformedProjectBoardItem{}

for _, item := range queryCiSignalProjectBoard.Node.ProjectNext.Items.Nodes {
transFields := map[fieldName]fieldValue{}
itemBlacklisted := false

for _, field := range item.FieldValues.Nodes {
fieldVal := field.Value
// To check if the field value is blacklisted
Expand All @@ -267,9 +277,11 @@ func GetGithubReportData(ctx context.Context, cfg Config, denyListFieldFilter, a
for _, bv := range denyListValues {
if fieldVal == string(bv) {
itemBlacklisted = true

break
}
}

if itemBlacklisted {
break
}
Expand All @@ -281,24 +293,31 @@ func GetGithubReportData(ctx context.Context, cfg Config, denyListFieldFilter, a
for _, bv := range allowListValues {
if fieldVal != string(bv) {
itemBlacklisted = true

break
}
}

if itemBlacklisted {
break
}
}

transFields[fieldName(field.ProjectField.Name)] = fieldValue(fieldVal)
}

if itemBlacklisted {
continue
}

if item.Content.Issue.URL != "" {
transFields[fieldName("Issue URL")] = fieldValue(item.Content.Issue.URL)
}

if item.Content.PullRequest.URL != "" {
transFields[fieldName("PullRequest URL")] = fieldValue(item.Content.PullRequest.URL)
}

transformedProjectBoardItems = append(transformedProjectBoardItems, &TransformedProjectBoardItem{
ID: item.ID,
Title: item.Title,
Expand Down
22 changes: 22 additions & 0 deletions cmd/ci-reporter/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +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(cmd.Context(), cfg, &selectedReporters)
},
}
Expand Down Expand Up @@ -72,6 +73,7 @@ func init() {
func RunReport(ctx context.Context, cfg *Config, reporters *CIReporters) error {
go func() {
s := spin.New()

for {
fmt.Printf("\rloading data %s ", s.Next())
time.Sleep(100 * time.Millisecond)
Expand Down Expand Up @@ -150,27 +152,34 @@ var AllImplementedReporters = CIReporters{GithubReporter{}, TestgridReporter{}}
// SearchReporter used to filter a implemented reporter by name.
func SearchReporter(ctx context.Context, reporterName string) (CIReporter, error) {
var reporter CIReporter

reporterFound := false

for _, r := range AllImplementedReporters {
if strings.EqualFold(string(r.GetCIReporterHead().Name), reporterName) {
reporter = r
reporterFound = true

break
}
}

if !reporterFound {
return nil, errors.New("could not find a implemented reporter")
}

return reporter, nil
}

// CollectReportDataFromReporters used to collect data for multiple reporters.
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(ctx, cfg)
if err != nil {
return nil, err
Expand All @@ -181,6 +190,7 @@ func (r *CIReporters) CollectReportDataFromReporters(ctx context.Context, cfg *C
Records: reportData,
})
}

return &collectedReports, nil
}

Expand All @@ -192,16 +202,19 @@ func (r *CIReporters) CollectReportDataFromReporters(ctx context.Context, cfg *C
func PrintReporterData(cfg *Config, reports *CIReportDataFields) error {
// Get a stream to write the data to (file stream / standard out stream)
var out *os.File

if cfg.Filepath != "" {
// open output file
fileOut, err := os.OpenFile(cfg.Filepath, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return fmt.Errorf("could not open or create a file at %s to write the ci signal report to: %w", cfg.Filepath, err)
}

out = fileOut
} else {
out = os.Stdout
}

defer func() {
if err := out.Close(); err != nil {
panic(err)
Expand All @@ -215,10 +228,12 @@ func PrintReporterData(cfg *Config, reports *CIReportDataFields) error {
if err != nil {
return fmt.Errorf("could not marshal report data: %w", err)
}

_, err = out.Write(d)
if err != nil {
return fmt.Errorf("could not write to output stream: %w", err)
}

return nil
}

Expand All @@ -236,11 +251,13 @@ func PrintReporterData(cfg *Config, reports *CIReportDataFields) error {
// table in short version differs from regular table
if cfg.ShortReport {
table.SetHeader([]string{"TESTGRID BOARD", "TITLE", "STATUS", "STATUS DETAILS"})

for _, record := range r.Records {
data = append(data, []string{record.TestgridBoard, record.Title, record.Status, record.StatusDetails})
}
} else {
table.SetHeader([]string{"TESTGRID BOARD", "TITLE", "STATUS", "STATUS DETAILS", "URL", "UPDATED AT"})

for _, record := range r.Records {
data = append(data, []string{
record.TestgridBoard,
Expand All @@ -251,6 +268,7 @@ func PrintReporterData(cfg *Config, reports *CIReportDataFields) error {
})
}
}

table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.AppendBulk(data)
table.SetCenterSeparator("|")
Expand All @@ -259,16 +277,20 @@ func PrintReporterData(cfg *Config, reports *CIReportDataFields) error {
// write a summary
countCategories := map[string]int{}
categoryIndex := 2

for i := range data {
countCategories[data[i][categoryIndex]]++
}

categoryCounts := ""
for category, categoryCount := range countCategories {
categoryCounts += fmt.Sprintf("%s:%d ", category, categoryCount)
}

if _, err := fmt.Fprintf(out, "\nSUMMARY - Total:%d %s\n", len(data), categoryCounts); err != nil {
return fmt.Errorf("could not write to output stream: %w", err)
}
}

return nil
}
9 changes: 9 additions & 0 deletions cmd/ci-reporter/cmd/testgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func (r TestgridReporter) CollectReportData(ctx context.Context, cfg *Config) ([
if err != nil {
return nil, err
}

records := []*CIReportRecord{}

for dashboardName, jobData := range testgridReportData {
for jobName := range jobData {
jobSummary := jobData[jobName]
Expand All @@ -77,6 +79,7 @@ func (r TestgridReporter) CollectReportData(ctx context.Context, cfg *Config) ([
}
}
}

return records, nil
}

Expand All @@ -89,17 +92,23 @@ func GetTestgridReportData(ctx context.Context, cfg Config) (testgrid.DashboardD
testgrid.DashboardName(fmt.Sprintf("sig-release-%s-informing", cfg.ReleaseVersion)),
}...)
}

dashboardData := testgrid.DashboardData{}

for i := range testgridDashboardNames {
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]))

continue
}

return nil, err
}

dashboardData[testgridDashboardNames[i]] = d
}

return dashboardData, nil
}
1 change: 1 addition & 0 deletions cmd/gcbuilder/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func run() error {
if len(buildErrors) != 0 {
logrus.Fatalf("Failed to run some build jobs: %v", buildErrors)
}

logrus.Info("Finished.")

return nil
Expand Down
5 changes: 5 additions & 0 deletions cmd/krel/cmd/announce_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func runBuildBranchAnnounce(opts *buildBranchAnnounceOptions, buildOpts *buildAn
if err != nil {
return err
}

announcement := bytes.Buffer{}
if err := t.Execute(&announcement, struct {
Branch string
Expand Down Expand Up @@ -232,12 +233,14 @@ func (opts *buildAnnounceOptions) saveAnnouncement(announcement bytes.Buffer) er

absOutputPath := filepath.Join(opts.workDir, "announcement.html")
logrus.Infof("Writing HTML file to %s", absOutputPath)

err := os.WriteFile(absOutputPath, announcement.Bytes(), os.FileMode(0o644))
if err != nil {
return fmt.Errorf("saving announcement.html: %w", err)
}

logrus.Info("Kubernetes Announcement created.")

return nil
}

Expand All @@ -248,6 +251,8 @@ func getGoVersion() (string, error) {
if err != nil {
return "", fmt.Errorf("get go version: %w", err)
}

versionRegex := regexp.MustCompile(semVerRegex)

return versionRegex.FindString(strings.TrimSpace(cmdStatus.Output())), nil
}
Loading

0 comments on commit a38acaf

Please sign in to comment.