2020-09-17 11:01:19 +02:00
package cmd
import (
"encoding/json"
2021-05-12 19:59:48 +02:00
"fmt"
2021-12-09 13:54:18 +02:00
"net/url"
2020-09-17 11:01:19 +02:00
2021-12-09 13:54:18 +02:00
"github.com/SAP/jenkins-library/pkg/abap/aakaas"
2020-09-17 11:01:19 +02:00
abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
"github.com/SAP/jenkins-library/pkg/abaputils"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
2021-12-09 13:54:18 +02:00
"github.com/pkg/errors"
2020-09-17 11:01:19 +02:00
)
func abapAddonAssemblyKitCheckCVs ( config abapAddonAssemblyKitCheckCVsOptions , telemetryData * telemetry . CustomData , cpe * abapAddonAssemblyKitCheckCVsCommonPipelineEnvironment ) {
2021-12-09 13:54:18 +02:00
utils := aakaas . NewAakBundle ( )
if err := runAbapAddonAssemblyKitCheckCVs ( & config , telemetryData , & utils , cpe ) ; err != nil {
2020-09-17 11:01:19 +02:00
log . Entry ( ) . WithError ( err ) . Fatal ( "step execution failed" )
}
}
2021-12-09 13:54:18 +02:00
func runAbapAddonAssemblyKitCheckCVs ( config * abapAddonAssemblyKitCheckCVsOptions , telemetryData * telemetry . CustomData , utils * aakaas . AakUtils , cpe * abapAddonAssemblyKitCheckCVsCommonPipelineEnvironment ) error {
2020-09-30 10:30:53 +02:00
conn := new ( abapbuild . Connector )
2021-12-09 13:54:18 +02:00
if err := conn . InitAAKaaS ( config . AbapAddonAssemblyKitEndpoint , config . Username , config . Password , * utils ) ; err != nil {
return err
}
2020-09-30 10:30:53 +02:00
log . Entry ( ) . Infof ( "Reading Product Version Information from addonDescriptor (aka addon.yml) file: %s" , config . AddonDescriptorFileName )
2021-12-09 13:54:18 +02:00
addonDescriptor , err := ( * utils ) . ReadAddonDescriptor ( config . AddonDescriptorFileName )
2020-09-17 11:01:19 +02:00
if err != nil {
return err
}
for i := range addonDescriptor . Repositories {
2020-09-17 15:56:14 +02:00
var c componentVersion
2020-09-17 11:01:19 +02:00
c . initCV ( addonDescriptor . Repositories [ i ] , * conn )
err := c . validate ( )
if err != nil {
return err
}
c . copyFieldsToRepo ( & addonDescriptor . Repositories [ i ] )
}
2020-09-30 10:30:53 +02:00
// now Software Component Versions fields are valid, but maybe Product Version was checked before, so copy that part from CPE
// we don't care for errors
// scenario 1: config.AddonDescriptor is empty since checkCVs is the first step in the pipeline, then the empty result is fine anyway
// 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
addonDescriptorCPE , _ := abaputils . ConstructAddonDescriptorFromJSON ( [ ] byte ( config . AddonDescriptor ) )
if len ( addonDescriptorCPE . AddonProduct ) == 0 {
log . Entry ( ) . Info ( "No Product Version information present yet in the addonDescriptor of CommonPipelineEnvironment" )
} else {
log . Entry ( ) . Infof ( "Information for Product Version %s taken from addonDescriptor of CommonPipelineEnvironment" , addonDescriptorCPE . AddonProduct )
}
addonDescriptorCPE . SetRepositories ( addonDescriptor . Repositories )
cpe . abap . addonDescriptor = string ( addonDescriptorCPE . AsJSON ( ) )
log . Entry ( ) . Info ( "Wrote addonDescriptor to CommonPipelineEnvironment" )
2020-09-17 11:01:19 +02:00
return nil
}
//take the product part from CPE and the repositories part from the YAML file
func combineYAMLRepositoriesWithCPEProduct ( addonDescriptor abaputils . AddonDescriptor , addonDescriptorFromCPE abaputils . AddonDescriptor ) abaputils . AddonDescriptor {
addonDescriptorFromCPE . Repositories = addonDescriptor . Repositories
return addonDescriptorFromCPE
}
2020-09-17 15:56:14 +02:00
func ( c * componentVersion ) initCV ( repo abaputils . Repository , conn abapbuild . Connector ) {
2020-09-17 11:01:19 +02:00
c . Connector = conn
c . Name = repo . Name
c . VersionYAML = repo . VersionYAML
2021-05-12 19:59:48 +02:00
c . CommitID = repo . CommitID
2020-09-17 11:01:19 +02:00
}
2020-09-17 15:56:14 +02:00
func ( c * componentVersion ) copyFieldsToRepo ( initialRepo * abaputils . Repository ) {
2020-09-17 11:01:19 +02:00
initialRepo . Version = c . Version
initialRepo . SpLevel = c . SpLevel
initialRepo . PatchLevel = c . PatchLevel
}
2020-09-17 15:56:14 +02:00
func ( c * componentVersion ) validate ( ) error {
2020-09-17 11:01:19 +02:00
log . Entry ( ) . Infof ( "Validate component %s version %s and resolve version" , c . Name , c . VersionYAML )
2021-12-09 13:54:18 +02:00
appendum := "/odata/aas_ocs_package/ValidateComponentVersion?Name='" + url . QueryEscape ( c . Name ) + "'&Version='" + url . QueryEscape ( c . VersionYAML ) + "'"
2020-09-17 11:01:19 +02:00
body , err := c . Connector . Get ( appendum )
if err != nil {
return err
}
2020-09-17 15:56:14 +02:00
var jCV jsonComponentVersion
2021-12-09 13:54:18 +02:00
if err := json . Unmarshal ( body , & jCV ) ; err != nil {
return errors . Wrap ( err , "Unexpected AAKaaS response for Validate Component Version: " + string ( body ) )
}
2020-09-17 15:56:14 +02:00
c . Name = jCV . ComponentVersion . Name
c . Version = jCV . ComponentVersion . Version
c . SpLevel = jCV . ComponentVersion . SpLevel
c . PatchLevel = jCV . ComponentVersion . PatchLevel
2020-09-17 11:01:19 +02:00
log . Entry ( ) . Infof ( "Resolved version %s, splevel %s, patchlevel %s" , c . Version , c . SpLevel , c . PatchLevel )
2021-05-12 19:59:48 +02:00
if c . CommitID == "" {
return fmt . Errorf ( "CommitID missing in repo '%s' of the addon.yml" , c . Name )
}
2020-09-17 11:01:19 +02:00
return nil
}
2020-09-17 15:56:14 +02:00
type jsonComponentVersion struct {
ComponentVersion * componentVersion ` json:"d" `
2020-09-17 11:01:19 +02:00
}
2020-09-17 15:56:14 +02:00
type componentVersion struct {
2020-09-17 11:01:19 +02:00
abapbuild . Connector
Name string ` json:"Name" `
VersionYAML string
Version string ` json:"Version" `
SpLevel string ` json:"SpLevel" `
PatchLevel string ` json:"PatchLevel" `
2021-05-12 19:59:48 +02:00
CommitID string
2020-09-17 11:01:19 +02:00
}