@@ -10,10 +10,22 @@ import com.cloudbees.jenkins.GitHubRepositoryName
1010* - https://github.com/jenkinsci/github-plugin/blob/master/src/main/java/com/cloudbees/jenkins/GitHubRepositoryName.java
1111* */
1212class GitHubHelper {
13+
14+ static String getRepositoryUrl (CpsScript script ){
15+ return script. scm. getUserRemoteConfigs()[0 ]. getUrl()
16+ }
17+
1318 static GHRepository getGitHubRepository (CpsScript script ){
1419 return getGitHubRepository(script. scm. getUserRemoteConfigs()[0 ]. getUrl())
1520 }
1621
22+ @NonCPS
23+ private static String stackTraceAsString (Throwable t ) {
24+ StringWriter sw = new StringWriter ();
25+ t. printStackTrace(new PrintWriter (sw));
26+ return sw. toString()
27+ }
28+
1729 @NonCPS
1830 static GHRepository getGitHubRepository (String url ){
1931 return GitHubRepositoryName . create(url). resolveOne()
@@ -28,7 +40,7 @@ class GitHubHelper {
2840 }
2941
3042 @NonCPS
31- static boolean mergeAndClosePullRequest (String repositoryUrl , int prNumber ){
43+ static boolean mergeAndClosePullRequest (String repositoryUrl , int prNumber , String mergeMethod ){
3244 GHRepository repo= getGitHubRepository(repositoryUrl)
3345 GHPullRequest pullRequest = repo. getPullRequest(prNumber)
3446 Boolean mergeable = pullRequest. getMergeable()
@@ -38,22 +50,23 @@ class GitHubHelper {
3850
3951 if (state != GHIssueState . CLOSED ) {
4052 GHCommitPointer head = pullRequest. getHead()
41- if (pullRequest. getRepository(). getFullName(). equalsIgnoreCase(head. getRepository(). getFullName())) {
42- if (! pullRequest. isMerged()) {
43- if (mergeable != null && mergeable. booleanValue() == true ) {
44- pullRequest. merge(" Merged PR-${ prNumber} " , head. getSha(), GHPullRequest.MergeMethod . MERGE )
45- } else {
46- doClose = false
47- }
53+ if (! pullRequest. isMerged()) {
54+ if (mergeable != null && mergeable. booleanValue() == true ) {
55+ pullRequest. merge(" Merged PR-${ prNumber} " , head. getSha(), GHPullRequest.MergeMethod . valueOf(mergeMethod. toUpperCase()))
56+ } else {
57+ doClose = false
4858 }
59+ }
4960
61+ if (doClose && pullRequest. getRepository(). getFullName(). equalsIgnoreCase(head. getRepository(). getFullName())) {
5062 if (head. getRef() != null ) {
5163 GHRef headRef = repo. getRef(' heads/' + head. getRef())
5264 if (headRef != null ) {
5365 headRef. delete()
5466 }
5567 }
5668 }
69+
5770 if (doClose){
5871 pullRequest. close()
5972 ret = true
@@ -64,9 +77,35 @@ class GitHubHelper {
6477
6578 return ret
6679 }
67-
6880 static boolean mergeAndClosePullRequest (CpsScript script ) {
69- return mergeAndClosePullRequest(script. scm. getUserRemoteConfigs()[0 ]. getUrl(), Integer . parseInt(script. env. CHANGE_ID ))
81+ return mergeAndClosePullRequest(script, ' merge' )
82+ }
83+ static boolean mergeAndClosePullRequest (CpsScript script , String mergeMethod ) {
84+ try {
85+ return mergeAndClosePullRequest(getRepositoryUrl(script), Integer . parseInt(script. env. CHANGE_ID ), mergeMethod)
86+ }catch (ex){
87+ // This need to be done because the github API does NOT return serializable Exceptions
88+ script. echo " Original Stack Trace:\n ${ stackTraceAsString(ex)} "
89+ throw new IOException (ex. message)
90+ }
91+ }
92+
93+ static void commentOnPullRequest (CpsScript script , String comment ) {
94+ try {
95+ commentOnPullRequest(getRepositoryUrl(script), Integer . parseInt(script. env. CHANGE_ID ), comment)
96+ }catch (ex){
97+ // This need to be done because the github API does NOT return serializable Exceptions
98+ script. echo " Original Stack Trace:\n ${ stackTraceAsString(ex)} "
99+ throw new IOException (ex. message)
100+ }
101+ }
102+
103+ @NonCPS
104+ static void commentOnPullRequest (String repositoryUrl , int pullRequestNumber , String comment ) {
105+ GHRepository repo= getGitHubRepository(repositoryUrl)
106+ GHPullRequest pullRequest = repo. getPullRequest(pullRequestNumber)
107+ pullRequest. comment(comment)
108+
70109 }
71110
72111 static GHDeploymentBuilder createDeployment (CpsScript script , String ref ) {
@@ -160,14 +199,14 @@ class GitHubHelper {
160199 return createDeploymentStatus(script. scm. getUserRemoteConfigs()[0 ]. getUrl(), deploymentId, statusName, config)
161200 }
162201 @NonCPS
163- void createCommitStatus (String url , String sha1 , String statusName , String targetUrl , String description , String context ) {
202+ static void createCommitStatus (String url , String sha1 , String statusName , String targetUrl , String description , String context ) {
164203 def ghRepo= getGitHubRepository(url)
165204 def ghCommitState= GHCommitState . valueOf(statusName)
166205
167206 ghRepo. createCommitStatus(sha1, ghCommitState, targetUrl, description, context)
168207 }
169208
170- void createCommitStatus (CpsScript script , String ref , String statusName , String targetUrl , String description , String context ) {
209+ static void createCommitStatus (CpsScript script , String ref , String statusName , String targetUrl , String description , String context ) {
171210 createCommitStatus(script. scm. getUserRemoteConfigs()[0 ]. getUrl() as String , ref, statusName, targetUrl, description, context)
172211 }
173212}
0 commit comments