-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrecord_coverage
More file actions
executable file
·92 lines (74 loc) · 2.24 KB
/
record_coverage
File metadata and controls
executable file
·92 lines (74 loc) · 2.24 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
84
85
86
87
88
89
90
91
92
#!/bin/bash -eu
# Record coverage
#
# This script uses the Github APIs to poke a comment into a PR about test coverage.
#
# To work, the GITHUB_TOKEN var must be in the environment
#
CURL_ARGS="-s -S -f"
function graceful_exit() {
echo "*** Something failed! Exiting gracefully so the build doesn't fail overall"
exit 0
}
#
# Wrapper for the Github GraphQL API
#
function gh_query() {
# Build and escape our JSON
json=$(jq -n --arg q "$*" '{query: $q}')
curl $CURL_ARGS -H "Authorization: bearer $GITHUB_TOKEN" -X POST -d "$json" https://api.github.com/graphql
}
# Trap any fails, and force a successful exit.
trap graceful_exit ERR
clover_xml=coverage/clover.xml
if ! [ -s $clover_xml ] ; then
echo "*** No $clover_xml file found."
exit 0
fi
sudo apt update -qq
sudo apt install -qq --no-install-recommends -y xmlstarlet
which jq > /dev/null || sudo apt-get install -y jq
which curl > /dev/null || sudo apt-get install -y curl
# This is the message that makes it into github
msg="* Github [Run ${GITHUB_RUN_ID}]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID)\n"
msg="$msg* Test coverage: "
statements=$(xmlstarlet sel -t -v '/coverage/project[@name="All files"]/metrics/@statements' $clover_xml)
coveredstatements=$(xmlstarlet sel -t -v '/coverage/project[@name="All files"]/metrics/@coveredstatements' $clover_xml)
# Bash doesn't do floating point.
coverage=$((coveredstatements*100/statements))
if [ "${coverage}" = "null" ] ; then
echo "*** Failed to determine coverage"
exit 0
fi
msg="$msg $coverage%\n\n"
# Find associated PR. *NB* we're assuming that the first, open PR is the one
# to comment on.
q="query {
repository(name: \"${GITHUB_REPOSITORY##*/}\", owner: \"${GITHUB_REPOSITORY%%/*}\") {
ref(qualifiedName: \"${GITHUB_REF_NAME}\") {
associatedPullRequests(first: 1) {
nodes {
id
}
}
}
}
}"
pr_response=$(gh_query $q)
pr_node=$(echo $pr_response | jq -r ".data.repository.ref.associatedPullRequests.nodes[0].id")
if [ "$pr_node" = "null" ] ; then
echo "*** No PR found"
exit 0
fi
echo ">>> Posting code coverage comment"
m="mutation {
addComment(input: {
subjectId: \"${pr_node}\",
body: \"${msg}\"
}) {
subject {
id
}
}
}"
gh_query $m