2020-09-17 15:56:14 +02:00
package cmd
import (
"encoding/json"
2020-12-16 12:19:48 +02:00
"net/url"
2020-09-17 15:56:14 +02:00
2021-12-09 13:54:18 +02:00
"github.com/SAP/jenkins-library/pkg/abap/aakaas"
2020-09-17 15:56:14 +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"
2021-11-02 12:27:58 +02:00
"github.com/SAP/jenkins-library/pkg/piperutils"
2020-09-17 15:56:14 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
2021-12-09 13:54:18 +02:00
"github.com/pkg/errors"
2020-09-17 15:56:14 +02:00
)
func abapAddonAssemblyKitCheckPV ( config abapAddonAssemblyKitCheckPVOptions , telemetryData * telemetry . CustomData , cpe * abapAddonAssemblyKitCheckPVCommonPipelineEnvironment ) {
2021-12-09 13:54:18 +02:00
utils := aakaas . NewAakBundle ( )
2020-09-17 15:56:14 +02:00
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
2022-08-09 10:57:02 +02:00
if err := runAbapAddonAssemblyKitCheckPV ( & config , telemetryData , utils , cpe ) ; err != nil {
2020-09-17 15:56:14 +02:00
log . Entry ( ) . WithError ( err ) . Fatal ( "step execution failed" )
}
}
2022-08-09 10:57:02 +02:00
func runAbapAddonAssemblyKitCheckPV ( config * abapAddonAssemblyKitCheckPVOptions , telemetryData * telemetry . CustomData , utils aakaas . AakUtils , cpe * abapAddonAssemblyKitCheckPVCommonPipelineEnvironment ) error {
2020-09-30 10:30:53 +02:00
conn := new ( abapbuild . Connector )
2022-08-09 10:57:02 +02:00
if err := conn . InitAAKaaS ( config . AbapAddonAssemblyKitEndpoint , config . Username , config . Password , utils ) ; err != nil {
2021-12-09 13:54:18 +02:00
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 )
2022-08-09 10:57:02 +02:00
addonDescriptor , err := utils . ReadAddonDescriptor ( config . AddonDescriptorFileName )
2020-09-17 15:56:14 +02:00
if err != nil {
return err
}
2020-09-30 10:30:53 +02:00
pv := new ( productVersion ) . init ( addonDescriptor , * conn )
err = pv . validateAndResolveVersionFields ( )
2020-09-17 15:56:14 +02:00
if err != nil {
return err
}
2020-09-30 10:30:53 +02:00
pv . transferVersionFields ( & addonDescriptor )
// now Product Version fields are valid, but maybe Component Versions (Repositories) were checked before, so copy that part from CPE
// we don't care for errors
// scenario 1: config.AddonDescriptor is empty since checkPV 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 . Repositories ) == 0 {
log . Entry ( ) . Info ( "No Software Component Information present yet in the addonDescriptor of CommonPipelineEnvironment" )
} else {
log . Entry ( ) . Infof ( "Information for %v Software Component Repositories taken from addonDescriptor of CommonPipelineEnvironment" , len ( addonDescriptorCPE . Repositories ) )
}
addonDescriptor . SetRepositories ( addonDescriptorCPE . Repositories )
cpe . abap . addonDescriptor = string ( addonDescriptor . AsJSON ( ) )
log . Entry ( ) . Info ( "Wrote addonDescriptor to CommonPipelineEnvironment" )
2021-11-02 12:27:58 +02:00
var filesToPublish [ ] piperutils . Path
log . Entry ( ) . Infof ( "Add %s to be published" , config . AddonDescriptorFileName )
filesToPublish = append ( filesToPublish , piperutils . Path { Target : config . AddonDescriptorFileName , Name : "AddonDescriptor" , Mandatory : true } )
2022-08-09 10:57:02 +02:00
log . Entry ( ) . Infof ( "Publishing %v files" , len ( filesToPublish ) )
if err := piperutils . PersistReportsAndLinks ( "abapAddonAssemblyKitCheckPV" , "" , utils , filesToPublish , nil ) ; err != nil {
log . Entry ( ) . WithError ( err ) . Error ( "failed to persist report information" )
}
2021-11-02 12:27:58 +02:00
2020-09-17 15:56:14 +02:00
return nil
}
2020-09-30 10:30:53 +02:00
func ( p * productVersion ) init ( desc abaputils . AddonDescriptor , conn abapbuild . Connector ) * productVersion {
2020-09-17 15:56:14 +02:00
p . Connector = conn
p . Name = desc . AddonProduct
p . VersionYAML = desc . AddonVersionYAML
2020-09-30 10:30:53 +02:00
return p
2020-09-17 15:56:14 +02:00
}
2020-09-30 10:30:53 +02:00
func ( p * productVersion ) transferVersionFields ( initialAddonDescriptor * abaputils . AddonDescriptor ) {
2020-09-17 15:56:14 +02:00
initialAddonDescriptor . AddonVersion = p . Version
initialAddonDescriptor . AddonSpsLevel = p . SpsLevel
initialAddonDescriptor . AddonPatchLevel = p . PatchLevel
}
2020-09-30 10:30:53 +02:00
func ( p * productVersion ) validateAndResolveVersionFields ( ) error {
2020-12-16 12:19:48 +02:00
log . Entry ( ) . Infof ( "Validate product '%s' version '%s' and resolve version" , p . Name , p . VersionYAML )
appendum := "/odata/aas_ocs_package/ValidateProductVersion?Name='" + url . QueryEscape ( p . Name ) + "'&Version='" + url . QueryEscape ( p . VersionYAML ) + "'"
2020-09-17 15:56:14 +02:00
body , err := p . Connector . Get ( appendum )
if err != nil {
return err
}
var jPV jsonProductVersion
2021-12-09 13:54:18 +02:00
if err := json . Unmarshal ( body , & jPV ) ; err != nil {
return errors . Wrap ( err , "Unexpected AAKaaS response for Validate Product Version: " + string ( body ) )
}
2020-09-17 15:56:14 +02:00
p . Name = jPV . ProductVersion . Name
p . Version = jPV . ProductVersion . Version
p . SpsLevel = jPV . ProductVersion . SpsLevel
p . PatchLevel = jPV . ProductVersion . PatchLevel
log . Entry ( ) . Infof ( "Resolved version %s, spslevel %s, patchlevel %s" , p . Version , p . SpsLevel , p . PatchLevel )
return nil
}
type jsonProductVersion struct {
ProductVersion * productVersion ` json:"d" `
}
type productVersion struct {
abapbuild . Connector
Name string ` json:"Name" `
VersionYAML string
Version string ` json:"Version" `
SpsLevel string ` json:"SpsLevel" `
PatchLevel string ` json:"PatchLevel" `
TargetVectorID string
}