Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions catalog/stall-sweep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
id: "stall-sweep"
name: "Stall sweep"
trigger: {"cron": "20 * * * *", "timezone": "UTC"}
check: {"run": "stall-sweep/check.sh", "timeout": 60}
thread: "same"
secrets: []
priority: 2
params: {"repo": "example-org/example-repo", "idle_minutes": 60}
---
Stall sweep for {{repo}}. The check below lists each open pull request that
has had no progress for {{idle_minutes}} minutes or more, with why it looks
stalled.

For each line:

1. Read the pull request, its checks and its comments, and confirm the
reason. If it moved since the check ran, skip it.
2. Name who owns the next step, by the repository's own rules: its builder
for a conflict, failing checks or requested changes; its reviewer, or
whoever starts reviews, when it waits for a review; whoever merges when it
is approved and green; whoever runs CI when its checks never finished.
3. Tell that owner, once, in one short comment on the pull request or a
message to them: what is waiting, for how long, and why.

Do not merge, review, push or rerun anything yourself unless the
repository's rules make that your job. Your comment counts as progress, so
the next sweep stays quiet about that pull request until it is idle again.
Reply with one line per pull request: its number, the reason and who you
told.
58 changes: 58 additions & 0 deletions catalog/stall-sweep/check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/sh
# stall-sweep's check: prints one line per open pull request in {{repo}}
# that has had no progress for {{idle_minutes}} minutes or more, with why it
# looks stalled. Prints nothing when nothing is stalled, so no agent starts.
# Any update counts as progress: a push, a comment, a review or a label.
# A read that fails, or that cannot list every open pull request (more than
# 1000), fails the run (exit 2) rather than looking quiet.
set -u
prs=$(timeout 40 gh pr list -R "{{repo}}" --state open --limit 1001 \
--json number,isDraft,updatedAt,mergeable,reviewDecision,statusCheckRollup) || {
echo "stall-sweep: could not list pull requests in {{repo}}" >&2
exit 2
}
printf '%s' "$prs" | python3 -I -c '
import datetime, json, sys
idle_limit = int(sys.argv[1])
now = datetime.datetime.now(datetime.timezone.utc)
prs = json.load(sys.stdin)
if len(prs) > 1000:
sys.stderr.write("stall-sweep: more than 1000 open pull requests; cannot read them all\n")
sys.exit(2)
bad = ("FAILURE", "ERROR", "TIMED_OUT", "CANCELLED", "ACTION_REQUIRED", "STARTUP_FAILURE")
for p in sorted(prs, key=lambda p: p["number"]):
if p["isDraft"]:
continue
updated = datetime.datetime.fromisoformat(p["updatedAt"].replace("Z", "+00:00"))
idle = int((now - updated).total_seconds() // 60)
if idle < idle_limit:
continue
failing, running = [], False
for c in p.get("statusCheckRollup") or []:
if c.get("__typename") == "StatusContext":
state, name = c.get("state"), c.get("context")
if state in bad:
failing.append(name)
elif state in ("PENDING", "EXPECTED"):
running = True
elif c.get("status") != "COMPLETED":
running = True
elif c.get("conclusion") in bad:
failing.append(c.get("name"))
why = []
if p.get("mergeable") == "CONFLICTING":
why.append("conflicts with its base")
if failing:
why.append("failing checks: " + ", ".join(failing))
elif running:
why.append("checks not finished")
if not why:
decision = p.get("reviewDecision")
if decision == "APPROVED":
why.append("approved and green, not merged")
elif decision == "CHANGES_REQUESTED":
why.append("changes requested, no new push")
else:
why.append("green, waiting for a review")
print("#%d idle %d min: %s" % (p["number"], idle, "; ".join(why)))
' "{{idle_minutes}}" || exit 2
95 changes: 95 additions & 0 deletions catalog/stall-sweep/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/sh
# Runs stall-sweep's check against a stand-in `gh` that prints synthetic
# pull requests, and checks what the check prints. Needs sh, sed, date and
# python3. Run from anywhere: sh catalog/stall-sweep/test.sh
set -u
here=$(cd "$(dirname "$0")" && pwd)
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
mkdir "$work/bin"
cat > "$work/bin/gh" <<'GH'
#!/bin/sh
# Like gh pr list: returns at most --limit pull requests (default 30), newest first.
[ -f "$FIXTURE" ] || { echo "gh: could not reach GitHub" >&2; exit 1; }
limit=30
while [ $# -gt 0 ]; do [ "$1" = "--limit" ] && limit=$2; shift; done
python3 -I -c 'import json, sys
prs = sorted(json.load(open(sys.argv[1])), key=lambda p: -p["number"])
print(json.dumps(prs[:int(sys.argv[2])]))' "$FIXTURE" "$limit"
GH
chmod +x "$work/bin/gh"
# The check as an adapter applies it: params replaced by their values.
sed -e 's|{{repo}}|example-org/example-repo|g' -e 's|{{idle_minutes}}|60|g' \
"$here/check.sh" > "$work/check.sh"

# Half a minute past each whole minute, so a slow run cannot change the count.
ago() { date -u -d "-$(( $1 * 60 + 30 )) seconds" +%Y-%m-%dT%H:%M:%SZ; }
fail=0
# run <name> <fixture file or "none"> <expected exit> <expected stdout>
run() {
out=$(cd "$work" && FIXTURE="$2" PATH="$work/bin:$PATH" sh ./check.sh 2>/dev/null)
code=$?
if [ "$code" != "$3" ] || [ "$out" != "$4" ]; then
printf 'FAIL %s\n exit %s, want %s\n got: %s\n want: %s\n' "$1" "$code" "$3" "$out" "$4"
fail=1
else
echo "ok $1"
fi
}

# catches: a quiet wake that still prints, so an agent starts for nothing.
echo '[]' > "$work/none.json"
run "no open pull requests: quiet" "$work/none.json" 0 ""

# catches: waking on work that moved recently, or on a draft nobody waits for.
cat > "$work/fresh.json" <<J
[{"number": 3, "title": "#2: fresh", "isDraft": false, "updatedAt": "$(ago 10)", "mergeable": "CONFLICTING", "reviewDecision": "", "statusCheckRollup": []},
{"number": 4, "title": "#2: a draft", "isDraft": true, "updatedAt": "$(ago 600)", "mergeable": "MERGEABLE", "reviewDecision": "", "statusCheckRollup": []}]
J
run "fresh and draft pull requests: quiet" "$work/fresh.json" 0 ""

# catches: a stalled pull request missed, or reported without the right reason.
cat > "$work/stalled.json" <<J
[{"number": 12, "title": "#5: conflicted and red", "isDraft": false, "updatedAt": "$(ago 90)", "mergeable": "CONFLICTING", "reviewDecision": "",
"statusCheckRollup": [{"__typename": "CheckRun", "name": "build", "status": "COMPLETED", "conclusion": "FAILURE"},
{"__typename": "CheckRun", "name": "lint", "status": "COMPLETED", "conclusion": "SUCCESS"}]},
{"number": 13, "title": "#6: approved", "isDraft": false, "updatedAt": "$(ago 125)", "mergeable": "MERGEABLE", "reviewDecision": "APPROVED",
"statusCheckRollup": [{"__typename": "StatusContext", "context": "ci", "state": "SUCCESS"}]},
{"number": 14, "title": "#7: stuck checks", "isDraft": false, "updatedAt": "$(ago 61)", "mergeable": "MERGEABLE", "reviewDecision": "",
"statusCheckRollup": [{"__typename": "CheckRun", "name": "build", "status": "QUEUED", "conclusion": ""}]},
{"number": 15, "title": "#8: nobody reviewed", "isDraft": false, "updatedAt": "$(ago 70)", "mergeable": "MERGEABLE", "reviewDecision": "REVIEW_REQUIRED",
"statusCheckRollup": [{"__typename": "CheckRun", "name": "build", "status": "COMPLETED", "conclusion": "SUCCESS"}]},
{"number": 16, "title": "#9: changes asked", "isDraft": false, "updatedAt": "$(ago 200)", "mergeable": "MERGEABLE", "reviewDecision": "CHANGES_REQUESTED",
"statusCheckRollup": []}]
J
run "stalled pull requests: one line each, with why" "$work/stalled.json" 0 "#12 idle 90 min: conflicts with its base; failing checks: build
#13 idle 125 min: approved and green, not merged
#14 idle 61 min: checks not finished
#15 idle 70 min: green, waiting for a review
#16 idle 200 min: changes requested, no new push"

# many <file> <count>: <count> open pull requests, #2 upward fresh, #1 stalled.
many() {
python3 -I -c 'import json, sys
fresh, old = sys.argv[2], sys.argv[3]
prs = [{"number": n, "isDraft": False, "updatedAt": fresh, "mergeable": "MERGEABLE",
"reviewDecision": "", "statusCheckRollup": []} for n in range(2, int(sys.argv[1]) + 1)]
prs.append({"number": 1, "isDraft": False, "updatedAt": old, "mergeable": "CONFLICTING",
"reviewDecision": "", "statusCheckRollup": []})
print(json.dumps(prs))' "$2" "$(ago 10)" "$(ago 1440)" > "$1"
}

# catches: a capped read that drops the oldest pull request, so a stalled one
# behind many fresh ones is never seen and the wake looks quiet.
many "$work/many.json" 201
run "201 open, the oldest stalled: reported" "$work/many.json" 0 "#1 idle 1440 min: conflicts with its base"

# catches: a repository with more open pull requests than the check can read
# looking quiet instead of failing the run.
many "$work/toomany.json" 1001
run "1001 open: the run fails, nothing printed" "$work/toomany.json" 2 ""

# catches: an unreachable GitHub read as "nothing is due", hiding every stall.
run "gh fails: the run fails, nothing printed" "none" 2 ""

exit $fail
Loading