-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
103 lines (90 loc) · 3.86 KB
/
on-pr-opened-updated.yml
File metadata and controls
103 lines (90 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
name: On PR opened/updated
on:
# _target is required
pull_request_target:
# default: opened, synchronize, reopened
workflow_dispatch:
inputs:
pr_number:
description: 'PR number'
required: true
jobs:
conflicts_with_target:
if: github.repository == 'JabRef/jabref'
name: Conflicts with target branch
runs-on: ubuntu-slim
permissions:
actions: write
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v6
with:
show-progress: 'false'
- name: Check PR mergeability
id: check_mergeable
run: |
set -ex
echo "Checking PR [#$PR_NUMBER](https://github.com/JabRef/jabref/pull/$PR_NUMBER)" >> $GITHUB_STEP_SUMMARY
# Retry a few times until mergeable != UNKNOWN
# See https://github.com/cli/cli/discussions/8020#discussioncomment-7057040 for details
for i in 1 2 3 4 5; do
MERGEABLE=$(gh pr view "$PR_NUMBER" --json mergeable --template '{{.mergeable}}')
echo "Attempt $i: $MERGEABLE"
if [ "$MERGEABLE" != "UNKNOWN" ]; then
break
fi
sleep 5
done
echo "Result: $MERGEABLE" >> $GITHUB_STEP_SUMMARY
if [ "$MERGEABLE" == "CONFLICTING" ]; then
echo "❌ Merge conflicts"
echo "❌ Merge conflicts" >> $GITHUB_STEP_SUMMARY
gh issue edit $PR_NUMBER --remove-label "status: ready-for-review,status: no-bot-comments" --add-label "status: changes-required"
# "workflow dispatch" does not trigger subsequent "on completion" runs.
# Therefore, we emulate ghprcomment here
if [ "$EVENT" = "workflow_dispatch" ]; then
HAS_CONFLICT_COMMENT=$(
gh api graphql -f query="
query {
repository(owner: \"JabRef\", name: \"JabRef\") {
pullRequest(number: $PR_NUMBER) {
comments(last: 20) {
nodes { body }
}
}
}
}
" \
| jq -r '.data.repository.pullRequest.comments.nodes[].body' \
| grep -q "Conflicts with target branch" \
&& echo "true" \
|| echo "false"
)
if [ "$HAS_CONFLICT_COMMENT" = "true" ]; then
echo "Already commented about conflicts."
# When triggered by another workflow, success is also if there is a merge conflict
exit 0;
fi
echo "Commenting on PR..."
cat > body.txt <<EOF
Your pull request conflicts with the target branch.
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>.
<!-- ghprcomment
On PR opened/updated (check)
Conflicts with target branch
-->
EOF
gh issue comment "$PR_NUMBER" --body-file body.txt
echo "Commented on PR" >> $GITHUB_STEP_SUMMARY
# When triggered by another workflow, success is also if there is a merge conflict
exit 0;
fi;
exit 1
fi
echo "✅ No merge conflicts"
echo "✅ No merge conflicts" >> $GITHUB_STEP_SUMMARY
env:
EVENT: ${{ github.event_name }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }}