forked from cycleapple/PR-Update
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.yml
More file actions
141 lines (123 loc) · 4.91 KB
/
action.yml
File metadata and controls
141 lines (123 loc) · 4.91 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
name: 'PR Up to Date'
description: 'Check if branches are up to date and merge the base branch into the pull request branch if necessary.'
inputs:
baseBranch:
required: false
description: 'The base branch to compare and merge. Defaults to PR base ref when triggered by pull_request.'
default: '${{ github.event.pull_request.base.ref }}'
prBranch:
required: false
description: 'The pull request branch to compare and merge. Defaults to PR head ref when triggered by pull_request.'
default: '${{ github.event.pull_request.head.ref }}'
autoMerge:
required: false
description: 'Automatically merge the base branch into the pull request branch if necessary.'
default: 'true'
descriptionMerged:
required: false
description: 'Description of the merge commit.'
default: 'Merge [BASE_BRANCH] into [PR_BRANCH]'
descriptionReminder:
required: false
description: 'Description of the PR comment if "autoMerge" is false.'
default: 'Please merge [BASE_BRANCH] into [PR_BRANCH].'
runs:
using: 'composite'
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: fregante/setup-git-user@v2
- name: Validate inputs
shell: bash
env:
PR_BRANCH: ${{ inputs.prBranch }}
BASE_BRANCH: ${{ inputs.baseBranch }}
run: |
if [ -z "${BASE_BRANCH}" ]; then
echo "::error::baseBranch is required. When running outside a pull_request event, provide it explicitly."
exit 1
fi
if [ -z "${PR_BRANCH}" ]; then
echo "::error::prBranch is required. When running outside a pull_request event, provide it explicitly."
exit 1
fi
- name: Fetch branches and update working directory
shell: bash
env:
PR_BRANCH: ${{ inputs.prBranch }}
BASE_BRANCH: ${{ inputs.baseBranch }}
run: |
echo "Fetching pull request branch: ${PR_BRANCH}"
git fetch origin "${PR_BRANCH}"
echo "Fetching base branch: ${BASE_BRANCH}"
git fetch origin "${BASE_BRANCH}"
- name: Check if branches are up to date
shell: bash
id: checkBranches
env:
PR_BRANCH: ${{ inputs.prBranch }}
BASE_BRANCH: ${{ inputs.baseBranch }}
run: |
git checkout "${PR_BRANCH}"
BASE=$(git merge-base "origin/${BASE_BRANCH}" HEAD)
if [ $BASE = $(git rev-parse "origin/${BASE_BRANCH}") ]; then
echo "Branches are up to date, skipping merge."
echo "mergeRequired=false" >> $GITHUB_OUTPUT
else
echo "Branches are not up to date, merge is required."
echo "mergeRequired=true" >> $GITHUB_OUTPUT
fi
- name: Build merge description
id: merge-description
shell: bash
env:
PR_BRANCH: ${{ inputs.prBranch }}
BASE_BRANCH: ${{ inputs.baseBranch }}
run: |
DESCRIPTION=$(echo "${{ inputs.descriptionMerged }}" | sed "s|\[BASE_BRANCH\]|${BASE_BRANCH}|g" | sed "s|\[PR_BRANCH\]|${PR_BRANCH}|g")
echo "mergeDescription=${DESCRIPTION}" >> $GITHUB_OUTPUT
- name: Build reminder description
id: reminder-description
shell: bash
env:
PR_BRANCH: ${{ inputs.prBranch }}
BASE_BRANCH: ${{ inputs.baseBranch }}
run: |
DESCRIPTION=$(echo "${{ inputs.descriptionReminder }}" | sed "s|\[BASE_BRANCH\]|${BASE_BRANCH}|g" | sed "s|\[PR_BRANCH\]|${PR_BRANCH}|g")
echo "reminderDescription=${DESCRIPTION}" >> $GITHUB_OUTPUT
- name: Merge base branch into PR branch
shell: bash
if: steps.checkBranches.outputs.mergeRequired == 'true' && inputs.autoMerge == 'true'
env:
PR_BRANCH: ${{ inputs.prBranch }}
BASE_BRANCH: ${{ inputs.baseBranch }}
run: |
git merge --no-edit --no-commit "origin/${BASE_BRANCH}"
if [ $? -ne 0 ]; then
echo "Merge conflict detected. Please resolve conflicts before merging."
exit 1
fi
git commit -m "${{ steps.merge-description.outputs.mergeDescription }}"
- name: Push changes
shell: bash
if: steps.checkBranches.outputs.mergeRequired == 'true' && inputs.autoMerge == 'true'
env:
PR_BRANCH: ${{ inputs.prBranch }}
BASE_BRANCH: ${{ inputs.baseBranch }}
run: |
git push origin "${PR_BRANCH}"
- name: Command on PR to remind the PR branch is not up to date
if: steps.checkBranches.outputs.mergeRequired == 'true' && inputs.autoMerge == 'false'
uses: thollander/actions-comment-pull-request@v3.0.1
with:
message: |
## This branch is out-of-date with the base branch
${{ steps.reminder-description.outputs.reminderDescription }}
comment-tag: pr-up-to-date
- name: Block the workflow if the PR branch is not up to date
shell: bash
if: steps.checkBranches.outputs.mergeRequired == 'true' && inputs.autoMerge == 'false'
run: exit 1
branding:
icon: 'git-pull-request'
color: 'blue'