2020-02-04 12:43:27 +02:00
package cmd
import (
2020-10-05 14:38:35 +02:00
"fmt"
2020-02-04 12:43:27 +02:00
"time"
2020-07-23 10:26:50 +02:00
"github.com/SAP/jenkins-library/pkg/abaputils"
2020-02-04 12:43:27 +02:00
"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"
)
2021-08-04 17:31:16 +02:00
func abapEnvironmentPullGitRepo ( options abapEnvironmentPullGitRepoOptions , _ * telemetry . CustomData ) {
2020-07-31 14:43:23 +02:00
// for command execution use Command
c := command . Command { }
// reroute command output to logging framework
c . Stdout ( log . Writer ( ) )
c . Stderr ( log . Writer ( ) )
var autils = abaputils . AbapUtils {
Exec : & c ,
}
2023-11-28 14:26:31 +02:00
apiManager := abaputils . SoftwareComponentApiManager {
Client : & piperhttp . Client { } ,
PollIntervall : 5 * time . Second ,
}
2020-07-31 14:43:23 +02:00
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
2023-11-28 14:26:31 +02:00
err := runAbapEnvironmentPullGitRepo ( & options , & autils , & apiManager )
2020-07-31 14:43:23 +02:00
if err != nil {
log . Entry ( ) . WithError ( err ) . Fatal ( "step execution failed" )
}
}
2023-11-28 14:26:31 +02:00
func runAbapEnvironmentPullGitRepo ( options * abapEnvironmentPullGitRepoOptions , com abaputils . Communication , apiManager abaputils . SoftwareComponentApiManagerInterface ) ( err error ) {
2020-07-23 10:26:50 +02:00
2020-11-02 15:17:13 +02:00
subOptions := convertPullConfig ( options )
2020-07-23 10:26:50 +02:00
2020-04-08 12:43:41 +02:00
// Determine the host, user and password, either via the input parameters or via a cloud foundry service key
2023-11-28 14:26:31 +02:00
connectionDetails , err := com . GetAbapCommunicationArrangementInfo ( subOptions , "" )
2020-10-05 14:38:35 +02:00
if err != nil {
return errors . Wrap ( err , "Parameters for the ABAP Connection not available" )
2020-02-04 12:43:27 +02:00
}
2022-07-06 14:29:04 +02:00
var repositories [ ] abaputils . Repository
2020-10-05 14:38:35 +02:00
err = checkPullRepositoryConfiguration ( * options )
2021-12-20 18:58:58 +02:00
if err != nil {
return err
2020-10-05 14:38:35 +02:00
}
2022-06-28 11:02:15 +02:00
repositories , err = abaputils . GetRepositories ( & abaputils . RepositoriesConfig { RepositoryNames : options . RepositoryNames , Repositories : options . Repositories , RepositoryName : options . RepositoryName , CommitID : options . CommitID } , false )
2021-12-20 18:58:58 +02:00
handleIgnoreCommit ( repositories , options . IgnoreCommit )
2020-10-05 14:38:35 +02:00
if err != nil {
2021-12-20 18:58:58 +02:00
return err
2020-05-07 15:51:11 +02:00
}
2020-10-05 14:38:35 +02:00
2023-11-28 14:26:31 +02:00
err = pullRepositories ( repositories , connectionDetails , apiManager )
2020-10-05 14:38:35 +02:00
return err
2021-12-20 18:58:58 +02:00
2020-10-05 14:38:35 +02:00
}
2023-11-28 14:26:31 +02:00
func pullRepositories ( repositories [ ] abaputils . Repository , pullConnectionDetails abaputils . ConnectionDetailsHTTP , apiManager abaputils . SoftwareComponentApiManagerInterface ) ( err error ) {
2020-11-02 15:17:13 +02:00
log . Entry ( ) . Infof ( "Start pulling %v repositories" , len ( repositories ) )
2020-10-05 14:38:35 +02:00
for _ , repo := range repositories {
2023-11-28 14:26:31 +02:00
err = handlePull ( repo , pullConnectionDetails , apiManager )
2020-10-05 14:38:35 +02:00
if err != nil {
break
}
2020-11-18 12:18:06 +02:00
}
if err == nil {
2020-10-05 14:38:35 +02:00
finishPullLogs ( )
}
return err
2020-02-04 12:43:27 +02:00
}
2023-11-28 14:26:31 +02:00
func handlePull ( repo abaputils . Repository , con abaputils . ConnectionDetailsHTTP , apiManager abaputils . SoftwareComponentApiManagerInterface ) ( err error ) {
2020-11-02 15:17:13 +02:00
2021-12-20 18:58:58 +02:00
logString := repo . GetPullLogString ( )
errorString := "Pull of the " + logString + " failed on the ABAP system"
2020-11-02 15:17:13 +02:00
2023-11-28 14:26:31 +02:00
abaputils . AddDefaultDashedLine ( 1 )
2021-12-20 18:58:58 +02:00
log . Entry ( ) . Info ( "Start pulling the " + logString )
2023-11-28 14:26:31 +02:00
abaputils . AddDefaultDashedLine ( 1 )
api , errGetAPI := apiManager . GetAPI ( con , repo )
if errGetAPI != nil {
return errors . Wrap ( errGetAPI , "Could not initialize the connection to the system" )
}
2020-11-02 15:17:13 +02:00
2023-11-28 14:26:31 +02:00
err = api . Pull ( )
2020-11-02 15:17:13 +02:00
if err != nil {
2021-12-20 18:58:58 +02:00
return errors . Wrapf ( err , errorString )
2020-11-02 15:17:13 +02:00
}
// Polling the status of the repository import on the ABAP Environment system
2023-11-28 14:26:31 +02:00
status , errorPollEntity := abaputils . PollEntity ( api , apiManager . GetPollIntervall ( ) )
2020-11-02 15:17:13 +02:00
if errorPollEntity != nil {
2021-12-20 18:58:58 +02:00
return errors . Wrapf ( errorPollEntity , errorString )
2020-11-02 15:17:13 +02:00
}
if status == "E" {
2021-12-20 18:58:58 +02:00
return errors . New ( errorString )
2020-11-02 15:17:13 +02:00
}
log . Entry ( ) . Info ( repo . Name + " was pulled successfully" )
return err
}
2020-10-05 14:38:35 +02:00
func checkPullRepositoryConfiguration ( options abapEnvironmentPullGitRepoOptions ) error {
2022-06-28 11:02:15 +02:00
if ( len ( options . RepositoryNames ) > 0 && options . Repositories != "" ) || ( len ( options . RepositoryNames ) > 0 && options . RepositoryName != "" ) || ( options . RepositoryName != "" && options . Repositories != "" ) {
return fmt . Errorf ( "Checking configuration failed: %w" , errors . New ( "Only one of the paramters `RepositoryName`,`RepositoryNames` or `Repositories` may be configured at the same time" ) )
2020-10-05 14:38:35 +02:00
}
2022-06-28 11:02:15 +02:00
if len ( options . RepositoryNames ) == 0 && options . Repositories == "" && options . RepositoryName == "" {
2020-10-05 14:38:35 +02:00
return fmt . Errorf ( "Checking configuration failed: %w" , errors . New ( "You have not specified any repository configuration to be pulled into the ABAP Environment System. Please make sure that you specified the repositories that should be pulled either in a dedicated file or via the parameter 'repositoryNames'. For more information please read the User documentation" ) )
}
return nil
}
func finishPullLogs ( ) {
2023-11-28 14:26:31 +02:00
abaputils . AddDefaultDashedLine ( 1 )
2020-10-05 14:38:35 +02:00
log . Entry ( ) . Info ( "All repositories were pulled successfully" )
}
2020-11-02 15:17:13 +02:00
func convertPullConfig ( config * abapEnvironmentPullGitRepoOptions ) 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
}
func handleIgnoreCommit ( repositories [ ] abaputils . Repository , ignoreCommit bool ) {
for i := range repositories {
if ignoreCommit {
repositories [ i ] . CommitID = ""
}
}
}