-
Notifications
You must be signed in to change notification settings - Fork 1
84 lines (77 loc) · 2.99 KB
/
merge.yml
File metadata and controls
84 lines (77 loc) · 2.99 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
name: Override Merge
on:
issue_comment:
types: [created]
jobs:
merge_comment:
# Run only if
# 1. The comment was made on a PR.
# 2. The comment was made by @samkit-jain.
# 3. The comment was exactly "/merge" sans the quotes.
# 4. The PR is open.
if: |
github.event.issue.pull_request &&
github.event.comment.body == '/merge' &&
!github.event.issue.closed_at &&
github.event.issue.state == 'open' &&
github.event.comment.user.login == 'samkit-jain'
runs-on: ubuntu-latest
name: Merge PR
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
token: ${{ secrets.GH_TOKEN }}
# Get details of the PR. The target and base branch. And also whether the PR can be merged in or not.
- name: Get PR details
uses: octokit/[email protected]
id: get-pr-details
with:
route: GET /repos/{repository}/pulls/{pull_number}
repository: ${{ github.repository }}
pull_number: ${{ github.event.issue.number }}
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
# Merge (rebase) the PR if it is allowed.
- name: Merge the PR
id: merge-status
shell: bash
env:
MERGEABLE_STATUS: ${{ fromJson(steps.get-pr-details.outputs.data).mergeable_state }}
BASE_BRANCH: ${{ fromJson(steps.get-pr-details.outputs.data).base.ref }}
HEAD_BRANCH: ${{ fromJson(steps.get-pr-details.outputs.data).head.ref }}
run: |
if [ "$MERGEABLE_STATUS" = "clean" ]; then
git config --global user.email "<>"
git config --global user.name "GitHub Actions"
git checkout $HEAD_BRANCH
git pull origin $HEAD_BRANCH
git checkout $BASE_BRANCH
git pull origin $BASE_BRANCH
git rebase $HEAD_BRANCH
git push origin $BASE_BRANCH
echo "::set-output name=message::'PR merged in succesfully.'"
else
echo "::set-output name=message::'PR cannot be merged in.'"
fi
# Post a success/failure comment to the PR.
- name: Add comment to PR
uses: octokit/[email protected]
with:
route: POST /repos/{repository}/issues/{issue_number}/comments
repository: ${{ github.repository }}
issue_number: ${{ github.event.issue.number }}
body: ${{ steps.merge-status.outputs.message }}
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
# Post a failure message when any of the previous steps fail.
- name: Add failure comment to PR
if: ${{ failure() }}
uses: octokit/[email protected]
with:
route: POST /repos/{repository}/issues/{issue_number}/comments
repository: ${{ github.repository }}
issue_number: ${{ github.event.issue.number }}
body: PR cannot be merged in. Check the Actions execution tab for details.
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}