Skip to content

Commit 6971afb

Browse files
authored
feat: acknowledge pull request dispatch receipts (#1080)
1 parent 06bc47a commit 6971afb

3 files changed

Lines changed: 224 additions & 0 deletions

File tree

‎.github/workflows/clawsweeper-dispatch.yml‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,70 @@ jobs:
6969
permission-issues: write
7070
permission-pull-requests: read
7171

72+
- name: Create target PR acknowledgement token
73+
id: pr_ack_token
74+
if: >-
75+
${{
76+
github.event_name == 'pull_request_target' &&
77+
(
78+
github.event.action == 'ready_for_review' ||
79+
(github.event.action == 'opened' && github.event.pull_request.draft == false)
80+
) &&
81+
env.HAS_CLAWSWEEPER_APP_PRIVATE_KEY == 'true'
82+
}}
83+
continue-on-error: true
84+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
85+
with:
86+
client-id: ${{ env.CLAWSWEEPER_APP_CLIENT_ID }}
87+
private-key: ${{ secrets.CLAWSWEEPER_APP_PRIVATE_KEY }}
88+
owner: ${{ github.repository_owner }}
89+
repositories: ${{ github.event.repository.name }}
90+
permission-issues: write
91+
92+
- name: Acknowledge received pull request
93+
if: >-
94+
${{
95+
github.event_name == 'pull_request_target' &&
96+
(
97+
github.event.action == 'ready_for_review' ||
98+
(github.event.action == 'opened' && github.event.pull_request.draft == false)
99+
) &&
100+
env.HAS_CLAWSWEEPER_APP_PRIVATE_KEY == 'true'
101+
}}
102+
continue-on-error: true
103+
env:
104+
ACK_TOKEN: ${{ steps.pr_ack_token.outputs.token }}
105+
TARGET_REPO: ${{ github.repository }}
106+
ITEM_NUMBER: ${{ github.event.pull_request.number }}
107+
SOURCE_ACTION: ${{ github.event.action }}
108+
run: |
109+
set -euo pipefail
110+
if [ -z "$ACK_TOKEN" ]; then
111+
echo "::notice::Skipping ClawSweeper pull request acknowledgement because no target credential is configured."
112+
exit 0
113+
fi
114+
comments="$(GH_TOKEN="$ACK_TOKEN" gh api \
115+
"repos/$TARGET_REPO/issues/$ITEM_NUMBER/comments?per_page=100")"
116+
if jq -e \
117+
--arg marker_prefix "clawsweeper-pr-ack:" \
118+
--arg marker_suffix " item=$ITEM_NUMBER -->" \
119+
'any(.[]; (.body // "") as $body | ($body | contains($marker_prefix)) and ($body | contains($marker_suffix)))' \
120+
<<< "$comments" >/dev/null; then
121+
echo "ClawSweeper pull request acknowledgement already exists."
122+
exit 0
123+
fi
124+
ack_body="$(printf '%s\n' \
125+
"<!-- clawsweeper-pr-ack:$SOURCE_ACTION item=$ITEM_NUMBER -->" \
126+
"🦞👀" \
127+
"ClawSweeper picked this up." \
128+
"" \
129+
"Pull request received. I will update this pull request when review starts.")"
130+
ack_payload="$(jq -nc --arg body "$ack_body" '{body:$body}')"
131+
GH_TOKEN="$ACK_TOKEN" gh api \
132+
"repos/$TARGET_REPO/issues/$ITEM_NUMBER/comments" \
133+
--method POST \
134+
--input - <<< "$ack_payload"
135+
72136
- name: Dispatch exact ClawSweeper review
73137
if: ${{ github.event_name != 'issue_comment' }}
74138
env:

‎docs/target-dispatcher.md‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,70 @@ jobs:
114114
permission-issues: write
115115
permission-pull-requests: read
116116

