github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/abapAddonAssemblyKitCheckCVs.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/SAP/jenkins-library/pkg/abap/aakaas"
     5  	abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
     6  	"github.com/SAP/jenkins-library/pkg/abaputils"
     7  	"github.com/SAP/jenkins-library/pkg/log"
     8  	"github.com/SAP/jenkins-library/pkg/telemetry"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  func abapAddonAssemblyKitCheckCVs(config abapAddonAssemblyKitCheckCVsOptions, telemetryData *telemetry.CustomData, cpe *abapAddonAssemblyKitCheckCVsCommonPipelineEnvironment) {
    13  	utils := aakaas.NewAakBundle()
    14  	if err := runAbapAddonAssemblyKitCheckCVs(&config, telemetryData, &utils, cpe); err != nil {
    15  		log.Entry().WithError(err).Fatal("step execution failed")
    16  	}
    17  }
    18  
    19  func runAbapAddonAssemblyKitCheckCVs(config *abapAddonAssemblyKitCheckCVsOptions, telemetryData *telemetry.CustomData, utils *aakaas.AakUtils, cpe *abapAddonAssemblyKitCheckCVsCommonPipelineEnvironment) error {
    20  
    21  	log.Entry().Info("╔══════════════════════════════╗")
    22  	log.Entry().Info("║ abapAddonAssemblyKitCheckCVs ║")
    23  	log.Entry().Info("╚══════════════════════════════╝")
    24  
    25  	conn := new(abapbuild.Connector)
    26  	if err := conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, *utils); err != nil {
    27  		return err
    28  	}
    29  
    30  	log.Entry().Infof("Reading Product Version Information from addonDescriptor (aka addon.yml) file: %s", config.AddonDescriptorFileName)
    31  	addonDescriptor, err := (*utils).ReadAddonDescriptor(config.AddonDescriptorFileName)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	for i, repo := range addonDescriptor.Repositories {
    37  		componentVersion := new(aakaas.ComponentVersion)
    38  		if err := componentVersion.ConstructComponentVersion(addonDescriptor.Repositories[i], *conn); err != nil {
    39  			return err
    40  		}
    41  		if err := componentVersion.Validate(); err != nil {
    42  			return err
    43  		}
    44  		componentVersion.CopyVersionFieldsToRepo(&addonDescriptor.Repositories[i])
    45  
    46  		log.Entry().Infof("Using cCTS %t", repo.UseClassicCTS)
    47  		log.Entry().Infof("CommitId %s", repo.CommitID)
    48  
    49  		if !repo.UseClassicCTS && repo.CommitID == "" {
    50  			return errors.Errorf("CommitID missing in repo '%s' of the addon.yml", repo.Name)
    51  		}
    52  	}
    53  
    54  	// now Software Component Versions fields are valid, but maybe Product Version was checked before, so copy that part from CPE
    55  	// we don't care for errors
    56  	// scenario 1: config.AddonDescriptor is empty since checkCVs is the first step in the pipeline, then the empty result is fine anyway
    57  	// 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
    58  	addonDescriptorCPE, _ := abaputils.ConstructAddonDescriptorFromJSON([]byte(config.AddonDescriptor))
    59  	if len(addonDescriptorCPE.AddonProduct) == 0 {
    60  		log.Entry().Info("No Product Version information present yet in the addonDescriptor of CommonPipelineEnvironment")
    61  	} else {
    62  		log.Entry().Infof("Information for Product Version %s taken from addonDescriptor of CommonPipelineEnvironment", addonDescriptorCPE.AddonProduct)
    63  	}
    64  	addonDescriptorCPE.SetRepositories(addonDescriptor.Repositories)
    65  	cpe.abap.addonDescriptor = string(addonDescriptorCPE.AsJSON())
    66  	log.Entry().Info("Wrote addonDescriptor to CommonPipelineEnvironment")
    67  	return nil
    68  }
    69  
    70  // take the product part from CPE and the repositories part from the YAML file
    71  func combineYAMLRepositoriesWithCPEProduct(addonDescriptor abaputils.AddonDescriptor, addonDescriptorFromCPE abaputils.AddonDescriptor) abaputils.AddonDescriptor {
    72  	addonDescriptorFromCPE.Repositories = addonDescriptor.Repositories
    73  	return addonDescriptorFromCPE
    74  }