Skip to content

Commit 1ece2d5

Browse files
authored
kie-issues#768 - Use Github CLI instead of hub CLI in jenkins shared libraries (#1158)
* Replace hub cli with Github CLI * Fix log messages * Update tests * Update Jenkins Jira plugin version * Fix tests * Fix tests
1 parent e633caa commit 1ece2d5

File tree

3 files changed

+67
-58
lines changed

3 files changed

+67
-58
lines changed

jenkins-pipeline-shared-libraries/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@
624624
<dependency>
625625
<groupId>org.jenkins-ci.plugins</groupId>
626626
<artifactId>jira</artifactId>
627-
<version>3.0.8</version>
627+
<version>3.12</version>
628628
<scope>test</scope>
629629
</dependency>
630630
<dependency>

jenkins-pipeline-shared-libraries/test/vars/GithubScmSpec.groovy

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,12 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
364364
when:
365365
groovyScript.forkRepo()
366366
then:
367-
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
368367
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
369368
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
370369

371370
1 * getPipelineMock("sh")('git config user.email [email protected]')
372371
1 * getPipelineMock("sh")('git config user.name user')
373-
1 * getPipelineMock("sh")('git config hub.protocol https')
374-
1 * getPipelineMock("sh")('hub fork --remote-name=origin')
372+
1 * getPipelineMock("sh")('gh repo fork --remote-name=origin')
375373
1 * getPipelineMock("sh")('git remote -v')
376374
}
377375

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

389386
1 * getPipelineMock("sh")('git config user.email [email protected]')
390387
1 * getPipelineMock("sh")('git config user.name user')
391-
1 * getPipelineMock("sh")('git config hub.protocol https')
392-
1 * getPipelineMock("sh")('hub fork --remote-name=origin')
388+
1 * getPipelineMock("sh")('gh repo fork --remote-name=origin')
393389
1 * getPipelineMock("sh")('git remote -v')
394390
}
395391

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

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

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

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

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

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

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

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

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

533-
def "[githubscm.groovy] executeHub with credentials"() {
520+
def "[githubscm.groovy] executeGHCli with credentials"() {
534521
setup:
535522
groovyScript.getBinding().setVariable("GITHUB_USER", 'user')
536523
groovyScript.getBinding().setVariable("GITHUB_TOKEN", 'password')
537524
when:
538-
def result = groovyScript.executeHub('hubCommand', 'credentialsId')
525+
def result = groovyScript.executeGHCli('command', 'credentialsId')
539526
then:
540-
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
541527
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'credentialsId', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
542528
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
543529
1 * getPipelineMock("sh")('git config user.email [email protected]')
544530
1 * getPipelineMock("sh")('git config user.name user')
545-
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "hubCommand"]) >> 'shResult'
531+
1 * getPipelineMock("sh")(['returnStdout': true, 'script': "command"]) >> 'shResult'
546532
}
547533

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

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

@@ -583,12 +567,40 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
583567
when:
584568
groovyScript.mergePR('pullRequestLink', 'credentialsId')
585569
then:
586-
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
587570
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'credentialsId', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
588571
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
589572
1 * getPipelineMock("sh")('git config user.email [email protected]')
590573
1 * getPipelineMock("sh")('git config user.name user')
591-
1 * getPipelineMock("sh")('hub merge pullRequestLink')
574+
1 * getPipelineMock("sh")('gh pr merge --admin --rebase --delete-branch pullRequestLink')
575+
}
576+
577+
def "[githubscm.groovy] approvePR without Credentials throwing exception"() {
578+
setup:
579+
groovyScript.getBinding().setVariable("GITHUB_USER", 'user')
580+
groovyScript.getBinding().setVariable("GITHUB_TOKEN", 'password')
581+
when:
582+
groovyScript.approvePR('pullRequestLink')
583+
then:
584+
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'kie-ci', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
585+
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
586+
1 * getPipelineMock("sh")('git config user.email [email protected]')
587+
1 * getPipelineMock("sh")('git config user.name user')
588+
1 * getPipelineMock("sh")('gh pr review pullRequestLink --approve') >> { throw new Exception('approve error') }
589+
thrown(Exception)
590+
}
591+
592+
def "[githubscm.groovy] approvePR with Credentials"() {
593+
setup:
594+
groovyScript.getBinding().setVariable("GITHUB_USER", 'user')
595+
groovyScript.getBinding().setVariable("GITHUB_TOKEN", 'password')
596+
when:
597+
groovyScript.approvePR('pullRequestLink', 'credentialsId')
598+
then:
599+
1 * getPipelineMock('usernamePassword.call')([credentialsId: 'credentialsId', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN']) >> 'userNamePassword'
600+
1 * getPipelineMock("withCredentials")(['userNamePassword'], _ as Closure)
601+
1 * getPipelineMock("sh")('git config user.email [email protected]')
602+
1 * getPipelineMock("sh")('git config user.name user')
603+
1 * getPipelineMock("sh")('gh pr review pullRequestLink --approve')
592604
}
593605

594606
def "[githubscm.groovy] tagRepository with buildTag"() {
@@ -1023,13 +1035,6 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
10231035
result == '* BXMSPROD-819'
10241036
}
10251037
1026-
def "[githubscm.groovy] cleanHubAuth"() {
1027-
when:
1028-
groovyScript.cleanHubAuth()
1029-
then:
1030-
1 * getPipelineMock("sh")("rm -rf ~/.config/hub")
1031-
}
1032-
10331038
def "[githubscm.groovy] cleanWorkingTree"() {
10341039
when:
10351040
groovyScript.cleanWorkingTree()

0 commit comments

Comments
 (0)