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

fix(setupCommonPipelineEnvironment): handling tags from scm info (#5219)

* added check if git branch name starts with refs/

* added check if branch name from jenkins starts with refs/

* added tests

* hardcoded refs/tags to test

* removed hardcode

---------

Co-authored-by: sumeet patil <sumeet.patil@sap.com>
This commit is contained in:
Daria Kuznetsova 2025-01-09 11:19:26 +01:00 committed by GitHub
parent 957f1f1676
commit 38f9e5b7f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 47 additions and 3 deletions

View File

@ -249,6 +249,8 @@ func (j *jenkinsConfigProvider) GitReference() string {
return ref
} else if strings.Contains(ref, "PR") {
return "refs/pull/" + strings.Split(ref, "-")[1] + "/head"
} else if strings.HasPrefix(ref, "refs/") {
return ref
} else {
return "refs/heads/" + ref
}

View File

@ -6,14 +6,13 @@ package orchestrator
import (
"encoding/json"
"fmt"
"net/http"
"os"
"testing"
"time"
"github.com/pkg/errors"
"net/http"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
@ -40,6 +39,26 @@ func TestJenkins(t *testing.T) {
assert.Equal(t, "Jenkins", p.OrchestratorType())
})
t.Run("TagBuild", func(t *testing.T) {
defer resetEnv(os.Environ())
os.Clearenv()
os.Setenv("JENKINS_URL", "FOO BAR BAZ")
os.Setenv("BUILD_URL", "https://jaas.url/job/foo/job/bar/job/main/1234/")
os.Setenv("BRANCH_NAME", "refs/tags/rel-1.0.0")
os.Setenv("GIT_COMMIT", "abcdef42713")
os.Setenv("GIT_URL", "github.com/foo/bar")
p := &jenkinsConfigProvider{}
assert.False(t, p.IsPullRequest())
assert.Equal(t, "https://jaas.url/job/foo/job/bar/job/main/1234/", p.BuildURL())
assert.Equal(t, "refs/tags/rel-1.0.0", p.Branch())
assert.Equal(t, "refs/tags/rel-1.0.0", p.GitReference())
assert.Equal(t, "abcdef42713", p.CommitSHA())
assert.Equal(t, "github.com/foo/bar", p.RepoURL())
assert.Equal(t, "Jenkins", p.OrchestratorType())
})
t.Run("PR", func(t *testing.T) {
defer resetEnv(os.Environ())
os.Clearenv()

View File

@ -374,6 +374,25 @@ class SetupCommonPipelineEnvironmentTest extends BasePiperTest {
assertThat(nullScript.commonPipelineEnvironment.gitRef, is('refs/heads/testbranch/001'))
}
@Test
void "Set scmInfo parameter sets git reference for tag"() {
def GitUtils gitUtils = new GitUtils() {
boolean isMergeCommit(){
return false
}
}
helper.registerAllowedMethod("fileExists", [String], { String path ->
return path.endsWith('.pipeline/config.yml')
})
def dummyScmInfo = [GIT_COMMIT: 'dummy_git_commit_id', GIT_BRANCH: 'refs/tags/tag-1.0.0']
stepRule.step.setupCommonPipelineEnvironment(script: nullScript, utils: utilsMock, scmInfo: dummyScmInfo, gitUtils: gitUtils)
assertThat(nullScript.commonPipelineEnvironment.gitRef, is('refs/tags/tag-1.0.0'))
}
@Test
void "sets gitReference and gitRemoteCommit for pull request, head strategy"() {

View File

@ -272,7 +272,11 @@ private void setGitRefOnCommonPipelineEnvironment(script, String gitCommit, Stri
}
if (!gitBranch.contains("PR")) {
script.commonPipelineEnvironment.setGitRef("refs/heads/" + gitBranch)
if (gitBranch.startsWith("refs/") ){
script.commonPipelineEnvironment.setGitRef(gitBranch)
} else {
script.commonPipelineEnvironment.setGitRef("refs/heads/" + gitBranch)
}
script.commonPipelineEnvironment.setGitRemoteCommitId(gitCommit)
return
}