Skip to content

Commit 739e136

Browse files
Use an alternate algorthim for finding … (#419)
The base branch. This is the same as is used in our githooks with the exception of factoring the "commits behind" into the distance from the base branch. Update distroVersion() test. Signed-off-by: Brian J. Murrell <[email protected]>
1 parent 716fbf7 commit 739e136

File tree

7 files changed

+116
-85
lines changed

7 files changed

+116
-85
lines changed

Jenkinsfile

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ String test_branch(String target) {
8080
'-' + target.replaceAll('/', '-')
8181
}
8282

83+
Void distro_version_test(String branch, String distro, String expected) {
84+
println("Test branch: ${branch}, distro: ${distro}, expecting: ${expected}")
85+
withEnv(["BRANCH_NAME=${branch}"]) {
86+
String dv = distroVersion(distro)
87+
if (dv == null || !dv.startsWith(expected)) {
88+
unstable("distroVersion() returned ${dv} " +
89+
"instead of string starting with '${expected}'")
90+
}
91+
}
92+
}
93+
8394
/* groovylint-disable-next-line CompileStatic */
8495
pipeline {
8596
agent { label 'lightweight' }
@@ -113,13 +124,11 @@ pipeline {
113124
pragmasToEnv()
114125
}
115126
}
116-
stage('Determine Base Branch') {
127+
stage('Determine Release Branch') {
117128
steps {
118129
script {
119-
env.BASE_BRANCH_NAME = sh(label: 'Determine base branch name',
120-
script: 'utils/scripts/get_base_branch',
121-
returnStdout: true).trim()
122-
echo 'Base branch == ' + env.BASE_BRANCH_NAME
130+
env.RELEASE_BRANCH = releaseBranch()
131+
echo 'Base branch == ' + env.RELEASE_BRANCH
123132
}
124133
}
125134
}
@@ -146,33 +155,9 @@ pipeline {
146155
}
147156
stage('distroVersion() tests') {
148157
steps {
149-
withEnv(['BRANCH_NAME=release/2.4']) {
150-
script {
151-
String dv = distroVersion('el8')
152-
if (dv == null || !dv.startsWith('8')) {
153-
unstable("distroVersion() returned ${dv} " +
154-
"instead of string starting with '8'")
155-
}
156-
}
157-
}
158-
withEnv(['BRANCH_NAME=release/2.4']) {
159-
script {
160-
String dv = distroVersion('leap15')
161-
if (dv == null || !dv.startsWith('15')) {
162-
unstable("distroVersion() returned ${dv} " +
163-
"instead of string starting with '15'")
164-
}
165-
}
166-
}
167-
withEnv(['BRANCH_NAME=master']) {
168-
script {
169-
String dv = distroVersion('el9')
170-
if (dv == null || !dv.startsWith('9')) {
171-
unstable("distroVersion() returned ${dv} " +
172-
"instead of string starting with '9'")
173-
}
174-
}
175-
}
158+
distro_version_test('release/2.4', 'el8', '8')
159+
distro_version_test('release/2.4', 'leap15', '15')
160+
distro_version_test('master', 'el9', '9')
176161
}
177162
}
178163
stage('grep JUnit results tests failure case') {

utils/scripts/get_base_branch

Lines changed: 0 additions & 19 deletions
This file was deleted.

vars/distroVersion.groovy

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,17 @@ String call(String distro) {
1616
if (env.BRANCH_NAME =~ branchTypeRE('release') ||
1717
env.BRANCH_NAME =~ branchTypeRE('testing')) {
1818
if (env.BRANCH_NAME =~ /\d+\.\d+/) {
19-
branch = env.BRANCH_NAME.replaceFirst(/^.*(\d+\.\d+).*$/, '\$1')
19+
branch = env.BRANCH_NAME
2020
}
2121
} else {
22-
if (env.BASE_BRANCH_NAME) {
23-
branch = env.BASE_BRANCH_NAME
22+
if (env.RELEASE_BRANCH) {
23+
branch = env.RELEASE_BRANCH
2424
} else {
25-
// find the base branch
26-
branch = sh(label: 'Find base branch',
27-
script: '''set -eux -o pipefail
28-
max_commits=200
29-
base_branch_re='^ origin/(master|release/)'
30-
n=0
31-
while [ "$n" -lt "$max_commits" ]; do
32-
if git branch -r --contains HEAD~$n |
33-
grep -E "$base_branch_re" | sed -e 's/^ *[^\\/]*\\///'; then
34-
exit 0
35-
fi
36-
((n++)) || true
37-
done
38-
echo "Could not find a base branch within $max_commits commits" >&2
39-
exit 1''',
40-
returnStdout: true).trim().replaceFirst(/^.*(\d+\.\d+).*$/, '\$1')
25+
branch = releaseBranch()
4126
}
4227
}
4328

44-
return distroVersion(distro, branch)
29+
return distroVersion(distro, branch.replaceFirst(/^.*(\d+\.\d+).*$/, '\$1'))
4530
}
4631

4732
String call(String distro, String branch) {
@@ -54,5 +39,36 @@ String call(String distro, String branch) {
5439
}
5540

5641
/* groovylint-disable-next-line CompileStatic */
57-
assert(call('leap15', 'release/2.2') == '15.4')
58-
assert(call('el8', 'release/2.2') == '8.6')
42+
assert(call('leap15', '2.4') == '15.5')
43+
assert(call('leap15', 'master') == '15.5')
44+
assert(call('el8', '2.4') == '8.8')
45+
assert(call('el8', 'master') == '8.8')
46+
47+
/* Uncomment to do further testing
48+
env = [:]
49+
String branchTypeRE(String foo) {
50+
return 'nomatch'
51+
}
52+
53+
String distroVersion(String distro, String branch) {
54+
call(distro, branch)
55+
}
56+
57+
String sh(Map args) {
58+
def sout = new StringBuilder(), serr = new StringBuilder()
59+
def cmd = ['/bin/bash', '-c', args['script']]
60+
def proc = cmd.execute()
61+
proc.consumeProcessOutput(sout, serr)
62+
proc.waitForOrKill(1000)
63+
//println "out> $sout\nerr> $serr"
64+
65+
return sout
66+
}
67+
68+
String releaseBranch() {
69+
return 'release/2.4'
70+
}
71+
72+
assert(call('leap15') == '15.5')
73+
assert(call('el8') == '8.8')
74+
*/

vars/packageBuildingPipeline.groovy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,8 @@ void call(Map pipeline_args) {
124124
stage('Determine Base Branch') {
125125
steps {
126126
script {
127-
env.BASE_BRANCH_NAME = sh(label: 'Determine base branch name',
128-
script: 'packaging/get_base_branch',
129-
returnStdout: true).trim()
130-
echo 'Base branch == ' + env.BASE_BRANCH_NAME
127+
env.RELEASE_BRANCH = releaseBranch()
128+
echo 'Release branch == ' + env.RELEASE_BRANCH
131129
}
132130
}
133131
}

vars/packageBuildingPipelineDAOS.groovy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,8 @@ void call(Map pipeline_args) {
129129
stage('Determine Base Branch') {
130130
steps {
131131
script {
132-
env.BASE_BRANCH_NAME = sh(label: 'Determine base branch name',
133-
script: 'packaging/get_base_branch',
134-
returnStdout: true).trim()
135-
echo 'Base branch == ' + env.BASE_BRANCH_NAME
132+
env.RELEASE_BRANCH = releaseBranch()
133+
echo 'Release branch == ' + env.RELEASE_BRANCH
136134
}
137135
}
138136
}

vars/packageBuildingPipelineDAOSTest.groovy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,8 @@ void call(Map pipeline_args) {
127127
stage('Determine Base Branch') {
128128
steps {
129129
script {
130-
env.BASE_BRANCH_NAME = sh(label: 'Determine base branch name',
131-
script: 'packaging/get_base_branch',
132-
returnStdout: true).trim()
133-
echo 'Base branch == ' + env.BASE_BRANCH_NAME
130+
env.RELEASE_BRANCH = releaseBranch()
131+
echo 'Release branch == ' + env.RELEASE_BRANCH
134132
}
135133
}
136134
}

vars/releaseBranch.groovy

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* groovylint-disable DuplicateStringLiteral */
2+
// vars/releaseBranch.groovy
3+
4+
/**
5+
* releaseBranch.groovy
6+
*
7+
* Return the release branch the PR will be landed to
8+
*/
9+
10+
String call() {
11+
/* groovylint-disable-next-line GStringExpressionWithinString */
12+
String script = '''set -eux -o pipefail
13+
mapfile -t all_bases < <(echo "master"; git branch -r |
14+
sed -ne "/^ origin\\/release\\/[0-9]/s/^ origin\\///p")
15+
target="master"
16+
min_diff=-1
17+
for base in "${all_bases[@]}"; do
18+
git rev-parse --verify "origin/$base" &> /dev/null || continue
19+
commits_ahead=$(git log --oneline "origin/$base..HEAD" | wc -l)
20+
if [ "$min_diff" -eq -1 ] || [ "$min_diff" -gt "$commits_ahead" ]; then
21+
target="$base"
22+
min_diff=$commits_ahead
23+
fi
24+
done
25+
echo "$target"
26+
exit 0'''
27+
if (fileExists('utils/rpms/packaging/get_release_branch')) {
28+
script = 'utils/rpms/packaging/get_release_branch'
29+
} else if (fileExists('packaging/get_release_branch')) {
30+
script = 'packaging/get_release_branch'
31+
}
32+
// find the release branch
33+
return sh(label: 'Find release branch',
34+
script: script,
35+
returnStdout: true).trim()
36+
}
37+
38+
/* Uncomment for some UT
39+
Boolean fileExists(String path) {
40+
return false
41+
}
42+
43+
String sh(Map args) {
44+
def sout = new StringBuilder(), serr = new StringBuilder()
45+
def cmd = ['/bin/bash', '-c', args['script']]
46+
def proc = cmd.execute()
47+
proc.consumeProcessOutput(sout, serr)
48+
proc.waitForOrKill(1000)
49+
//println "out> $sout\nerr> $serr"
50+
51+
return sout
52+
}
53+
54+
println(call())
55+
*/

0 commit comments

Comments
 (0)