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

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     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/log"
    12  	"github.com/SAP/jenkins-library/pkg/telemetry"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  func abapAddonAssemblyKitCheckCVs(config abapAddonAssemblyKitCheckCVsOptions, telemetryData *telemetry.CustomData, cpe *abapAddonAssemblyKitCheckCVsCommonPipelineEnvironment) {
    17  	utils := aakaas.NewAakBundle()
    18  	if err := runAbapAddonAssemblyKitCheckCVs(&config, telemetryData, &utils, cpe); err != nil {
    19  		log.Entry().WithError(err).Fatal("step execution failed")
    20  	}
    21  }
    22  
    23  func runAbapAddonAssemblyKitCheckCVs(config *abapAddonAssemblyKitCheckCVsOptions, telemetryData *telemetry.CustomData, utils *aakaas.AakUtils, cpe *abapAddonAssemblyKitCheckCVsCommonPipelineEnvironment) error {
    24  	conn := new(abapbuild.Connector)
    25  	if err := conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, *utils); err != nil {
    26  		return err
    27  	}
    28  
    29  	log.Entry().Infof("Reading Product Version Information from addonDescriptor (aka addon.yml) file: %s", config.AddonDescriptorFileName)
    30  	addonDescriptor, err := (*utils).ReadAddonDescriptor(config.AddonDescriptorFileName)
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	for i := range addonDescriptor.Repositories {
    36  		var c componentVersion
    37  		c.initCV(addonDescriptor.Repositories[i], *conn)
    38  		err := c.validate()
    39  		if err != nil {
    40  			return err
    41  		}
    42  		c.copyFieldsToRepo(&addonDescriptor.Repositories[i])
    43  	}
    44  
    45  	// now Software Component Versions fields are valid, but maybe Product Version was checked before, so copy that part from CPE
    46  	// we don't care for errors
    47  	// scenario 1: config.AddonDescriptor is empty since checkCVs is the first step in the pipeline, then the empty result is fine anyway
    48  	// scenario 2: for some reason config.AddonDescriptor is corrupt - then we insert the valid data but delete the repositories which will ensure issue is found later on
    49  	addonDescriptorCPE, _ := abaputils.ConstructAddonDescriptorFromJSON([]byte(config.AddonDescriptor))
    50  	if len(addonDescriptorCPE.AddonProduct) == 0 {
    51  		log.Entry().Info("No Product Version information present yet in the addonDescriptor of CommonPipelineEnvironment")
    52  	} else {
    53  		log.Entry().Infof("Information for Product Version %s taken from addonDescriptor of CommonPipelineEnvironment", addonDescriptorCPE.AddonProduct)
    54  	}
    55  	addonDescriptorCPE.SetRepositories(addonDescriptor.Repositories)
    56  	cpe.abap.addonDescriptor = string(addonDescriptorCPE.AsJSON())
    57  	log.Entry().Info("Wrote addonDescriptor to CommonPipelineEnvironment")
    58  	return nil
    59  }
    60  
    61  //take the product part from CPE and the repositories part from the YAML file
    62  func combineYAMLRepositoriesWithCPEProduct(addonDescriptor abaputils.AddonDescriptor, addonDescriptorFromCPE abaputils.AddonDescriptor) abaputils.AddonDescriptor {
    63  	addonDescriptorFromCPE.Repositories = addonDescriptor.Repositories
    64  	return addonDescriptorFromCPE
    65  }
    66  
    67  func (c *componentVersion) initCV(repo abaputils.Repository, conn abapbuild.Connector) {
    68  	c.Connector = conn
    69  	c.Name = repo.Name
    70  	c.VersionYAML = repo.VersionYAML
    71  	c.CommitID = repo.CommitID
    72  }
    73  
    74  func (c *componentVersion) copyFieldsToRepo(initialRepo *abaputils.Repository) {
    75  	initialRepo.Version = c.Version
    76  	initialRepo.SpLevel = c.SpLevel
    77  	initialRepo.PatchLevel = c.PatchLevel
    78  }
    79  
    80  func (c *componentVersion) validate() error {
    81  	log.Entry().Infof("Validate component %s version %s and resolve version", c.Name, c.VersionYAML)
    82  	appendum := "/odata/aas_ocs_package/ValidateComponentVersion?Name='" + url.QueryEscape(c.Name) + "'&Version='" + url.QueryEscape(c.VersionYAML) + "'"
    83  	body, err := c.Connector.Get(appendum)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	var jCV jsonComponentVersion
    88  	if err := json.Unmarshal(body, &jCV); err != nil {
    89  		return errors.Wrap(err, "Unexpected AAKaaS response for Validate Component Version: "+string(body))
    90  	}
    91  	c.Name = jCV.ComponentVersion.Name
    92  	c.Version = jCV.ComponentVersion.Version
    93  	c.SpLevel = jCV.ComponentVersion.SpLevel
    94  	c.PatchLevel = jCV.ComponentVersion.PatchLevel
    95  	log.Entry().Infof("Resolved version %s, splevel %s, patchlevel %s", c.Version, c.SpLevel, c.PatchLevel)
    96  
    97  	if c.CommitID == "" {
    98  		return fmt.Errorf("CommitID missing in repo '%s' of the addon.yml", c.Name)
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  type jsonComponentVersion struct {
   105  	ComponentVersion *componentVersion `json:"d"`
   106  }
   107  
   108  type componentVersion struct {
   109  	abapbuild.Connector
   110  	Name        string `json:"Name"`
   111  	VersionYAML string
   112  	Version     string `json:"Version"`
   113  	SpLevel     string `json:"SpLevel"`
   114  	PatchLevel  string `json:"PatchLevel"`
   115  	CommitID    string
   116  }