Skip to content

Commit

Permalink
kie-issues#768 - Use Github CLI instead of hub CLI in jenkins shared …
Browse files Browse the repository at this point in the history
…libraries (apache#1158)

* Replace hub cli with Github CLI

* Fix log messages

* Update tests

* Update Jenkins Jira plugin version

* Fix tests

* Fix tests
  • Loading branch information
rodrigonull authored Jan 23, 2024
1 parent e633caa commit 1ece2d5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 58 deletions.
2 changes: 1 addition & 1 deletion jenkins-pipeline-shared-libraries/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jira</artifactId>
<version>3.0.8</version>
<version>3.12</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
87 changes: 46 additions & 41 deletions jenkins-pipeline-shared-libraries/test/vars/GithubScmSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,12 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
groovyScript.forkRepo()
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)

1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")('git config hub.protocol https')
1 * getPipelineMock("sh")('hub fork --remote-name=origin')
1 * getPipelineMock("sh")('gh repo fork --remote-name=origin')
1 * getPipelineMock("sh")('git remote -v')
}

Expand All @@ -382,14 +380,12 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
groovyScript.forkRepo('credentialsId')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'credentialsId', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)

1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")('git config hub.protocol https')
1 * getPipelineMock("sh")('hub fork --remote-name=origin')
1 * getPipelineMock("sh")('gh repo fork --remote-name=origin')
1 * getPipelineMock("sh")('git remote -v')
}

Expand All @@ -400,12 +396,11 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
def result = groovyScript.createPR('PR Title')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hub pull-request -m 'PR Title' -m '' -b 'main'"]) >> 'shResult'
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "gh pr create -t 'PR Title' -b '' -B 'main'"]) >> 'shResult'
}

def "[githubscm.groovy] createPR without Credentials and target branch"() {
Expand All @@ -415,12 +410,11 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
def result = groovyScript.createPR('PR Title', 'PR body.')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hub pull-request -m 'PR Title' -m 'PR body.' -b 'main'"]) >> 'shResult'
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "gh pr create -t 'PR Title' -b 'PR body.' -B 'main'"]) >> 'shResult'
}

def "[githubscm.groovy] createPR without Credentials and target branch throwing exception"() {
Expand All @@ -430,12 +424,11 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
def result = groovyScript.createPR('PR Title')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hub pull-request -m 'PR Title' -m '' -b 'main'"]) >> { throw new Exception('error') }
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "gh pr create -t 'PR Title' -b '' -B 'main'"]) >> { throw new Exception('error') }
thrown(Exception)
}

Expand All @@ -446,12 +439,11 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
def result = groovyScript.createPR('PR Title', 'PR body.', 'targetBranch', 'credentialsId')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'credentialsId', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hub pull-request -m 'PR Title' -m 'PR body.' -b 'targetBranch'"]) >> 'shResult'
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "gh pr create -t 'PR Title' -b 'PR body.' -B 'targetBranch'"]) >> 'shResult'
}

def "[githubscm.groovy] createPrAsDraft without body, Credentials and target branch"() {
Expand All @@ -461,12 +453,11 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
def result = groovyScript.createPrAsDraft('PR Title')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hub pull-request -d -m 'PR Title' -m '' -b 'main'"]) >> 'shResult'
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "gh pr create -d -t 'PR Title' -b '' -B 'main'"]) >> 'shResult'
}

def "[githubscm.groovy] createPrAsDraft without Credentials and target branch"() {
Expand All @@ -476,12 +467,11 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
def result = groovyScript.createPrAsDraft('PR Title', 'PR body.')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hub pull-request -d -m 'PR Title' -m 'PR body.' -b 'main'"]) >> 'shResult'
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "gh pr create -d -t 'PR Title' -b 'PR body.' -B 'main'"]) >> 'shResult'
}

def "[githubscm.groovy] createPrAsDraft without Credentials and target branch throwing exception"() {
Expand All @@ -491,12 +481,11 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
def result = groovyScript.createPrAsDraft('PR Title')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hub pull-request -d -m 'PR Title' -m '' -b 'main'"]) >> { throw new Exception('draft error') }
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "gh pr create -d -t 'PR Title' -b '' -B 'main'"]) >> { throw new Exception('draft error') }
thrown(Exception)
}

