mattermost-mobile/scripts/compare-coverage.sh
Rahim Rahman abb526cf83
ci(MM-63199): code coverage tracking (#8642)
* ci(MM-63199): code coverage tracking

* try to download existing coverage file

* read coverage

* add token

* use github.token instead

* passing github token

* github_token passing from workflow

* remove download

* re-add download

* wrong param

* try download all artifacts

* add run-id so to retrieve with download later

* remove read coverage temp

* use run-id to download

* put files into current-coverage

* using last run id

* temporary comment

* can retrieve last run id?

* remove hard-coding

* echo into github_env vs export

* comparing new and old

* comparison improvement

* post to github

* fix coverage text

* refactor to main from current-coverage

* formatting changes

* fix missing content

* small tweaking

* showing the Warning to make sure

* formatting

* remove +

* checking to see if the error shows via echo

* revert the change to error

* separate to a new file

* comment the actual test for now

* prep node deps

* only run certain things on main

* trying cache-hit

* real trying cache-hit

* testing to make sure cache-run-id runs

* save-always true

* save-always deprecated

* let's try different strategy

* add key

* restore-key adding a -

* only perform on `main`

* only run on main or if its a PR

* coverage_threshold

* remove comments

* add total

* removing unneeded comments

* calculate total

* run test in `release-*` only

* making sure that only PR will run

* only do more steps if upload-coverage successful

* trying thollander/actions-comment-pull-request

* using diff way to comment.

* comment on how things work

* testing to trigger warning and see if comment is updated vs new comment

* omit echo messages

* see if giving github token would work.

* wrong use of param

* try without github token

* adding a very simple change to see where it lands

* using cache hit instead.

* creating the cache again.  how did i lose it?

* revert back

* cache-hit might be off

* debug

* debug with failing cache restoration

* check for run-id.txt instead

* all into action

* missing "

* remove unneeded actions
* change threshold to 0.5

* relative time

* skeptical about date formatting

* revert back to the threshold trigger

* below 80% total coverage threshold

* only show one error/warning at  time.

* testing if the coverage drop below 80

* debug output

* add Reset Test Coverage label use

* try using contains vs direct comparsion

* remove the label checker

* temp change

* ooops

* revert back

* let's post before exiting

* consistency

* total coverage threshold reset to 80%
2025-03-14 13:21:25 -06:00

75 lines
2.7 KiB
Bash
Executable file

#!/bin/bash
COVERAGE_THRESHOLD=0.5
PR_COVERAGE_THRESHOLD=80.0
MAIN_COVERAGE_FILE="$1/coverage-summary.json"
RECENT_COVERAGE_FILE="$2/coverage-summary.json"
PR_NUMBER="$3"
GITHUB_TOKEN="$4"
if [ ! -f "$MAIN_COVERAGE_FILE" ] || [ ! -f "$RECENT_COVERAGE_FILE" ]; then
echo "One or both coverage files not found"
exit 0
fi
COMMENT_BODY="### Coverage Comparison Report
<relative-time datetime=\"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\">Generated on $(date '+%B %d, %Y at %H:%M:%S UTC')</relative-time>
\`\`\`
+-----------------+------------+------------+-----------+
| Metric | Main | This PR | Diff |
+-----------------+------------+------------+-----------+"
HAS_DECREASE=0
BELOW_THRESHOLD=0
# Calculate average total coverage since the summary doesn't provide an overall total
main_total=0
pr_total=0
metric_count=0
for metric in lines statements branches functions; do
main=$(jq ".total.${metric}.pct" "$MAIN_COVERAGE_FILE")
pr=$(jq ".total.${metric}.pct" "$RECENT_COVERAGE_FILE")
diff=$(echo "$pr - $main" | bc)
main_total=$(echo "$main_total + $main" | bc)
pr_total=$(echo "$pr_total + $pr" | bc)
metric_count=$((metric_count + 1))
row=$(printf "| %-15s | %9.2f%% | %9.2f%% | %8.2f%% |" "${metric^}" "$main" "$pr" "$diff")
COMMENT_BODY+=$'\n'"$row"
if (( $(echo "$diff < -$COVERAGE_THRESHOLD" | bc -l) )); then
# Write error messages to stderr instead of stdout, since we don't want them to be shown in the PR comment
echo "::error::${metric^} coverage has decreased by more than ${COVERAGE_THRESHOLD}% ($diff%)" >&2
HAS_DECREASE=1
fi
done
COMMENT_BODY+=$'\n'"+-----------------+------------+------------+-----------+"
main_avg=$(echo "scale=2; $main_total / $metric_count" | bc)
pr_avg=$(echo "scale=2; $pr_total / $metric_count" | bc)
total_diff=$(echo "$pr_avg - $main_avg" | bc)
if (( $(echo "$pr_avg < $PR_COVERAGE_THRESHOLD" | bc -l) )); then
echo "::error::Total coverage ($pr_avg%) is below the minimum required coverage of ${PR_COVERAGE_THRESHOLD}%" >&2
BELOW_THRESHOLD=1
HAS_DECREASE=1
fi
row=$(printf "| %-15s | %9.2f%% | %9.2f%% | %8.2f%% |" "Total" "$main_avg" "$pr_avg" "$total_diff")
COMMENT_BODY+=$'\n'"$row"
COMMENT_BODY+=$'\n'"+-----------------+------------+------------+-----------+
\`\`\`"
if [ "$BELOW_THRESHOLD" -eq 1 ]; then
COMMENT_BODY+=$'\n\n'"🚨 **Error:** Total coverage ($pr_avg%) is below the minimum required coverage of ${PR_COVERAGE_THRESHOLD}%"
elif [ "$HAS_DECREASE" -eq 1 ]; then
COMMENT_BODY+=$'\n\n'"⚠️ **Warning:** One or more coverage metrics have decreased by more than ${COVERAGE_THRESHOLD}%"
fi
echo "$COMMENT_BODY"
echo "status=$BELOW_THRESHOLD" >> $GITHUB_OUTPUT