117+
- name: Create target PR acknowledgement token
118+
id: pr_ack_token
119+
if: >-
120+
${{
121+
github.event_name == 'pull_request_target' &&
122+
(
123+
github.event.action == 'ready_for_review' ||
124+
(github.event.action == 'opened' && github.event.pull_request.draft == false)
125+
) &&
126+
env.HAS_CLAWSWEEPER_APP_PRIVATE_KEY == 'true'
127+
}}
128+
continue-on-error: true
129+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
130+
with:
131+
client-id: ${{ env.CLAWSWEEPER_APP_CLIENT_ID }}
132+
private-key: ${{ secrets.CLAWSWEEPER_APP_PRIVATE_KEY }}
133+
owner: ${{ github.repository_owner }}
134+
repositories: ${{ github.event.repository.name }}
135+
permission-issues: write
136+
137+
- name: Acknowledge received pull request
138+
if: >-
139+
${{
140+
github.event_name == 'pull_request_target' &&
141+
(
142+
github.event.action == 'ready_for_review' ||
143+
(github.event.action == 'opened' && github.event.pull_request.draft == false)
144+
) &&
145+
env.HAS_CLAWSWEEPER_APP_PRIVATE_KEY == 'true'
146+
}}
147+
continue-on-error: true
148+
env:
149+
ACK_TOKEN: ${{ steps.pr_ack_token.outputs.token }}
150+
TARGET_REPO: ${{ github.repository }}
151+
ITEM_NUMBER: ${{ github.event.pull_request.number }}
152+
SOURCE_ACTION: ${{ github.event.action }}
153+
run: |
154+
set -euo pipefail
155+
if [ -z "$ACK_TOKEN" ]; then
156+
echo "::notice::Skipping ClawSweeper pull request acknowledgement because no target credential is configured."
157+
exit 0
158+
fi
159+
comments="$(GH_TOKEN="$ACK_TOKEN" gh api \
160+
"repos/$TARGET_REPO/issues/$ITEM_NUMBER/comments?per_page=100")"
161+
if jq -e \
162+
--arg marker_prefix "clawsweeper-pr-ack:" \
163+
--arg marker_suffix " item=$ITEM_NUMBER -->" \
164+
'any(.[]; (.body // "") as $body | ($body | contains($marker_prefix)) and ($body | contains($marker_suffix)))' \
165+
<<< "$comments" >/dev/null; then
166+
echo "ClawSweeper pull request acknowledgement already exists."
167+
exit 0
168+
fi
169+
ack_body="$(printf '%s\n' \
170+
"<!-- clawsweeper-pr-ack:$SOURCE_ACTION item=$ITEM_NUMBER -->" \
171+
"🦞👀" \
172+
"ClawSweeper picked this up." \
173+
"" \
174+
"Pull request received. I will update this pull request when review starts.")"
175+
ack_payload="$(jq -nc --arg body "$ack_body" '{body:$body}')"
176+
GH_TOKEN="$ACK_TOKEN" gh api \
177+
"repos/$TARGET_REPO/issues/$ITEM_NUMBER/comments" \
178+
--method POST \
179+
--input - <<< "$ack_payload"
180+
117181
- name: Dispatch exact ClawSweeper review
118182
if: ${{ github.event_name != 'issue_comment' }}
119183
env:
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import assert from "node:assert/strict";
2+
import { readFileSync } from "node:fs";
3+
import test from "node:test";
4+
import { parse } from "yaml";
5+
6+
const liveWorkflow = readFileSync(".github/workflows/clawsweeper-dispatch.yml", "utf8").replace(
7+
/\r\n/g,
8+
"\n",
9+
);
10+
const documentation = readFileSync("docs/target-dispatcher.md", "utf8").replace(/\r\n/g, "\n");
11+
const templateMatch = documentation.match(/```yaml\n(name: ClawSweeper Dispatch[\s\S]*?)\n```/);
12+
13+
assert.ok(templateMatch, "target dispatcher workflow template is missing");
14+
const documentedWorkflow = `${templateMatch[1]}\n`;
15+
16+
type WorkflowStep = {
17+
name?: string;
18+
if?: string;
19+
run?: string;
20+
env?: Record<string, string>;
21+
with?: Record<string, string>;
22+
"continue-on-error"?: boolean;
23+
};
24+
25+
function dispatchSteps(source: string): WorkflowStep[] {
26+
const workflow = parse(source) as {
27+
jobs?: { dispatch?: { steps?: WorkflowStep[] } };
28+
};
29+
return workflow.jobs?.dispatch?.steps ?? [];
30+
}
31+
32+
function namedStep(steps: WorkflowStep[], name: string): WorkflowStep {
33+
const step = steps.find((candidate) => candidate.name === name);
34+
assert.ok(step, `missing workflow step: ${name}`);
35+
return step;
36+
}
37+
38+
function normalizeWhitespace(value: string | undefined): string {
39+
return (value ?? "").replace(/\s+/g, " ").trim();
40+
}
41+
42+
test("documented target dispatcher template matches the live workflow", () => {
43+
assert.equal(documentedWorkflow, liveWorkflow);
44+
});
45+
46+
test("target dispatcher acknowledges non-draft PR receipts before review dispatch", () => {
47+
for (const source of [liveWorkflow, documentedWorkflow]) {
48+
const steps = dispatchSteps(source);
49+
const tokenIndex = steps.findIndex(
50+
(step) => step.name === "Create target PR acknowledgement token",
51+
);
52+
const acknowledgementIndex = steps.findIndex(
53+
(step) => step.name === "Acknowledge received pull request",
54+
);
55+
const dispatchIndex = steps.findIndex(
56+
(step) => step.name === "Dispatch exact ClawSweeper review",
57+
);
58+
assert.ok(tokenIndex >= 0 && tokenIndex < acknowledgementIndex);
59+
assert.ok(acknowledgementIndex < dispatchIndex);
60+
61+
const token = namedStep(steps, "Create target PR acknowledgement token");
62+
const acknowledgement = namedStep(steps, "Acknowledge received pull request");
63+
const expectedGate = [
64+
"${{",
65+
"github.event_name == 'pull_request_target' &&",
66+
"(",
67+
"github.event.action == 'ready_for_review' ||",
68+
"(github.event.action == 'opened' && github.event.pull_request.draft == false)",
69+
") &&",
70+
"env.HAS_CLAWSWEEPER_APP_PRIVATE_KEY == 'true'",
71+
"}}",
72+
].join(" ");
73+
74+
assert.equal(normalizeWhitespace(token.if), expectedGate);
75+
assert.equal(normalizeWhitespace(acknowledgement.if), expectedGate);
76+
assert.equal(token["continue-on-error"], true);
77+
assert.equal(acknowledgement["continue-on-error"], true);
78+
assert.deepEqual(
79+
Object.keys(token.with ?? {}).filter((key) => key.startsWith("permission-")),
80+
["permission-issues"],
81+
);
82+
assert.equal(token.with?.["permission-issues"], "write");
83+
assert.equal(acknowledgement.env?.ACK_TOKEN, "${{ steps.pr_ack_token.outputs.token }}");
84+
85+
const run = acknowledgement.run ?? "";
86+
assert.match(run, /issues\/\$ITEM_NUMBER\/comments\?per_page=100/);
87+
assert.match(run, /--arg marker_prefix "clawsweeper-pr-ack:"/);
88+
assert.match(run, /--arg marker_suffix " item=\$ITEM_NUMBER -->"/);
89+
assert.match(run, /"<!-- clawsweeper-pr-ack:\$SOURCE_ACTION item=\$ITEM_NUMBER -->"/);
90+
assert.match(
91+
run,
92+
/"Pull request received\. I will update this pull request when review starts\."/,
93+
);
94+
assert.match(run, /issues\/\$ITEM_NUMBER\/comments"\s*\\\s*--method POST/);
95+
}
96+
});

0 commit comments

Comments
 (0)