github.com/xgoffin/jenkins-library@v1.154.0/cmd/abapAddonAssemblyKitRegisterPackages.go (about)

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  
     8  	"github.com/SAP/jenkins-library/pkg/abap/aakaas"
     9  	abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
    10  	"github.com/SAP/jenkins-library/pkg/abaputils"
    11  	"github.com/SAP/jenkins-library/pkg/command"
    12  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    13  	"github.com/SAP/jenkins-library/pkg/log"
    14  	"github.com/SAP/jenkins-library/pkg/telemetry"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  func abapAddonAssemblyKitRegisterPackages(config abapAddonAssemblyKitRegisterPackagesOptions, telemetryData *telemetry.CustomData, cpe *abapAddonAssemblyKitRegisterPackagesCommonPipelineEnvironment) {
    19  	// for command execution use Command
    20  	c := command.Command{}
    21  	// reroute command output to logging framework
    22  	c.Stdout(log.Writer())
    23  	c.Stderr(log.Writer())
    24  
    25  	client := piperhttp.Client{}
    26  
    27  	// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
    28  	err := runAbapAddonAssemblyKitRegisterPackages(&config, telemetryData, &client, cpe, reader)
    29  	if err != nil {
    30  		log.Entry().WithError(err).Fatal("step execution failed")
    31  	}
    32  }
    33  
    34  func runAbapAddonAssemblyKitRegisterPackages(config *abapAddonAssemblyKitRegisterPackagesOptions, telemetryData *telemetry.CustomData, client piperhttp.Sender,
    35  	cpe *abapAddonAssemblyKitRegisterPackagesCommonPipelineEnvironment, fileReader readFile) error {
    36  
    37  	var addonDescriptor abaputils.AddonDescriptor
    38  	json.Unmarshal([]byte(config.AddonDescriptor), &addonDescriptor)
    39  
    40  	conn := new(abapbuild.Connector)
    41  	if err := conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, client); err != nil {
    42  		return err
    43  	}
    44  
    45  	if err := uploadSarFiles(addonDescriptor.Repositories, *conn, fileReader); err != nil {
    46  		return err
    47  	}
    48  
    49  	conn2 := new(abapbuild.Connector) // we need a second connector without the added Header
    50  	if err := conn2.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, client); err != nil {
    51  		return err
    52  	}
    53  
    54  	var err error
    55  	addonDescriptor.Repositories, err = registerPackages(addonDescriptor.Repositories, *conn2)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	log.Entry().Info("Writing package status to CommonPipelineEnvironment")
    61  	cpe.abap.addonDescriptor = addonDescriptor.AsJSONstring()
    62  
    63  	return nil
    64  }
    65  
    66  func uploadSarFiles(repos []abaputils.Repository, conn abapbuild.Connector, readFileFunc readFile) error {
    67  	for i := range repos {
    68  		if repos[i].Status == string(aakaas.PackageStatusPlanned) {
    69  			if repos[i].SarXMLFilePath == "" {
    70  				return errors.New("Parameter missing. Please provide the path to the SAR file")
    71  			}
    72  			filename := filepath.Base(repos[i].SarXMLFilePath)
    73  			log.Entry().Infof("Trying to read file %s", repos[i].SarXMLFilePath)
    74  			sarFile, err := readFileFunc(repos[i].SarXMLFilePath)
    75  			if err != nil {
    76  				return err
    77  			}
    78  			log.Entry().Infof("... %d bytes read", len(sarFile))
    79  			if len(sarFile) == 0 {
    80  				return errors.New("File has no content - 0 bytes")
    81  			}
    82  			log.Entry().Infof("Upload SAR file %s in chunks", filename)
    83  			err = conn.UploadSarFileInChunks("/odata/aas_file_upload", filename, sarFile)
    84  			if err != nil {
    85  				return err
    86  			}
    87  			log.Entry().Info("...done")
    88  		} else {
    89  			log.Entry().Infof("Package %s has status %s, cannot upload the SAR file of this package", repos[i].PackageName, repos[i].Status)
    90  		}
    91  	}
    92  	return nil
    93  }
    94  
    95  // for moocking
    96  type readFile func(path string) ([]byte, error)
    97  
    98  func reader(path string) ([]byte, error) {
    99  	return ioutil.ReadFile(path)
   100  }
   101  
   102  func registerPackages(repos []abaputils.Repository, conn abapbuild.Connector) ([]abaputils.Repository, error) {
   103  	for i := range repos {
   104  		var pack aakaas.Package
   105  		pack.InitPackage(repos[i], conn)
   106  		if repos[i].Status == string(aakaas.PackageStatusPlanned) {
   107  			log.Entry().Infof("Trying to Register Package %s", pack.PackageName)
   108  			err := pack.Register()
   109  			if err != nil {
   110  				return repos, err
   111  			}
   112  			log.Entry().Info("...done, take over new status")
   113  			pack.ChangeStatus(&repos[i])
   114  			log.Entry().Info("...done")
   115  		} else {
   116  			log.Entry().Infof("Package %s has status %s, cannot register this package", pack.PackageName, pack.Status)
   117  		}
   118  	}
   119  	return repos, nil
   120  }