Skip to content

Commit a1fc7f1

Browse files
chore: publish from staged
1 parent febaac3 commit a1fc7f1

1 file changed

Lines changed: 63 additions & 18 deletions

File tree

.github/workflows/check-pr-target.yml

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ name: Check PR Target Branch
22

33
on:
44
pull_request_target:
5-
branches: [main]
6-
types: [opened]
5+
types: [opened, edited, reopened, synchronize]
6+
7+
concurrency:
8+
group: check-pr-target-${{ github.event.pull_request.number }}
9+
cancel-in-progress: true
710

811
permissions:
912
pull-requests: write
@@ -16,20 +19,62 @@ jobs:
1619
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
1720
with:
1821
script: |
19-
const body = [
20-
'⚠️ **This PR targets `main`, but PRs should target `staged`.**',
21-
'',
22-
'The `main` branch is auto-published from `staged` and should not receive direct PRs.',
23-
'Please close this PR and re-open it against the `staged` branch.',
24-
'',
25-
'You can change the base branch using the **Edit** button at the top of this PR,',
26-
'or run: `gh pr edit ${{ github.event.pull_request.number }} --base staged`'
27-
].join('\n');
28-
29-
await github.rest.pulls.createReview({
30-
owner: context.repo.owner,
31-
repo: context.repo.repo,
32-
pull_number: context.issue.number,
33-
event: 'REQUEST_CHANGES',
34-
body
22+
const pull = context.payload.pull_request;
23+
const owner = context.repo.owner;
24+
const repo = context.repo.repo;
25+
const pullNumber = context.issue.number;
26+
const botLogin = 'github-actions[bot]';
27+
28+
const { data: reviews } = await github.rest.pulls.listReviews({
29+
owner,
30+
repo,
31+
pull_number: pullNumber,
32+
per_page: 100
3533
});
34+
35+
const latestBotReview = reviews
36+
.filter((review) => review.user?.login === botLogin)
37+
.sort((a, b) => new Date(a.submitted_at ?? a.created_at) - new Date(b.submitted_at ?? b.created_at))
38+
.at(-1);
39+
40+
const latestBotState = latestBotReview?.state;
41+
42+
if (pull.base.ref === 'main') {
43+
if (latestBotState !== 'CHANGES_REQUESTED') {
44+
const requestChangesBody = [
45+
'⚠️ **This PR targets `main`, but PRs should target `staged`.**',
46+
'',
47+
'The `main` branch is auto-published from `staged` and should not receive direct PRs.',
48+
'Please close this PR and re-open it against the `staged` branch.',
49+
'',
50+
'You can change the base branch using the **Edit** button at the top of this PR,',
51+
'or run: `gh pr edit ${{ github.event.pull_request.number }} --base staged`'
52+
].join('\n');
53+
54+
await github.rest.pulls.createReview({
55+
owner,
56+
repo,
57+
pull_number: pullNumber,
58+
event: 'REQUEST_CHANGES',
59+
body: requestChangesBody
60+
});
61+
}
62+
63+
return;
64+
}
65+
66+
if (latestBotState === 'CHANGES_REQUESTED') {
67+
const approveBody = [
68+
'✅ Base branch is now set correctly.',
69+
'',
70+
'Removing the prior block because this PR no longer targets `main`.'
71+
].join('\n');
72+
73+
await github.rest.pulls.createReview({
74+
owner,
75+
repo,
76+
pull_number: pullNumber,
77+
event: 'APPROVE',
78+
body: approveBody
79+
});
80+
}

0 commit comments

Comments
 (0)