Skip to content

Commit

Permalink
fix(setupCommonPipelineEnvironment): handling tags from scm info (#5219)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
daskuznetsova and sumeetpatil authored Jan 9, 2025
1 parent 957f1f1 commit 38f9e5b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/orchestrator/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 21 additions & 2 deletions pkg/orchestrator/jenkins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down
19 changes: 19 additions & 0 deletions test/groovy/SetupCommonPipelineEnvironmentTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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"() {

Expand Down
6 changes: 5 additions & 1 deletion vars/setupCommonPipelineEnvironment.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 38f9e5b

Please sign in to comment.