Files
cinny/scripts/ci-checks.sh
T
litruv a77f72f5d6
Checks / checks (push) Successful in 1m56s
Trigger cinny-mobile / dispatch (push) Successful in 1s
Strip the ANSI codes from check output before matching findings.
2026-11-01 03:37:32 +11:00

86 lines
3.7 KiB
Bash

#!/usr/bin/env bash
# Files one open Gitea issue per rule key, deduped by the marker line in the issue body.
# Never exits non-zero: findings are reported, not enforced.
set -u
API_BASE="${API_BASE:?API_BASE is required}"
REPO="${REPO:?REPO is required}"
TOKEN="${TOKEN:?TOKEN is required}"
LABEL="ci-checks"
SHOW="${SHOW:-50}"
cd "$(dirname "$0")/.."
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
FINDINGS="$WORK/findings.tsv"
: > "$FINDINGS"
# key<TAB>violation, one line per finding.
npx eslint src --format json > "$WORK/eslint.json" || true
jq -r --arg root "$PWD" '.[] | .filePath as $f | .messages[] | select(.ruleId) | "eslint/" + .ruleId + "\t" + (($f | ltrimstr($root + "/")) + ":" + (.line | tostring))' "$WORK/eslint.json" >> "$FINDINGS"
npx prettier --check . > "$WORK/prettier.log" 2>&1 || true
# Prettier forces colour in CI, so strip the ANSI codes before matching.
sed -E 's/\x1b\[[0-9;]*m//g' "$WORK/prettier.log" | sed -nE 's/^\[warn\] (.*)$/prettier\t\1/p' >> "$FINDINGS"
npx tsc --noEmit > "$WORK/tsc.log" 2>&1 || true
sed -E 's/\x1b\[[0-9;]*m//g' "$WORK/tsc.log" | sed -nE 's/^(.*)\(([0-9]+),([0-9]+)\): error (TS[0-9]+):.*/tsc\/\4\t\1(\2,\3)/p' >> "$FINDINGS"
api() { # api METHOD PATH [JSON] -> response body on stdout, non-zero on HTTP failure
local method=$1 path=$2 data=${3-} code
code=$(curl -sS -o "$WORK/resp" -w '%{http_code}' -X "$method" \
-H "Authorization: token $TOKEN" -H 'Content-Type: application/json' \
${data:+-d "$data"} "$API_BASE/repos/$REPO$path") || return 1
case "$code" in
200 | 201) cat "$WORK/resp" ;;
*) echo "HTTP $code $method $path: $(cat "$WORK/resp")" >&2; return 1 ;;
esac
}
body() { # body KEY HASH VIOLATIONS_FILE
printf '<!-- ci-check: %s -->\n<!-- ci-check-hash: %s -->\n\n`%s`: %d violations.\n\n```text\n' \
"$1" "$2" "$1" "$(wc -l < "$3")"
head -n "$SHOW" "$3"
printf '```\n'
}
LABEL_ID=$(api GET "/labels?limit=100" | jq -r --arg n "$LABEL" '[.[] | select(.name == $n) | .id] | first // empty')
if [ -z "$LABEL_ID" ]; then
LABEL_ID=$(api POST "/labels" "$(jq -n --arg n "$LABEL" '{name: $n, color: "006060"}')" | jq -r '.id')
fi
# Gitea caps per_page at 50, so paging is required or dedupe misses issues past the first page.
# Accumulate as JSONL: passing the accumulated set as a jq program argument overflows argv.
OPEN="$WORK/open.jsonl"
: > "$OPEN"
for page in 1 2 3 4 5 6 7 8; do
api GET "/issues?state=open&limit=50&page=$page" | jq -c --arg l "$LABEL" '[.[] | select((.labels // []) | any(.name == $l))]' > "$WORK/page.json"
n=$(jq -r 'length' "$WORK/page.json")
[ -z "$n" ] && break
jq -c '.[]' "$WORK/page.json" >> "$OPEN"
[ "$n" -lt 50 ] && break
done
for key in $(cut -f1 "$FINDINGS" | sort -u); do
vf="$WORK/violations.tsv"
awk -F'\t' -v k="$key" '$1 == k' "$FINDINGS" | sort > "$vf"
hash=$(sha256sum < "$vf" | cut -d' ' -f1)
row=$(jq -c --arg k "$key" 'select((.body // "") | contains("<!-- ci-check: " + $k + " -->"))' "$OPEN" | head -1)
if [ -z "$row" ]; then
api POST "/issues" "$(jq -n --arg t "ci-check: $key" --arg b "$(body "$key" "$hash" "$vf")" --argjson l "$LABEL_ID" '{title: $t, body: $b, labels: [$l]}')" > /dev/null
elif ! echo "$row" | jq -e --arg h "$hash" '(.body // "") | contains("<!-- ci-check-hash: " + $h + " -->")' > /dev/null; then
api PATCH "/issues/$(jq -r '.number' <<< "$row")" "$(jq -n --arg b "$(body "$key" "$hash" "$vf")" '{body: $b}')" > /dev/null
fi
done
jq -r '[((.body // "") | capture("<!-- ci-check: (?<k>[^ ]+) -->").k), .number] | @tsv' "$OPEN" > "$WORK/keys.tsv"
while IFS=$'\t' read -r key number; do
if ! grep -qF -- "$key"$'\t' "$FINDINGS"; then
api PATCH "/issues/$number" '{"state": "closed"}' > /dev/null
fi
done < "$WORK/keys.tsv"
exit 0