github.com/jaylevin/jenkins-library@v1.230.4/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 c.UseClassicCTS = repo.UseClassicCTS 73 } 74 75 func (c *componentVersion) copyFieldsToRepo(initialRepo *abaputils.Repository) { 76 initialRepo.Version = c.Version 77 initialRepo.SpLevel = c.SpLevel 78 initialRepo.PatchLevel = c.PatchLevel 79 } 80 81 func (c *componentVersion) validate() error { 82 log.Entry().Infof("Validate component %s version %s and resolve version", c.Name, c.VersionYAML) 83 appendum := "/odata/aas_ocs_package/ValidateComponentVersion?Name='" + url.QueryEscape(c.Name) + "'&Version='" + url.QueryEscape(c.VersionYAML) + "'" 84 body, err := c.Connector.Get(appendum) 85 if err != nil { 86 return err 87 } 88 var jCV jsonComponentVersion 89 if err := json.Unmarshal(body, &jCV); err != nil { 90 return errors.Wrap(err, "Unexpected AAKaaS response for Validate Component Version: "+string(body)) 91 } 92 c.Name = jCV.ComponentVersion.Name 93 c.Version = jCV.ComponentVersion.Version 94 c.SpLevel = jCV.ComponentVersion.SpLevel 95 c.PatchLevel = jCV.ComponentVersion.PatchLevel 96 log.Entry().Infof("Resolved version %s, splevel %s, patchlevel %s", c.Version, c.SpLevel, c.PatchLevel) 97 log.Entry().Infof("Using cCTS %t", c.UseClassicCTS) 98 99 if !c.UseClassicCTS && c.CommitID == "" { 100 return fmt.Errorf("CommitID missing in repo '%s' of the addon.yml", c.Name) 101 } 102 103 return nil 104 } 105 106 type jsonComponentVersion struct { 107 ComponentVersion *componentVersion `json:"d"` 108 } 109 110 type componentVersion struct { 111 abapbuild.Connector 112 Name string `json:"Name"` 113 VersionYAML string 114 Version string `json:"Version"` 115 SpLevel string `json:"SpLevel"` 116 PatchLevel string `json:"PatchLevel"` 117 UseClassicCTS bool 118 CommitID string 119 }