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
16 changes: 11 additions & 5 deletions plugins/setkernel/skills/request-review/request-review.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
# the PR isn't labelled `human`. sk-agent asks peers only for the PRs it opens, so this does the same
# for a person's PR: reviewers first, the label last, as sk-agent does.
#
# Candidates: the org team's members, minus the PR author, the caller and Raghav, least busy first
# Candidates: the members of team seats, minus the PR author, the caller and Raghav, least busy first
# (open review requests in the org), random among ties. One REST call per login; a refused login is
# skipped; a login GitHub dropped (read back after each call) is asked once more. A seat is a team
# member other than Raghav or the author who is a requested reviewer. With two seats on the PR the
# label goes on and it exits 0; a re-run asks nobody new. Fewer than two after every candidate: no
# label, exit 1. It never approves, merges, comments or edits the PR body. POSIX sh, gh and jq only.
set -u
org=setkernel
team=engineering
team=seats
owner=realRaghavGupta
label=agent-review
want=2
Expand Down Expand Up @@ -46,11 +46,17 @@ printf '%s' "$pr" | jq -e '[.labels[].name] | index("human")' >/dev/null &&
die "$url is labelled human: agents don't review it."

me=$(gh api user --jq .login) && [ -n "$me" ] || die "cannot tell who is calling (gh api user)."
members=$(gh api --paginate "orgs/$org/teams/$team/members" --jq '.[].login') && [ -n "$members" ] ||
die "cannot list the members of $org/$team (the gh login needs read:org)."

tmpd=$(mktemp -d) || die "mktemp failed"
trap 'rm -rf "$tmpd"' EXIT
# A 404 here means the team is missing (or hidden from this login), not a short scope: say which.
if members=$(gh api --paginate "orgs/$org/teams/$team/members" --jq '.[].login' 2>"$tmpd/err"); then
[ -n "$members" ] || die "the team $org/$team has no members."
else
if grep -q 'HTTP 404' "$tmpd/err"; then
die "the team $org/$team does not exist (or this gh login cannot see it): $(cat "$tmpd/err")"
fi
die "cannot list the members of $org/$team: $(cat "$tmpd/err")"
fi
# pool: the logins that count as seats. requested: the logins now requested on the PR.
printf '%s\n' "$members" | grep -Fvx -e "$owner" -e "$author" >"$tmpd/pool"
requested() { gh api "repos/$repo/pulls/$num/requested_reviewers" --jq '.users[].login'; }
Expand Down
12 changes: 10 additions & 2 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,10 @@ cat >"$rstub/gh" <<EOF
echo "\$*" >>"$rs/calls"
case "\$*" in
"api user --jq .login") echo zz-me ;;
"api --paginate orgs/setkernel/teams/engineering/members --jq .[].login") printf '%s\n' zz-author zz-me realRaghavGupta zz-a zz-b zz-c ;;
"api --paginate orgs/setkernel/teams/seats/members --jq .[].login")
[ -f "$rs/noteam" ] && { echo "gh: Not Found (HTTP 404)" >&2; exit 1; }
[ -f "$rs/noscope" ] && { echo "gh: Resource not accessible (HTTP 403)" >&2; exit 1; }
printf '%s\n' zz-author zz-me realRaghavGupta zz-a zz-b zz-c ;;
"api repos/example/app/pulls/1") cat "$rs/pr.json" ;;
"api repos/example/app/pulls/1/requested_reviewers --jq .users[].login") cat "$rs/requested" ;;
*" search/issues "*) l=\${*##*review-requested:}; l=\${l%% *}; cat "$rs/load-\$l" 2>/dev/null || echo 0 ;;
Expand Down Expand Up @@ -976,7 +979,7 @@ rr_pr open false ''
expect 0 "^Review requested from zz-b zz-c on https://github.com/example/app/pull/1; labelled agent-review.$" \
"request-review asks the two least busy seats and prints them with the PR URL" -- rr_run
expect 0 "^none$" "the author, the caller and Raghav are neither counted nor asked" -- \
sh -c "grep -E 'zz-author|zz-me|realRaghavGupta' '$rs/calls' | grep -v 'teams/engineering' || echo none"
sh -c "grep -E 'zz-author|zz-me|realRaghavGupta' '$rs/calls' | grep -v 'teams/seats' || echo none"
expect 0 "^2$" "one POST per seat" -- posts pulls/1/requested_reviewers
expect 0 "^0$" "each POST carries one login" -- sh -c "grep -c 'reviewers\[\]=.*reviewers\[\]=' '$rs/calls' || true"
expect 0 "^label last$" "the label comes after the reviewers" -- \
Expand All @@ -1003,6 +1006,11 @@ rr_refuses open true '' "is a draft"
rr_refuses open false '{"name":"human"}' "is labelled human"
rr_pr open false ''
expect 1 "refused inside an sk-agent run: sk-agent asks its own peers" "request-review refuses inside an sk-agent run" -- rr_run SETKERNEL_GUARD_NO_PUSH=1
rr_pr open false '' && : >"$rs/noteam"
expect 1 "the team setkernel/seats does not exist" "a missing team is named as missing" -- rr_run
rr_pr open false '' && : >"$rs/noscope"
expect 1 "cannot list the members of setkernel/seats: gh: Resource not accessible" "another failure keeps gh's own message" -- rr_run
rr_pr open false ''
expect 1 "not an owner/repo" "and a malformed repo" -- env PATH="$rstub:$PATH" $sh_cmd "$rr" 'example/app/x' 1

rr_pr open false '' && printf '%s\n' zz-c zz-a >"$rs/requested"
Expand Down
Loading