Skip to content

Commit

Permalink
feat: allow linting failures (#4355)
Browse files Browse the repository at this point in the history
Co-authored-by: Anil Keshav <[email protected]>
  • Loading branch information
OliverNocon and anilkeshav27 authored Jan 25, 2024
1 parent 4be7b99 commit cd2fb91
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
14 changes: 5 additions & 9 deletions cmd/golangBuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func runGolangBuild(config *golangBuildOptions, telemetryData *telemetry.CustomD
"additionalParams": "",
}

if err := runGolangciLint(utils, golangciLintDir, lintSettings); err != nil {
if err := runGolangciLint(utils, golangciLintDir, config.FailOnLintingError, lintSettings); err != nil {
return err
}
}
Expand All @@ -213,14 +213,12 @@ func runGolangBuild(config *golangBuildOptions, telemetryData *telemetry.CustomD

var binaries []string
platforms, err := multiarch.ParsePlatformStrings(config.TargetArchitectures)

if err != nil {
return err
}

for _, platform := range platforms {
binaryNames, err := runGolangBuildPerArchitecture(config, goModFile, utils, ldflags, platform)

if err != nil {
return err
}
Expand Down Expand Up @@ -262,7 +260,6 @@ func runGolangBuild(config *golangBuildOptions, telemetryData *telemetry.CustomD
}

artifact, err := versioning.GetArtifact("golang", "", &artifactOpts, utils)

if err != nil {
return err
}
Expand Down Expand Up @@ -304,7 +301,6 @@ func runGolangBuild(config *golangBuildOptions, telemetryData *telemetry.CustomD
log.Entry().Infof("publishing artifact: %s", targetURL)

response, err := utils.UploadRequest(http.MethodPut, targetURL, binary, "", nil, nil, "binary")

if err != nil {
return fmt.Errorf("couldn't upload artifact: %w", err)
}
Expand Down Expand Up @@ -407,7 +403,7 @@ func reportGolangTestCoverage(config *golangBuildOptions, utils golangBuildUtils
}
utils.Stdout(log.Writer())

err = utils.FileWrite("cobertura-coverage.xml", coverageOutput.Bytes(), 0666)
err = utils.FileWrite("cobertura-coverage.xml", coverageOutput.Bytes(), 0o666)
if err != nil {
return fmt.Errorf("failed to create cobertura coverage file: %w", err)
}
Expand Down Expand Up @@ -436,7 +432,7 @@ func retrieveGolangciLint(utils golangBuildUtils, golangciLintDir, golangciLintU
return nil
}

func runGolangciLint(utils golangBuildUtils, golangciLintDir string, lintSettings map[string]string) error {
func runGolangciLint(utils golangBuildUtils, golangciLintDir string, failOnError bool, lintSettings map[string]string) error {
binaryPath := filepath.Join(golangciLintDir, "golangci-lint")

var outputBuffer bytes.Buffer
Expand All @@ -448,12 +444,12 @@ func runGolangciLint(utils golangBuildUtils, golangciLintDir string, lintSetting

log.Entry().Infof("lint report: \n" + outputBuffer.String())
log.Entry().Infof("writing lint report to %s", lintSettings["reportOutputPath"])
err = utils.FileWrite(lintSettings["reportOutputPath"], outputBuffer.Bytes(), 0644)
err = utils.FileWrite(lintSettings["reportOutputPath"], outputBuffer.Bytes(), 0o644)
if err != nil {
return fmt.Errorf("writing golangci-lint report failed: %w", err)
}

if utils.GetExitCode() == 1 {
if utils.GetExitCode() == 1 && failOnError {
return fmt.Errorf("golangci-lint found issues, see report above")
}

Expand Down
11 changes: 11 additions & 0 deletions cmd/golangBuild_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions cmd/golangBuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ func TestPrepareLdflags(t *testing.T) {
t.Parallel()
dir := t.TempDir()

err := os.Mkdir(filepath.Join(dir, "commonPipelineEnvironment"), 0777)
err := os.Mkdir(filepath.Join(dir, "commonPipelineEnvironment"), 0o777)
assert.NoError(t, err, "Error when creating folder structure")

err = os.WriteFile(filepath.Join(dir, "commonPipelineEnvironment", "artifactVersion"), []byte("1.2.3"), 0666)
Expand Down Expand Up @@ -869,7 +869,6 @@ func TestRunGolangBuildPerArchitecture(t *testing.T) {
_, err := runGolangBuildPerArchitecture(&config, &goModFile, utils, ldflags, architecture)
assert.EqualError(t, err, "failed to run build for linux.amd64: execution error")
})

}

func TestPrepareGolangEnvironment(t *testing.T) {
Expand Down Expand Up @@ -961,6 +960,7 @@ func TestRunGolangciLint(t *testing.T) {
exitCode int
expectedCommand []string
expectedErr error
failOnLintingError bool
}{
{
name: "success",
Expand Down Expand Up @@ -992,6 +992,15 @@ func TestRunGolangciLint(t *testing.T) {
exitCode: 1,
expectedCommand: []string{},
expectedErr: fmt.Errorf("golangci-lint found issues, see report above"),
failOnLintingError: true,
},
{
name: "success - ignore failed with ExitCode == 1",
shouldFailOnCommand: map[string]error{},
exitCode: 1,
expectedCommand: []string{binaryPath, "run", "--out-format", lintSettings["reportStyle"]},
expectedErr: nil,
failOnLintingError: false,
},
}

Expand All @@ -1003,7 +1012,7 @@ func TestRunGolangciLint(t *testing.T) {
utils.ShouldFailOnCommand = test.shouldFailOnCommand
utils.FileWriteError = test.fileWriteError
utils.ExitCode = test.exitCode
err := runGolangciLint(utils, golangciLintDir, lintSettings)
err := runGolangciLint(utils, golangciLintDir, test.failOnLintingError, lintSettings)

if test.expectedErr == nil {
assert.Equal(t, test.expectedCommand[0], utils.Calls[0].Exec)
Expand Down
8 changes: 8 additions & 0 deletions resources/metadata/golangBuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ spec:
- STAGES
- STEPS
default: true
- name: failOnLintingError
type: bool
description: "Defines if step will return an error in case linting runs into a problem"
scope:
- PARAMETERS
- STAGES
- STEPS
default: true
- name: ldflagsTemplate
type: string
description: Defines the content of -ldflags option in a golang template format.
Expand Down

0 comments on commit cd2fb91

Please sign in to comment.