scripts: rework the pkgcheck report comment
Compare findings without the version, so a finding that a bump carries over from the previous ebuild is no longer reported as new. Group new findings by level and list the existing ones, because a flat list did not show which findings break the package. Fail the report check on a new error-level finding. Mark the scan unfinished when pkgcheck fails, which previously read as a clean report, and scan with --config no, because the exit keywords in metadata/pkgcheck.conf made a NonexistentDeps finding look like a failed scan. Signed-off-by: Zakk <zakk@gentoozh.org>
This commit is contained in:
1 parent
5cce2bfbe8
commit
d42a62a2fa
3 files changed
+120
-81
No files matched your search
+55
-37
@@ -2,7 +2,9 @@
|
||||
# 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
|
||||
|
||||
@@ -18,25 +20,57 @@ if [ -z "$pr" ]; then
|
||||
fi
|
||||
echo "pull request #${pr}"
|
||||
|
||||
if [ ! -f "$findings" ]; then
|
||||
echo 'the report predates findings.json'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# list <new> <level>...: one line per finding, existing ones prefixed with their level
|
||||
list() {
|
||||
jq -r --argjson new "$1" '
|
||||
.[] | select(.new == $new and (.level | IN($ARGS.positional[])))
|
||||
| (if $new then "" else "\(.level): " end) + "\(.target): \(.keyword): \(.desc)"
|
||||
' "$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 '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
|
||||
errors=$(list true error)
|
||||
warnings=$(list true warning)
|
||||
minor=$(list true style info)
|
||||
|
||||
{
|
||||
echo '## Pull request pkgcheck report'
|
||||
echo
|
||||
echo "*Newest commit scanned*: \`${commit}\`"
|
||||
echo "*Newest commit scanned*: \`$(cat "$report_dir"/commit)\`"
|
||||
if [ "${count}" -le 10 ]; then
|
||||
packages=()
|
||||
while IFS= read -r package; do
|
||||
@@ -48,37 +82,21 @@ fi
|
||||
fi
|
||||
if [ -f "$report_dir"/incomplete ]; 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'
|
||||
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
|
||||
echo '*Status*: :warning: **new issues**'
|
||||
echo
|
||||
if [ "${new}" -gt 10 ]; then
|
||||
echo "<details><summary>New issues caused by PR (${new})</summary>"
|
||||
echo
|
||||
close='</details>'
|
||||
if [ -n "$errors" ]; then
|
||||
echo '*Status*: :x: **new errors**'
|
||||
else
|
||||
echo 'New issues caused by PR:'
|
||||
echo
|
||||
close=''
|
||||
echo '*Status*: :warning: **new issues**'
|
||||
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."
|
||||
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
|
||||
echo
|
||||
echo "${MARKER}"
|
||||
|
||||
@@ -1,56 +1,72 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Print the pkgcheck findings a pull request adds, dropping the ones already there.
|
||||
"""Mark which pkgcheck findings a pull request adds and print all of them as JSON.
|
||||
|
||||
pkgcheck plain output puts a package on its own line and indents its findings under
|
||||
it. Findings are compared without their line numbers, so one that merely moved to
|
||||
another line does not count as new; a finding printed more often than in the base
|
||||
report counts as new that many times.
|
||||
Both reports are pkgcheck JsonStream output. A head finding is new unless the base
|
||||
report has the same keyword and message for the same package. The version, line
|
||||
numbers and the version inside the message are left out of the comparison, so a
|
||||
finding that a bump carries over from the previous ebuild is not new.
|
||||
|
||||
Lists the findings on stderr for the job log, and exits 3 when a new finding has the
|
||||
error level.
|
||||
|
||||
Usage: pkgcheck-new-findings.py BASE_REPORT HEAD_REPORT
|
||||
"""
|
||||
|
||||
from collections import Counter
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
|
||||
from pkgcheck.reporters import JsonStream
|
||||
|
||||
LEVELS = ("error", "warning", "style", "info")
|
||||
LINE_NUMBERS = re.compile(r"\blines?:? \d+(?:, \d+)*")
|
||||
|
||||
|
||||
def parse(path):
|
||||
"""(package, finding without line numbers, finding as pkgcheck printed it) for each finding."""
|
||||
findings = []
|
||||
package = None
|
||||
def load(path):
|
||||
with open(path) as report:
|
||||
for line in report:
|
||||
line = line.rstrip("\n")
|
||||
if not line:
|
||||
continue
|
||||
if not line.startswith(" "):
|
||||
package = line
|
||||
continue
|
||||
findings.append((package, LINE_NUMBERS.sub("line", line.strip()), line))
|
||||
return findings
|
||||
return list(JsonStream.from_iter(report))
|
||||
|
||||
|
||||
def describe(result):
|
||||
"""(package, target as pkgcheck names it, message with version and line numbers removed)."""
|
||||
category = getattr(result, "category", None)
|
||||
package = getattr(result, "package", None)
|
||||
version = getattr(result, "version", None)
|
||||
message = LINE_NUMBERS.sub("line", result.desc)
|
||||
target = "/".join(filter(None, (category, package)))
|
||||
if version:
|
||||
target += f"-{version}"
|
||||
for ver in {version, version.split("-r")[0]}:
|
||||
message = re.sub(rf"(?<![\d.]){re.escape(ver)}(?!\.?\d)", "VERSION", message)
|
||||
return (category, package), target, message
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 3:
|
||||
sys.exit(__doc__)
|
||||
existing = Counter((package, finding) for package, finding, _ in parse(sys.argv[1]))
|
||||
added = []
|
||||
for package, finding, line in parse(sys.argv[2]):
|
||||
if existing[(package, finding)]:
|
||||
existing[(package, finding)] -= 1
|
||||
else:
|
||||
added.append((package, line))
|
||||
existing = set()
|
||||
for result in load(sys.argv[1]):
|
||||
package, _, message = describe(result)
|
||||
existing.add((package, result.name, message))
|
||||
|
||||
package = None
|
||||
for pkg, line in added:
|
||||
if pkg != package:
|
||||
if package is not None:
|
||||
print()
|
||||
print(pkg)
|
||||
package = pkg
|
||||
print(line)
|
||||
findings = []
|
||||
for result in load(sys.argv[2]):
|
||||
package, target, message = describe(result)
|
||||
findings.append({
|
||||
"level": result.level,
|
||||
"keyword": result.name,
|
||||
"target": target,
|
||||
"desc": result.desc,
|
||||
"new": (package, result.name, message) not in existing,
|
||||
})
|
||||
findings.sort(key=lambda f: (LEVELS.index(f["level"]), f["target"], f["keyword"]))
|
||||
json.dump(findings, sys.stdout, indent=1)
|
||||
print()
|
||||
for f in findings:
|
||||
mark = "new" if f["new"] else "old"
|
||||
print(f"{mark} {f['level']}: {f['target']}: {f['keyword']}: {f['desc']}", file=sys.stderr)
|
||||
if any(f["new"] and f["level"] == "error" for f in findings):
|
||||
sys.exit(3)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
+15
-10
@@ -20,38 +20,43 @@ case "${1:?packages, scan or stage}" in
|
||||
pmaint sync gentoo
|
||||
head=$(git rev-parse HEAD)
|
||||
|
||||
# --config no drops the exit keywords in metadata/pkgcheck.conf, so a failure means the
|
||||
# scan broke rather than that it found something.
|
||||
scan() {
|
||||
local out=$1
|
||||
shift
|
||||
pmaint regen --dir ~/.cache/pkgcheck/repos . > /dev/null
|
||||
pkgcheck scan --checks=-RedundantVersionCheck --keywords=-NonsolvableDepsInDev \
|
||||
"$@" > "${out}" 2>&1 || true
|
||||
pkgcheck scan --config no --checks=-RedundantVersionCheck --keywords=-NonsolvableDepsInDev \
|
||||
--reporter JsonStream "$@" > "${out}"
|
||||
}
|
||||
|
||||
mapfile -t pkgs < packages.txt
|
||||
scan report.txt "${pkgs[@]}"
|
||||
scan report.json "${pkgs[@]}"
|
||||
|
||||
git checkout --quiet 'HEAD^1'
|
||||
: > base-report.txt
|
||||
: > base-report.json
|
||||
mapfile -t base_pkgs < <(
|
||||
for pkg in "${pkgs[@]}"; do
|
||||
ls "${pkg}"/*.ebuild > /dev/null 2>&1 && echo "${pkg}"
|
||||
done
|
||||
)
|
||||
[ "${#base_pkgs[@]}" -eq 0 ] || scan base-report.txt "${base_pkgs[@]}"
|
||||
[ "${#base_pkgs[@]}" -eq 0 ] || scan base-report.json "${base_pkgs[@]}"
|
||||
git checkout --quiet "${head}"
|
||||
|
||||
python3 scripts/pkgcheck-new-findings.py base-report.txt report.txt > new.txt
|
||||
echo '--- new ---'; cat new.txt
|
||||
echo '--- all ---'; cat report.txt
|
||||
rc=0
|
||||
python3 scripts/pkgcheck-new-findings.py base-report.json report.json > findings.json || rc=$?
|
||||
[ "${rc}" -eq 0 ] || [ "${rc}" -eq 3 ] || exit "${rc}"
|
||||
: > scan-complete
|
||||
if [ "${rc}" -eq 3 ]; then
|
||||
echo '::error::this pull request adds error level pkgcheck findings'
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
stage)
|
||||
mkdir -p pkgcheck-report
|
||||
git rev-parse --short 'HEAD^2' > pkgcheck-report/commit
|
||||
cp packages.txt pkgcheck-report/packages 2>/dev/null || : > pkgcheck-report/packages
|
||||
cp report.txt pkgcheck-report/report.txt 2>/dev/null || : > pkgcheck-report/report.txt
|
||||
cp new.txt pkgcheck-report/new.txt 2>/dev/null || : > pkgcheck-report/new.txt
|
||||
cp findings.json pkgcheck-report/findings.json 2>/dev/null || echo '[]' > pkgcheck-report/findings.json
|
||||
# the comment runs master's scripts, which may be newer than the ones that made this artifact
|
||||
[ -f scan-complete ] || : > pkgcheck-report/incomplete
|
||||
;;
|
||||
|
||||
Reference in new issue
Block a user