1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Add parameter to pull step (#3851)

* Add parameter to pull step

* Add test for config

* Adapt docu

* Update resources/metadata/abapEnvironmentPullGitRepo.yaml

Co-authored-by: tiloKo <70266685+tiloKo@users.noreply.github.com>

* Regenerate

Co-authored-by: tiloKo <70266685+tiloKo@users.noreply.github.com>
This commit is contained in:
Daniel Mieg 2022-06-28 11:02:15 +02:00 committed by GitHub
parent 64a1cfdee4
commit 3184785c5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 146 additions and 17 deletions

View File

@ -66,7 +66,7 @@ func runAbapEnvironmentCheckoutBranch(options *abapEnvironmentCheckoutBranchOpti
err = checkCheckoutBranchRepositoryConfiguration(*options)
if err == nil {
repositories, err = abaputils.GetRepositories(&abaputils.RepositoriesConfig{BranchName: options.BranchName, RepositoryName: options.RepositoryName, Repositories: options.Repositories})
repositories, err = abaputils.GetRepositories(&abaputils.RepositoriesConfig{BranchName: options.BranchName, RepositoryName: options.RepositoryName, Repositories: options.Repositories}, true)
}
if err == nil {
err = checkoutBranches(repositories, connectionDetails, client, pollIntervall)

View File

@ -59,7 +59,7 @@ func runAbapEnvironmentCloneGitRepo(config *abapEnvironmentCloneGitRepoOptions,
Password: connectionDetails.Password,
})
repositories, errGetRepos := abaputils.GetRepositories(&abaputils.RepositoriesConfig{BranchName: config.BranchName, RepositoryName: config.RepositoryName, Repositories: config.Repositories})
repositories, errGetRepos := abaputils.GetRepositories(&abaputils.RepositoriesConfig{BranchName: config.BranchName, RepositoryName: config.RepositoryName, Repositories: config.Repositories}, true)
if errGetRepos != nil {
return fmt.Errorf("Something failed during the clone: %w", errGetRepos)
}

View File

@ -65,7 +65,7 @@ func runAbapEnvironmentPullGitRepo(options *abapEnvironmentPullGitRepoOptions, c
if err != nil {
return err
}
repositories, err = abaputils.GetRepositories(&abaputils.RepositoriesConfig{RepositoryNames: options.RepositoryNames, Repositories: options.Repositories})
repositories, err = abaputils.GetRepositories(&abaputils.RepositoriesConfig{RepositoryNames: options.RepositoryNames, Repositories: options.Repositories, RepositoryName: options.RepositoryName, CommitID: options.CommitID}, false)
handleIgnoreCommit(repositories, options.IgnoreCommit)
if err != nil {
return err
@ -168,10 +168,11 @@ func triggerPull(repo abaputils.Repository, pullConnectionDetails abaputils.Conn
}
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 != "") || (len(options.RepositoryNames) > 0 && options.RepositoryName != "") || (options.RepositoryName != "" && options.Repositories != "") {
return fmt.Errorf("Checking configuration failed: %w", errors.New("Only one of the paramters `RepositoryName`,`RepositoryNames` or `Repositories` may be configured at the same time"))
}
if len(options.RepositoryNames) == 0 && options.Repositories == "" {
if len(options.RepositoryNames) == 0 && options.Repositories == "" && options.RepositoryName == "" {
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

View File

@ -20,6 +20,8 @@ type abapEnvironmentPullGitRepoOptions struct {
Password string `json:"password,omitempty"`
RepositoryNames []string `json:"repositoryNames,omitempty"`
Repositories string `json:"repositories,omitempty"`
RepositoryName string `json:"repositoryName,omitempty"`
CommitID string `json:"commitID,omitempty"`
Host string `json:"host,omitempty"`
CfAPIEndpoint string `json:"cfApiEndpoint,omitempty"`
CfOrg string `json:"cfOrg,omitempty"`
@ -133,6 +135,8 @@ func addAbapEnvironmentPullGitRepoFlags(cmd *cobra.Command, stepConfig *abapEnvi
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().StringSliceVar(&stepConfig.RepositoryNames, "repositoryNames", []string{}, "Specifies a list of Repositories (Software Components) on the SAP BTP ABAP Environment system")
cmd.Flags().StringVar(&stepConfig.Repositories, "repositories", os.Getenv("PIPER_repositories"), "Specifies a YAML file containing the repositories configuration")
cmd.Flags().StringVar(&stepConfig.RepositoryName, "repositoryName", os.Getenv("PIPER_repositoryName"), "Specifies a repository (Software Component) on the SAP BTP ABAP Environment system")
cmd.Flags().StringVar(&stepConfig.CommitID, "commitID", os.Getenv("PIPER_commitID"), "Specifies a commitID of the repository, configured via \"repositoryName\" on the SAP BTP ABAP Environment system")
cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the host address of the SAP BTP 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")
@ -207,6 +211,24 @@ func abapEnvironmentPullGitRepoMetadata() config.StepData {
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_repositories"),
},
{
Name: "repositoryName",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_repositoryName"),
},
{
Name: "commitID",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_commitID"),
},
{
Name: "host",
ResourceRef: []config.ResourceReference{},

View File

@ -251,6 +251,83 @@ repositories:
}
})
t.Run("Status Error, With Commit", func(t *testing.T) {
var autils = abaputils.AUtilsMock{}
defer autils.Cleanup()
autils.ReturnedConnectionDetailsHTTP.Password = "password"
autils.ReturnedConnectionDetailsHTTP.User = "user"
autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
config := abapEnvironmentPullGitRepoOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
CfServiceInstance: "testInstance",
CfServiceKeyName: "testServiceKey",
Username: "testUser",
Password: "testPassword",
RepositoryName: "/DMO/SWC",
CommitID: "123456",
IgnoreCommit: false,
}
logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
logResultError,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "E" } }`,
`{"d" : { "status" : "R" } }`,
`{"d" : { "status" : "R" } }`,
},
Token: "myToken",
StatusCode: 200,
}
err := runAbapEnvironmentPullGitRepo(&config, &autils, client)
if assert.Error(t, err, "Expected error") {
assert.Equal(t, "Pull of the repository / software component '/DMO/SWC', commit '123456' failed on the ABAP system", err.Error(), "Expected different error message")
}
})
t.Run("Status Error, RepositoryName without commit", func(t *testing.T) {
var autils = abaputils.AUtilsMock{}
defer autils.Cleanup()
autils.ReturnedConnectionDetailsHTTP.Password = "password"
autils.ReturnedConnectionDetailsHTTP.User = "user"
autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
config := abapEnvironmentPullGitRepoOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
CfServiceInstance: "testInstance",
CfServiceKeyName: "testServiceKey",
Username: "testUser",
Password: "testPassword",
RepositoryName: "/DMO/SWC",
IgnoreCommit: false,
}
logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
logResultError,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "E" } }`,
`{"d" : { "status" : "R" } }`,
`{"d" : { "status" : "R" } }`,
},
Token: "myToken",
StatusCode: 200,
}
err := runAbapEnvironmentPullGitRepo(&config, &autils, client)
if assert.Error(t, err, "Expected error") {
assert.Equal(t, "Pull of the repository / software component '/DMO/SWC' failed on the ABAP system", err.Error(), "Expected different error message")
}
})
t.Run("Failure case: pull repos from empty file config", func(t *testing.T) {
expectedErrorMessage := "Error in config file repositoriesTest.yml, AddonDescriptor doesn't contain any repositories"
@ -429,6 +506,16 @@ func TestPullConfigChecker(t *testing.T) {
err := checkPullRepositoryConfiguration(config)
assert.Equal(t, errorMessage, err.Error(), "Different error message expected")
})
t.Run("Failure case: config overload", func(t *testing.T) {
errorMessage := "Checking configuration failed: Only one of the paramters `RepositoryName`,`RepositoryNames` or `Repositories` may be configured at the same time"
config := abapEnvironmentPullGitRepoOptions{
RepositoryNames: []string{"testRepo", "testRepo2"},
RepositoryName: "Test",
CommitID: "123456",
}
err := checkPullRepositoryConfiguration(config)
assert.Equal(t, errorMessage, err.Error(), "Different error message expected")
})
}
func TestHelpFunctions(t *testing.T) {

View File

@ -165,7 +165,7 @@ func resolveATCConfiguration(config abapEnvironmentRunATCCheckOptions) (atcConfi
} else if config.Repositories != "" {
// Fallback / EasyMode is the Repositories configuration
log.Entry().Infof("ATC Configuration derived from: %s", config.Repositories)
repositories, err := abaputils.GetRepositories((&abaputils.RepositoriesConfig{Repositories: config.Repositories}))
repositories, err := abaputils.GetRepositories((&abaputils.RepositoriesConfig{Repositories: config.Repositories}), false)
if err != nil {
return atcConfig, err
}

View File

@ -104,7 +104,7 @@ func resolveAUnitConfiguration(config abapEnvironmentRunAUnitTestOptions) (aUnit
} else if config.Repositories != "" {
// Fallback / EasyMode is the Repositories configuration
log.Entry().Infof("AUnit Configuration derived from: %s", config.Repositories)
repos, err := abaputils.GetRepositories((&abaputils.RepositoriesConfig{Repositories: config.Repositories}))
repos, err := abaputils.GetRepositories((&abaputils.RepositoriesConfig{Repositories: config.Repositories}), false)
if err != nil {
return aUnitConfig, err
}

View File

@ -26,7 +26,7 @@ If you want to provide the host and credentials of the Communication Arrangement
```yaml
steps:
abapEnvironmentPullGitRepo:
repositoryNames: ['/DMO/GIT_REPOSITORY']
repositoryName: '/DMO/GIT_REPOSITORY'
abapCredentialsId: 'abapCredentialsId'
host: '1234-abcd-5678-efgh-ijk.abap.eu10.hana.ondemand.com'
```
@ -63,7 +63,7 @@ If you want to read the host and credentials from the cloud foundry service key
```yaml
steps:
abapEnvironmentPullGitRepo:
repositoryNames: ['/DMO/GIT_REPOSITORY']
repositoryName: '/DMO/GIT_REPOSITORY'
cfCredentialsId: 'cfCredentialsId'
cfApiEndpoint: 'https://test.server.com'
cfOrg: 'cfOrg'
@ -80,7 +80,8 @@ In the first example, the host and the credentialsId of the Communication Arrang
```groovy
abapEnvironmentPullGitRepo (
script: this,
repositoryNames: ['/DMO/GIT_REPOSITORY'],
repositoryName: '/DMO/GIT_REPOSITORY',
CommitID: 'abcd1234'
abapCredentialsId: 'abapCredentialsId',
host: '1234-abcd-5678-efgh-ijk.abap.eu10.hana.ondemand.com'
)
@ -91,7 +92,7 @@ In the second example, the host and credentialsId will be read from the provided
```groovy
abapEnvironmentPullGitRepo (
script: this,
repositoryNames: ['/DMO/GIT_REPOSITORY', '/DMO/GIT_REPO'],
repositoryName: '/DMO/GIT_REPOSITORY',
abapCredentialsId: 'cfCredentialsId',
cfApiEndpoint: 'https://test.server.com',
cfOrg: 'cfOrg',

View File

@ -243,7 +243,7 @@ func GetStatus(failureMessage string, connectionDetails ConnectionDetailsHTTP, c
}
//GetRepositories for parsing one or multiple branches and repositories from repositories file or branchName and repositoryName configuration
func GetRepositories(config *RepositoriesConfig) ([]Repository, error) {
func GetRepositories(config *RepositoriesConfig, branchRequired bool) ([]Repository, error) {
var repositories = make([]Repository, 0)
if reflect.DeepEqual(RepositoriesConfig{}, config) {
log.SetErrorCategory(log.ErrorConfiguration)
@ -269,6 +269,9 @@ func GetRepositories(config *RepositoriesConfig) ([]Repository, error) {
if config.RepositoryName != "" && config.BranchName != "" {
repositories = append(repositories, Repository{Name: config.RepositoryName, Branch: config.BranchName})
}
if config.RepositoryName != "" && !branchRequired {
repositories = append(repositories, Repository{Name: config.RepositoryName, CommitID: config.CommitID})
}
if len(config.RepositoryNames) > 0 {
for _, repository := range config.RepositoryNames {
repositories = append(repositories, Repository{Name: repository})
@ -420,6 +423,7 @@ type LogResults struct {
//RepositoriesConfig struct for parsing one or multiple branches and repositories configurations
type RepositoriesConfig struct {
BranchName string
CommitID string
RepositoryName string
RepositoryNames []string
Repositories string

View File

@ -139,7 +139,7 @@ repositories:
Repositories: "repositoriesTest.yml",
}
repositories, err := GetRepositories(&config)
repositories, err := GetRepositories(&config, true)
assert.Equal(t, expectedRepositoryList, repositories)
assert.NoError(t, err)
@ -172,7 +172,7 @@ repositories:
Repositories: "repositoriesTest.yml",
}
repositories, err := GetRepositories(&config)
repositories, err := GetRepositories(&config, false)
assert.Equal(t, expectedRepositoryList, repositories)
assert.EqualError(t, err, expectedErrorMessage)
@ -205,7 +205,7 @@ repositories:
Repositories: "repositoriesTest.yml",
}
repositories, err := GetRepositories(&config)
repositories, err := GetRepositories(&config, false)
assert.Equal(t, expectedRepositoryList, repositories)
assert.EqualError(t, err, expectedErrorMessage)
@ -216,7 +216,7 @@ repositories:
config := RepositoriesConfig{}
repositories, err := GetRepositories(&config)
repositories, err := GetRepositories(&config, false)
assert.Equal(t, expectedRepositoryList, repositories)
assert.EqualError(t, err, expectedErrorMessage)

View File

@ -60,6 +60,20 @@ spec:
- PARAMETERS
- STAGES
- STEPS
- name: repositoryName
type: string
description: Specifies a repository (Software Component) on the SAP BTP ABAP Environment system
scope:
- PARAMETERS
- STAGES
- STEPS
- name: commitID
type: string
description: Specifies a commitID of the repository, configured via "repositoryName" on the SAP BTP ABAP Environment system
scope:
- PARAMETERS
- STAGES
- STEPS
- name: host
type: string
description: Specifies the host address of the SAP BTP ABAP Environment system