2021-02-12 09:50:38 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-05 12:33:19 +02:00
|
|
|
"strings"
|
|
|
|
|
2021-02-12 09:50:38 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
|
|
)
|
|
|
|
|
2021-10-04 13:08:34 +02:00
|
|
|
type isChangeInDevelopmentUtils interface {
|
2021-02-12 09:50:38 +02:00
|
|
|
command.ExecRunner
|
|
|
|
GetExitCode() int
|
|
|
|
|
|
|
|
// Add more methods here, or embed additional interfaces, or remove/replace as required.
|
2021-10-04 13:08:34 +02:00
|
|
|
// The isChangeInDevelopmentUtils interface should be descriptive of your runtime dependencies,
|
2021-02-12 09:50:38 +02:00
|
|
|
// i.e. include everything you need to be able to mock in tests.
|
|
|
|
// Unit tests shall be executable in parallel (not depend on global state), and don't (re-)test dependencies.
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:08:34 +02:00
|
|
|
type isChangeInDevelopmentUtilsBundle struct {
|
2021-02-12 09:50:38 +02:00
|
|
|
*command.Command
|
|
|
|
|
2021-10-04 13:08:34 +02:00
|
|
|
// Embed more structs as necessary to implement methods or interfaces you add to isChangeInDevelopmentUtils.
|
2021-02-12 09:50:38 +02:00
|
|
|
// Structs embedded in this way must each have a unique set of methods attached.
|
|
|
|
// If there is no struct which implements the method you need, attach the method to
|
2021-10-04 13:08:34 +02:00
|
|
|
// isChangeInDevelopmentUtilsBundle and forward to the implementation of the dependency.
|
2021-02-12 09:50:38 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 13:08:34 +02:00
|
|
|
func newIsChangeInDevelopmentUtils() isChangeInDevelopmentUtils {
|
|
|
|
utils := isChangeInDevelopmentUtilsBundle{
|
2021-02-12 09:50:38 +02:00
|
|
|
Command: &command.Command{},
|
|
|
|
}
|
|
|
|
// Reroute command output to logging framework
|
|
|
|
utils.Stdout(log.Writer())
|
|
|
|
utils.Stderr(log.Writer())
|
|
|
|
return &utils
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:35:38 +02:00
|
|
|
func isChangeInDevelopment(config isChangeInDevelopmentOptions,
|
|
|
|
telemetryData *telemetry.CustomData,
|
|
|
|
commonPipelineEnvironment *isChangeInDevelopmentCommonPipelineEnvironment) {
|
2021-02-12 09:50:38 +02:00
|
|
|
// Utils can be used wherever the command.ExecRunner interface is expected.
|
|
|
|
// It can also be used for example as a mavenExecRunner.
|
2021-10-04 13:08:34 +02:00
|
|
|
utils := newIsChangeInDevelopmentUtils()
|
2021-02-12 09:50:38 +02:00
|
|
|
|
|
|
|
// For HTTP calls import piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
|
|
|
// and use a &piperhttp.Client{} in a custom system
|
|
|
|
// Example: step checkmarxExecuteScan.go
|
|
|
|
|
|
|
|
// Error situations should be bubbled up until they reach the line below which will then stop execution
|
|
|
|
// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
|
2021-10-04 14:35:38 +02:00
|
|
|
err := runIsChangeInDevelopment(&config, telemetryData, utils, commonPipelineEnvironment)
|
2021-02-12 09:50:38 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("step execution failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:35:38 +02:00
|
|
|
func runIsChangeInDevelopment(config *isChangeInDevelopmentOptions,
|
|
|
|
telemetryData *telemetry.CustomData,
|
|
|
|
utils isChangeInDevelopmentUtils,
|
|
|
|
commonPipelineEnvironment *isChangeInDevelopmentCommonPipelineEnvironment) error {
|
2021-02-12 09:50:38 +02:00
|
|
|
|
|
|
|
log.Entry().Infof("Checking change status for change '%s'", config.ChangeDocumentID)
|
|
|
|
|
2021-10-04 13:08:34 +02:00
|
|
|
isInDevelopment, err := perform(config, utils)
|
2021-10-04 14:35:38 +02:00
|
|
|
|
2021-02-12 09:50:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:35:38 +02:00
|
|
|
commonPipelineEnvironment.custom.isChangeInDevelopment = isInDevelopment
|
|
|
|
|
2021-02-12 09:50:38 +02:00
|
|
|
if isInDevelopment {
|
|
|
|
log.Entry().Infof("Change '%s' is in status 'in development'.", config.ChangeDocumentID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if config.FailIfStatusIsNotInDevelopment {
|
2021-10-04 13:08:34 +02:00
|
|
|
return fmt.Errorf("change '%s' is not in status 'in development'", config.ChangeDocumentID)
|
2021-02-12 09:50:38 +02:00
|
|
|
}
|
|
|
|
log.Entry().Warningf("Change '%s' is not in status 'in development'. Failing the step has been explicitly disabled.", config.ChangeDocumentID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:08:34 +02:00
|
|
|
func perform(config *isChangeInDevelopmentOptions, utils isChangeInDevelopmentUtils) (bool, error) {
|
2021-02-12 09:50:38 +02:00
|
|
|
|
2021-10-04 14:35:38 +02:00
|
|
|
if len(config.CmClientOpts) > 0 {
|
|
|
|
utils.AppendEnv([]string{fmt.Sprintf("CMCLIENT_OPTS=%s", strings.Join(config.CmClientOpts, " "))})
|
2021-02-12 09:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := utils.RunExecutable("cmclient",
|
|
|
|
"--endpoint", config.Endpoint,
|
|
|
|
"--user", config.Username,
|
|
|
|
"--password", config.Password,
|
|
|
|
"is-change-in-development",
|
|
|
|
"--change-id", config.ChangeDocumentID,
|
|
|
|
"--return-code")
|
|
|
|
|
2021-10-04 14:35:38 +02:00
|
|
|
exitCode := utils.GetExitCode()
|
|
|
|
|
|
|
|
hint := "check log for details"
|
2021-02-12 09:50:38 +02:00
|
|
|
if err != nil {
|
2021-10-04 14:35:38 +02:00
|
|
|
hint = err.Error()
|
2021-02-12 09:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if exitCode == 0 {
|
|
|
|
return true, nil
|
|
|
|
} else if exitCode == 3 {
|
|
|
|
return false, nil
|
|
|
|
} else if exitCode == 2 {
|
2021-10-04 14:35:38 +02:00
|
|
|
hint = "invalid credentials"
|
2021-02-12 09:50:38 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 13:08:34 +02:00
|
|
|
return false, fmt.Errorf("cannot retrieve change status: %s", hint)
|
2021-02-12 09:50:38 +02:00
|
|
|
}
|