1
0
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:
Daniel Mieg
2020-05-07 15:51:11 +02:00
committed by GitHub
parent fd858dafcd
commit 02ede6d28b
5 changed files with 66 additions and 54 deletions

View File

@@ -41,27 +41,38 @@ func abapEnvironmentPullGitRepo(config abapEnvironmentPullGitRepoOptions, teleme
Password: connectionDetails.Password, Password: connectionDetails.Password,
} }
client.SetOptions(clientOptions) 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 // 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 { 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 // Polling the status of the repository import on the ABAP Environment system
pollIntervall := 10 * time.Second status, errorPollEntity := pollEntity(repositoryName, uriConnectionDetails, &client, pollIntervall)
status, errorPollEntity := pollEntity(config, uriConnectionDetails, &client, pollIntervall)
if errorPollEntity != nil { 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" { 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 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 := pullConnectionDetails
uriConnectionDetails.URL = "" uriConnectionDetails.URL = ""
@@ -79,17 +90,17 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
pullConnectionDetails.XCsrfToken = uriConnectionDetails.XCsrfToken pullConnectionDetails.XCsrfToken = uriConnectionDetails.XCsrfToken
// Trigger the Pull of a Repository // Trigger the Pull of a Repository
if config.RepositoryName == "" { if repositoryName == "" {
return uriConnectionDetails, errors.New("An empty string was passed for the parameter '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) resp, err = getHTTPResponse("POST", pullConnectionDetails, jsonBody, client)
if err != nil { 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 return uriConnectionDetails, err
} }
defer resp.Body.Close() 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 // Parse Response
var body abapEntity var body abapEntity
@@ -101,7 +112,7 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
json.Unmarshal(bodyText, &abapResp) json.Unmarshal(bodyText, &abapResp)
json.Unmarshal(*abapResp["d"], &body) json.Unmarshal(*abapResp["d"], &body)
if reflect.DeepEqual(abapEntity{}, 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") err := errors.New("Request to ABAP System not successful")
return uriConnectionDetails, err return uriConnectionDetails, err
} }
@@ -111,7 +122,7 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
return uriConnectionDetails, nil 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...") log.Entry().Info("Start polling the status...")
var status string = "R" var status string = "R"
@@ -119,7 +130,7 @@ func pollEntity(config abapEnvironmentPullGitRepoOptions, connectionDetails conn
for { for {
var resp, err = getHTTPResponse("GET", connectionDetails, nil, client) var resp, err = getHTTPResponse("GET", connectionDetails, nil, client)
if err != nil { 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 return "", err
} }
defer resp.Body.Close() defer resp.Body.Close()
@@ -131,7 +142,7 @@ func pollEntity(config abapEnvironmentPullGitRepoOptions, connectionDetails conn
json.Unmarshal(bodyText, &abapResp) json.Unmarshal(bodyText, &abapResp)
json.Unmarshal(*abapResp["d"], &body) json.Unmarshal(*abapResp["d"], &body)
if reflect.DeepEqual(abapEntity{}, 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") var err = errors.New("Request to ABAP System not successful")
return "", err return "", err
} }

View File

@@ -16,7 +16,7 @@ import (
type abapEnvironmentPullGitRepoOptions struct { type abapEnvironmentPullGitRepoOptions struct {
Username string `json:"username,omitempty"` Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
RepositoryName string `json:"repositoryName,omitempty"` RepositoryNames []string `json:"repositoryNames,omitempty"`
Host string `json:"host,omitempty"` Host string `json:"host,omitempty"`
CfAPIEndpoint string `json:"cfApiEndpoint,omitempty"` CfAPIEndpoint string `json:"cfApiEndpoint,omitempty"`
CfOrg string `json:"cfOrg,omitempty"` CfOrg string `json:"cfOrg,omitempty"`
@@ -87,7 +87,7 @@ Please provide either of the following options:
func addAbapEnvironmentPullGitRepoFlags(cmd *cobra.Command, stepConfig *abapEnvironmentPullGitRepoOptions) { 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.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.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.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.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API Enpoint")
cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry target organization") 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("username")
cmd.MarkFlagRequired("password") cmd.MarkFlagRequired("password")
cmd.MarkFlagRequired("repositoryName") cmd.MarkFlagRequired("repositoryNames")
} }
// retrieve step metadata // retrieve step metadata
@@ -127,10 +127,10 @@ func abapEnvironmentPullGitRepoMetadata() config.StepData {
Aliases: []config.Alias{}, Aliases: []config.Alias{},
}, },
{ {
Name: "repositoryName", Name: "repositoryNames",
ResourceRef: []config.ResourceReference{}, ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string", Type: "[]string",
Mandatory: true, Mandatory: true,
Aliases: []config.Alias{}, Aliases: []config.Alias{},
}, },

View File

@@ -35,7 +35,7 @@ func TestTriggerPull(t *testing.T) {
CfServiceKey: "testServiceKey", CfServiceKey: "testServiceKey",
Username: "testUser", Username: "testUser",
Password: "testPassword", Password: "testPassword",
RepositoryName: "testRepo", RepositoryNames: []string{"testRepo1", "testRepo2"},
} }
con := connectionDetailsHTTP{ con := connectionDetailsHTTP{
@@ -43,7 +43,7 @@ func TestTriggerPull(t *testing.T) {
Password: "MY_PW", Password: "MY_PW",
URL: "https://api.endpoint.com/Entity/", 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.Nil(t, err)
assert.Equal(t, uriExpected, entityConnection.URL) assert.Equal(t, uriExpected, entityConnection.URL)
assert.Equal(t, tokenExpected, entityConnection.XCsrfToken) assert.Equal(t, tokenExpected, entityConnection.XCsrfToken)
@@ -70,6 +70,7 @@ func TestTriggerPull(t *testing.T) {
CfServiceKey: "testServiceKey", CfServiceKey: "testServiceKey",
Username: "testUser", Username: "testUser",
Password: "testPassword", Password: "testPassword",
RepositoryNames: []string{"testRepo1", "testRepo2"},
} }
con := connectionDetailsHTTP{ con := connectionDetailsHTTP{
@@ -77,7 +78,7 @@ func TestTriggerPull(t *testing.T) {
Password: "MY_PW", Password: "MY_PW",
URL: "https://api.endpoint.com/Entity/", 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") assert.Equal(t, combinedErrorMessage, err.Error(), "Different error message expected")
}) })
@@ -103,6 +104,7 @@ func TestPollEntity(t *testing.T) {
CfServiceKey: "testServiceKey", CfServiceKey: "testServiceKey",
Username: "testUser", Username: "testUser",
Password: "testPassword", Password: "testPassword",
RepositoryNames: []string{"testRepo1", "testRepo2"},
} }
con := connectionDetailsHTTP{ con := connectionDetailsHTTP{
@@ -111,7 +113,7 @@ func TestPollEntity(t *testing.T) {
URL: "https://api.endpoint.com/Entity/", URL: "https://api.endpoint.com/Entity/",
XCsrfToken: "MY_TOKEN", XCsrfToken: "MY_TOKEN",
} }
status, _ := pollEntity(config, con, client, 0) status, _ := pollEntity(config.RepositoryNames[0], con, client, 0)
assert.Equal(t, "S", status) assert.Equal(t, "S", status)
}) })
@@ -133,6 +135,7 @@ func TestPollEntity(t *testing.T) {
CfServiceKey: "testServiceKey", CfServiceKey: "testServiceKey",
Username: "testUser", Username: "testUser",
Password: "testPassword", Password: "testPassword",
RepositoryNames: []string{"testRepo1", "testRepo2"},
} }
con := connectionDetailsHTTP{ con := connectionDetailsHTTP{
@@ -141,7 +144,7 @@ func TestPollEntity(t *testing.T) {
URL: "https://api.endpoint.com/Entity/", URL: "https://api.endpoint.com/Entity/",
XCsrfToken: "MY_TOKEN", XCsrfToken: "MY_TOKEN",
} }
status, _ := pollEntity(config, con, client, 0) status, _ := pollEntity(config.RepositoryNames[0], con, client, 0)
assert.Equal(t, "E", status) assert.Equal(t, "E", status)
}) })

