|
5 | 5 | pull_request_target: |
6 | 6 | # default: opened, synchronize, reopened |
7 | 7 |
|
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + pr_number: |
| 11 | + description: 'PR number' |
| 12 | + required: true |
| 13 | + |
8 | 14 | jobs: |
9 | 15 | conflicts_with_target: |
10 | 16 | if: github.repository == 'JabRef/jabref' |
11 | 17 | name: Conflicts with target branch |
12 | 18 | runs-on: ubuntu-latest |
13 | 19 | permissions: |
14 | | - contents: read |
15 | 20 | actions: write |
| 21 | + contents: read |
| 22 | + pull-requests: write |
16 | 23 | steps: |
17 | 24 | - uses: actions/checkout@v6 |
18 | 25 | with: |
19 | 26 | show-progress: 'false' |
20 | 27 | - name: Check PR mergeability |
21 | 28 | id: check_mergeable |
22 | 29 | 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}}') |
24 | 32 | if [ "$MERGEABLE" == "CONFLICTING" ]; then |
25 | 33 | 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 | +
|
27 | 66 | exit 1 |
28 | 67 | fi |
29 | 68 | echo "✅ No merge conflicts" |
| 69 | + echo "✅ No merge conflicts at PR [#$PR_NUMBER](https://github.com/JabRef/jabref/pull/$PR_NUMBER)" >> $GITHUB_STEP_SUMMARY |
30 | 70 | env: |
| 71 | + EVENT: ${{ github.event_name }} |
31 | 72 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 73 | + PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }} |
0 commit comments