1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-04-17 12:06:28 +02:00

Improve determining PR key in Azure DevOps provider. (#3007)

This commit is contained in:
Martin Zuber 2021-08-02 10:08:33 +02:00 committed by GitHub
parent 6d2bc023b0
commit aa50cfb78d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -25,10 +25,20 @@ func (a *AzureDevOpsConfigProvider) GetRepoUrl() string {
}
func (a *AzureDevOpsConfigProvider) GetPullRequestConfig() PullRequestConfig {
prKey := os.Getenv("SYSTEM_PULLREQUEST_PULLREQUESTID")
// This variable is populated for pull requests which have a different pull request ID and pull request number.
// In this case the pull request ID will contain an internal numeric ID and the pull request number will be provided
// as part of the 'SYSTEM_PULLREQUEST_PULLREQUESTNUMBER' environment variable.
prNumber, prNumberEnvVarSet := os.LookupEnv("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER")
if prNumberEnvVarSet == true {
prKey = prNumber
}
return PullRequestConfig{
Branch: os.Getenv("SYSTEM_PULLREQUEST_SOURCEBRANCH"),
Base: os.Getenv("SYSTEM_PULLREQUEST_TARGETBRANCH"),
Key: os.Getenv("SYSTEM_PULLREQUEST_PULLREQUESTID"),
Key: prKey,
}
}

View File

@ -45,6 +45,24 @@ func TestAzure(t *testing.T) {
assert.Equal(t, "42", c.Key)
})
t.Run("PR - Branch Policy", func(t *testing.T) {
defer resetEnv(os.Environ())
os.Clearenv()
os.Setenv("SYSTEM_PULLREQUEST_SOURCEBRANCH", "feat/test-azure")
os.Setenv("SYSTEM_PULLREQUEST_TARGETBRANCH", "main")
os.Setenv("SYSTEM_PULLREQUEST_PULLREQUESTID", "123456789")
os.Setenv("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER", "42")
os.Setenv("BUILD_REASON", "PullRequest")
p := AzureDevOpsConfigProvider{}
c := p.GetPullRequestConfig()
assert.True(t, p.IsPullRequest())
assert.Equal(t, "feat/test-azure", c.Branch)
assert.Equal(t, "main", c.Base)
assert.Equal(t, "42", c.Key)
})
t.Run("Azure DevOps - false", func(t *testing.T) {
defer resetEnv(os.Environ())
os.Clearenv()