Skip to content

Commit d4ff84c

Browse files
committed
chore(automation): merge changelogs PR when draft release is created
Merge the version bump/changelog PR right after the draft release is created instead of after Marketplace publish, so main gets the new version before OSS clearance starts. The PR is looked up by its head branch (draft releases have no git tag yet) and merged without waiting for checks. The publish-time merge step stays as a no-op fallback.
1 parent 6c31008 commit d4ff84c

5 files changed

Lines changed: 96 additions & 18 deletions

File tree

‎.agents/skills/release-widget/SKILL.md‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ Keep `--branch`: without it, `-L 1` returns the newest run on _any_ branch.
143143

144144
Wait (re-poll, don't ask the user to check) until `status == completed`. Report the conclusion and the draft release URL.
145145
146+
The workflow's last step (`Merge changelogs PR`) merges the changelog/version-bump PR into `main` right away — without waiting for PR checks, after verifying it only touches `CHANGELOG.md`/`package.json`/`package.xml`. Confirm before moving on to OSS clearance:
147+
148+
```bash
149+
gh pr list --head "tmp/<release-tag>" --state all --json number,state,url
150+
```
151+
152+
Expect `MERGED`. Still `OPEN` → read that step's log (`gh run view <databaseId> --log | grep -A20 "Merge changelogs PR"`): unexpected files or a merge conflict with `main`. Report it — merging by hand is the user's job (branch protection).
153+
154+
**Abandoned draft:** `main` already carries `<version>` and its changelog section, so the same version can't be re-cut (`rui-bump-version` requires a greater version, and the changelog has no unreleased entries left). Release the next patch instead.
155+
146156
### Phase 4 — OSS clearance SBOM (autonomous prep, manual submission)
147157
148158
```bash
@@ -197,13 +207,7 @@ Find the run matching this tag/branch.
197207

198208
- `conclusion: success` → doesn't mean the version is live yet. Confirm: `marketplace-mcp`'s `get_content_versions` with `contentId` = `appNumber` from Phase 0, check `<version>` is listed. If `marketplace-mcp` isn't connected or errors, ask the user to check the widget's Marketplace listing page for `<version>`. Don't declare done until one of the two confirms it.
199209
200-
Then verify changelog PR merged:
201-
202-
```bash
203-
gh pr list --head "tmp/<release-tag>" --json number,state
204-
```
205-
206-
Still open after successful publish → check whether `merge-changelogs-pr` step ran. Don't merge it yourself: branch protection requires team approvals — tell the user it's on them.
210+
The changelog PR should already be merged since Phase 3; `PublishMarketplace.yml` retries the merge as a fallback. If it's still open, tell the user — don't merge it yourself (branch protection).
207211
208212
- `conclusion: failure` → **check run history before escalating**:
209213
```bash

‎.github/workflows/CreateGitHubRelease.yml‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ jobs:
2828
cache: "pnpm"
2929
- name: Search for package in workspace
3030
run: >
31-
if [[ -z "$(pnpm ls --json --filter=$PACKAGE)" ]];
32-
then
31+
if [[ -z "$(pnpm ls --json --filter=$PACKAGE)" ]]; then
3332
echo "::error::Package $PACKAGE not found in workspace.";
3433
exit 1;
3534
fi
@@ -42,3 +41,9 @@ jobs:
4241
GH_EMAIL: ${{ secrets.GH_EMAIL }}
4342
GH_NAME: ${{ secrets.GH_NAME }}
4443
GH_PAT: ${{ secrets.GH_PAT }}
44+
- name: Merge changelogs PR
45+
run: |
46+
VERSION=$(pnpm --silent --filter=$PACKAGE exec node -p 'require("./package.json").version')
47+
pnpm run merge-changelogs-pr "${PACKAGE#@mendix/}-v${VERSION}"
48+
env:
49+
GH_PAT: ${{ secrets.GH_PAT }} # needs to act as uicontent user

‎.github/workflows/PublishMarketplace.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
- name: Install dependencies
4949
run: pnpm install
5050
- name: Include OSS README in MPK artifact
51-
run: pnpm run include-oss-in-artifact
51+
run: pnpm run include-oss-in-artifact
5252
env:
5353
GH_PAT: ${{ secrets.GITHUB_TOKEN }}
5454
- name: Run publish script
@@ -59,6 +59,7 @@ jobs:
5959
CPAPI_USER_OPENID: ${{ secrets.SRV_WIDGETS_OPENID }}
6060
CPAPI_PASS: ${{ secrets.CPAPI_PASS }}
6161
GH_PAT: ${{ secrets.GITHUB_TOKEN }}
62+
# Normally already merged by "Create draft release"; kept as a fallback (no-op when merged).
6263
- name: Merge changelogs PR
6364
run: pnpm run merge-changelogs-pr ${{ env.TAG }}
6465
env:

‎automation/utils/bin/rui-merge-changelogs-pr.ts‎

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,53 @@
11
#!/usr/bin/env node
22

3-
import { GitHub } from "../src/github.ts";
3+
import { setTimeout as sleep } from "node:timers/promises";
4+
import { GitHub, type GitHubPR, MergePRError } from "../src/github.ts";
45

56
const ALLOWED_FILENAMES = new Set(["package.xml", "CHANGELOG.md", "package.json"]);
67

8+
// Right after a PR is opened GitHub may still be computing mergeability and answers 405.
9+
const MERGE_ATTEMPTS = 6;
10+
const MERGE_RETRY_DELAY_MS = 10_000;
11+
712
/** Thrown when the script should stop gracefully without failing the workflow. */
813
class SkipError extends Error {}
914

15+
/**
16+
* Finds the changelog PR for a release tag. Looks it up by head branch first, because
17+
* draft releases have no git tag yet; falls back to the tag for already published releases.
18+
* Branch names mirror resolveChangelogBranch in rui-create-gh-release.
19+
*/
20+
async function findChangelogPR(gh: GitHub, tag: string): Promise<GitHubPR | undefined> {
21+
for (const branch of [`tmp/${tag}`, `${tag}-update-changelog`]) {
22+
const pr = await gh.getPRByHeadBranch(branch);
23+
if (pr) {
24+
return pr;
25+
}
26+
}
27+
28+
try {
29+
return await gh.getPRByReleaseTag(tag);
30+
} catch {
31+
return undefined;
32+
}
33+
}
34+
35+
async function mergeWithRetry(gh: GitHub, prNumber: number): Promise<void> {
36+
for (let attempt = 1; ; attempt++) {
37+
try {
38+
await gh.mergePR(prNumber, "squash");
39+
return;
40+
} catch (e) {
41+
const retriable = e instanceof MergePRError && e.status === 405;
42+
if (!retriable || attempt === MERGE_ATTEMPTS) {
43+
throw e;
44+
}
45+
console.warn(`PR #${prNumber} is not mergeable yet (attempt ${attempt}/${MERGE_ATTEMPTS}), retrying...`);
46+
await sleep(MERGE_RETRY_DELAY_MS);
47+
}
48+
}
49+
}
50+
1051
async function main(): Promise<void> {
1152
const tag = process.argv[2];
1253

@@ -19,9 +60,9 @@ async function main(): Promise<void> {
1960
const gh = new GitHub();
2061
await gh.ensureAuth();
2162

22-
// 1. Fetch PR by release tag
23-
console.log(`\nLooking up PR for tag: ${tag}`);
24-
const pr = await gh.getPRByReleaseTag(tag);
63+
// 1. Find the changelog PR
64+
console.log(`\nLooking up changelog PR for: ${tag}`);
65+
const pr = await findChangelogPR(gh, tag);
2566

2667
if (!pr) {
2768
throw new SkipError(`No PR found for tag "${tag}". Skipping.`);
@@ -53,9 +94,10 @@ async function main(): Promise<void> {
5394
console.log(`Changed files (${filenames.length}):`);
5495
filenames.forEach(f => console.log(` ✓ ${f}`));
5596

56-
// 3. Merge
97+
// 3. Merge. Checks are intentionally not awaited: the PR only touches release metadata,
98+
// and flaky checks must not block the release.
5799
console.log(`\nMerging PR #${pr.number}...`);
58-
await gh.mergePR(pr.number, "squash");
100+
await mergeWithRetry(gh, pr.number);
59101
console.log(`\n✅ PR #${pr.number} merged successfully.`);
60102
}
61103

‎automation/utils/src/github.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ export interface GitHubPR {
1616
base: { ref: string };
1717
}
1818

19+
export class MergePRError extends Error {
20+
readonly status: number;
21+
22+
constructor(message: string, status: number) {
23+
super(message);
24+
this.status = status;
25+
}
26+
}
27+
1928
export interface GitHubPRFile {
2029
filename: string;
2130
status: string;
@@ -417,6 +426,22 @@ export class GitHub {
417426
return prs[0];
418427
}
419428

429+
/**
430+
* Find the most recent PR (any state) opened from the given head branch.
431+
*/
432+
async getPRByHeadBranch(branch: string): Promise<GitHubPR | undefined> {
433+
await this.ensureAuth();
434+
435+
const prs = await fetch<GitHubPR[]>(
436+
"GET",
437+
`https://api.github.com/repos/${this.owner}/${this.repo}/pulls?state=all&head=${encodeURIComponent(`${this.owner}:${branch}`)}`,
438+
undefined,
439+
this.ghAPIHeaders
440+
);
441+
442+
return prs[0];
443+
}
444+
420445
/**
421446
* List filenames changed in a PR.
422447
*/
@@ -451,8 +476,9 @@ export class GitHub {
451476

452477
if (!response.ok) {
453478
const errorText = await response.text();
454-
throw new Error(
455-
`Failed to merge PR #${prNumber}: ${response.status} ${response.statusText} - ${errorText}`
479+
throw new MergePRError(
480+
`Failed to merge PR #${prNumber}: ${response.status} ${response.statusText} - ${errorText}`,
481+
response.status
456482
);
457483
}
458484
}

0 commit comments

Comments
 (0)