-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpost-cdash-status
More file actions
executable file
·83 lines (68 loc) · 2.53 KB
/
Copy pathpost-cdash-status
File metadata and controls
executable file
·83 lines (68 loc) · 2.53 KB
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
#!/bin/bash
set -eo pipefail
readonly API_BASE="https://api.github.com/repos/${GITHUB_REPOSITORY}"
function require_cmd() {
command -v "${1}" &> /dev/null || { echo "${1} not found"; exit 1; }
}
function require_env_var() {
if [ -z "${!1}" ]; then
echo "${1} env variable empty"
exit 2
fi
}
#==============================================================================
require_cmd "curl"
require_cmd "jq"
require_env_var "CDASH_BASE_URL"
require_env_var "CDASH_PROJECT"
require_env_var "COMMIT_SHA"
require_env_var "GITHUB_REPOSITORY"
require_env_var "GITHUB_STATUS_NAME"
require_env_var "GITHUB_TOKEN"
#==============================================================================
readonly status_response=$(
curl -q -s \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${API_BASE}/commits/${COMMIT_SHA}/statuses")
# The API returns an empty array if no statuses were found
# This can happen if the COMMIT_SHA doesn't point to the head of the branch/PR
if [ "true" == $(jq -r '. == []' <<<$status_response) ]; then
echo "No statuses found for '${API_BASE}/commits/${COMMIT_SHA}'"
exit 1
fi
readonly statuses=$(jq -r '[.[].context] | @json' <<< $status_response)
if [ -z "$statuses" ]; then
echo "Failed to parse statuses for '${API_BASE}/commits/${COMMIT_SHA}'"
echo -e "Request returned:\n$status_response"
exit 2
fi
post_body="$(cat<<EOF
{
"state": "success",
"target_url": "${CDASH_BASE_URL}/index.php?compare1=61&filtercount=1&field1=revision&project=${CDASH_PROJECT}&showfilters=0&limit=100&value1=${COMMIT_SHA}&showfeed=0",
"description": "Build and test results available on CDash",
"context": "${GITHUB_STATUS_NAME}"
}
EOF
)"
post_url="${API_BASE}/statuses/${COMMIT_SHA}"
if [ "true" == $(jq -re "all(. != \"${GITHUB_STATUS_NAME}\")" <<<"${statuses}") ]; then
echo "Need to post a status for context ${GITHUB_STATUS_NAME}"
readonly response=$(
curl -X POST -q -s \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d "${post_body}" "${post_url}")
if [ "false" == $(jq -r 'has("creator")') <<<"$response" ]; then
echo "Failed to create status for '${API_BASE}/commits/${COMMIT_SHA}'"
echo -e "Request returned:\n$response"
exit 3
fi
else
echo "No status updates needed."
fi