Skip to content

Commit abb51e1

Browse files
authored
ci(competitive-matrix): land the feature-watch report through a bot-merged PR (#583)
## Problem The weekly competitor feature-watch report PR is opened with the workflow `GITHUB_TOKEN`. GitHub does not start workflows from that token's events, so no CI runs on the PR and it waits for a human push. #581 is today's example. ## Change A run whose only change is `scripts/competitive-watch-state.json` now lands the way the adoption wall does (60fe76c): - `Verify the auto-land diff is confined to the state file`: auto-land only when `git status --porcelain -- docs/ scripts/competitive-watch-state.json` is exactly ` M scripts/competitive-watch-state.json`. - `Mint app token`: same action SHA, app `1108748`, `secrets.DEVOPS_BOT_PRIVATE_KEY`, `contents`/`pull-requests` write only, minted only on the auto-land path. - `Auto-land via bot PR`: per-command auth through `GIT_CONFIG_*` env, `copilotkit-bot[bot]` identity, `bot/competitive-watch-*` branch, stages that one file, `gh pr merge --merge --admin` into `GITHUB_REF_NAME`. If the merge fails, the PR stays open and the job fails. - Any `docs/` change (homepage or migration pages) still goes to the existing review PR on the workflow token. - Visibility: the report stays in the PR body, is now also written to the job summary, and the Slack digest (`SLACK_WEBHOOK`) gets a "merged via a bot PR" line. - Also as in adoption-wall: `HUSKY=0` and `--no-verify` on the bot commit, and `cancel-in-progress: false`. No new secrets. No new actions. ## Red / green RED, workflow: `gh pr checks 581` returns `no checks reported on the 'auto/competitive-matrix-20261026-40297803406' branch` (exit 1). The check-runs API gives `total_count: 0`. RED, Slack contract test (`src/__tests__/competitive-matrix-slack.test.ts`, which runs the real `Notify Slack` step) against the old workflow: ``` × says the bot merged the report, with the digest, when the state auto-landed Tests 1 failed | 4 passed (5) ``` GREEN, same test against this workflow: ``` Tests 5 passed (5) ``` Workflow and competitive tests: 24 files, 1410 passed. Lint: `actionlint` exit 0. `zizmor` 1.26.1 with `.github/zizmor.yml`, offline and online: exit 0, no findings. The end-to-end check is a `workflow_dispatch` run after this merges.
2 parents 054709b + 40e325c commit abb51e1

2 files changed

Lines changed: 164 additions & 6 deletions

File tree

‎.github/workflows/update-competitive-matrix.yml‎

Lines changed: 134 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@ on:
77

88
concurrency:
99
group: competitive-matrix
10-
cancel-in-progress: true
10+
# NOT cancel-in-progress, same as update-adoption-wall.yml: this job pushes a
11+
# branch, opens a PR and (for a state-only report) merges it. Cancelling it
12+
# between `git push` and `gh pr merge` leaves a pushed branch or an open PR
13+
# that nobody is looking at. A re-run queues behind the running one instead.
14+
cancel-in-progress: false
1115

1216
permissions:
1317
contents: read
1418

19+
env:
20+
# `pnpm install` runs `prepare` -> husky, which would make the unattended
21+
# `git commit` below run lint-staged. HUSKY=0 asks husky not to install the
22+
# hooks; the `--no-verify` on the bot commit is the part that does not depend
23+
# on husky honouring it. Same as update-adoption-wall.yml.
24+
HUSKY: "0"
25+
1526
jobs:
1627
update-matrix:
1728
runs-on: ubuntu-latest
@@ -51,15 +62,126 @@ jobs:
5162
echo "docs_changed=false" >> "$GITHUB_OUTPUT"
5263
fi
5364
65+
# The run's report, where a human can read it without a PR: the job
66+
# summary page. Reports only; it must never fail the job.
67+
- name: Publish run summary
68+
if: always()
69+
run: |
70+
SUMMARY=/tmp/matrix-summary.md
71+
# $GITHUB_STEP_SUMMARY is capped at 1 MiB and an oversized file is
72+
# discarded, not truncated. Leave headroom for the notice.
73+
LIMIT=$((1024 * 1024 - 4096))
74+
if [ -s "$SUMMARY" ]; then
75+
head -c "$LIMIT" "$SUMMARY" >> "$GITHUB_STEP_SUMMARY" || true
76+
if [ "$(wc -c < "$SUMMARY")" -gt "$LIMIT" ]; then
77+
{
78+
echo
79+
echo "_Truncated at ${LIMIT} bytes for the job-summary cap._"
80+
} >> "$GITHUB_STEP_SUMMARY" || true
81+
fi
82+
else
83+
echo "## Competitive matrix: no summary was produced; see the \`Update competitive matrix\` step log." >> "$GITHUB_STEP_SUMMARY" || true
84+
fi
85+
86+
# A run whose ONLY change is the feature-watch state file is a report, not
87+
# a page edit: it lands itself through a bot PR (below), and its content
88+
# stays visible in the PR body, the job summary and the Slack digest.
89+
# Anything that touches docs/ (a homepage or migration page edit) still
90+
# goes to a review PR for a human. This step decides which path applies,
91+
# on the exact set of paths the commit stages.
92+
- name: Verify the auto-land diff is confined to the state file
93+
id: confine
94+
if: >-
95+
steps.changes.outputs.changed == 'true' &&
96+
steps.changes.outputs.docs_changed == 'false'
97+
run: |
98+
STATUS=$(git status --porcelain -- docs/ scripts/competitive-watch-state.json)
99+
if [ "$STATUS" = " M scripts/competitive-watch-state.json" ]; then
100+
echo "confined=true" >> "$GITHUB_OUTPUT"
101+
echo "Only scripts/competitive-watch-state.json changed; it may auto-land."
102+
else
103+
echo "confined=false" >> "$GITHUB_OUTPUT"
104+
echo "::warning::The change is not exactly a modified scripts/competitive-watch-state.json; it goes to a review PR instead:"
105+
echo "$STATUS"
106+
fi
107+
108+
# The workflow token cannot land this: a PR it opens triggers no CI, and
109+
# ruleset `blorpathupap` requires an approved PR on the default branch.
110+
# copilotkit-bot (app 1108748) is a PULL-REQUEST-ONLY bypass actor on that
111+
# ruleset. This is the same app, the same pinned action, the same secret
112+
# and the same scopes that update-adoption-wall.yml and fix-drift.yml use.
113+
# Minted behind the SAME condition as the push it serves, so a run that is
114+
# not auto-landing never mints a token. The token reaches git and gh
115+
# through the environment only.
116+
- name: Mint app token
117+
id: app-token
118+
if: >-
119+
steps.changes.outputs.changed == 'true' &&
120+
steps.changes.outputs.docs_changed == 'false' &&
121+
steps.confine.outputs.confined == 'true'
122+
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
123+
with:
124+
app-id: "1108748"
125+
private-key: ${{ secrets.DEVOPS_BOT_PRIVATE_KEY }}
126+
permission-contents: write
127+
permission-pull-requests: write
128+
129+
- name: Auto-land via bot PR
130+
id: land
131+
if: >-
132+
steps.changes.outputs.changed == 'true' &&
133+
steps.changes.outputs.docs_changed == 'false' &&
134+
steps.confine.outputs.confined == 'true'
135+
env:
136+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
137+
run: |
138+
AUTH=$(printf 'x-access-token:%s' "$GH_TOKEN" | base64 | tr -d '\n')
139+
echo "::add-mask::${AUTH}"
140+
export GIT_CONFIG_COUNT=1
141+
export GIT_CONFIG_KEY_0="http.https://github.com/.extraheader"
142+
export GIT_CONFIG_VALUE_0="AUTHORIZATION: basic ${AUTH}"
143+
git config user.name "copilotkit-bot[bot]"
144+
git config user.email "194550712+copilotkit-bot[bot]@users.noreply.github.com"
145+
# Run id + attempt: a same-day re-run must not collide with the first
146+
# run's branch.
147+
BRANCH="bot/competitive-watch-$(date -u +%Y%m%d)-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
148+
git checkout -b "$BRANCH"
149+
# ONE NAMED FILE: the bot merges the state file and nothing else.
150+
git add -- scripts/competitive-watch-state.json
151+
# --no-verify: see the HUSKY note in the workflow env block.
152+
git commit --no-verify -m "chore: update competitor feature-watch state"
153+
git push -u origin "$BRANCH"
154+
PR_URL=$(gh pr create \
155+
--title "chore: competitor feature-watch report" \
156+
--body-file /tmp/matrix-summary.md \
157+
--base "${GITHUB_REF_NAME}" \
158+
--head "$BRANCH")
159+
echo "url=${PR_URL}" >> "$GITHUB_OUTPUT"
160+
# --admin merges now, through the bot's pull-request bypass (--auto
161+
# would wait forever for the approval the ruleset requires). --merge
162+
# because the ruleset allows merge commits only. A real conflict
163+
# fails here and leaves the PR open for a human.
164+
if ! gh pr merge "$BRANCH" --merge --admin; then
165+
echo "::error::Opened the feature-watch PR for ${BRANCH} but could not merge it into ${GITHUB_REF_NAME}; it is left open for a human."
166+
exit 1
167+
fi
168+
echo "merged=true" >> "$GITHUB_OUTPUT"
169+
170+
# A docs/ change, or a state change that is not confined, goes to a
171+
# review PR on the workflow token, as before.
54172
- name: Configure git credentials
55-
if: steps.changes.outputs.changed == 'true'
173+
if: >-
174+
steps.changes.outputs.changed == 'true' &&
175+
steps.confine.outputs.confined != 'true'
56176
run: |
57177
git config --local --unset-all http.https://github.com/.extraheader || true
58178
git config --local url."https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/".insteadOf "https://github.com/"
59179
60180
- name: Create PR
61181
id: pr
62-
if: steps.changes.outputs.changed == 'true'
182+
if: >-
183+
steps.changes.outputs.changed == 'true' &&
184+
steps.confine.outputs.confined != 'true'
63185
env:
64186
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
65187
RUN_ID: ${{ github.run_id }}
@@ -91,14 +213,20 @@ jobs:
91213
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
92214
CHANGED: ${{ steps.changes.outputs.changed }}
93215
DOCS_CHANGED: ${{ steps.changes.outputs.docs_changed }}
94-
PR_URL: ${{ steps.pr.outputs.url }}
216+
PR_URL: ${{ steps.land.outputs.url || steps.pr.outputs.url }}
217+
LANDED: ${{ steps.land.outputs.merged }}
95218
JOB_STATUS: ${{ job.status }}
96219
RUN_ID: ${{ github.run_id }}
97220
run: |
98221
if [ -z "$SLACK_WEBHOOK" ]; then echo "SLACK_WEBHOOK not set, skipping"; exit 0; fi
99222
# "PR created" only when the PR step really produced a URL; a failed
100-
# Create PR falls through to the failure message.
101-
if [ "$CHANGED" = "true" ] && [ -n "$PR_URL" ] && [ "$DOCS_CHANGED" = "true" ]; then
223+
# Create PR falls through to the failure message. "Merged" only when
224+
# the bot PR's merge really succeeded; an unmerged bot PR is left open
225+
# and reported as a PR to review.
226+
if [ "$LANDED" = "true" ] && [ -n "$PR_URL" ]; then
227+
EMOJI="🔎"
228+
MSG="*Competitor feature-watch report* — state merged into main via a bot PR; check the changes below. <https://github.com/CopilotKit/aimock/actions/runs/${RUN_ID}|View run>"
229+
elif [ "$CHANGED" = "true" ] && [ -n "$PR_URL" ] && [ "$DOCS_CHANGED" = "true" ]; then
102230
EMOJI="📊"
103231
MSG="*Competitive matrix changes detected* — PR created with updated migration pages. <https://github.com/CopilotKit/aimock/actions/runs/${RUN_ID}|View run>"
104232
elif [ "$CHANGED" = "true" ] && [ -n "$PR_URL" ]; then

‎src/__tests__/competitive-matrix-slack.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ interface SlackEnv {
5656
DOCS_CHANGED: string;
5757
PR_URL: string;
5858
JOB_STATUS: string;
59+
LANDED?: string;
5960
}
6061

6162
/** Runs the real Notify Slack step on `summary`; returns the posted `text`. */
@@ -220,4 +221,33 @@ describe("Notify Slack digest (workflow jq/awk contract)", () => {
220221
"<https://github.com/CopilotKit/aimock/actions/runs/42|View run>",
221222
);
222223
});
224+
225+
it("says the bot merged the report, with the digest, when the state auto-landed", () => {
226+
const text = runNotifySlack(formatSummary([], [], [], [], [], HOSTILE), {
227+
CHANGED: "true",
228+
DOCS_CHANGED: "false",
229+
PR_URL,
230+
JOB_STATUS: "success",
231+
LANDED: "true",
232+
});
233+
const [first, second, , heading] = text.split("\n");
234+
expect(first).toBe(
235+
"🔎 *Competitor feature-watch report* — state merged into main via a bot PR; check the changes below. " +
236+
"<https://github.com/CopilotKit/aimock/actions/runs/42|View run>",
237+
);
238+
expect(second).toBe(PR_URL);
239+
expect(heading).toBe("*Feature watch*");
240+
expect(text).toContain("• *hostile_src* (AT&amp;T &lt;Mock&gt;; C-S1, C-S2): changed");
241+
});
242+
243+
it("reports an unmerged bot PR as a PR to review, not as merged", () => {
244+
const text = runNotifySlack(formatSummary([], [], [], [], [], HOSTILE), {
245+
CHANGED: "true",
246+
DOCS_CHANGED: "false",
247+
PR_URL,
248+
JOB_STATUS: "failure",
249+
LANDED: "",
250+
});
251+
expect(text.split("\n")[0]).toBe(REPORT_MSG);
252+
});
223253
});

0 commit comments

Comments
 (0)