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