Skip to content

Commit

Permalink
fixed setting git ref for branch with slashes in name (#4554)
Browse files Browse the repository at this point in the history
Co-authored-by: sumeet patil <[email protected]>
  • Loading branch information
daskuznetsova and sumeetpatil authored Sep 12, 2023
1 parent 1aac091 commit 1704758
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
38 changes: 38 additions & 0 deletions test/groovy/SetupCommonPipelineEnvironmentTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,44 @@ class SetupCommonPipelineEnvironmentTest extends BasePiperTest {
assertThat(nullScript.commonPipelineEnvironment.gitRef, is('refs/heads/testbranch'))
}

@Test
void "Set scmInfo parameter sets git reference for branch with slashes in name, origin"() {

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: 'origin/testbranch/001']

stepRule.step.setupCommonPipelineEnvironment(script: nullScript, utils: utilsMock, scmInfo: dummyScmInfo, gitUtils: gitUtils)
assertThat(nullScript.commonPipelineEnvironment.gitRef, is('refs/heads/testbranch/001'))
}

@Test
void "Set scmInfo parameter sets git reference for branch with slashes in name, not origin"() {

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: 'testbranch/001']

stepRule.step.setupCommonPipelineEnvironment(script: nullScript, utils: utilsMock, scmInfo: dummyScmInfo, gitUtils: gitUtils)
assertThat(nullScript.commonPipelineEnvironment.gitRef, is('refs/heads/testbranch/001'))
}

@Test
void "sets gitReference and gitRemoteCommit for pull request, head strategy"() {

Expand Down
5 changes: 4 additions & 1 deletion vars/setupCommonPipelineEnvironment.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ private void setGitRefOnCommonPipelineEnvironment(script, String gitCommit, Stri
}

if(gitBranch.contains("/")){
gitBranch = gitBranch.split("/")[1]
gitBranchSplit = gitBranch.split("/")
if(gitBranchSplit[0] == "origin") {
gitBranch = gitBranchSplit[1..-1].join("/")
}
}

if (!gitBranch.contains("PR")) {
Expand Down

0 comments on commit 1704758

Please sign in to comment.