-
Notifications
You must be signed in to change notification settings - Fork 30
/
pr_check.sh
executable file
·143 lines (124 loc) · 4.47 KB
/
pr_check.sh
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
#!/bin/bash
# --------------------------------------------
# Options that must be configured by app owner
# --------------------------------------------
export APP_NAME="patchman" # name of app-sre "application" folder this component lives in
export COMPONENT_NAME="patchman" # name of app-sre "resourceTemplate" in deploy.yaml for this component
export IMAGE="quay.io/cloudservices/patchman-engine-app"
export DOCKERFILE="Dockerfile"
export COMPONENTS_W_RESOURCES="vmaas content-sources-backend"
export IQE_PLUGINS="patchman"
export IQE_MARKER_EXPRESSION="patch_smoke"
export IQE_FILTER_EXPRESSION=""
export IQE_CJI_TIMEOUT="30m"
export TMPDIR=/var/lib/jenkins
# Heavily inspired by project-koku pr_check
# https://github.com/project-koku/koku/blob/main/pr_check.sh
LABELS_DIR="$WORKSPACE/github_labels"
export ARTIFACTS_DIR="$WORKSPACE/artifacts"
mkdir -p $LABELS_DIR
exit_code=0
task_arr=([1]="Build" [2]="Deploy" [3]="Smoke Tests")
error_arr=([1]="The PR is labeled to not build the test image" \
[2]="The PR is labeled to not deploy to ephemeral" \
[3]="The PR is labeled to not run smoke tests")
function check_for_labels() {
if [ -f $LABELS_DIR/github_labels.txt ]; then
egrep "$1" $LABELS_DIR/github_labels.txt &>/dev/null
fi
}
function _process_requirements_labels() {
# $1 env var to export
# $@ labels to process
env_var_name=$1; shift;
labels=$@
if [ -n "$labels" ]; then
labels=$(echo "$labels" | sed -e 's/"//g')
# contents of labels is different in different shells
# it can be one of
# labels="REQ1\nREQ2"
# labels="REQ1 REQ2"
lines=$(echo "$labels" | wc -l | xargs)
words=$(echo "$labels" | wc -w | xargs)
if [[ "$lines" == "1" ]] && [[ "$words" == "1" ]]; then
export "$env_var_name=$labels"
return
fi
processed=""
for req in $labels; do
processed="$processed$req,"
done
# delete extra comma
processed="${processed%?}"
export "$env_var_name=$processed"
fi
}
function process_requirements_labels() {
if [ -f $LABELS_DIR/github_labels.txt ]; then
requirements=$(egrep "^\"[A-Z]+-[A-Z-]*\"$" $LABELS_DIR/github_labels.txt)
requirements_priority=$(egrep "^\"(critical|high|medium|low)-requirements\"$" $LABELS_DIR/github_labels.txt | sed -e 's/-requirements//g')
_process_requirements_labels IQE_REQUIREMENTS $requirements
_process_requirements_labels IQE_REQUIREMENTS_PRIORITY $requirements_priority
fi
}
function build_image() {
source $CICD_ROOT/build.sh
}
function deploy_ephemeral() {
source $CICD_ROOT/deploy_ephemeral_env.sh
}
function run_smoke_tests() {
source $CICD_ROOT/cji_smoke_test.sh
}
function make_results_xml() {
cat << EOF > $WORKSPACE/artifacts/junit-pr_check.xml
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite id="pr_check" name="PR Check" tests="1" failures="0">
<testcase id="pr_check.${task_arr[$exit_code]}" name="${task_arr[$exit_code]}">
</testcase>
</testsuite>
EOF
}
# Save PR labels into a file
set -ex
# GH_API_URL="${GITHUB_API_URL:-https://api.github.com/}" # don't use app-sre github api mirror for now
curl -s -H "Accept: application/vnd.github.v3+json" -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/search/issues\?q\=sha:$ghprbActualCommit | jq '.items[].labels[].name' > $LABELS_DIR/github_labels.txt
if check_for_labels "keep-namespace"; then
export RELEASE_NAMESPACE=false
fi
if check_for_labels "skip-build"; then
echo "PR check skipped"
exit_code=1
else
# Install bonfire repo/initialize
CICD_URL=https://raw.githubusercontent.com/RedHatInsights/bonfire/master/cicd
curl -s $CICD_URL/bootstrap.sh > .cicd_bootstrap.sh && source .cicd_bootstrap.sh
echo "creating PR image"
build_image
fi
if [[ $exit_code == 0 ]]; then
if check_for_labels "skip-deploy"; then
echo "deployment to ephemeral skipped"
exit_code=2
else
echo "deploying to ephemeral"
deploy_ephemeral
if check_for_labels "skip-tests"; then
echo "PR smoke tests skipped"
exit_code=3
else
echo "running PR smoke tests"
set +e
process_requirements_labels
set -e
run_smoke_tests
fi
fi
fi
mkdir -p $ARTIFACTS_DIR
cp $LABELS_DIR/github_labels.txt $ARTIFACTS_DIR/github_labels.txt
if [[ $exit_code != 0 ]]
then
echo "PR check failed"
make_results_xml
fi