View File

@@ -20,7 +20,7 @@ In the first example, the host and the credentialsId of the Communication Arrang
```groovy ```groovy
abapEnvironmentPullGitRepo ( abapEnvironmentPullGitRepo (
script: this, script: this,
repositoryName: '/DMO/GIT_REPOSITORY', repositoryNames: ['/DMO/GIT_REPOSITORY'],
credentialsId: 'abapCredentialsId', credentialsId: 'abapCredentialsId',
host: '1234-abcd-5678-efgh-ijk.abap.eu10.hana.ondemand.com' 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 ```groovy
abapEnvironmentPullGitRepo ( abapEnvironmentPullGitRepo (
script: this, script: this,
repositoryName: '/DMO/GIT_REPOSITORY', repositoryNames: ['/DMO/GIT_REPOSITORY', '/DMO/GIT_REPO'],
credentialsId: 'cfCredentialsId', credentialsId: 'cfCredentialsId',
cloudFoundry: [ cfApiEndpoint: 'https://test.server.com',
apiEndpoint: 'https://test.server.com', cfOrg: 'cfOrg',
org: 'cfOrg', cfSpace: 'cfSpace',
space: 'cfSpace', cfServiceInstance: 'cfServiceInstance',
serviceInstance: 'cfServiceInstance', cfServiceKey: 'cfServiceKey'
serviceKey: 'cfServiceKey',
]
) )
``` ```

View File

@@ -34,9 +34,9 @@ spec:
- STEPS - STEPS
mandatory: true mandatory: true
secret: true secret: true
- name: repositoryName - name: repositoryNames
type: string type: '[]string'
description: Specifies the name of the Repository (Software Component) on the SAP Cloud Platform ABAP Environment system description: Specifies a list of Repositories (Software Components) on the SAP Cloud Platform ABAP Environment system
scope: scope:
- PARAMETERS - PARAMETERS
- STAGES - STAGES