2020-02-04 12:43:27 +02:00
package cmd
import (
"encoding/json"
"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"
)
2020-07-31 14:43:23 +02:00
func abapEnvironmentPullGitRepo ( options abapEnvironmentPullGitRepoOptions , telemetryData * telemetry . CustomData ) {
// 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 { }
// for http calls import piperhttp "github.com/SAP/jenkins-library/pkg/http"
// and use a &piperhttp.Client{} in a custom system
// Example: step checkmarxExecuteScan.go
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
err := runAbapEnvironmentPullGitRepo ( & options , telemetryData , & autils , & client )
if err != nil {
log . Entry ( ) . WithError ( err ) . Fatal ( "step execution failed" )
}
}
func runAbapEnvironmentPullGitRepo ( options * abapEnvironmentPullGitRepoOptions , telemetryData * telemetry . CustomData , com abaputils . Communication , client piperhttp . Sender ) error {
2020-07-23 10:26:50 +02:00
// Mapping for options
subOptions := abaputils . AbapEnvironmentOptions { }
subOptions . CfAPIEndpoint = options . CfAPIEndpoint
subOptions . CfServiceInstance = options . CfServiceInstance
subOptions . CfServiceKeyName = options . CfServiceKeyName
subOptions . CfOrg = options . CfOrg
subOptions . CfSpace = options . CfSpace
subOptions . Host = options . Host
subOptions . Password = options . Password
subOptions . Username = options . Username
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-07-31 14:43:23 +02:00
connectionDetails , errorGetInfo := com . GetAbapCommunicationArrangementInfo ( subOptions , "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull" )
2020-04-08 12:43:41 +02:00
if errorGetInfo != nil {
2020-07-31 14:43:23 +02:00
return errors . Wrap ( errorGetInfo , "Parameters for the ABAP Connection not available" )
2020-02-04 12:43:27 +02:00
}
2020-04-08 12:43:41 +02:00
// Configuring the HTTP Client and CookieJar
2020-07-31 14:43:23 +02:00
2020-04-08 12:43:41 +02:00
cookieJar , errorCookieJar := cookiejar . New ( nil )
if errorCookieJar != nil {
2020-07-31 14:43:23 +02:00
return errors . Wrap ( errorCookieJar , "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-07-23 10:26:50 +02:00
log . Entry ( ) . Infof ( "Start pulling %v repositories" , len ( options . RepositoryNames ) )
for _ , repositoryName := range options . RepositoryNames {
2020-02-04 12:43:27 +02:00
2020-05-07 15:51:11 +02:00
log . Entry ( ) . Info ( "-------------------------" )
log . Entry ( ) . Info ( "Start pulling " + repositoryName )
log . Entry ( ) . Info ( "-------------------------" )
2020-02-04 12:43:27 +02:00
2020-05-07 15:51:11 +02:00
// Triggering the Pull of the repository into the ABAP Environment system
2020-07-31 14:43:23 +02:00
uriConnectionDetails , errorTriggerPull := triggerPull ( repositoryName , connectionDetails , client )
2020-05-07 15:51:11 +02:00
if errorTriggerPull != nil {
2020-07-31 14:43:23 +02:00
return errors . Wrapf ( errorTriggerPull , "Pull of '%s' failed on the ABAP System" , repositoryName )
2020-05-07 15:51:11 +02:00
}
// Polling the status of the repository import on the ABAP Environment system
2020-08-04 17:52:28 +02:00
status , errorPollEntity := abaputils . PollEntity ( repositoryName , uriConnectionDetails , client , pollIntervall )
2020-05-07 15:51:11 +02:00
if errorPollEntity != nil {
2020-07-31 14:43:23 +02:00
return errors . Wrapf ( errorPollEntity , "Pull of '%s' failed on the ABAP System" , repositoryName )
2020-05-07 15:51:11 +02:00
}
if status == "E" {
2020-07-31 14:43:23 +02:00
return errors . New ( "Pull of " + repositoryName + " failed on the ABAP System" )
2020-05-07 15:51:11 +02:00
}
log . Entry ( ) . Info ( repositoryName + " was pulled successfully" )
}
log . Entry ( ) . Info ( "-------------------------" )
log . Entry ( ) . Info ( "All repositories were pulled successfully" )
2020-02-04 12:43:27 +02:00
return nil
}
2020-07-23 10:26:50 +02:00
func triggerPull ( repositoryName string , 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-02-04 12:43:27 +02:00
log . Entry ( ) . WithField ( "StatusCode" , resp . Status ) . WithField ( "ABAP Endpoint" , pullConnectionDetails . URL ) . Info ( "Authentication on the ABAP system successfull" )
uriConnectionDetails . XCsrfToken = resp . Header . Get ( "X-Csrf-Token" )
pullConnectionDetails . XCsrfToken = uriConnectionDetails . XCsrfToken
// Trigger the Pull of a Repository
2020-05-07 15:51:11 +02:00
if repositoryName == "" {
2020-04-08 12:43:41 +02:00
return uriConnectionDetails , errors . New ( "An empty string was passed for the parameter 'repositoryName'" )
}
2020-05-07 15:51:11 +02:00
jsonBody := [ ] byte ( ` { "sc_name":" ` + repositoryName + ` "} ` )
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-08-04 17:52:28 +02:00
err = abaputils . HandleHTTPError ( resp , err , "Could not pull the Repository / Software Component " + repositoryName , 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-05-07 15:51:11 +02:00
log . Entry ( ) . WithField ( "StatusCode" , resp . Status ) . WithField ( "repositoryName" , repositoryName ) . Info ( "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-05-07 15:51:11 +02:00
log . Entry ( ) . WithField ( "StatusCode" , resp . Status ) . WithField ( "repositoryName" , repositoryName ) . 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
}