2020-02-04 12:43:27 +02:00
package cmd
import (
"encoding/json"
2020-10-05 14:38:35 +02:00
"fmt"
2020-02-04 12:43:27 +02:00
"io/ioutil"
"net/http/cookiejar"
2020-04-08 12:43:41 +02:00
"reflect"
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 ,
}
client := piperhttp . Client { }
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
2021-08-04 17:31:16 +02:00
err := runAbapEnvironmentPullGitRepo ( & options , & autils , & client )
2020-07-31 14:43:23 +02:00
if err != nil {
log . Entry ( ) . WithError ( err ) . Fatal ( "step execution failed" )
}
}
2021-08-04 17:31:16 +02:00
func runAbapEnvironmentPullGitRepo ( options * abapEnvironmentPullGitRepoOptions , com abaputils . Communication , client piperhttp . Sender ) ( 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
2020-10-05 14:38:35 +02:00
connectionDetails , err := com . GetAbapCommunicationArrangementInfo ( subOptions , "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull" )
if err != nil {
return errors . Wrap ( err , "Parameters for the ABAP Connection not available" )
2020-02-04 12:43:27 +02:00
}
2020-10-05 14:38:35 +02:00
cookieJar , err := cookiejar . New ( nil )
if err != nil {
return errors . Wrap ( err , "Could not create a Cookie Jar" )
2020-04-08 12:43:41 +02:00
}
2020-02-04 12:43:27 +02:00
clientOptions := piperhttp . ClientOptions {
2020-05-28 13:08:26 +02:00
MaxRequestDuration : 180 * time . Second ,
2020-03-23 16:02:22 +02:00
CookieJar : cookieJar ,
Username : connectionDetails . User ,
Password : connectionDetails . Password ,
2020-02-04 12:43:27 +02:00
}
client . SetOptions ( clientOptions )
2020-08-07 11:09:58 +02:00
pollIntervall := com . GetPollIntervall ( )
2020-02-04 12:43:27 +02:00
2020-10-05 14:38:35 +02:00
repositories := [ ] abaputils . Repository { }
err = checkPullRepositoryConfiguration ( * options )
2020-02-04 12:43:27 +02:00
2020-10-05 14:38:35 +02:00
if err == nil {
repositories , err = abaputils . GetRepositories ( & abaputils . RepositoriesConfig { RepositoryNames : options . RepositoryNames , Repositories : options . Repositories } )
2020-11-02 15:17:13 +02:00
handleIgnoreCommit ( repositories , options . IgnoreCommit )
2020-10-05 14:38:35 +02:00
}
2020-05-07 15:51:11 +02:00
2020-10-05 14:38:35 +02:00
if err == nil {
err = pullRepositories ( repositories , connectionDetails , client , pollIntervall )
}
2020-05-07 15:51:11 +02:00
2020-10-05 14:38:35 +02:00
if err != nil {
return fmt . Errorf ( "Something failed during the pull of the repositories: %w" , err )
2020-05-07 15:51:11 +02:00
}
2020-10-05 14:38:35 +02:00
return err
}
func pullRepositories ( repositories [ ] abaputils . Repository , pullConnectionDetails abaputils . ConnectionDetailsHTTP , client piperhttp . Sender , pollIntervall time . Duration ) ( 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 {
err = handlePull ( repo , pullConnectionDetails , client , pollIntervall )
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
}
2020-11-02 15:17:13 +02:00
func handlePull ( repo abaputils . Repository , pullConnectionDetails abaputils . ConnectionDetailsHTTP , client piperhttp . Sender , pollIntervall time . Duration ) ( err error ) {
startPullLogs ( repo )
_ , commitString := abaputils . GetCommitStrings ( repo . CommitID )
uriConnectionDetails , err := triggerPull ( repo , pullConnectionDetails , client )
if err != nil {
return errors . Wrapf ( err , "Pull of '%s'%s failed on the ABAP System" , repo . Name , commitString )
}
// Polling the status of the repository import on the ABAP Environment system
status , errorPollEntity := abaputils . PollEntity ( repo . Name , uriConnectionDetails , client , pollIntervall )
if errorPollEntity != nil {
return errors . Wrapf ( errorPollEntity , "Pull of '%s'%s failed on the ABAP System" , repo . Name , commitString )
}
if status == "E" {
return errors . New ( "Pull of '" + repo . Name + "'" + commitString + " failed on the ABAP System" )
}
log . Entry ( ) . Info ( repo . Name + " was pulled successfully" )
return err
}
func triggerPull ( repo abaputils . Repository , pullConnectionDetails abaputils . ConnectionDetailsHTTP , client piperhttp . Sender ) ( abaputils . ConnectionDetailsHTTP , error ) {
2020-02-04 12:43:27 +02:00
uriConnectionDetails := pullConnectionDetails
uriConnectionDetails . URL = ""
pullConnectionDetails . XCsrfToken = "fetch"
// Loging into the ABAP System - getting the x-csrf-token and cookies
2020-08-04 17:52:28 +02:00
resp , err := abaputils . GetHTTPResponse ( "HEAD" , pullConnectionDetails , nil , client )
2020-02-04 12:43:27 +02:00
if err != nil {
2020-08-04 17:52:28 +02:00
err = abaputils . HandleHTTPError ( resp , err , "Authentication on the ABAP system failed" , pullConnectionDetails )
2020-02-04 12:43:27 +02:00
return uriConnectionDetails , err
}
2020-03-17 11:54:48 +02:00
defer resp . Body . Close ( )
2020-10-08 11:08:58 +02:00
2020-11-02 15:17:13 +02:00
log . Entry ( ) . WithField ( "StatusCode" , resp . Status ) . WithField ( "ABAP Endpoint" , pullConnectionDetails . URL ) . Debug ( "Authentication on the ABAP system successful" )
2020-02-04 12:43:27 +02:00
uriConnectionDetails . XCsrfToken = resp . Header . Get ( "X-Csrf-Token" )
pullConnectionDetails . XCsrfToken = uriConnectionDetails . XCsrfToken
// Trigger the Pull of a Repository
2020-11-02 15:17:13 +02:00
if repo . Name == "" {
2020-04-08 12:43:41 +02:00
return uriConnectionDetails , errors . New ( "An empty string was passed for the parameter 'repositoryName'" )
}
2020-11-02 15:17:13 +02:00
commitQuery , commitString := abaputils . GetCommitStrings ( repo . CommitID )
jsonBody := [ ] byte ( ` { "sc_name":" ` + repo . Name + ` " ` + commitQuery + ` } ` )
2020-08-04 17:52:28 +02:00
resp , err = abaputils . GetHTTPResponse ( "POST" , pullConnectionDetails , jsonBody , client )
2020-02-04 12:43:27 +02:00
if err != nil {
2020-11-02 15:17:13 +02:00
err = abaputils . HandleHTTPError ( resp , err , "Could not pull the Repository / Software Component '" + repo . Name + "'" + commitString , uriConnectionDetails )
2020-02-04 12:43:27 +02:00
return uriConnectionDetails , err
}
2020-03-17 11:54:48 +02:00
defer resp . Body . Close ( )
2020-11-02 15:17:13 +02:00
log . Entry ( ) . WithField ( "StatusCode" , resp . Status ) . WithField ( "repositoryName" , repo . Name ) . WithField ( "commitID" , repo . CommitID ) . Debug ( "Triggered Pull of Repository / Software Component" )
2020-02-04 12:43:27 +02:00
// Parse Response
2020-07-23 10:26:50 +02:00
var body abaputils . PullEntity
2020-02-04 12:43:27 +02:00
var abapResp map [ string ] * json . RawMessage
2020-04-08 12:43:41 +02:00
bodyText , errRead := ioutil . ReadAll ( resp . Body )
if errRead != nil {
return uriConnectionDetails , err
}
2020-02-04 12:43:27 +02:00
json . Unmarshal ( bodyText , & abapResp )
json . Unmarshal ( * abapResp [ "d" ] , & body )
2020-07-23 10:26:50 +02:00
if reflect . DeepEqual ( abaputils . PullEntity { } , body ) {
2020-11-02 15:17:13 +02:00
log . Entry ( ) . WithField ( "StatusCode" , resp . Status ) . WithField ( "repositoryName" , repo . Name ) . WithField ( "commitID" , repo . CommitID ) . Error ( "Could not pull the Repository / Software Component" )
2020-04-08 12:43:41 +02:00
err := errors . New ( "Request to ABAP System not successful" )
2020-02-04 12:43:27 +02:00
return uriConnectionDetails , err
}
2020-04-08 12:43:41 +02:00
expandLog := "?$expand=to_Execution_log,to_Transport_log"
uriConnectionDetails . URL = body . Metadata . URI + expandLog
2020-02-04 12:43:27 +02:00
return uriConnectionDetails , nil
}
2020-10-05 14:38:35 +02:00
func checkPullRepositoryConfiguration ( options abapEnvironmentPullGitRepoOptions ) error {
if len ( options . RepositoryNames ) > 0 && options . Repositories != "" {
log . Entry ( ) . Info ( "It seems like you have specified repositories directly via the configuration parameter 'repositoryNames' as well as in the dedicated repositories configuration file. Please note that in this case both configurations will be handled and pulled." )
}
if len ( options . RepositoryNames ) == 0 && options . Repositories == "" {
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
}
2020-11-02 15:17:13 +02:00
func startPullLogs ( repo abaputils . Repository ) {
_ , commitString := abaputils . GetCommitStrings ( repo . CommitID )
2020-10-05 14:38:35 +02:00
log . Entry ( ) . Info ( "-------------------------" )
2020-11-02 15:17:13 +02:00
log . Entry ( ) . Info ( "Start pulling '" + repo . Name + "'" + commitString )
2020-10-05 14:38:35 +02:00
log . Entry ( ) . Info ( "-------------------------" )
}
func finishPullLogs ( ) {
log . Entry ( ) . Info ( "-------------------------" )
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 = ""
}
}
}