Skip to content

Commit 9bbb9a0

Browse files
committed
ci: try fix warns on master
1 parent e5c22b0 commit 9bbb9a0

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

.github/workflows/warn-master-pr.yml

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,68 @@ jobs:
2929
}
3030
3131
// Check if this is a PR from a fork (external contributor)
32-
const isFromFork = context.payload.pull_request.head.repo.full_name !== context.payload.pull_request.base.repo.full_name;
32+
const headRepo = context.payload.pull_request.head.repo?.full_name;
33+
const baseRepo = context.payload.pull_request.base.repo.full_name;
34+
const isFromFork = headRepo && headRepo !== baseRepo;
35+
36+
console.log(`PR Details: Head repo: ${headRepo}, Base repo: ${baseRepo}, Is fork: ${isFromFork}`);
3337
3438
if (isFromFork) {
3539
console.log('PR is from a fork (external contributor)');
36-
// For fork PRs, we can only create PR reviews, not issue comments
40+
// For fork PRs, try comment first, then review as fallback
3741
try {
38-
// Check if we already have a review
39-
const reviews = await github.rest.pulls.listReviews({
42+
// Check if we already commented
43+
const comments = await github.rest.issues.listComments({
4044
owner,
4145
repo,
42-
pull_number: issue_number
46+
issue_number
4347
});
4448
45-
const botReview = reviews.data.find(review =>
46-
review.user.login === 'github-actions[bot]' &&
47-
review.body && review.body.includes('⚠️ Warning: PR targeting master branch')
49+
const botComment = comments.data.find(comment =>
50+
comment.user.login === 'github-actions[bot]' &&
51+
comment.body.includes('⚠️ Warning: PR targeting master branch')
4852
);
4953
50-
if (!botReview) {
51-
await github.rest.pulls.createReview({
54+
if (!botComment) {
55+
await github.rest.issues.createComment({
5256
owner,
5357
repo,
54-
pull_number: issue_number,
55-
event: 'REQUEST_CHANGES',
56-
body: "⚠️ **Warning: PR targeting master branch detected!**\n\n**This PR is targeting `master` but should target `dev` instead.**\n\nAccording to our [CONTRIBUTING.md](https://github.com/HyDE-Project/HyDE/blob/master/CONTRIBUTING.md) and [RELEASE_POLICY.md](https://github.com/HyDE-Project/HyDE/blob/master/RELEASE_POLICY.md):\n\n- **All pull requests must be submitted to the `dev` branch**\n- Changes go through `dev` first for testing before being merged to `master` during release windows\n- **PRs to `master` are only allowed for emergencies**\n\n**Required Action:**\n1. **Change the base branch from `master` to `dev`**\n2. Follow the [pull request template](https://github.com/HyDE-Project/HyDE/blob/master/.github/PULL_REQUEST_TEMPLATE.md)\n\n**If this is an emergency fix, please add a comment explaining why it needs to target `master` directly.**\n\n---\n*This is an automated review enforcing our contribution workflow.*"
58+
issue_number,
59+
body: "⚠️ **Warning: PR targeting master branch detected!** Please change the base branch to `dev` instead of `master`. See our [CONTRIBUTING.md](https://github.com/HyDE-Project/HyDE/blob/master/CONTRIBUTING.md) for details."
5760
});
58-
console.log('Warning review added to fork PR #' + issue_number);
61+
console.log('Comment added to fork PR #' + issue_number);
5962
} else {
60-
console.log('Warning review already exists for fork PR #' + issue_number);
63+
console.log('Comment already exists for fork PR #' + issue_number);
6164
}
6265
} catch (error) {
63-
console.log('Failed to create review for fork PR:', error.message);
64-
// Fallback: try to add a regular comment anyway
66+
console.log('Failed to create comment for fork PR, trying review:', error.message);
67+
// Fallback: try to add a review
6568
try {
66-
await github.rest.issues.createComment({
69+
const reviews = await github.rest.pulls.listReviews({
6770
owner,
6871
repo,
69-
issue_number,
70-
body: "⚠️ **Warning: PR targeting master branch detected!** Please change the base branch to `dev` instead of `master`. See our [CONTRIBUTING.md](https://github.com/HyDE-Project/HyDE/blob/master/CONTRIBUTING.md) for details."
72+
pull_number: issue_number
7173
});
72-
console.log('Fallback comment added to fork PR #' + issue_number);
73-
} catch (fallbackError) {
74-
console.log('Both review and comment failed for fork PR:', fallbackError.message);
74+
75+
const botReview = reviews.data.find(review =>
76+
review.user.login === 'github-actions[bot]' &&
77+
review.body && review.body.includes('⚠️ Warning: PR targeting master branch')
78+
);
79+
80+
if (!botReview) {
81+
await github.rest.pulls.createReview({
82+
owner,
83+
repo,
84+
pull_number: issue_number,
85+
event: 'COMMENT',
86+
body: "⚠️ **Warning: PR targeting master branch detected!** Please change the base branch to `dev` instead of `master`. See our [CONTRIBUTING.md](https://github.com/HyDE-Project/HyDE/blob/master/CONTRIBUTING.md) for details."
87+
});
88+
console.log('Review added to fork PR #' + issue_number);
89+
} else {
90+
console.log('Review already exists for fork PR #' + issue_number);
91+
}
92+
} catch (reviewError) {
93+
console.log('Both comment and review failed for fork PR:', reviewError.message);
7594
}
7695
}
7796
} else {

0 commit comments

Comments
 (0)