Skip to content

Commit e3221b5

Browse files
Merge pull request #23720 from alexlarsson/check-jira-links-4.9
[v4.9-rhel] Update CI on v4.9-rhel to check for jira links
2 parents 55a785c + e3efdbc commit e3221b5

File tree

6 files changed

+235
-1
lines changed

6 files changed

+235
-1
lines changed

.cirrus.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ env:
66
#### Global variables used for all tasks
77
####
88
# Name of the ultimate destination branch for this CI run, PR or post-merge.
9-
DEST_BRANCH: "v4.9"
9+
DEST_BRANCH: "v4.9-rhel"
1010
# Sane (default) value for GOPROXY and GOSUMDB.
1111
GOPROXY: "https://proxy.golang.org,direct"
1212
GOSUMDB: "sum.golang.org"

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,10 @@ test-binaries: test/checkseccomp/checkseccomp test/goecho/goecho install.cataton
699699
tests-included:
700700
contrib/cirrus/pr-should-include-tests
701701

702+
.PHONY: test-jira-links-included
703+
test-jira-links-included:
704+
contrib/cirrus/pr-should-link-jira
705+
702706
.PHONY: tests-expect-exit
703707
tests-expect-exit:
704708
@if grep -E --line-number 'Expect.*ExitCode' test/e2e/*.go | grep -E -v ', ".*"\)'; then \

contrib/cirrus/pr-should-link-jira

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
#
3+
# Intended for use in CI, for -rhel branches: check git PR, barf if no jira links
4+
#
5+
6+
ME=$(basename $0)
7+
8+
set -e
9+
10+
# Github label which allows overriding this check
11+
OVERRIDE_LABEL="No Jira Link"
12+
13+
# Only -rhel branches need jira links
14+
BRANCH_REGEX="v.*-rhel"
15+
16+
LINK_REGEX="Fixes:?[[:space:]]+https://issues.redhat.com/[[:alnum:]/-]*"
17+
18+
if [[ ! "${DEST_BRANCH}" =~ $BRANCH_REGEX ]]; then
19+
exit 0
20+
fi
21+
22+
if [[ "${CIRRUS_CHANGE_MESSAGE}" =~ $LINK_REGEX ]]; then
23+
exit 0
24+
fi
25+
26+
# Nope. Only allow if the github 'No Jira Link' label is set
27+
if [[ -z "$CIRRUS_PR" ]]; then
28+
if [[ ! "$CIRRUS_BRANCH" =~ pull ]] || [[ -n "$CIRRUS_TAG" ]]; then
29+
echo "Warning: $ME only intended for use on PRs in CI"
30+
exit 0
31+
fi
32+
echo "$ME: cannot query github: \$CIRRUS_PR is undefined" >&2
33+
exit 1
34+
fi
35+
36+
if [[ -z "$CIRRUS_REPO_CLONE_TOKEN" ]]; then
37+
echo "$ME: cannot query github: \$CIRRUS_REPO_CLONE_TOKEN is undefined" >&2
38+
exit 1
39+
fi
40+
41+
query="{
42+
\"query\": \"query {
43+
repository(owner: \\\"containers\\\", name: \\\"podman\\\") {
44+
pullRequest(number: $CIRRUS_PR) {
45+
labels(first: 100) {
46+
nodes {
47+
name
48+
}
49+
}
50+
}
51+
}
52+
}\"
53+
}"
54+
55+
result=$(curl -s -H "Authorization: bearer $CIRRUS_REPO_CLONE_TOKEN" -H "Accept: application/vnd.github.antiope-preview+json" -H "Content-Type: application/json" -X POST --data @- https://api.github.com/graphql <<<"$query")
56+
57+
labels=$(jq -r '.data.repository.pullRequest.labels.nodes[]?.name' <<<"$result")
58+
59+
if grep -F -x -q "$OVERRIDE_LABEL" <<<"$labels"; then
60+
# PR has the label set
61+
exit 0
62+
fi
63+
64+
cat <<EOF
65+
$ME: PR does not include required references to Jira issues
66+
67+
Please add a reference to the related Jira ticket(s) by adding to the
68+
description of the PR something like:
69+
70+
Fixes: https://issues.redhat.com/browse/RHEL-50507
71+
72+
You can use multiple lines like this, but only one issue per line.
73+
74+
If your commit really, truly does not need a jira link, you can proceed
75+
by asking a repo maintainer to set the '$OVERRIDE_LABEL' github label.
76+
This will only be done when there's no reasonable alternative.
77+
EOF
78+
79+
exit 1

contrib/cirrus/pr-should-link-jira.t

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#!/bin/bash
2+
#
3+
# tests for pr-should-link-jira.t
4+
#
5+
6+
ME=$(basename $0)
7+
8+
# Our test script queries github, for which we need token
9+
if [[ -z "$CIRRUS_REPO_CLONE_TOKEN" ]]; then
10+
if [[ -n "$GITHUB_TOKEN" ]]; then
11+
export CIRRUS_REPO_CLONE_TOKEN="$GITHUB_TOKEN"
12+
else
13+
echo "$ME: Please set \$CIRRUS_REPO_CLONE_TOKEN" >&2
14+
exit 1
15+
fi
16+
fi
17+
18+
###############################################################################
19+
# BEGIN test cases
20+
#
21+
22+
read -d '\n' msg_no_jira << EndOfText
23+
This is some text
24+
without a jira
25+
EndOfText
26+
27+
read -d '\n' msg_invalid << EndOfText
28+
This is some text
29+
without a jira
30+
Fixes #42
31+
More text...
32+
EndOfText
33+
34+
read -d '\n' msg_jira << EndOfText
35+
This is some text
36+
with a jira
37+
Fixes https://issues.redhat.com/browse/RHEL-50507
38+
More text...
39+
EndOfText
40+
41+
read -d '\n' msg_jira2 << EndOfText
42+
This is some text
43+
with a jira
44+
Fixes: https://issues.redhat.com/browse/RHEL-50507
45+
More text...
46+
EndOfText
47+
48+
read -d '\n' msg_multiple << EndOfText
49+
This is some text
50+
with multiple jira lines
51+
Fixes https://issues.redhat.com/browse/RHEL-50507
52+
More text...
53+
Fixes: https://issues.redhat.com/browse/RHEL-50506
54+
More text...
55+
EndOfText
56+
57+
# Feel free to add as needed. Syntax is:
58+
# <exit status> <pr> <commit message> <dest branch> # comments
59+
#
60+
# Where:
61+
# exit status is the expected exit status of the script
62+
# pr pr number (only used to get tag, 0000 if doesn't matter)
63+
# commit message commit message
64+
# dest branch name of branch
65+
#
66+
67+
tests="
68+
0 0000 msg_no_jira main not rhel branch, no link, should pass
69+
0 0000 msg_jira main not rhel branch, link, should pass
70+
0 0000 msg_invalid main not rhel branch, invalid link, should pass
71+
0 0000 msg_no_jira v4.9 not rhel branch, no link, should pass
72+
1 23514 msg_no_jira v4.9-rhel no link, no tag, should fail
73+
0 8890 msg_no_jira v4.9-rhel no link, tag, should work
74+
1 23514 msg_invalid v4.9-rhel invalid link, no tag, should fail
75+
0 0000 msg_jira v4.9-rhel link, should work
76+
0 0000 msg_jira2 v4.9-rhel link with colon, should work
77+
0 0000 msg_multiple v4.9-rhel multiple links, should work
78+
"
79+
80+
# The script we're testing
81+
test_script=$(dirname $0)/$(basename $0 .t)
82+
83+
# END test cases
84+
###############################################################################
85+
# BEGIN test-script runner and status checker
86+
87+
function run_test_script() {
88+
local expected_rc=$1
89+
local testname=$2
90+
91+
testnum=$(( testnum + 1 ))
92+
93+
# DO NOT COMBINE 'local output=...' INTO ONE LINE. If you do, you lose $?
94+
local output
95+
output=$( $test_script )
96+
local actual_rc=$?
97+
98+
if [[ $actual_rc != $expected_rc ]]; then
99+
echo "not ok $testnum $testname"
100+
echo "# expected rc $expected_rc"
101+
echo "# actual rc $actual_rc"
102+
if [[ -n "$output" ]]; then
103+
echo "# script output: $output"
104+
fi
105+
rc=1
106+
else
107+
if [[ $expected_rc == 1 ]]; then
108+
# Confirm we get an error message
109+
if [[ ! "$output" =~ "Please add a reference" ]]; then
110+
echo "not ok $testnum $testname"
111+
echo "# Expected: ~ 'Please add a reference'"
112+
echo "# Actual: $output"
113+
rc=1
114+
else
115+
echo "ok $testnum $testname - rc=$expected_rc"
116+
fi
117+
else
118+
echo "ok $testnum $testname - rc=$expected_rc"
119+
fi
120+
fi
121+
}
122+
123+
# END test-script runner and status checker
124+
###############################################################################
125+
# BEGIN test-case parsing
126+
127+
rc=0
128+
testnum=0
129+
tested_override=
130+
131+
while read expected_rc pr msg branch rest; do
132+
# Skip blank lines
133+
test -z "$expected_rc" && continue
134+
135+
export DEST_BRANCH=$branch
136+
export CIRRUS_CHANGE_MESSAGE="${!msg}"
137+
export CIRRUS_PR=$pr
138+
139+
run_test_script $expected_rc "PR $pr $msg $branch - $rest"
140+
done <<<"$tests"
141+
142+
echo "1..$testnum"
143+
exit $rc
144+
145+
# END Test-case parsing
146+
###############################################################################

contrib/cirrus/prebuild.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ if [[ "${DISTRO_NV}" == "$PRIOR_FEDORA_NAME" ]]; then
6262
# Tests for lib.sh
6363
showrun ${SCRIPT_BASE}/lib.sh.t
6464

65+
# Tests for pr-should-link-jira
66+
showrun ${SCRIPT_BASE}/pr-should-link-jira.t
67+
6568
# Run this during daily cron job to prevent a GraphQL API change/breakage
6669
# from impacting every PR. Down-side being if it does fail, a maintainer
6770
# will need to do some archaeology to find it.

contrib/cirrus/runner.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ function _run_validate() {
3434
warn "Skipping git-validation since \$EPOCH_TEST_COMMIT is empty"
3535
fi
3636

37+
# make sure PRs have jira links (if needed for branch)
38+
showrun make test-jira-links-included
3739
}
3840

3941
function _run_unit() {

0 commit comments

Comments
 (0)