Files
gentoo-zh/scripts/pkgcheck-comment.sh
T
Zakk f9717dd2ad scripts: keep the pkgcheck comment to the findings it quotes
pkgcheck-comment.sh runs from master with a token that can write
comments, but takes the commit, the package names and the findings from
the report that the pull request's own run produced. A finding
description with a newline and six backticks closed the code block it
was quoted in, so a pull request could add any Markdown, link or
mention to the bot comment.

The commit and package names now keep only atom characters, as
emerge-elog-comment.sh does with its artifact fields. Each finding has
its control characters replaced by spaces and is cut to 300
characters, so it stays one line inside its code block.

Signed-off-by: Zakk <zakk@gentoozh.org>
2026-10-22 20:46:05 +11:00

118 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# usage: pkgcheck-comment.sh <report-directory>
set -euo pipefail
MARKER='<!-- pkgcheck-report -->'
MAX_LINES=30
report_dir=${1:?report-directory}
findings=$report_dir/findings.json
body_file=$(mktemp)
trap 'rm -f "$body_file"' EXIT
pr=$(scripts/pr-at-commit.sh "$GITHUB_REPOSITORY" "$HEAD_SHA")
if [ -z "$pr" ]; then
echo 'no open pull request at this commit'
exit 0
fi
echo "pull request #${pr}"
if [ ! -f "$findings" ]; then
echo 'the report predates findings.json'
exit 0
fi
# atom: stdin cut to 200 atom characters. The pull request's run wrote the report, so
# the commit and package names go through it.
atom() {
head -c 200 | tr -cd '[:alnum:]/._+=~@:-'
}
# list <new> <level>...: one line per finding, existing ones prefixed with their level
# and control characters turned into spaces, so a finding cannot leave its code block
list() {
jq -r --argjson new "$1" '
.[] | select(.new == $new and (.level | IN($ARGS.positional[])))
| (if $new then "" else "\(.level): " end) + "\(.target): \(.keyword): \(.desc)"
| gsub("[[:cntrl:]]"; " ") | .[:300]
' "$findings" --args "${@:2}"
}
# block <title> <collapsed> <lines>
block() {
local title=$1 collapsed=$2 lines=$3 n
n=$(grep -c . <<< "$lines" || true)
[ "$n" -gt 0 ] || return 0
echo
if [ "$collapsed" = yes ]; then
echo "<details><summary>${title} (${n})</summary>"
else
echo "### ${title} (${n})"
fi
echo
echo '``````'
head -n "$MAX_LINES" <<< "$lines"
echo '``````'
if [ "$n" -gt "$MAX_LINES" ]; then
echo
echo "$(( n - MAX_LINES )) more in the [run log](${RUN_URL})."
fi
if [ "$collapsed" = yes ]; then
echo
echo '</details>'
fi
}
count=$(wc -l < "$report_dir"/packages)
if [ "$count" -eq 0 ]; then
{
echo '## Pull request pkgcheck report'
echo
echo "*Newest commit scanned*: \`$(atom < "$report_dir"/commit)\`"
echo '*Status*: :white_check_mark: no packages touched'
} > "$body_file"
scripts/upsert-comment-section.sh "$GITHUB_REPOSITORY" "$pr" "$MARKER" pkgcheck "$body_file"
# emerge-on-pr skips a pull request that only touches .github, so drop its report here
: > "$body_file"
scripts/upsert-comment-section.sh "$GITHUB_REPOSITORY" "$pr" "$MARKER" elog "$body_file"
echo 'no packages touched'
exit 0
fi
errors=$(list true error)
warnings=$(list true warning)
minor=$(list true style info)
{
echo '## Pull request pkgcheck report'
echo
echo "*Newest commit scanned*: \`$(atom < "$report_dir"/commit)\`"
if [ "${count}" -le 10 ]; then
packages=()
while IFS= read -r package; do
packages+=("\`$(atom <<< "$package")\`")
done < "$report_dir/packages"
echo "*Packages scanned*: ${packages[*]}"
else
echo "*Packages scanned*: ${count}"
fi
if [ -f "$report_dir"/incomplete ]; then
echo "*Status*: :x: the scan did not finish, see the [run](${RUN_URL})"
elif [ -z "$errors$warnings$minor" ]; then
echo '*Status*: :white_check_mark: no new findings'
block 'Existing findings in these packages' yes "$(list false error warning style info)"
else
if [ -n "$errors" ]; then
echo '*Status*: :x: **new errors**'
else
echo '*Status*: :warning: **new issues**'
fi
echo
echo "This pull request adds these findings. Errors fail the \`report\` check."
block 'New errors' no "$errors"
block 'New warnings' no "$warnings"
block 'New style and info findings' yes "$minor"
block 'Existing findings in these packages' yes "$(list false error warning style info)"
fi
} > "$body_file"
scripts/upsert-comment-section.sh "$GITHUB_REPOSITORY" "$pr" "$MARKER" pkgcheck "$body_file"
echo "commented on #${pr}"