-
Notifications
You must be signed in to change notification settings - Fork 7
144 lines (129 loc) · 5.43 KB
/
notify-coverage.yml
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
143
144
name: Notify coverage changes
# Daily notification for coverage changes in `main`.
#
# PR coverage diffs are computed directly in the `Continuous Integration` workflow.
on:
schedule:
# 04:00 daily
- cron: '0 4 * * *'
workflow_dispatch: {}
jobs:
check-coverage:
runs-on: ubuntu-latest
outputs:
msg: ${{ steps.make_msg.outputs.msg }}
should_notify: ${{ steps.get_coverage.outputs.should_notify }}
steps:
- name: Download commit sha of the most recent successful run
uses: dawidd6/action-download-artifact@v2
with:
# Downloads the artifact from the most recent successful run
workflow: 'notify-coverage.yml'
name: head-sha.txt
if_no_artifact_found: ignore
- name: Get today's and yesterday's coverage trends from codecov
id: get_coverage
# API reference: https://docs.codecov.com/reference/repos_totals_retrieve
run: |
# Get the previous commit coverage, if the last sha is available
if [ ! -f head-sha.txt ]
then
echo "No previous coverage found."
# Update the head-sha.txt file with the current sha,
# so next time we campare against the current coverage.
echo ${{ github.sha }} > head-sha.txt
echo "save_current=true" >> "$GITHUB_OUTPUT"
echo "should_notify=false" >> "$GITHUB_OUTPUT"
exit 0
fi
PREV_SHA=$( cat head-sha.txt )
echo "Previous sha: \"$PREV_SHA\""
# Check if the sha has changed
if [ "$PREV_SHA" == "${{ github.sha }}" ]
then
echo "No new commits since last run."
echo "save_current=false" >> "$GITHUB_OUTPUT"
echo "should_notify=false" >> "$GITHUB_OUTPUT"
exit 0
fi
# Update the head-sha.txt file with the current sha,
# so next time we campare against the current coverage.
echo ${{ github.sha }} > head-sha.txt
# Query the previous coverage from codecov
curl --request GET \
--url "https://api.codecov.io/api/v2/github/${{ github.repository_owner }}/repos/${{ github.event.repository.name }}/totals/?sha=$PREV_SHA" \
--header 'accept: application/json' \
--header "authorization: Bearer ${{ secrets.CODECOV_GET_TOKEN }}" \
> coverage-prev.json
cat coverage-prev.json | jq ".totals.coverage" > coverage-prev.txt
echo "Previous coverage query result:"
cat coverage-prev.json | jq "del(.files)"
echo
# Query the current coverage from codecov
curl --request GET \
--url "https://api.codecov.io/api/v2/github/${{ github.repository_owner }}/repos/${{ github.event.repository.name }}/totals/?sha=${{ github.sha }}" \
--header 'accept: application/json' \
--header "authorization: Bearer ${{ secrets.CODECOV_GET_TOKEN }}" \
> coverage.json
cat coverage.json | jq ".totals.coverage" > coverage.txt
echo "Current coverage query result:"
cat coverage.json | jq "del(.files)"
echo
echo
echo "Previous coverage: `cat coverage-prev.txt`%"
echo "Current coverage: `cat coverage.txt`%"
# A `null` in either coverage means that the coverage is not available,
# so we don't want to notify about that.
if [ "$( cat coverage-prev.txt )" == "null" ]
then
echo "Previous coverage not available."
echo "save_current=true" >> "$GITHUB_OUTPUT"
echo "should_notify=false" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ "$( cat coverage.txt )" == "null" ]
then
echo "Current coverage not available."
echo "save_current=false" >> "$GITHUB_OUTPUT"
echo "should_notify=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "save_current=true" >> "$GITHUB_OUTPUT"
echo "should_notify=true" >> "$GITHUB_OUTPUT"
- name: Compare with previous summary and make message
id: make_msg
if: steps.get_coverage.outputs.should_notify == 'true'
run: |
prev=`cat coverage-prev.txt`
current=`cat coverage-prev.txt`
change=`printf "%.2f%% --> %.2f%%" $prev $current`
codecov="https://codecov.io/gh/${{ github.repository }}?search=&trend=7%20days"
if (( $(echo "$prev < $current + 0.04" | bc -l) ))
then
MSG="msg=Coverage check for hugr shows no regression (${change}). ✅ ${codecov}"
else
MSG="msg=Coverage check for hugr shows regression (${change}). ❌ ${codecov}"
fi
echo $MSG
echo $MSG >> "$GITHUB_OUTPUT"
- name: Upload current HEAD sha
if: steps.get_coverage.outputs.save_current == 'true'
uses: actions/upload-artifact@v3
with:
name: head-sha.txt
path: head-sha.txt
notify-slack:
needs: check-coverage
runs-on: ubuntu-latest
if: needs.check-coverage.outputs.should_notify == 'true'
steps:
- name: Dummy
run: |
echo "test"
#- name: Send notification
# uses: slackapi/[email protected]
# with:
# channel-id: 'C04SHCL4FKP'
# slack-message: ${{ needs.check-coverage.outputs.msg }}
# env:
# SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}