github.com/jaylevin/jenkins-library@v1.230.4/cmd/transportRequestUploadSOLMAN.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/piperutils"
     7  	"github.com/SAP/jenkins-library/pkg/telemetry"
     8  	"github.com/SAP/jenkins-library/pkg/transportrequest/solman"
     9  )
    10  
    11  type transportRequestUploadSOLMANUtils interface {
    12  	solman.Exec
    13  	FileExists(filename string) (bool, error)
    14  
    15  	// Add more methods here, or embed additional interfaces, or remove/replace as required.
    16  	// The transportRequestUploadSOLMANUtils interface should be descriptive of your runtime dependencies,
    17  	// i.e. include everything you need to be able to mock in tests.
    18  	// Unit tests shall be executable in parallel (not depend on global state), and don't (re-)test dependencies.
    19  }
    20  
    21  type transportRequestUploadSOLMANUtilsBundle struct {
    22  	*command.Command
    23  	*piperutils.Files
    24  
    25  	// Embed more structs as necessary to implement methods or interfaces you add to transportRequestUploadSOLMANUtils.
    26  	// Structs embedded in this way must each have a unique set of methods attached.
    27  	// If there is no struct which implements the method you need, attach the method to
    28  	// transportRequestUploadSOLMANUtilsBundle and forward to the implementation of the dependency.
    29  }
    30  
    31  func newTransportRequestUploadSOLMANUtils() transportRequestUploadSOLMANUtils {
    32  	utils := transportRequestUploadSOLMANUtilsBundle{
    33  		Command: &command.Command{},
    34  		Files:   &piperutils.Files{},
    35  	}
    36  	// Reroute command output to logging framework
    37  	utils.Stdout(log.Writer())
    38  	utils.Stderr(log.Writer())
    39  	return &utils
    40  }
    41  
    42  func transportRequestUploadSOLMAN(config transportRequestUploadSOLMANOptions,
    43  	telemetryData *telemetry.CustomData,
    44  	commonPipelineEnvironment *transportRequestUploadSOLMANCommonPipelineEnvironment) {
    45  	// Utils can be used wherever the command.ExecRunner interface is expected.
    46  	// It can also be used for example as a mavenExecRunner.
    47  	utils := newTransportRequestUploadSOLMANUtils()
    48  
    49  	// For HTTP calls import  piperhttp "github.com/SAP/jenkins-library/pkg/http"
    50  	// and use a  &piperhttp.Client{} in a custom system
    51  	// Example: step checkmarxExecuteScan.go
    52  
    53  	// Error situations should be bubbled up until they reach the line below which will then stop execution
    54  	// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
    55  	err := runTransportRequestUploadSOLMAN(&config, &solman.UploadAction{}, telemetryData, utils, commonPipelineEnvironment)
    56  	if err != nil {
    57  		log.Entry().WithError(err).Fatal("step execution failed")
    58  	}
    59  }
    60  
    61  func runTransportRequestUploadSOLMAN(config *transportRequestUploadSOLMANOptions,
    62  	action solman.Action,
    63  	telemetryData *telemetry.CustomData,
    64  	utils transportRequestUploadSOLMANUtils,
    65  	commonPipelineEnvironment *transportRequestUploadSOLMANCommonPipelineEnvironment) error {
    66  
    67  	action.WithConnection(solman.Connection{
    68  		Endpoint: config.Endpoint,
    69  		User:     config.Username,
    70  		Password: config.Password,
    71  	})
    72  
    73  	action.WithTransportRequestID(config.TransportRequestID)
    74  	action.WithChangeDocumentID(config.ChangeDocumentID)
    75  	action.WithApplicationID(config.ApplicationID)
    76  	action.WithFile(config.FilePath)
    77  	action.WithCMOpts(config.CmClientOpts)
    78  
    79  	commonPipelineEnvironment.custom.transportRequestID = config.TransportRequestID
    80  	commonPipelineEnvironment.custom.changeDocumentID = config.ChangeDocumentID
    81  
    82  	err := action.Perform(utils, utils)
    83  
    84  	if err == nil {
    85  		log.Entry().Infof("Upload of artifact '%s' to SAP Solution Manager succeeded (ChangeDocumentId: '%s', TransportRequestId: '%s').",
    86  			config.FilePath,
    87  			config.ChangeDocumentID,
    88  			config.TransportRequestID,
    89  		)
    90  	}
    91  	return err
    92  }