Skip to content

Commit c90dc14

Browse files
authored
Trigger conflict-detection on push on main (#14479)
1 parent c1fb67b commit c90dc14

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

.github/ghprcomment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
149149
In future, **please avoid that**. For now, you can continue working.
150150
- jobName: 'Conflicts with target branch'
151-
workflowName: 'On PR opened/updated'
151+
workflowName: 'On PR opened/updated (check)'
152152
message: >
153153
Your pull request conflicts with the target branch.
154154

.github/workflows/on-pr-opened-updated-check.yml

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,69 @@ on:
55
pull_request_target:
66
# default: opened, synchronize, reopened
77

8+
workflow_dispatch:
9+
inputs:
10+
pr_number:
11+
description: 'PR number'
12+
required: true
13+
814
jobs:
915
conflicts_with_target:
1016
if: github.repository == 'JabRef/jabref'
1117
name: Conflicts with target branch
1218
runs-on: ubuntu-latest
1319
permissions:
14-
contents: read
1520
actions: write
21+
contents: read
22+
pull-requests: write
1623
steps:
1724
- uses: actions/checkout@v6
1825
with:
1926
show-progress: 'false'
2027
- name: Check PR mergeability
2128
id: check_mergeable
2229
run: |
23-
MERGEABLE=$(gh pr view --json mergeable ${{ github.event.pull_request.number }} --template '{{.mergeable}}')
30+
set -e
31+
MERGEABLE=$(gh pr view --json mergeable $PR_NUMBER --template '{{.mergeable}}')
2432
if [ "$MERGEABLE" == "CONFLICTING" ]; then
2533
echo "❌ Merge conflicts"
26-
gh issue --repo $REPO edit $PR_NUMBER --remove-label "status: ready-for-review" --add-label "status: changes-required"
34+
echo "❌ Merge conflicts at PR [#$PR_NUMBER](https://github.com/JabRef/jabref/pull/$PR_NUMBER)" >> $GITHUB_STEP_SUMMARY
35+
gh issue edit $PR_NUMBER --remove-label "status: ready-for-review" --add-label "status: changes-required"
36+
37+
# "workflow dispatch" does not trigger subsequent "on completion" runs
38+
# Therefore, we emulate ghprcomment here
39+
if [ "$EVENT" = "workflow_dispatch" ]; then
40+
HAS_CONFLICT_COMMENT=$(gh api repos/jabref/JabRef/issues/$PR_NUMBER/comments | jq '.[].body | contains("Conflicts with target branch")' | grep true || true)
41+
42+
if [ -n "$HAS_CONFLICT_COMMENT" ]; then
43+
echo "Already commented about conflicts."
44+
# When triggered by another workflow, success is also if there is a merge conflict
45+
exit 0;
46+
fi
47+
48+
echo "Commenting on PR..."
49+
cat > body.txt <<EOF
50+
Your pull request conflicts with the target branch.
51+
52+
Please [merge `upstream/main`](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork#syncing-a-fork-branch-from-the-command-line) with your code. For a step-by-step guide to resolve merge conflicts, see <https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/addressing-merge-conflicts/resolving-a-merge-conflict-using-the-command-line>.
53+
54+
<!-- ghprcomment
55+
On PR opened/updated (check)
56+
Conflicts with target branch
57+
-->
58+
EOF
59+
gh issue comment "$PR_NUMBER" --body-file body.txt
60+
echo "Commented on PR" >> $GITHUB_STEP_SUMMARY
61+
62+
# When triggered by another workflow, success is also if there is a merge conflict
63+
exit 0;
64+
fi;
65+
2766
exit 1
2867
fi
2968
echo "✅ No merge conflicts"
69+
echo "✅ No merge conflicts at PR [#$PR_NUMBER](https://github.com/JabRef/jabref/pull/$PR_NUMBER)" >> $GITHUB_STEP_SUMMARY
3070
env:
71+
EVENT: ${{ github.event_name }}
3172
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
73+
PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }}

.github/workflows/pr-comment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ name: Comment on PR
88
on:
99
workflow_run:
1010
# note when updating via a PR and testing - `workflow_run` executes from the `main` branch and not the PR branch
11-
workflows: ["Source Code Tests", "On PR opened/updated", "Check PR Format", "Link PR to Issue", "Check PR Modifications", "Check PR CHANGELOG.md"]
11+
workflows: ["Source Code Tests", "On PR opened/updated (check)", "Check PR Format", "Link PR to Issue", "Check PR Modifications", "Check PR CHANGELOG.md"]
1212
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-a-workflow-based-on-the-conclusion-of-another-workflow
1313
types: [completed]
1414
workflow_dispatch:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Rerun PR merge conclicts check
2+
3+
on:
4+
push:
5+
branches:
6+
- add-rerun
7+
schedule:
8+
- cron: "0 3 * * *"
9+
workflow_dispatch:
10+
11+
jobs:
12+
rerun:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v6
16+
with:
17+
show-progress: 'false'
18+
- name: Trigger "On PR opened/updated (check)" for all open PRs
19+
env:
20+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
run: |
22+
workflow_name="On PR opened/updated (check)"
23+
24+
gh pr list --state open --json number --jq '.[] | "\(.number)"' |
25+
while read pr; do
26+
echo "Triggering '$workflow_name' for PR #$pr"
27+
gh workflow run "$workflow_name" --ref add-rerun --field pr_number="$pr"
28+
echo "Triggered '$workflow_name' for PR [#$pr](https://github.com/JabRef/jabref/pull/$pr)" >> $GITHUB_STEP_SUMMARY
29+
done

0 commit comments

Comments
 (0)