Skip to content

Commit

Permalink
[BXMSPROD-1126] Return null in getRepositoryScm when non-existent (ki…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin-Mok authored Dec 14, 2020
1 parent 10c7272 commit ac69447
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
14 changes: 13 additions & 1 deletion .jenkins/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,22 @@ pipeline {
}
}
}

stage('Test getRepositoryScm') {
steps {
script {
def existingBranch = githubscm.getRepositoryScm(repo, changeAuthor, changeBranch)
assert existingBranch != null

def nonExistentBranch = githubscm.getRepositoryScm(repo, 'kiegroup', 'non-existent-branch')
assert nonExistentBranch == null
}
}
}
}
post {
always {
cleanWs()
}
}
}
}
22 changes: 18 additions & 4 deletions test/vars/GithubScmSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
forkListInfo = mockJson('/forked_projects.json')
forkListInfoPage3 = mockJson('/forked_projects_page3.json')
forkListInfoEmpty = mockJson('/forked_projects_empty.json')

getPipelineMock("sh")([returnStdout: true, script: 'mktemp -d']) >> {
return 'tempDir'
}
}

def mockJson(def fileName) {
Expand Down Expand Up @@ -59,7 +63,7 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
groovyScript.checkoutIfExists('repository', 'author', 'branches', 'defaultAuthor', 'master')
then:
1 * getPipelineMock("checkout")(gitSCM)
2 * getPipelineMock("checkout")(gitSCM)
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "curl -H \"Authorization: token oauth_token\" 'https://api.github.com/repos/defaultAuthor/repository/pulls?head=author:branches&state=open'"]) >> pullRequestInfo
}

Expand All @@ -71,7 +75,7 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
then:
0 * getPipelineMock("checkout")(null)

1 * getPipelineMock("checkout")(gitSCM)
2 * getPipelineMock("checkout")(gitSCM)
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "curl -H \"Authorization: token oauth_token\" 'https://api.github.com/repos/defaultAuthor/repository/pulls?head=author:branches&state=open'"]) >> pullRequestInfo
}

Expand All @@ -83,7 +87,7 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
groovyScript.checkoutIfExists('repository', 'author', 'branches', 'defaultAuthor', 'master', true)
then:
0 * getPipelineMock('checkout')(repositoryScmInformation)
1 * getPipelineMock('checkout')(repositoryScmInformation)
1 * getPipelineMock('usernameColonPassword.call')([credentialsId: 'kie-ci', variable: 'kieCiUserPassword']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock('checkout')(repositoryScmInformationMaster)
Expand Down Expand Up @@ -153,7 +157,7 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
groovyScript.checkoutIfExists('repository', 'kiegroup', 'master', 'kiegroup', 'master')
then:
2 * getPipelineMock("sh")(['returnStdout': true, 'script': "curl -H \"Authorization: token oauth_token\" 'https://api.github.com/repos/kiegroup/repository/pulls?head=kiegroup:master&state=open'"]) >> pullRequestInfoEmpty
1 * getPipelineMock("checkout")(gitSCM)
2 * getPipelineMock("checkout")(gitSCM)
0 * getPipelineMock("sh")(['returnStdout': true, 'script': 'git log --oneline -1'])
}

Expand All @@ -175,6 +179,16 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
result == gitSCM
}

def "[githubscm.groovy] getRepositoryScm with non-existent branch"() {
setup:
def gitSCM = [$class: "GitSCM", branches: [[name: "branches"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: "CleanBeforeCheckout"], [$class: "SubmoduleOption", disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: "", trackingSubmodules: false], [$class: "RelativeTargetDirectory", relativeTargetDir: "./"]], "submoduleCfg": [], "userRemoteConfigs": [["credentialsId": "kie-ci", "url": "https://github.com/author/repository.git"]]]
getPipelineMock('checkout')(gitSCM) >> { throw new Exception() }
when:
def result = groovyScript.getRepositoryScm('repository', 'author', 'branches')
then:
result == null
}

def "[githubscm.groovy] mergeSourceIntoTarget"() {
setup:
groovyScript.getBinding().setVariable("kieCiUserPassword", 'user:password')
Expand Down
15 changes: 9 additions & 6 deletions vars/githubscm.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ def checkoutIfExists(String repository, String author, String branches, String d
}

def getRepositoryScm(String repository, String author, String branches, String credentialId = 'kie-ci') {
println "[INFO] Resolving repository ${repository} author ${author} branches ${branches}"
def repositoryScm = null
try {
repositoryScm = resolveRepository(repository, author, branches, true, credentialId)
} catch (Exception ex) {
println "[WARNING] Branches [${branches}] from repository ${repository} not found in ${author} organisation."
def repositoryScm = resolveRepository(repository, author, branches, true, credentialId)
def tempDir = sh(script: 'mktemp -d', returnStdout: true).trim()
dir(tempDir) {
try {
checkout repositoryScm
} catch (Exception ex) {
println "[WARNING] Branches [${branches}] from repository ${repository} not found in ${author} organisation."
repositoryScm = null
}
}
return repositoryScm
}
Expand Down

0 comments on commit ac69447

Please sign in to comment.