Skip to content

Commit

Permalink
kie-issues#771: fix pullrequest.getAuthorAndRepoForPr() method (#1148)
Browse files Browse the repository at this point in the history
Co-authored-by: jstastny-cz <[email protected]>
  • Loading branch information
jstastny-cz and jstastny-cz authored Dec 15, 2023
1 parent c15cc70 commit cfc00ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 12 additions & 0 deletions jenkins-pipeline-shared-libraries/test/vars/PullRequestSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ class PullRequestSpec extends JenkinsPipelineSpecification {
then:
result == 'owner/repo'
}

def "PR from fork with matching name getAuthorAndRepoForPr" () {
setup:
def env = [:]
env['CHANGE_FORK']='contributor'
env['CHANGE_URL']='https://github.com/owner/repo/pull/1'
groovyScript.getBinding().setVariable('env', env)
when:
def result = groovyScript.getAuthorAndRepoForPr()
then:
result == 'contributor/repo'
}
}
11 changes: 9 additions & 2 deletions jenkins-pipeline-shared-libraries/vars/pullrequest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ String getAuthorAndRepoForPr() {
if (!env.CHANGE_FORK && !env.CHANGE_URL) {
error "CHANGE_FORK neither CHANGE_URL variables are set. Are you sure you're running with Github Branch Source plugin?"
}
def group = ''
def repository = ''
if (env.CHANGE_FORK) {
return env.CHANGE_FORK
def parsedFork = env.CHANGE_FORK.split('/')
group = parsedFork[0]
if (parsedFork.length==2) {
repository = parsedFork[1]
}
}
String fullUrl = env.CHANGE_URL
String urlWithoutProtocol = fullUrl.split('://')[1]
String path = urlWithoutProtocol.substring(urlWithoutProtocol.indexOf('/'))
return path.substring(1, path.indexOf('/pull/'))
def parsedUrl = path.substring(1, path.indexOf('/pull/')).split('/')
return "${group?:parsedUrl[0]}/${repository?:parsedUrl[1]}"
}

0 comments on commit cfc00ab

Please sign in to comment.