You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-09-16 09:26:22 +02:00
feat!: Mass enable abapEnvironmentPullGitRepo (#1516)
* Enable multiple repositories * Improve readability * Adapt documentation * rerun generator * Add initial info line * Improve logging
This commit is contained in:
@@ -41,27 +41,38 @@ func abapEnvironmentPullGitRepo(config abapEnvironmentPullGitRepoOptions, teleme
|
||||
Password: connectionDetails.Password,
|
||||
}
|
||||
client.SetOptions(clientOptions)
|
||||
pollIntervall := 10 * time.Second
|
||||
|
||||
log.Entry().Infof("Start pulling %v repositories", len(config.RepositoryNames))
|
||||
for _, repositoryName := range config.RepositoryNames {
|
||||
|
||||
log.Entry().Info("-------------------------")
|
||||
log.Entry().Info("Start pulling " + repositoryName)
|
||||
log.Entry().Info("-------------------------")
|
||||
|
||||
// Triggering the Pull of the repository into the ABAP Environment system
|
||||
uriConnectionDetails, errorTriggerPull := triggerPull(config, connectionDetails, &client)
|
||||
uriConnectionDetails, errorTriggerPull := triggerPull(repositoryName, connectionDetails, &client)
|
||||
if errorTriggerPull != nil {
|
||||
log.Entry().WithError(errorTriggerPull).Fatal("Pull failed on the ABAP System")
|
||||
log.Entry().WithError(errorTriggerPull).Fatal("Pull of " + repositoryName + " failed on the ABAP System")
|
||||
}
|
||||
|
||||
// Polling the status of the repository import on the ABAP Environment system
|
||||
pollIntervall := 10 * time.Second
|
||||
status, errorPollEntity := pollEntity(config, uriConnectionDetails, &client, pollIntervall)
|
||||
status, errorPollEntity := pollEntity(repositoryName, uriConnectionDetails, &client, pollIntervall)
|
||||
if errorPollEntity != nil {
|
||||
log.Entry().WithError(errorPollEntity).Fatal("Pull failed on the ABAP System")
|
||||
log.Entry().WithError(errorPollEntity).Fatal("Pull of " + repositoryName + " failed on the ABAP System")
|
||||
}
|
||||
if status == "E" {
|
||||
log.Entry().Fatal("Pull failed on the ABAP System")
|
||||
log.Entry().Fatal("Pull of " + repositoryName + " failed on the ABAP System")
|
||||
}
|
||||
|
||||
log.Entry().Info(repositoryName + " was pulled successfully")
|
||||
}
|
||||
log.Entry().Info("-------------------------")
|
||||
log.Entry().Info("All repositories were pulled successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails connectionDetailsHTTP, client piperhttp.Sender) (connectionDetailsHTTP, error) {
|
||||
func triggerPull(repositoryName string, pullConnectionDetails connectionDetailsHTTP, client piperhttp.Sender) (connectionDetailsHTTP, error) {
|
||||
|
||||
uriConnectionDetails := pullConnectionDetails
|
||||
uriConnectionDetails.URL = ""
|
||||
@@ -79,17 +90,17 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
|
||||
pullConnectionDetails.XCsrfToken = uriConnectionDetails.XCsrfToken
|
||||
|
||||
// Trigger the Pull of a Repository
|
||||
if config.RepositoryName == "" {
|
||||
if repositoryName == "" {
|
||||
return uriConnectionDetails, errors.New("An empty string was passed for the parameter 'repositoryName'")
|
||||
}
|
||||
jsonBody := []byte(`{"sc_name":"` + config.RepositoryName + `"}`)
|
||||
jsonBody := []byte(`{"sc_name":"` + repositoryName + `"}`)
|
||||
resp, err = getHTTPResponse("POST", pullConnectionDetails, jsonBody, client)
|
||||
if err != nil {
|
||||
err = handleHTTPError(resp, err, "Could not pull the Repository / Software Component "+config.RepositoryName, uriConnectionDetails)
|
||||
err = handleHTTPError(resp, err, "Could not pull the Repository / Software Component "+repositoryName, uriConnectionDetails)
|
||||
return uriConnectionDetails, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", config.RepositoryName).Info("Triggered Pull of Repository / Software Component")
|
||||
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", repositoryName).Info("Triggered Pull of Repository / Software Component")
|
||||
|
||||
// Parse Response
|
||||
var body abapEntity
|
||||
@@ -101,7 +112,7 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
|
||||
json.Unmarshal(bodyText, &abapResp)
|
||||
json.Unmarshal(*abapResp["d"], &body)
|
||||
if reflect.DeepEqual(abapEntity{}, body) {
|
||||
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", config.RepositoryName).Error("Could not pull the Repository / Software Component")
|
||||
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", repositoryName).Error("Could not pull the Repository / Software Component")
|
||||
err := errors.New("Request to ABAP System not successful")
|
||||
return uriConnectionDetails, err
|
||||
}
|
||||
@@ -111,7 +122,7 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
|
||||
return uriConnectionDetails, nil
|
||||
}
|
||||
|
||||
func pollEntity(config abapEnvironmentPullGitRepoOptions, connectionDetails connectionDetailsHTTP, client piperhttp.Sender, pollIntervall time.Duration) (string, error) {
|
||||
func pollEntity(repositoryName string, connectionDetails connectionDetailsHTTP, client piperhttp.Sender, pollIntervall time.Duration) (string, error) {
|
||||
|
||||
log.Entry().Info("Start polling the status...")
|
||||
var status string = "R"
|
||||
@@ -119,7 +130,7 @@ func pollEntity(config abapEnvironmentPullGitRepoOptions, connectionDetails conn
|
||||
for {
|
||||
var resp, err = getHTTPResponse("GET", connectionDetails, nil, client)
|
||||
if err != nil {
|
||||
err = handleHTTPError(resp, err, "Could not pull the Repository / Software Component "+config.RepositoryName, connectionDetails)
|
||||
err = handleHTTPError(resp, err, "Could not pull the Repository / Software Component "+repositoryName, connectionDetails)
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
@@ -131,7 +142,7 @@ func pollEntity(config abapEnvironmentPullGitRepoOptions, connectionDetails conn
|
||||
json.Unmarshal(bodyText, &abapResp)
|
||||
json.Unmarshal(*abapResp["d"], &body)
|
||||
if reflect.DeepEqual(abapEntity{}, body) {
|
||||
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", config.RepositoryName).Error("Could not pull the Repository / Software Component")
|
||||
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", repositoryName).Error("Could not pull the Repository / Software Component")
|
||||
var err = errors.New("Request to ABAP System not successful")
|
||||
return "", err
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@ import (
|
||||
type abapEnvironmentPullGitRepoOptions struct {
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
RepositoryName string `json:"repositoryName,omitempty"`
|
||||
RepositoryNames []string `json:"repositoryNames,omitempty"`
|
||||
Host string `json:"host,omitempty"`
|
||||
CfAPIEndpoint string `json:"cfApiEndpoint,omitempty"`
|
||||
CfOrg string `json:"cfOrg,omitempty"`
|
||||
@@ -87,7 +87,7 @@ Please provide either of the following options:
|
||||
func addAbapEnvironmentPullGitRepoFlags(cmd *cobra.Command, stepConfig *abapEnvironmentPullGitRepoOptions) {
|
||||
cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0510")
|
||||
cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0510")
|
||||
cmd.Flags().StringVar(&stepConfig.RepositoryName, "repositoryName", os.Getenv("PIPER_repositoryName"), "Specifies the name of the Repository (Software Component) on the SAP Cloud Platform ABAP Environment system")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.RepositoryNames, "repositoryNames", []string{}, "Specifies a list of Repositories (Software Components) on the SAP Cloud Platform ABAP Environment system")
|
||||
cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the host address of the SAP Cloud Platform ABAP Environment system")
|
||||
cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API Enpoint")
|
||||
cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry target organization")
|
||||
@@ -97,7 +97,7 @@ func addAbapEnvironmentPullGitRepoFlags(cmd *cobra.Command, stepConfig *abapEnvi
|
||||
|
||||
cmd.MarkFlagRequired("username")
|
||||
cmd.MarkFlagRequired("password")
|
||||
cmd.MarkFlagRequired("repositoryName")
|
||||
cmd.MarkFlagRequired("repositoryNames")
|
||||
}
|
||||
|
||||
// retrieve step metadata
|
||||
@@ -127,10 +127,10 @@ func abapEnvironmentPullGitRepoMetadata() config.StepData {
|
||||
Aliases: []config.Alias{},
|
||||
},
|
||||
{
|
||||
Name: "repositoryName",
|
||||
Name: "repositoryNames",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "string",
|
||||
Type: "[]string",
|
||||
Mandatory: true,
|
||||
Aliases: []config.Alias{},
|
||||
},
|
||||
|
@@ -35,7 +35,7 @@ func TestTriggerPull(t *testing.T) {
|
||||
CfServiceKey: "testServiceKey",
|
||||
Username: "testUser",
|
||||
Password: "testPassword",
|
||||
RepositoryName: "testRepo",
|
||||
RepositoryNames: []string{"testRepo1", "testRepo2"},
|
||||
}
|
||||
|
||||
con := connectionDetailsHTTP{
|
||||
@@ -43,7 +43,7 @@ func TestTriggerPull(t *testing.T) {
|
||||
Password: "MY_PW",
|
||||
URL: "https://api.endpoint.com/Entity/",
|
||||
}
|
||||
entityConnection, err := triggerPull(config, con, client)
|
||||
entityConnection, err := triggerPull(config.RepositoryNames[0], con, client)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uriExpected, entityConnection.URL)
|
||||
assert.Equal(t, tokenExpected, entityConnection.XCsrfToken)
|
||||
@@ -70,6 +70,7 @@ func TestTriggerPull(t *testing.T) {
|
||||
CfServiceKey: "testServiceKey",
|
||||
Username: "testUser",
|
||||
Password: "testPassword",
|
||||
RepositoryNames: []string{"testRepo1", "testRepo2"},
|
||||
}
|
||||
|
||||
con := connectionDetailsHTTP{
|
||||
@@ -77,7 +78,7 @@ func TestTriggerPull(t *testing.T) {
|
||||
Password: "MY_PW",
|
||||
URL: "https://api.endpoint.com/Entity/",
|
||||
}
|
||||
_, err := triggerPull(config, con, client)
|
||||
_, err := triggerPull(config.RepositoryNames[0], con, client)
|
||||
assert.Equal(t, combinedErrorMessage, err.Error(), "Different error message expected")
|
||||
})
|
||||
|
||||
@@ -103,6 +104,7 @@ func TestPollEntity(t *testing.T) {
|
||||
CfServiceKey: "testServiceKey",
|
||||
Username: "testUser",
|
||||
Password: "testPassword",
|
||||
RepositoryNames: []string{"testRepo1", "testRepo2"},
|
||||
}
|
||||
|
||||
con := connectionDetailsHTTP{
|
||||
@@ -111,7 +113,7 @@ func TestPollEntity(t *testing.T) {
|
||||
URL: "https://api.endpoint.com/Entity/",
|
||||
XCsrfToken: "MY_TOKEN",
|
||||
}
|
||||
status, _ := pollEntity(config, con, client, 0)
|
||||
status, _ := pollEntity(config.RepositoryNames[0], con, client, 0)
|
||||
assert.Equal(t, "S", status)
|
||||
})
|
||||
|
||||
@@ -133,6 +135,7 @@ func TestPollEntity(t *testing.T) {
|
||||
CfServiceKey: "testServiceKey",
|
||||
Username: "testUser",
|
||||
Password: "testPassword",
|
||||
RepositoryNames: []string{"testRepo1", "testRepo2"},
|
||||
}
|
||||
|
||||
con := connectionDetailsHTTP{
|
||||
@@ -141,7 +144,7 @@ func TestPollEntity(t *testing.T) {
|
||||
URL: "https://api.endpoint.com/Entity/",
|
||||
XCsrfToken: "MY_TOKEN",
|
||||
}
|
||||
status, _ := pollEntity(config, con, client, 0)
|
||||
status, _ := pollEntity(config.RepositoryNames[0], con, client, 0)
|
||||
assert.Equal(t, "E", status)
|
||||
})
|
||||
|
||||
|
@@ -20,7 +20,7 @@ In the first example, the host and the credentialsId of the Communication Arrang
|
||||
```groovy
|
||||
abapEnvironmentPullGitRepo (
|
||||
script: this,
|
||||
repositoryName: '/DMO/GIT_REPOSITORY',
|
||||
repositoryNames: ['/DMO/GIT_REPOSITORY'],
|
||||
credentialsId: 'abapCredentialsId',
|
||||
host: '1234-abcd-5678-efgh-ijk.abap.eu10.hana.ondemand.com'
|
||||
)
|
||||
@@ -31,14 +31,12 @@ In the second example, the host and credentialsId will be read from the provided
|
||||
```groovy
|
||||
abapEnvironmentPullGitRepo (
|
||||
script: this,
|
||||
repositoryName: '/DMO/GIT_REPOSITORY',
|
||||
repositoryNames: ['/DMO/GIT_REPOSITORY', '/DMO/GIT_REPO'],
|
||||
credentialsId: 'cfCredentialsId',
|
||||
cloudFoundry: [
|
||||
apiEndpoint: 'https://test.server.com',
|
||||
org: 'cfOrg',
|
||||
space: 'cfSpace',
|
||||
serviceInstance: 'cfServiceInstance',
|
||||
serviceKey: 'cfServiceKey',
|
||||
]
|
||||
cfApiEndpoint: 'https://test.server.com',
|
||||
cfOrg: 'cfOrg',
|
||||
cfSpace: 'cfSpace',
|
||||
cfServiceInstance: 'cfServiceInstance',
|
||||
cfServiceKey: 'cfServiceKey'
|
||||
)
|
||||
```
|
||||
|
@@ -34,9 +34,9 @@ spec:
|
||||
- STEPS
|
||||
mandatory: true
|
||||
secret: true
|
||||
- name: repositoryName
|
||||
type: string
|
||||
description: Specifies the name of the Repository (Software Component) on the SAP Cloud Platform ABAP Environment system
|
||||
- name: repositoryNames
|
||||
type: '[]string'
|
||||
description: Specifies a list of Repositories (Software Components) on the SAP Cloud Platform ABAP Environment system
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
|
Reference in New Issue
Block a user