Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt clone step to work with customer-managed Repos (BYOG) #4966

Merged
merged 38 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
35aad23
adding byog credentials for clone command
doldsimo Jun 11, 2024
eace22e
Merge remote-tracking branch 'upstream/master' into apibyogclone
doldsimo Jun 11, 2024
eb16e7c
adding unit tests for clone body
doldsimo Jun 12, 2024
ecf5ffc
adding parameters
doldsimo Jun 12, 2024
1ff6f2f
adding optional byog parameters
doldsimo Jun 13, 2024
ca3f6c1
fixing typo in username
doldsimo Jun 14, 2024
c7f4687
remove aliases in config yaml
doldsimo Jun 14, 2024
65202b1
change yaml config
doldsimo Jun 14, 2024
0cc82a1
logs
doldsimo Jun 14, 2024
de1c00f
change info log
doldsimo Jun 14, 2024
fa5db99
change logs
doldsimo Jun 14, 2024
c11ba64
remove logs
doldsimo Jun 14, 2024
fe853b9
adding log statements
doldsimo Jun 19, 2024
d2f88d9
remove log statements
doldsimo Jun 19, 2024
9adcf26
fixing typo in test class
doldsimo Jun 19, 2024
5275375
change repoTest structure
doldsimo Jun 19, 2024
b1dab9d
merge master
doldsimo Jun 19, 2024
f77cb4b
remove comment
doldsimo Jun 19, 2024
e92f527
remove comment
doldsimo Jun 19, 2024
df2de60
generate
doldsimo Jun 19, 2024
4f452cd
adding unit test comments
doldsimo Jun 19, 2024
573a19b
adding error handling
doldsimo Jun 20, 2024
a907819
adding isByog check
doldsimo Jun 20, 2024
f091f0c
fixing unit test
doldsimo Jun 20, 2024
2ed8946
generate
doldsimo Jun 20, 2024
c14d990
Merge branch 'master' into apibyogclone
doldsimo Jun 20, 2024
1acc31c
Update manageGitRepositoryUtils_test.go
doldsimo Jun 20, 2024
2282a71
restructure isByog parameter
doldsimo Jun 21, 2024
fa916d9
Merge branch 'apibyogclone' of https://github.com/doldsimo/jenkins-li…
doldsimo Jun 21, 2024
4c67734
adding empty line for md linter
doldsimo Jun 21, 2024
09c79a0
adding config.yaml example to docs
doldsimo Jun 21, 2024
b4e4453
Update documentation/docs/steps/abapEnvironmentCloneGitRepo.md
doldsimo Jun 21, 2024
a95ca04
Update documentation/docs/steps/abapEnvironmentCloneGitRepo.md
doldsimo Jun 21, 2024
47c6cf3
Update documentation/docs/steps/abapEnvironmentCloneGitRepo.md
doldsimo Jun 21, 2024
297c8d5
Update documentation/docs/steps/abapEnvironmentCloneGitRepo.md
doldsimo Jun 21, 2024
f7b3725
Update documentation/docs/steps/abapEnvironmentCloneGitRepo.md
doldsimo Jun 21, 2024
567294f
adding release
doldsimo Jun 21, 2024
a6f25ea
Merge branch 'master' into apibyogclone
DanielMieg Jun 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cmd/abapEnvironmentCloneGitRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func runAbapEnvironmentCloneGitRepo(config *abapEnvironmentCloneGitRepoOptions,
return errors.Wrap(errConfig, "The provided configuration is not allowed")
}