Expand All @@ -507,12 +496,11 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
def result = groovyScript.createPrAsDraft('PR Title', 'PR body.', 'targetBranch', 'credentialsId')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'credentialsId', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hub pull-request -d -m 'PR Title' -m 'PR body.' -b 'targetBranch'"]) >> 'shResult'
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "gh pr create -d -t 'PR Title' -b 'PR body.' -B 'targetBranch'"]) >> 'shResult'
}

def "[githubscm.groovy] createPRWithLabels with body, Credentials and target branch"() {
Expand All @@ -522,27 +510,25 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
def result = groovyScript.createPRWithLabels('PR Title', 'PR body.', 'targetBranch', ['label1', 'label2'] as String[], 'credentialsId')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'credentialsId', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hub pull-request -m 'PR Title' -m 'PR body.' -b 'targetBranch' -l 'label1','label2'"]) >> 'shResult'
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "gh pr create -t 'PR Title' -b 'PR body.' -B 'targetBranch' -l 'label1','label2'"]) >> 'shResult'
}

def "[githubscm.groovy] executeHub with credentials"() {
def "[githubscm.groovy] executeGHCli with credentials"() {
setup:
groovyScript.getBinding().setVariable("GITHUB_USER", 'user')
groovyScript.getBinding().setVariable("GITHUB_TOKEN", 'password')
when:
def result = groovyScript.executeHub('hubCommand', 'credentialsId')
def result = groovyScript.executeGHCli('command', 'credentialsId')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'credentialsId', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hubCommand"]) >> 'shResult'
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "command"]) >> 'shResult'
}

def "[githubscm.groovy] mergePR without Credentials"() {
Expand All @@ -552,12 +538,11 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
groovyScript.mergePR('pullRequestLink')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")('hub merge pullRequestLink')
1 * getPipelineMock("sh")('gh pr merge --admin --rebase --delete-branch pullRequestLink')
}

def "[githubscm.groovy] mergePR without Credentials throwing exception"() {
Expand All @@ -567,12 +552,11 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
groovyScript.mergePR('pullRequestLink')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")('hub merge pullRequestLink') >> { throw new Exception('hub error') }
1 * getPipelineMock("sh")('gh pr merge --admin --rebase --delete-branch pullRequestLink') >> { throw new Exception('merge error') }
thrown(Exception)
}

Expand All @@ -583,12 +567,40 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
when:
groovyScript.mergePR('pullRequestLink', 'credentialsId')
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'credentialsId', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")('hub merge pullRequestLink')
1 * getPipelineMock("sh")('gh pr merge --admin --rebase --delete-branch pullRequestLink')
}

def "[githubscm.groovy] approvePR without Credentials throwing exception"() {
setup:
groovyScript.getBinding().setVariable("GITHUB_USER", 'user')
groovyScript.getBinding().setVariable("GITHUB_TOKEN", 'password')
when:
groovyScript.approvePR('pullRequestLink')
then:
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")('gh pr review pullRequestLink --approve') >> { throw new Exception('approve error') }
thrown(Exception)
}

def "[githubscm.groovy] approvePR with Credentials"() {
setup:
groovyScript.getBinding().setVariable("GITHUB_USER", 'user')
groovyScript.getBinding().setVariable("GITHUB_TOKEN", 'password')
when:
groovyScript.approvePR('pullRequestLink', 'credentialsId')
then:
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'credentialsId', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
1 * getPipelineMock("sh")('git config user.email [email protected]')
1 * getPipelineMock("sh")('git config user.name user')
1 * getPipelineMock("sh")('gh pr review pullRequestLink --approve')
}

def "[githubscm.groovy] tagRepository with buildTag"() {
Expand Down Expand Up @@ -1023,13 +1035,6 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
result == '* BXMSPROD-819'
}
def "[githubscm.groovy] cleanHubAuth"() {
when:
groovyScript.cleanHubAuth()
then:
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
}
def "[githubscm.groovy] cleanWorkingTree"() {
when:
groovyScript.cleanWorkingTree()
Expand Down
Loading

0 comments on commit 1ece2d5

Please sign in to comment.