Files
gentoo-zh/scripts/pkgcheck-comment.sh
T
Zakk 471c387b74 scripts: stop the pkgcheck report from passing unfinished or hidden findings
A failed `pmaint sync` or `pmaint regen` left empty report files, and the
comment counted zero new findings and posted good. The scan now leaves a
complete marker and the comment reports a scan that did not finish.

New findings were keyed by package, keyword and version, so a second
finding of the same keyword in an existing version was taken for the old
one. They are now compared by their message without line numbers, counted
as a multiset.

The marker lookup only takes comments written by a bot, so a user comment
that quotes the marker is not edited.

Signed-off-by: Zakk <zakk@gentoozh.org>
2026-10-09 11:09:43 +11:00

89 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# usage: pkgcheck-comment.sh <report-directory>
set -euo pipefail
MARKER='<!-- pkgcheck-report -->'
report_dir=${1:?report-directory}
body_file=$(mktemp)
trap 'rm -f "$body_file"' EXIT
# commits/{sha}/pulls returns nothing for a fork PR; the artifact is not trusted
pulls=$(gh api "repos/$REPO/pulls?state=open&per_page=100" --paginate --slurp)
matches=$(jq -r --arg head_sha "$HEAD_SHA" '
.[][] | select(.head.sha == $head_sha) | .number
' <<< "$pulls")
pr=${matches%%$'\n'*}
if [ -z "$pr" ]; then
echo 'no open pull request at this commit'
exit 0
fi
echo "pull request #${pr}"
count=$(wc -l < "$report_dir"/packages)
if [ "$count" -eq 0 ]; then
echo 'no packages touched'
exit 0
fi
commit=$(cat "$report_dir"/commit)
# findings are indented under a package name, package names are not
new=$(grep -c '^ ' "$report_dir"/new.txt || true)
existing=$(( $(grep -c '^ ' "$report_dir"/report.txt || true) - new ))
issue_word=issues
if [ "$existing" -eq 1 ]; then
issue_word=issue
fi
{
echo '## Pull request pkgcheck report'
echo
echo "*Newest commit scanned*: \`${commit}\`"
if [ "${count}" -le 10 ]; then
packages=()
while IFS= read -r package; do
packages+=("\`$package\`")
done < "$report_dir/packages"
echo "*Packages scanned*: ${packages[*]}"
else
echo "*Packages scanned*: ${count}"
fi
if [ ! -f "$report_dir"/complete ]; then
echo "*Status*: :x: the scan did not finish, see the [run](${RUN_URL})"
elif [ "${new}" -eq 0 ]; then
echo '*Status*: :white_check_mark: good'
else
echo '*Status*: :warning: **new issues**'
echo
if [ "${new}" -gt 10 ]; then
echo "<details><summary>New issues caused by PR (${new})</summary>"
echo
close='</details>'
else
echo 'New issues caused by PR:'
echo
close=''
fi
echo '``````'
if [ "$(wc -c < "$report_dir"/new.txt)" -le 55000 ]; then
cat "$report_dir"/new.txt
else
head -c 55000 "$report_dir"/new.txt | head -n -1
echo '[truncated, see the full report]'
fi
echo '``````'
if [ -n "$close" ]; then
echo
echo "$close"
fi
fi
if [ "${existing}" -gt 0 ]; then
echo
echo "There are ${existing} existing ${issue_word} in these packages. Please look"
echo "into the [full report](${RUN_URL}) to make sure none of them affect your change."
fi
echo
echo "${MARKER}"
} > "$body_file"
scripts/upsert-comment.sh "$REPO" "$pr" "$MARKER" "$body_file"
echo "commented on #${pr}"