repositories, errGetRepos := abaputils.GetRepositories(&abaputils.RepositoriesConfig{BranchName: config.BranchName, RepositoryName: config.RepositoryName, Repositories: config.Repositories}, false)
repositories, errGetRepos := abaputils.GetRepositories(&abaputils.RepositoriesConfig{BranchName: config.BranchName, RepositoryName: config.RepositoryName, Repositories: config.Repositories, ByogUsername: config.ByogUsername, ByogPassword: config.ByogPassword, ByogAuthMethod: config.ByogAuthMethod}, false)
if errGetRepos != nil {
return errors.Wrap(errGetRepos, "Could not read repositories")
}
Expand Down Expand Up @@ -85,12 +85,15 @@ func cloneSingleRepo(apiManager abaputils.SoftwareComponentApiManagerInterface,
log.Entry().Info("Start cloning " + logString)
abaputils.AddDefaultDashedLine(1)

alreadyCloned, activeBranch, errCheckCloned := api.GetRepository()
alreadyCloned, activeBranch, errCheckCloned, isByog := api.GetRepository()
if errCheckCloned != nil {
return errors.Wrapf(errCheckCloned, errorString)
}

if !alreadyCloned {
if isByog {
api.UpdateRepoWithBYOGCredentials(config.ByogAuthMethod, config.ByogUsername, config.ByogPassword)
}
errClone := api.Clone()
if errClone != nil {
return errors.Wrapf(errClone, errorString)
Expand Down Expand Up @@ -186,5 +189,8 @@ func convertCloneConfig(config *abapEnvironmentCloneGitRepoOptions) abaputils.Ab
subOptions.Host = config.Host
subOptions.Password = config.Password
subOptions.Username = config.Username
subOptions.ByogUsername = config.ByogUsername
subOptions.ByogPassword = config.ByogPassword
subOptions.ByogAuthMethod = config.ByogAuthMethod
return subOptions
}
50 changes: 50 additions & 0 deletions cmd/abapEnvironmentCloneGitRepo_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions documentation/docs/steps/abapEnvironmentCloneGitRepo.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,39 @@ abapEnvironmentCloneGitRepo (
cfServiceKeyName: 'cfServiceKeyName'
)
```

## Example: Cloning a Bring Your Own Git (BYOG) repository

> Feature will be available in November 2024.

Since a ByoG repository is an external repository, you must be authenticated to clone it.
For this, the corresponding credentials must be stored in Jenkins as a username and password/token.

<strong> Store the credentials: </strong> <br>
A new credential with the type username and password must be stored.<br>
`Jenkins Dashboard > Manage Jenkins > Credentials` <br>
These credentials are used to clone the ByoG repository.
More information on configuring the credentials can be found [here](https://www.jenkins.io/doc/book/using/using-credentials/).

The config.yaml should look like this:

```yaml
steps:
abapEnvironmentCloneGitRepo:
repositories: 'repos.yaml'
byogCredentialsId: 'byogCredentialsId'
abapCredentialsId: 'abapCredentialsId'
host: '1234-abcd-5678-efgh-ijk.abap.eu10.hana.ondemand.com'
```

`byogCredentialsId: 'byogCredentialsId'` is the reference to the defined credential in Jenkins. So take care that this matches with your setup.

After that, the ByoG repository that is to be cloned must be specified in the repos.yaml:

```yaml
repositories:
- name: '/DMO/REPO_BYOG'
branch: 'main'
```

After the pipeline has run through, the repository has been cloned.
3 changes: 3 additions & 0 deletions pkg/abaputils/abaputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ type AbapEnvironmentRunATCCheckOptions struct {
type AbapEnvironmentOptions struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
ByogUsername string `json:"byogUsername,omitempty"`
ByogPassword string `json:"byogPassword,omitempty"`
ByogAuthMethod string `json:"byogAuthMethod,omitempty"`
Host string `json:"host,omitempty"`
CfAPIEndpoint string `json:"cfApiEndpoint,omitempty"`
CfOrg string `json:"cfOrg,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/abaputils/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type Repository struct {
Tag string `json:"tag,omitempty"`
Branch string `json:"branch,omitempty"`
CommitID string `json:"commitID,omitempty"`
ByogUsername string `json:"byogUsername"`
ByogPassword string `json:"byogPassword"`
ByogAuthMethod string `json:"byogAuthMethod"`
IsByog bool `json:",omitempty"`
VersionYAML string `json:"version,omitempty"`
Version string `json:"versionAAK"`
AdditionalPiecelist string `json:"additionalPiecelist,omitempty"`
Expand Down
36 changes: 32 additions & 4 deletions pkg/abaputils/manageGitRepositoryUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func printHeader(logEntry LogResultsV2, api SoftwareComponentApiInterface) {
}
}

// GetRepositories for parsing one or multiple branches and repositories from repositories file or branchName and repositoryName configuration
// GetRepositories for parsing one or multiple branches and repositories from repositories file or branchName and repositoryName configuration
func GetRepositories(config *RepositoriesConfig, branchRequired bool) ([]Repository, error) {
var repositories = make([]Repository, 0)
if reflect.DeepEqual(RepositoriesConfig{}, config) {
Expand Down Expand Up @@ -193,6 +193,7 @@ func GetRepositories(config *RepositoriesConfig, branchRequired bool) ([]Reposit
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})
Expand All @@ -210,6 +211,25 @@ func (repo *Repository) GetRequestBodyForCommitOrTag() (requestBodyString string
return requestBodyString
}

func (repo *Repository) GetRequestBodyForBYOGCredentials() (string, error) {
var byogBodyString string

if repo.ByogAuthMethod != "" {
byogBodyString += `, "auth_method":"` + repo.ByogAuthMethod + `"`
}
if repo.ByogUsername != "" {
doldsimo marked this conversation as resolved.
Show resolved Hide resolved
byogBodyString += `, "username":"` + repo.ByogUsername + `"`
} else {
return "", fmt.Errorf("Failed to get BYOG credentials: %w", errors.New("Username for BYOG is missing, please provide git username to authenticate"))
}
if repo.ByogPassword != "" {
byogBodyString += `, "password":"` + repo.ByogPassword + `"`
} else {
return "", fmt.Errorf("Failed to get BYOG credentials: %w", errors.New("Password/Token for BYOG is missing, please provide git password or token to authenticate"))
}
return byogBodyString, nil
}

func (repo *Repository) GetLogStringForCommitOrTag() (logString string) {
if repo.CommitID != "" {
logString = ", commit '" + repo.CommitID + "'"
Expand All @@ -228,13 +248,21 @@ func (repo *Repository) GetCloneRequestBodyWithSWC() (body string) {
return body
}

func (repo *Repository) GetCloneRequestBody() (body string) {
func (repo *Repository) GetCloneRequestBody() (body string, err error) {
if repo.CommitID != "" && repo.Tag != "" {
log.Entry().WithField("Tag", repo.Tag).WithField("Commit ID", repo.CommitID).Info("The commit ID takes precedence over the tag")
}
requestBodyString := repo.GetRequestBodyForCommitOrTag()
body = `{"branch_name":"` + repo.Branch + `"` + requestBodyString + `}`
return body
var byogBodyString = ""
if repo.IsByog {
byogBodyString, err = repo.GetRequestBodyForBYOGCredentials()
if err != nil {
return "", err
}
}

body = `{"branch_name":"` + repo.Branch + `"` + requestBodyString + byogBodyString + `}`
return body, nil
}

func (repo *Repository) GetCloneLogString() (logString string) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/abaputils/manageGitRepositoryUtils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func TestCreateRequestBodies(t *testing.T) {
CommitID: "1234567",
Tag: "myTag",
}
body := repo.GetCloneRequestBody()
body, _ := repo.GetCloneRequestBody()
assert.Equal(t, `{"branch_name":"main", "commit_id":"1234567"}`, body, "Expected different body")
})
t.Run("Clone Body Tag", func(t *testing.T) {
Expand Down
23 changes: 14 additions & 9 deletions pkg/abaputils/sap_com_0510.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,44 +225,44 @@ func (api *SAP_COM_0510) GetAction() (string, error) {
return abapStatusCode, nil
}

func (api *SAP_COM_0510) GetRepository() (bool, string, error) {
func (api *SAP_COM_0510) GetRepository() (bool, string, error, bool) {

if api.repository.Name == "" {
return false, "", errors.New("An empty string was passed for the parameter 'repositoryName'")
return false, "", errors.New("An empty string was passed for the parameter 'repositoryName'"), false
}

swcConnectionDetails := api.con
swcConnectionDetails.URL = api.con.URL + api.path + api.repositoryEntity + "('" + strings.Replace(api.repository.Name, "/", "%2F", -1) + "')"
resp, err := GetHTTPResponse("GET", swcConnectionDetails, nil, api.client)
if err != nil {
_, errRepo := HandleHTTPError(resp, err, "Reading the Repository / Software Component failed", api.con)
return false, "", errRepo
return false, "", errRepo, false
}
defer resp.Body.Close()

var body RepositoryEntity
var abapResp map[string]*json.RawMessage
bodyText, errRead := io.ReadAll(resp.Body)
if errRead != nil {
return false, "", err
return false, "", err, false
}

if err := json.Unmarshal(bodyText, &abapResp); err != nil {
return false, "", err
return false, "", err, false
}
if err := json.Unmarshal(*abapResp["d"], &body); err != nil {
return false, "", err
return false, "", err, false
}
if reflect.DeepEqual(RepositoryEntity{}, body) {
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", api.repository.Name).WithField("branchName", api.repository.Branch).WithField("commitID", api.repository.CommitID).WithField("Tag", api.repository.Tag).Error("Could not Clone the Repository / Software Component")
err := errors.New("Request to ABAP System not successful")
return false, "", err
return false, "", err, false
}

if body.AvailOnInst {
return true, body.ActiveBranch, nil
return true, body.ActiveBranch, nil, false
}
return false, "", err
return false, "", err, false

}

Expand Down Expand Up @@ -399,3 +399,8 @@ func (api *SAP_COM_0510) ConvertTime(logTimeStamp string) time.Time {
t := time.Unix(n, 0).UTC()
return t
}

// Dummy implementation of the "optional" method UpdateRepoWithBYOGCredentials (only used in SAP_COM_0948)
func (api *SAP_COM_0510) UpdateRepoWithBYOGCredentials(byogAuthMethod string, byogUsername string, byogPassword string) {
panic("UpdateRepoWithBYOGCredentials cannot be used in SAP_COM_0510")
}
2 changes: 1 addition & 1 deletion pkg/abaputils/sap_com_0510_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func TestGetRepo(t *testing.T) {
assert.NoError(t, err)
assert.IsType(t, &SAP_COM_0510{}, api.(*SAP_COM_0510), "API has wrong type")

cloned, activeBranch, errAction := api.GetRepository()
cloned, activeBranch, errAction, _ := api.GetRepository()
assert.True(t, cloned)
assert.Equal(t, "testBranch1", activeBranch)
assert.NoError(t, errAction)
Expand Down
Loading
Loading