2022-05-23 15:15:22 +02:00
package cmd
import (
"fmt"
"strings"
"time"
"github.com/SAP/jenkins-library/pkg/abaputils"
"github.com/SAP/jenkins-library/pkg/command"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
)
2023-11-28 13:26:31 +01:00
func abapEnvironmentCreateTag ( config abapEnvironmentCreateTagOptions , _ * telemetry . CustomData ) {
2022-05-23 15:15:22 +02:00
c := command . Command { }
c . Stdout ( log . Writer ( ) )
c . Stderr ( log . Writer ( ) )
var autils = abaputils . AbapUtils {
Exec : & c ,
}
2023-11-28 13:26:31 +01:00
apiManager := abaputils . SoftwareComponentApiManager {
Client : & piperhttp . Client { } ,
PollIntervall : 5 * time . Second ,
}
2022-05-23 15:15:22 +02:00
2023-11-28 13:26:31 +01:00
if err := runAbapEnvironmentCreateTag ( & config , & autils , & apiManager ) ; err != nil {
2022-05-23 15:15:22 +02:00
log . Entry ( ) . WithError ( err ) . Fatal ( "step execution failed" )
}
}
2023-11-28 13:26:31 +01:00
func runAbapEnvironmentCreateTag ( config * abapEnvironmentCreateTagOptions , com abaputils . Communication , apiManager abaputils . SoftwareComponentApiManagerInterface ) error {
2022-05-23 15:15:22 +02:00
connectionDetails , errorGetInfo := com . GetAbapCommunicationArrangementInfo ( convertTagConfig ( config ) , "" )
if errorGetInfo != nil {
return errors . Wrap ( errorGetInfo , "Parameters for the ABAP Connection not available" )
}
backlog , errorPrepare := prepareBacklog ( config )
if errorPrepare != nil {
return fmt . Errorf ( "Something failed during the tag creation: %w" , errorPrepare )
}
2023-11-28 13:26:31 +01:00
return createTags ( backlog , connectionDetails , apiManager )
2022-05-23 15:15:22 +02:00
}
2023-11-28 13:26:31 +01:00
func createTags ( backlog [ ] abaputils . CreateTagBacklog , con abaputils . ConnectionDetailsHTTP , apiManager abaputils . SoftwareComponentApiManagerInterface ) ( err error ) {
2022-05-23 15:15:22 +02:00
errorOccurred := false
for _ , item := range backlog {
2023-11-28 13:26:31 +01:00
err = createTagsForSingleItem ( item , con , apiManager )
2022-05-23 15:15:22 +02:00
if err != nil {
errorOccurred = true
}
}
if errorOccurred {
message := "At least one tag has not been created"
log . Entry ( ) . Errorf ( message )
return errors . New ( message )
}
return nil
}
2023-11-28 13:26:31 +01:00
func createTagsForSingleItem ( item abaputils . CreateTagBacklog , con abaputils . ConnectionDetailsHTTP , apiManager abaputils . SoftwareComponentApiManagerInterface ) ( err error ) {
2022-05-23 15:15:22 +02:00
errorOccurred := false
2023-11-28 13:26:31 +01:00
for index := range item . Tags {
err = createSingleTag ( item , index , con , apiManager )
2022-05-23 15:15:22 +02:00
if err != nil {
errorOccurred = true
}
}
if errorOccurred {
message := "At least one tag has not been created"
err = errors . New ( message )
}
return err
}
2023-11-28 13:26:31 +01:00
func createSingleTag ( item abaputils . CreateTagBacklog , index int , con abaputils . ConnectionDetailsHTTP , apiManager abaputils . SoftwareComponentApiManagerInterface ) ( err error ) {
2022-05-23 15:15:22 +02:00
2023-11-28 13:26:31 +01:00
api , errGetAPI := apiManager . GetAPI ( con , abaputils . Repository { Name : item . RepositoryName , CommitID : item . CommitID } )
if errGetAPI != nil {
return errors . Wrap ( errGetAPI , "Could not initialize the connection to the system" )
2022-05-23 15:15:22 +02:00
}
2023-11-28 13:26:31 +01:00
createTagError := api . CreateTag ( item . Tags [ index ] )
if createTagError != nil {
return errors . Wrapf ( err , "Creation of Tag failed on the ABAP system" )
2022-05-23 15:15:22 +02:00
}
2023-11-28 13:26:31 +01:00
status , errorPollEntity := abaputils . PollEntity ( api , apiManager . GetPollIntervall ( ) )
2022-05-23 15:15:22 +02:00
2023-11-28 13:26:31 +01:00
if errorPollEntity == nil && status == "S" {
log . Entry ( ) . Info ( "Created tag " + item . Tags [ index ] . TagName + " for repository " + item . RepositoryName + " with commitID " + item . CommitID )
2022-05-23 15:15:22 +02:00
} else {
2023-11-28 13:26:31 +01:00
log . Entry ( ) . Error ( "NOT created: Tag " + item . Tags [ index ] . TagName + " for repository " + item . RepositoryName + " with commitID " + item . CommitID )
err = errors . New ( "Creation of Tag failed on the ABAP system" )
2022-05-23 15:15:22 +02:00
}
return err
}
2023-11-28 13:26:31 +01:00
func prepareBacklog ( config * abapEnvironmentCreateTagOptions ) ( backlog [ ] abaputils . CreateTagBacklog , err error ) {
2022-05-23 15:15:22 +02:00
if config . Repositories != "" && config . RepositoryName != "" {
return nil , errors . New ( "Configuring the parameter repositories and the parameter repositoryName at the same time is not allowed" )
}
if config . RepositoryName != "" && config . CommitID != "" {
2023-11-28 13:26:31 +01:00
backlog = append ( backlog , abaputils . CreateTagBacklog { RepositoryName : config . RepositoryName , CommitID : config . CommitID } )
2022-05-23 15:15:22 +02:00
}
if config . Repositories != "" {
descriptor , err := abaputils . ReadAddonDescriptor ( config . Repositories ) //config.Repositories should contain a file name
if err != nil {
return nil , err
}
for _ , repo := range descriptor . Repositories {
2023-11-28 13:26:31 +01:00
backlogInstance := abaputils . CreateTagBacklog { RepositoryName : repo . Name , CommitID : repo . CommitID }
2022-05-23 15:15:22 +02:00
if config . GenerateTagForAddonComponentVersion && repo . VersionYAML != "" {
2023-11-28 13:26:31 +01:00
tag := abaputils . Tag { TagName : "v" + repo . VersionYAML , TagDescription : "Generated by the ABAP Environment Pipeline" }
backlogInstance . Tags = append ( backlogInstance . Tags , tag )
2022-05-23 15:15:22 +02:00
}
backlog = append ( backlog , backlogInstance )
}
if config . GenerateTagForAddonProductVersion {
if descriptor . AddonProduct != "" && descriptor . AddonVersionYAML != "" {
addonProductDash := strings . Replace ( descriptor . AddonProduct , "/" , "-" , 2 )
backlog = addTagToList ( backlog , addonProductDash + "-" + descriptor . AddonVersionYAML , "Generated by the ABAP Environment Pipeline" )
} else {
log . Entry ( ) . WithField ( "generateTagForAddonProductVersion" , config . GenerateTagForAddonProductVersion ) . WithField ( "AddonProduct" , descriptor . AddonProduct ) . WithField ( "AddonVersion" , descriptor . AddonVersionYAML ) . Infof ( "Not all required values are provided to create an addon product version tag" )
}
}
}
if config . TagName != "" {
backlog = addTagToList ( backlog , config . TagName , config . TagDescription )
}
return backlog , nil
}
2023-11-28 13:26:31 +01:00
func addTagToList ( backlog [ ] abaputils . CreateTagBacklog , tag string , description string ) [ ] abaputils . CreateTagBacklog {
2022-05-23 15:15:22 +02:00
for i , item := range backlog {
2023-11-28 13:26:31 +01:00
tag := abaputils . Tag { TagName : tag , TagDescription : description }
backlog [ i ] . Tags = append ( item . Tags , tag )
2022-05-23 15:15:22 +02:00
}
return backlog
}
func convertTagConfig ( config * abapEnvironmentCreateTagOptions ) abaputils . AbapEnvironmentOptions {
subOptions := abaputils . AbapEnvironmentOptions { }
subOptions . CfAPIEndpoint = config . CfAPIEndpoint
subOptions . CfServiceInstance = config . CfServiceInstance
subOptions . CfServiceKeyName = config . CfServiceKeyName
subOptions . CfOrg = config . CfOrg
subOptions . CfSpace = config . CfSpace
subOptions . Host = config . Host
subOptions . Password = config . Password
subOptions . Username = config . Username
return subOptions
}