2020-08-21 14:49:48 +02:00
package cmd
import (
"encoding/json"
2020-10-05 14:38:35 +02:00
"fmt"
2020-08-21 14:49:48 +02:00
"io/ioutil"
"net/http/cookiejar"
"reflect"
"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"
)
func abapEnvironmentCloneGitRepo ( config abapEnvironmentCloneGitRepoOptions , telemetryData * telemetry . CustomData ) {
c := command . Command { }
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
err := runAbapEnvironmentCloneGitRepo ( & config , telemetryData , & autils , & client )
if err != nil {
log . Entry ( ) . WithError ( err ) . Fatal ( "step execution failed" )
}
}
func runAbapEnvironmentCloneGitRepo ( config * abapEnvironmentCloneGitRepoOptions , telemetryData * telemetry . CustomData , com abaputils . Communication , client piperhttp . Sender ) error {
// Mapping for options
2020-11-02 15:17:13 +02:00
subOptions := convertCloneConfig ( config )
2020-08-21 14:49:48 +02:00
// Determine the host, user and password, either via the input parameters or via a cloud foundry service key
2020-12-08 10:31:08 +02:00
connectionDetails , errorGetInfo := com . GetAbapCommunicationArrangementInfo ( subOptions , "" )
2020-08-21 14:49:48 +02:00
if errorGetInfo != nil {
return errors . Wrap ( errorGetInfo , "Parameters for the ABAP Connection not available" )
}
// Configuring the HTTP Client and CookieJar
cookieJar , errorCookieJar := cookiejar . New ( nil )
if errorCookieJar != nil {
return errors . Wrap ( errorCookieJar , "Could not create a Cookie Jar" )
}
client . SetOptions ( piperhttp . ClientOptions {
MaxRequestDuration : 180 * time . Second ,
CookieJar : cookieJar ,
Username : connectionDetails . User ,
Password : connectionDetails . Password ,
} )
2020-10-05 14:38:35 +02:00
repositories , errGetRepos := abaputils . GetRepositories ( & abaputils . RepositoriesConfig { BranchName : config . BranchName , RepositoryName : config . RepositoryName , Repositories : config . Repositories } )
2020-08-21 14:49:48 +02:00
if errGetRepos != nil {
2020-10-05 14:38:35 +02:00
return fmt . Errorf ( "Something failed during the clone: %w" , errGetRepos )
2020-08-21 14:49:48 +02:00
}
log . Entry ( ) . Infof ( "Start cloning %v repositories" , len ( repositories ) )
for _ , repo := range repositories {
2020-11-02 15:17:13 +02:00
commitString := ""
if repo . CommitID != "" {
commitString = ", commit '" + repo . CommitID + "'"
}
2020-08-21 14:49:48 +02:00
log . Entry ( ) . Info ( "-------------------------" )
2020-11-02 15:17:13 +02:00
log . Entry ( ) . Info ( "Start cloning " + repo . Name + ", branch " + repo . Branch + commitString )
2020-08-21 14:49:48 +02:00
log . Entry ( ) . Info ( "-------------------------" )
// Triggering the Clone of the repository into the ABAP Environment system
2020-11-02 15:17:13 +02:00
uriConnectionDetails , errorTriggerClone := triggerClone ( repo , connectionDetails , client )
2020-08-21 14:49:48 +02:00
if errorTriggerClone != nil {
2020-11-02 15:17:13 +02:00
return errors . Wrapf ( errorTriggerClone , "Clone of '%s', branch '%s'%s failed on the ABAP System" , repo . Name , repo . Branch , commitString )
2020-08-21 14:49:48 +02:00
}
// Polling the status of the repository import on the ABAP Environment system
status , errorPollEntity := abaputils . PollEntity ( repo . Name , uriConnectionDetails , client , com . GetPollIntervall ( ) )
if errorPollEntity != nil {
2020-11-02 15:17:13 +02:00
return errors . Wrapf ( errorPollEntity , "Clone of '%s', branch '%s'%s failed on the ABAP System" , repo . Name , repo . Branch , commitString )
2020-08-21 14:49:48 +02:00
}
if status == "E" {
2020-11-02 15:17:13 +02:00
return errors . New ( "Clone of '" + repo . Name + "', branch '" + repo . Branch + "'" + commitString + " failed on the ABAP System" )
2020-08-21 14:49:48 +02:00
}
2020-11-02 15:17:13 +02:00
log . Entry ( ) . Info ( repo . Name + ", branch " + repo . Branch + commitString + " was cloned successfully" )
2020-08-21 14:49:48 +02:00
}
log . Entry ( ) . Info ( "-------------------------" )
log . Entry ( ) . Info ( "All repositories were cloned successfully" )
return nil
}
2020-11-02 15:17:13 +02:00
func triggerClone ( repo abaputils . Repository , cloneConnectionDetails abaputils . ConnectionDetailsHTTP , client piperhttp . Sender ) ( abaputils . ConnectionDetailsHTTP , error ) {
2020-08-21 14:49:48 +02:00
uriConnectionDetails := cloneConnectionDetails
cloneConnectionDetails . XCsrfToken = "fetch"
2020-12-08 10:31:08 +02:00
cloneConnectionDetails . URL = cloneConnectionDetails . URL + "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Clones"
2020-08-21 14:49:48 +02:00
// Loging into the ABAP System - getting the x-csrf-token and cookies
resp , err := abaputils . GetHTTPResponse ( "HEAD" , cloneConnectionDetails , nil , client )
if err != nil {
err = abaputils . HandleHTTPError ( resp , err , "Authentication on the ABAP system failed" , cloneConnectionDetails )
return uriConnectionDetails , err
}
defer resp . Body . Close ( )
2020-10-08 11:08:58 +02:00
// workaround until golang version 1.16 is used
2020-11-02 15:17:13 +02:00
time . Sleep ( 100 * time . Millisecond )
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" , cloneConnectionDetails . URL ) . Debug ( "Authentication on the ABAP system successful" )
2020-08-21 14:49:48 +02:00
uriConnectionDetails . XCsrfToken = resp . Header . Get ( "X-Csrf-Token" )
cloneConnectionDetails . XCsrfToken = uriConnectionDetails . XCsrfToken
// Trigger the Clone of a Repository
2020-11-02 15:17:13 +02:00
if repo . Name == "" {
2020-08-21 14:49:48 +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 + ` ", "branch_name":" ` + repo . Branch + ` " ` + commitQuery + ` } ` )
2020-08-21 14:49:48 +02:00
resp , err = abaputils . GetHTTPResponse ( "POST" , cloneConnectionDetails , jsonBody , client )
if err != nil {
2020-11-02 15:17:13 +02:00
err = abaputils . HandleHTTPError ( resp , err , "Could not clone the Repository / Software Component " + repo . Name + ", branch " + repo . Branch + commitString , uriConnectionDetails )
2020-08-21 14:49:48 +02:00
return uriConnectionDetails , err
}
defer resp . Body . Close ( )
2020-11-02 15:17:13 +02:00
log . Entry ( ) . WithField ( "StatusCode" , resp . Status ) . WithField ( "repositoryName" , repo . Name ) . WithField ( "branchName" , repo . Branch ) . WithField ( "commitID" , repo . CommitID ) . Info ( "Triggered Clone of Repository / Software Component" )
2020-08-21 14:49:48 +02:00
// Parse Response
var body abaputils . CloneEntity
var abapResp map [ string ] * json . RawMessage
bodyText , errRead := ioutil . ReadAll ( resp . Body )
if errRead != nil {
return uriConnectionDetails , err
}
json . Unmarshal ( bodyText , & abapResp )
json . Unmarshal ( * abapResp [ "d" ] , & body )
if reflect . DeepEqual ( abaputils . CloneEntity { } , body ) {
2020-11-02 15:17:13 +02:00
log . Entry ( ) . WithField ( "StatusCode" , resp . Status ) . WithField ( "repositoryName" , repo . Name ) . WithField ( "branchName" , repo . Branch ) . WithField ( "commitID" , repo . CommitID ) . Error ( "Could not Clone the Repository / Software Component" )
2020-08-21 14:49:48 +02:00
err := errors . New ( "Request to ABAP System not successful" )
return uriConnectionDetails , err
}
// The entity "Clones" does not allow for polling. To poll the progress, the related entity "Pull" has to be called
// While "Clones" has the key fields UUID, SC_NAME and BRANCH_NAME, "Pull" only has the key field UUID
2020-12-08 10:31:08 +02:00
uriConnectionDetails . URL = uriConnectionDetails . URL + "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull(uuid=guid'" + body . UUID + "')" + "?$expand=to_Execution_log,to_Transport_log"
2020-08-21 14:49:48 +02:00
return uriConnectionDetails , nil
}
2020-11-02 15:17:13 +02:00
func convertCloneConfig ( config * abapEnvironmentCloneGitRepoOptions ) abaputils . AbapEnvironmentOptions {
2020-08-21 14:49:48 +02:00
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
}