github.com/SAP/jenkins-library@v1.362.0/cmd/transportRequestUploadCTS.go (about) 1 package cmd 2 3 import ( 4 "github.com/SAP/jenkins-library/pkg/command" 5 "github.com/SAP/jenkins-library/pkg/log" 6 "github.com/SAP/jenkins-library/pkg/telemetry" 7 "github.com/SAP/jenkins-library/pkg/transportrequest/cts" 8 ) 9 10 type transportRequestUploadUtils interface { 11 command.ShellRunner 12 13 // Add more methods here, or embed additional interfaces, or remove/replace as required. 14 // The transportRequestUploadUtils interface should be descriptive of your runtime dependencies, 15 // i.e. include everything you need to be able to mock in tests. 16 // Unit tests shall be executable in parallel (not depend on global state), and don't (re-)test dependencies. 17 } 18 19 // UploadAction ... 20 type UploadAction interface { 21 Perform(command.ShellRunner) error 22 WithConnection(cts.Connection) 23 WithApplication(cts.Application) 24 WithNodeProperties(cts.Node) 25 WithTransportRequestID(string) 26 WithConfigFile(string) 27 WithDeployUser(string) 28 } 29 30 type transportRequestUploadCTSUtilsBundle struct { 31 *command.Command 32 33 // Embed more structs as necessary to implement methods or interfaces you add to transportRequestUploadUtils. 34 // Structs embedded in this way must each have a unique set of methods attached. 35 // If there is no struct which implements the method you need, attach the method to 36 // transportRequestUploadUtilsBundle and forward to the implementation of the dependency. 37 } 38 39 func newTransportRequestUploadCTSUtils() transportRequestUploadUtils { 40 utils := transportRequestUploadCTSUtilsBundle{ 41 Command: &command.Command{}, 42 } 43 // Reroute command output to logging framework 44 utils.Stdout(log.Writer()) 45 utils.Stderr(log.Writer()) 46 return &utils 47 } 48 49 func transportRequestUploadCTS(config transportRequestUploadCTSOptions, 50 telemetryData *telemetry.CustomData, 51 commonPipelineEnvironment *transportRequestUploadCTSCommonPipelineEnvironment) { 52 // Utils can be used wherever the command.ExecRunner interface is expected. 53 // It can also be used for example as a mavenExecRunner. 54 utils := newTransportRequestUploadCTSUtils() 55 56 // For HTTP calls import piperhttp "github.com/SAP/jenkins-library/pkg/http" 57 // and use a &piperhttp.Client{} in a custom system 58 // Example: step checkmarxExecuteScan.go 59 60 // Error situations should be bubbled up until they reach the line below which will then stop execution 61 // through the log.Entry().Fatal() call leading to an os.Exit(1) in the end. 62 err := runTransportRequestUploadCTS(&config, &cts.UploadAction{}, telemetryData, utils, commonPipelineEnvironment) 63 if err != nil { 64 log.Entry().WithError(err).Fatal("step execution failed") 65 } 66 } 67 68 func runTransportRequestUploadCTS( 69 config *transportRequestUploadCTSOptions, 70 action UploadAction, 71 telemetryData *telemetry.CustomData, 72 cmd command.ShellRunner, 73 commonPipelineEnvironment *transportRequestUploadCTSCommonPipelineEnvironment) error { 74 75 log.Entry().Debugf("Entering 'runTransportRequestUpload' with config: %v", config) 76 77 action.WithConnection(cts.Connection{ 78 Endpoint: config.Endpoint, 79 Client: config.Client, 80 User: config.Username, 81 Password: config.Password, 82 }) 83 action.WithApplication(cts.Application{ 84 Name: config.ApplicationName, 85 Pack: config.AbapPackage, 86 Desc: config.Description, 87 }) 88 action.WithNodeProperties(cts.Node{ 89 DeployDependencies: config.DeployToolDependencies, 90 InstallOpts: config.NpmInstallOpts, 91 }) 92 93 action.WithTransportRequestID(config.TransportRequestID) 94 action.WithConfigFile(config.DeployConfigFile) 95 action.WithDeployUser(config.OsDeployUser) 96 97 commonPipelineEnvironment.custom.transportRequestID = config.TransportRequestID 98 99 err := action.Perform(cmd) 100 101 if err == nil { 102 log.Entry().Infof("Upload of application '%s' to CTS succeeded (TransportRequestId: '%s').", 103 config.ApplicationName, 104 config.TransportRequestID, 105 ) 106 } 107 return err 108 }