-
Notifications
You must be signed in to change notification settings - Fork 210
/
Fabric8Commands.groovy
1042 lines (865 loc) · 35.2 KB
/
Fabric8Commands.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/groovy
package io.fabric8
import com.cloudbees.groovy.cps.NonCPS
import groovy.json.JsonSlurper
import io.fabric8.kubernetes.api.KubernetesHelper
import io.fabric8.kubernetes.client.DefaultKubernetesClient
import io.fabric8.kubernetes.client.KubernetesClient
import io.fabric8.openshift.client.DefaultOpenShiftClient
import io.fabric8.openshift.client.OpenShiftClient
import jenkins.model.Jenkins
import java.util.regex.Pattern
def swizzleImageName(text, match, replace) {
return Pattern.compile("image: ${match}:(.*)").matcher(text).replaceFirst("image: ${replace}")
}
def getReleaseVersionFromMavenMetadata(url) {
def cmd = "curl -L ${url} | grep '<latest' | cut -f2 -d'>'|cut -f1 -d'<'"
return sh(script: cmd, returnStdout: true).toString().trim()
}
def updatePackageJSONVersion(f, p, v) {
sh "sed -i -r 's/\"${p}\": \"[0-9][0-9]{0,2}.[0-9][0-9]{0,2}(.[0-9][0-9]{0,2})?(.[0-9][0-9]{0,2})?(-development)?\"/\"${p}\": \"${v}\"/g' ${f}"
}
def updateDockerfileEnvVar(f, p, v) {
sh "sed -i -r 's/ENV ${p}.*/ENV ${p} ${v}/g' ${f}"
}
def getProjectVersion() {
def file = readFile('pom.xml')
def project = new XmlSlurper().parseText(file)
return project.version.text()
}
def getReleaseVersion(String artifact) {
def modelMetaData = new XmlSlurper().parse("https://oss.sonatype.org/content/repositories/releases/${artifact}/maven-metadata.xml")
def version = modelMetaData.versioning.release.text()
return version
}
def getMavenCentralVersion(String artifact) {
def modelMetaData = new XmlSlurper().parse("http://central.maven.org/maven2/${artifact}/maven-metadata.xml")
def version = modelMetaData.versioning.release.text()
return version
}
def getVersion(String repo, String artifact) {
repo = removeTrailingSlash(repo)
artifact = removeTrailingSlash(artifact)
def modelMetaData = new XmlSlurper().parse(repo + '/' + artifact + '/maven-metadata.xml')
def version = modelMetaData.versioning.release.text()
return version
}
def isArtifactAvailableInRepo(String repo, String groupId, String artifactId, String version, String ext) {
repo = removeTrailingSlash(repo)
groupId = removeTrailingSlash(groupId)
artifactId = removeTrailingSlash(artifactId)
def url = new URL("${repo}/${groupId}/${artifactId}/${version}/${artifactId}-${version}.${ext}")
HttpURLConnection connection = url.openConnection()
connection.setRequestMethod("GET")
connection.setDoInput(true)
try {
connection.connect()
new InputStreamReader(connection.getInputStream(), "UTF-8")
return true
} catch (FileNotFoundException e1) {
echo "File not yet available: ${url.toString()}"
return false
} finally {
connection.disconnect()
}
}
def isFileAvailableInRepo(String repo, String path, String version, String artifact) {
repo = removeTrailingSlash(repo)
path = removeTrailingSlash(path)
version = removeTrailingSlash(version)
def url = new URL("${repo}/${path}/${version}/${artifact}")
HttpURLConnection connection = url.openConnection()
connection.setRequestMethod("GET")
connection.setDoInput(true)
try {
connection.connect()
new InputStreamReader(connection.getInputStream(), "UTF-8")
echo "File is available at: ${url.toString()}"
return true
} catch (FileNotFoundException e1) {
echo "File not yet available: ${url.toString()}"
return false
} finally {
connection.disconnect()
}
}
def removeTrailingSlash(String myString) {
if (myString.endsWith("/")) {
return myString.substring(0, myString.length() - 1)
}
return myString
}
def getRepoIds() {
// we could have multiple staging repos created, we need to write the names of all the generated files to a well known
// filename so we can use the workflow readFile (wildcards wont works and new File wont with slaves as groovy is executed on the master jenkins
// We write the names of the files that contain the repo ids used for staging. Each staging repo id is read from each file and returned as a list
sh 'find target/nexus-staging/staging/ -maxdepth 1 -name "*.properties" > target/nexus-staging/staging/repos.txt'
def repos = readFile('target/nexus-staging/staging/repos.txt')
def list = []
// workflow closure not working here https://issues.jenkins-ci.org/browse/JENKINS-26481
def filelines = new String(repos).split('\n')
for (int i = 0; i < filelines.size(); i++) {
def matcher = readFile(filelines[i]) =~ 'stagingRepository.id=(.+)'
list << matcher[0][1]
}
return list
}
def getDockerHubImageTags(String image) {
try {
return "https://registry.hub.docker.com/v1/repositories/${image}/tags".toURL().getText()
} catch (err) {
return "NO_IMAGE_FOUND"
}
}
def searchAndReplaceMavenVersionPropertyNoCommit(String property, String newVersion) {
// example matches <fabric8.version>2.3</fabric8.version> <fabric8.version>2.3.12</fabric8.version> <fabric8.version>2.3.12.5</fabric8.version>
sh "find -type f -name 'pom.xml' | xargs sed -i -r 's/${property}[0-9][0-9]{0,2}.[0-9][0-9]{0,2}(.[0-9][0-9]{0,2})?(.[0-9][0-9]{0,2})?</${property}${newVersion}</g'"
}
def searchAndReplaceMavenVersionProperty(String property, String newVersion) {
// example matches <fabric8.version>2.3</fabric8.version> <fabric8.version>2.3.12</fabric8.version> <fabric8.version>2.3.12.5</fabric8.version>
sh "find -type f -name 'pom.xml' | xargs sed -i -r 's/${property}[0-9][0-9]{0,2}.[0-9][0-9]{0,2}(.[0-9][0-9]{0,2})?(.[0-9][0-9]{0,2})?</${property}${newVersion}</g'"
sh "git commit -a -m 'Bump ${property} version'"
}
def searchAndReplaceMavenSnapshotProfileVersionProperty(String property, String newVersion) {
// example matches <fabric8.version>2.3-SNAPSHOT</fabric8.version> <fabric8.version>2.3.12-SNAPSHOT</fabric8.version> <fabric8.version>2.3.12.5-SNAPSHOT</fabric8.version>
sh "find -type f -name 'pom.xml' | xargs sed -i -r 's/${property}[0-9][0-9]{0,2}.[0-9][0-9]{0,2}(.[0-9][0-9]{0,2})?(.[0-9][0-9]{0,2})?-SNAPSHOT</${property}${newVersion}-SNAPSHOT</g'"
sh "git commit -a -m 'Bump ${property} development profile SNAPSHOT version'"
}
def setupWorkspaceForRelease(String project, Boolean useGitTagForNextVersion, String mvnExtraArgs = "", String currentVersion = "") {
setupGitSSH()
setupGPG()
sh "git tag -d \$(git tag)"
sh "git fetch --tags"
if (useGitTagForNextVersion) {
def newVersion = getNewVersionFromTag(currentVersion)
echo "New release version ${newVersion}"
sh "mvn -B -U versions:set -DnewVersion=${newVersion} " + mvnExtraArgs
sh "git commit -a -m 'release ${newVersion}'"
pushTag(newVersion)
} else {
sh 'mvn -B build-helper:parse-version versions:set -DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion} ' + mvnExtraArgs
}
def releaseVersion = getProjectVersion()
// delete any previous branches of this release
try {
sh "git checkout -b release-v${releaseVersion}"
} catch (err) {
sh "git branch -D release-v${releaseVersion}"
sh "git checkout -b release-v${releaseVersion}"
}
}
// if no previous tag found default 1.0.0 is used, else assume version is in the form major.minor or major.minor.micro version
def getNewVersionFromTag(pomVersion = null) {
def version = '1.0.0'
// Set known prerelease prefixes, needed for the proper sort order
// in the next command
sh "git config versionsort.prereleaseSuffix -RC"
sh "git config versionsort.prereleaseSuffix -M"
// if the repo has no tags this command will fail
sh "git tag --sort version:refname | tail -1 > version.tmp"
def tag = readFile 'version.tmp'
if (tag == null || tag.size() == 0) {
echo "no existing tag found using version ${version}"
return version
}
tag = tag.trim()
echo "Testing to see if version ${tag} is semver compatible"
def semver = tag =~ /(?i)\bv?(?<major>0|[1-9]\d*)(?:\.(?<minor>0|[1-9]\d*)(?:\.(?<patch>0|[1-9]\d*))?)?(?:-(?<prerelease>[\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+(?<build>[\da-z\-]+(?:\.[\da-z\-]+)*))?\b/
if (semver.matches()) {
echo "Version ${tag} is semver compatible"
def majorVersion = semver.group('major') as int
def minorVersion = (semver.group('minor') ?: 0) as int
def patchVersion = ((semver.group('patch') ?: 0) as int) + 1
echo "Testing to see if current POM version ${pomVersion} is semver compatible"
def pomSemver = pomVersion.trim() =~ /(?i)\bv?(?<major>0|[1-9]\d*)(?:\.(?<minor>0|[1-9]\d*)(?:\.(?<patch>0|[1-9]\d*))?)?(?:-(?<prerelease>[\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+(?<build>[\da-z\-]+(?:\.[\da-z\-]+)*))?\b/
if (pomSemver.matches()) {
echo "Current POM version ${pomVersion} is semver compatible"
def pomMajorVersion = pomSemver.group('major') as int
def pomMinorVersion = (pomSemver.group('minor') ?: 0) as int
def pomPatchVersion = (pomSemver.group('patch') ?: 0) as int
if (pomMajorVersion > majorVersion ||
(pomMajorVersion == majorVersion &&
(pomMinorVersion > minorVersion) || (pomMinorVersion == minorVersion && pomPatchVersion > patchVersion)
)
) {
majorVersion = pomMajorVersion
minorVersion = pomMinorVersion
patchVersion = pomPatchVersion
}
}
def newVersion = "${majorVersion}.${minorVersion}.${patchVersion}"
echo "New version is ${newVersion}"
return newVersion
} else {
echo "Version is not semver compatible"
// strip the v prefix from the tag so we can use in a maven version number
def previousReleaseVersion = tag.substring(tag.lastIndexOf('v') + 1)
echo "Previous version found ${previousReleaseVersion}"
// if there's an int as the version then turn it into a major.minor.micro version
if (previousReleaseVersion.isNumber()) {
return previousReleaseVersion + '.0.1'
} else {
// if previous tag is not a number and doesnt have a '.' version seperator then error until we have one
if (previousReleaseVersion.lastIndexOf('.') == 0) {
error "found invalid latest tag [${previousReleaseVersion}] set to major.minor.micro to calculate next release version"
}
// increment the release number after the last seperator '.'
def microVersion = previousReleaseVersion.substring(previousReleaseVersion.lastIndexOf('.') + 1) as int
return previousReleaseVersion.substring(0, previousReleaseVersion.lastIndexOf('.') + 1) + (microVersion + 1)
}
}
}
// Misspelled legacy definition for staging sonatype repository; hopefully deprecated
def stageSonartypeRepo() {
stageSonatypeRepo()
}
def stageSonatypeRepo() {
try {
sh "mvn clean -B"
sh "mvn -V -B -e -U install org.sonatype.plugins:nexus-staging-maven-plugin:1.6.7:deploy -P release -P openshift -DnexusUrl=https://oss.sonatype.org -DserverId=oss-sonatype-staging -Ddocker.push.registry=${env.FABRIC8_DOCKER_REGISTRY_SERVICE_HOST}:${env.FABRIC8_DOCKER_REGISTRY_SERVICE_PORT}"
// lets not archive artifacts until we if we just use nexus or a content repo
//step([$class: 'ArtifactArchiver', artifacts: '**/target/*.jar', fingerprint: true])
} catch (err) {
hubotSend room: 'release', message: "Release failed when building and deploying to Nexus ${err}", failOnError: false
currentBuild.result = 'FAILURE'
error "ERROR Release failed when building and deploying to Nexus ${err}"
}
// the sonatype staging repo id gets written to a file in the workspace
return getRepoIds()
}
// Misspelled legacy definition for releasing sonatype repository; hopefully deprecated
def releaseSonartypeRepo(String repoId) {
releaseSonatypeRepo(repoId)
}
def releaseSonatypeRepo(String repoId) {
try {
// release the sonatype staging repo
sh "mvn -B org.sonatype.plugins:nexus-staging-maven-plugin:1.6.5:rc-release -DserverId=oss-sonatype-staging -DnexusUrl=https://oss.sonatype.org -DstagingRepositoryId=${repoId} -Ddescription=\"Next release is ready\" -DstagingProgressTimeoutMinutes=60"
} catch (err) {
sh "mvn org.sonatype.plugins:nexus-staging-maven-plugin:1.6.5:rc-drop -DserverId=oss-sonatype-staging -DnexusUrl=https://oss.sonatype.org -DstagingRepositoryId=${repoId} -Ddescription=\"Error during release: ${err}\" -DstagingProgressTimeoutMinutes=60"
currentBuild.result = 'FAILURE'
error "ERROR releasing sonatype repo ${repoId}: ${err}"
}
}
def dropStagingRepo(String repoId) {
echo "Not a release so dropping staging repo ${repoId}"
sh "mvn org.sonatype.plugins:nexus-staging-maven-plugin:1.6.5:rc-drop -DserverId=oss-sonatype-staging -DnexusUrl=https://oss.sonatype.org -DstagingRepositoryId=${repoId} -Ddescription=\"Dry run\" -DstagingProgressTimeoutMinutes=60"
}
def helm() {
def pluginVersion = getReleaseVersion("io/fabric8/fabric8-maven-plugin")
try {
sh "mvn -B io.fabric8:fabric8-maven-plugin:${pluginVersion}:helm"
sh "mvn -B io.fabric8:fabric8-maven-plugin:${pluginVersion}:helm-push"
} catch (err) {
error "ERROR with helm push ${err}"
}
}
def pushTag(String releaseVersion) {
sh "git tag -fa v${releaseVersion} -m 'Release version ${releaseVersion}'"
sh "git push origin v${releaseVersion}"
}
def updateGithub() {
def releaseVersion = getProjectVersion()
sh "git push origin release-v${releaseVersion}"
}
def updateNextDevelopmentVersion(String releaseVersion, String mvnExtraArgs = "") {
// update poms back to snapshot again
sh 'mvn -B build-helper:parse-version versions:set -DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion}-SNAPSHOT ' + mvnExtraArgs
def snapshotVersion = getProjectVersion()
sh "git commit -a -m '[CD] prepare for next development iteration ${snapshotVersion}'"
sh "git push origin release-v${releaseVersion}"
}
def hasChangedSinceLastRelease() {
sh "git log --name-status HEAD^..HEAD -1 --grep=\"prepare for next development iteration\" --author='fusesource-ci' >> gitlog.tmp"
def myfile = readFile('gitlog.tmp')
//sh "rm gitlog.tmp"
// if the file size is 0 it means the CI user was not the last commit so project has changed
if (myfile.length() == 0) {
return true
} else {
return false
}
}
def getOldVersion() {
def matcher = readFile('website/src/docs/index.page') =~ '<h1>Documentation for version (.+)</h1>'
matcher ? matcher[0][1] : null
}
def updateDocsAndSite(String newVersion) {
// get previous version
def oldVersion = getOldVersion()
if (oldVersion == null) {
echo "No previous version found"
return
}
// use perl so that we we can easily turn off regex in the SED query as using dots in version numbers returns unwanted results otherwise
sh "find . -name '*.md' ! -name Changes.md ! -path '*/docs/jube/**.*' | xargs perl -p -i -e 's/\\Q${oldVersion}/${newVersion}/g'"
sh "find . -path '*/website/src/**.*' | xargs perl -p -i -e 's/\\Q${oldVersion}/${newVersion}/g'"
sh "git commit -a -m '[CD] Update docs following ${newVersion} release'"
}
def runSystemTests() {
sh 'cd systests && mvn clean && mvn integration-test verify'
}
def createPullRequest(String message, String project, String branch) {
def githubToken = getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/pulls")
echo "creating PR for ${apiUrl}"
try {
HttpURLConnection connection = apiUrl.openConnection()
if (githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("POST")
connection.setDoOutput(true)
connection.connect()
def body = """
{
"title": "${message}",
"head": "${branch}",
"base": "master"
}
"""
echo "sending body: ${body}\n"
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
// execute the POST request
def rs = new JsonSlurper().parse(new InputStreamReader(connection.getInputStream(), "UTF-8"))
connection.disconnect()
echo "Received PR id: ${rs.number}"
return rs.number + ''
} catch (err) {
error "ERROR ${err}"
}
}
def closePR(project, id, newVersion, newPRID) {
def githubToken = getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/pulls/${id}")
echo "deleting PR for ${apiUrl}"
HttpURLConnection connection = apiUrl.openConnection()
if (githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
connection.setRequestMethod("POST")
connection.setDoOutput(true)
connection.connect()
def body = """
{
"state": "closed",
"body": "Superseded by new version ${newVersion} #${newPRID}"
}
"""
echo "sending body: ${body}\n"
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
// execute the PATCH request
def rs = new JsonSlurper().parse(new InputStreamReader(connection.getInputStream(), "UTF-8"))
def code = connection.getResponseCode()
if (code != 200) {
error "${project} PR ${id} not merged. ${connection.getResponseMessage()}"
} else {
echo "${project} PR ${id} ${rs.message}"
}
connection.disconnect()
}
def getIssueComments(project, id, githubToken = null) {
if (!githubToken) {
githubToken = getGitHubToken()
}
def apiUrl = new URL("https://api.github.com/repos/${project}/issues/${id}/comments")
echo "getting comments for ${apiUrl}"
def HttpURLConnection connection = apiUrl.openConnection()
if (githubToken != null && githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("GET")
connection.setDoOutput(true)
connection.connect()
def rs = new JsonSlurper().parse(new InputStreamReader(connection.getInputStream(), "UTF-8"))
def code = 0
try {
code = connection.getResponseCode()
// } catch (org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException ex){
// echo "${ex} will try to continue"
} finally {
connection.disconnect()
}
if (code != 0 && code != 200) {
error "Cannot get ${project} PR ${id} comments. ${connection.getResponseMessage()}"
}
return rs
}
def waitUntilSuccessStatus(project, ref) {
def githubToken = getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/commits/${ref}/status")
waitUntil {
def HttpURLConnection connection = apiUrl.openConnection()
if (githubToken != null && githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("GET")
connection.setDoOutput(true)
connection.connect()
def rs
def code
try {
rs = new JsonSlurper().parse(new InputStreamReader(connection.getInputStream(), "UTF-8"))
code = connection.getResponseCode()
} catch (err) {
echo "CI checks have not passed yet so waiting before merging"
} finally {
connection.disconnect()
}
if (rs == null) {
echo "Error getting commit status, are CI builds enabled for this PR?"
return false
}
if (rs != null && rs.state == 'success') {
return true
} else {
echo "Commit status is ${rs.state}. Waiting to merge"
return false
}
}
}
def getGithubBranch(project, id, githubToken) {
def apiUrl = new URL("https://api.github.com/repos/${project}/pulls/${id}")
def HttpURLConnection connection = apiUrl.openConnection()
if (githubToken != null && githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("GET")
connection.setDoOutput(true)
connection.connect()
try {
def rs = new JsonSlurper().parse(new InputStreamReader(connection.getInputStream(), "UTF-8"))
branch = rs.head.ref
echo "${branch}"
return branch
} catch (err) {
echo "Error while fetching the github branch"
} finally {
if (connection) {
connection.disconnect()
}
}
}
def mergePR(project, id) {
def githubToken = getGitHubToken()
def branch = getGithubBranch(project, id, githubToken)
waitUntilSuccessStatus(project, branch)
def apiUrl = new URL("https://api.github.com/repos/${project}/pulls/${id}/merge")
def HttpURLConnection connection = apiUrl.openConnection()
if (githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("PUT")
connection.setDoOutput(true)
connection.connect()
// execute the request
def rs
try {
rs = new JsonSlurper().parse(new InputStreamReader(connection.getInputStream(), "UTF-8"))
def code = connection.getResponseCode()
if (code != 200) {
if (code == 405) {
error "${project} PR ${id} not merged. ${rs.message}"
} else {
error "${project} PR ${id} not merged. GitHub API Response code: ${code}"
}
} else {
echo "${project} PR ${id} ${rs.message}"
}
} catch (err) {
// if merge failed try to squash and merge
connection = null
rs = null
squashAndMerge(project, id)
} finally {
if (connection) {
connection.disconnect()
connection = null
}
rs = null
}
}
def squashAndMerge(project, id) {
def githubToken = getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/pulls/${id}/merge")
def HttpURLConnection connection = apiUrl.openConnection()
if (githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("PUT")
connection.setDoOutput(true)
connection.connect()
def body = "{\"merge_method\":\"squash\"}"
def rs
try {
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
rs = new JsonSlurper().parse(new InputStreamReader(connection.getInputStream(), "UTF-8"))
def code = connection.getResponseCode()
if (code != 200) {
if (code == 405) {
error "${project} PR ${id} not merged. ${rs.message}"
} else {
error "${project} PR ${id} not merged. GitHub API Response code: ${code}"
}
} else {
echo "${project} PR ${id} ${rs.message}"
}
} finally {
connection.disconnect()
connection = null
rs = null
}
}
def addCommentToPullRequest(comment, pr, project) {
def githubToken = getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/issues/${pr}/comments")
echo "adding ${comment} to ${apiUrl}"
try {
def HttpURLConnection connection = apiUrl.openConnection()
if (githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("POST")
connection.setDoOutput(true)
connection.connect()
def body = "{\"body\":\"${comment}\"}"
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
// execute the POST request
new InputStreamReader(connection.getInputStream())
connection.disconnect()
} catch (err) {
error "ERROR ${err}"
}
}
def addMergeCommentToPullRequest(String pr, String project) {
def githubToken = getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/issues/${pr}/comments")
echo "merge PR using comment sent to ${apiUrl}"
try {
def HttpURLConnection connection = apiUrl.openConnection()
if (githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("POST")
connection.setDoOutput(true)
connection.connect()
def body = '{"body":"[merge]"}'
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
// execute the POST request
new InputStreamReader(connection.getInputStream())
connection.disconnect()
} catch (err) {
error "ERROR ${err}"
}
}
def getGitHubProject() {
def url = getScmPushUrl()
if (!url.contains('github.com')) {
error "${url} is not a GitHub URL"
}
if (url.contains("https://github.com/")) {
url = url.replaceAll("https://github.com/", '')
} else if (url.contains("git@github.com:")) {
url = url.replaceAll("git@github.com:", '')
}
if (url.contains(".git")) {
url = url.replaceAll(".git", '')
}
return url.trim()
}
def isAuthorCollaborator(githubToken, project) {
if (!githubToken) {
githubToken = getGitHubToken()
if (!githubToken) {
echo "No GitHub api key found so trying annonynous GitHub api call"
}
}
if (!project) {
project = getGitHubProject()
}
def changeAuthor = env.CHANGE_AUTHOR
if (!changeAuthor) {
error "No commit author found. Is this a pull request pipeline?"
}
echo "Checking if user ${changeAuthor} is a collaborator on ${project}"
def apiUrl = new URL("https://api.github.com/repos/${project}/collaborators/${changeAuthor}")
def HttpURLConnection connection = apiUrl.openConnection()
if (githubToken != null && githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("GET")
connection.setDoOutput(true)
try {
connection.connect()
new InputStreamReader(connection.getInputStream(), "UTF-8")
return true
} catch (FileNotFoundException e1) {
return false
} finally {
connection.disconnect()
}
error "Error checking if user ${changeAuthor} is a collaborator on ${project}. GitHub API Response code: ${code}"
}
def getUrlAsString(urlString) {
def url = new URL(urlString)
def scan
def response
echo "getting string from URL: ${url}"
try {
scan = new Scanner(url.openStream(), "UTF-8")
response = scan.useDelimiter("\\A").next()
} finally {
scan.close()
}
return response
}
def drop(String pr, String project) {
def githubToken = getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/pulls/${pr}")
def branch
HttpURLConnection connection
OutputStreamWriter writer
echo "closing PR ${apiUrl}"
try {
connection = apiUrl.openConnection()
if (githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("POST")
connection.setDoOutput(true)
connection.connect()
def body = '''
{
"body": "release aborted",
"state": "closed"
}
'''
writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
// execute the POST request
def rs = new JsonSlurper().parse(new InputStreamReader(connection.getInputStream(), "UTF-8"))
connection.disconnect()
branchName = rs.head.ref
} catch (err) {
error "ERROR ${err}"
}
try {
apiUrl = new URL("https://api.github.com/repos/${project}/git/refs/heads/${branchName}")
connection = apiUrl.openConnection()
if (githubToken.length() > 0) {
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("DELETE")
connection.setDoOutput(true)
connection.connect()
writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
// execute the POST request
new InputStreamReader(connection.getInputStream())
connection.disconnect()
} catch (err) {
error "ERROR ${err}"
}
}
def deleteRemoteBranch(String branchName, containerName) {
container(name: containerName) {
setupGitSSH()
sh "git push origin --delete ${branchName}"
}
}
def getGitHubToken() {
def tokenPath = '/home/jenkins/.apitoken/hub'
def githubToken = readFile tokenPath
if (!githubToken?.trim()) {
error "No GitHub token found in ${tokenPath}"
}
return githubToken.trim()
}
@NonCPS
def isSingleNode() {
KubernetesClient kubernetes = new DefaultKubernetesClient()
if (kubernetes.nodes().list().getItems().size() == 1) {
return true
} else {
return false
}
}
@NonCPS
def hasService(String name) {
KubernetesClient kubernetes = new DefaultKubernetesClient()
def service = kubernetes.services().withName(name).get()
if (service != null) {
return service.metadata != null
}
return false
}
@NonCPS
def getServiceURL(String serviceName, String namespace = null, String protocol = "http", boolean external = true) {
KubernetesClient kubernetes = new DefaultKubernetesClient()
if (namespace == null) namespace = kubernetes.getNamespace()
return KubernetesHelper.getServiceURL(kubernetes, serviceName, namespace, protocol, external)
}
def hasOpenShiftYaml() {
def openshiftYaml = findFiles(glob: '**/openshift.yml')
try {
if (openshiftYaml) {
def contents = readFile(openshiftYaml[0].path)
if (contents != null) {
if (contents.contains('kind: "ImageStream"') || contents.contains('kind: ImageStream') || contents.contains('kind: \'ImageStream\'')) {
echo "OpenShift YAML contains an ImageStream"
return true
} else {
echo "OpenShift YAML does not contain an ImageStream so not using S2I binary mode"
}
}
} else {
echo "Warning OpenShift YAML ${openshiftYaml} does not exist!"
}
} catch (e) {
error "Failed to load ${openshiftYaml[0]} due to ${e}"
}
return false
}
/**
* Deletes the given namespace if it exists
*
* @param name the name of the namespace
* @return true if the delete was successful
*/
@NonCPS
def deleteNamespace(String name) {
KubernetesClient kubernetes = new DefaultKubernetesClient()
try {
def namespace = kubernetes.namespaces().withName(name).get()
if (namespace != null) {
echo "Deleting namespace ${name}..."
kubernetes.namespaces().withName(name).delete()
echo "Deleted namespace ${name}"
// TODO should we wait for the namespace to really go away???
namespace = kubernetes.namespaces().withName(name).get()
if (namespace != null) {
echo "Namespace ${name} still exists!"
}
return true
}
return false
} catch (e) {
// ignore errors
return false
}
}
@NonCPS
def isOpenShift() {
return new DefaultOpenShiftClient().isAdaptable(OpenShiftClient.class)
}
@NonCPS
def getCloudConfig() {
def openshiftCloudConfig = Jenkins.getInstance().getCloud('openshift')
return (openshiftCloudConfig) ? 'openshift' : 'kubernetes'
}
/**
* Should be called after checkout scm
*/
@NonCPS
def getScmPushUrl() {
def url = sh(returnStdout: true, script: 'git config --get remote.origin.url').trim()
if (!url) {
error "no URL found for git config --get remote.origin.url "
}
return url
}
@NonCPS
def openShiftImageStreamExists(String name) {
if (isOpenShift()) {
try {
def result = sh(returnStdout: true, script: 'oc describe is ${name} --namespace openshift')
if (result && result.contains(name)) {
echo "ImageStream ${name} is already installed globally"
return true;
} else {
//see if its already in our namespace
def namespace = kubernetes.getNamespace();
result = sh(returnStdout: true, script: 'oc describe is ${name} --namespace ${namespace}')
if (result && result.contains(name)) {
echo "ImageStream ${name} is already installed in project ${namespace}"
return true;
}
}
} catch (e) {
echo "Warning: ${e} "
}
}
return false;
}
@NonCPS
def openShiftImageStreamInstall(String name, String location) {
if (openShiftImageStreamExists(name)) {
echo "ImageStream ${name} does not exist - installing ..."
try {
def result = sh(returnStdout: true, script: 'oc create -f ${location}')
def namespace = kubernetes.getNamespace();
echo "ImageStream ${name} now installed in project ${namespace}"
return true;
} catch (e) {
echo "Warning: ${e} "
}
}
return false;
}
/*
* This is a temporary workaround till the secrets can be mounted with the
* right permissions.
*
* ~/.ssh must be a writable path for openssl to edit ~/.ssh/known_hosts; so
* mount the actual secret elsewhere and link the config file.
*
*/