-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
117 lines (100 loc) · 4.28 KB
/
action.yml
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
name: "Create and merge Pull Request"
description: "Action to create and merge Pull Request. Opinionated, set GitHub Actions bot as author, then rebase and merge."
author: Hoverkraft
branding:
icon: git-pull-request
color: gray-dark
inputs:
github-token:
description: "GitHub token for creating and merging pull request (permissions contents: write and pull-requests: write). See https://github.com/peter-evans/create-pull-request#action-inputs."
default: ${{ github.token }}
required: false
branch:
description: "The pull request branch name"
required: true
title:
description: "The pull request title"
required: true
body:
description: "The pull request body"
required: true
commit-message:
description: "The commit message for the pull request"
required: true
runs:
using: "composite"
steps:
- shell: bash
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
run: mkdir -p ./self-actions/ && cp -r $GITHUB_ACTION_PATH/../* ./self-actions/
- id: github-actions-bot-user
uses: ./self-actions/get-github-actions-bot-user
- shell: bash
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
run: |
rm -fr ./self-actions
- uses: peter-evans/create-pull-request@v6.0.2
id: create-pull-request
with:
token: ${{ inputs.github-token }}
signoff: true
base: ${{ github.event.repository.default_branch }}
delete-branch: true
branch: ${{ inputs.branch }}
title: ${{ inputs.title }}
body: ${{ inputs.body }}
commit-message: ${{ inputs.commit-message }}
author: ${{ steps.github-actions-bot-user.outputs.name }} <${{ steps.github-actions-bot-user.outputs.email }}>
committer: ${{ steps.github-actions-bot-user.outputs.name }} <${{ steps.github-actions-bot-user.outputs.email }}>
- id: wait-for-pull-request-mergeable-by-admin
if: steps.create-pull-request.outputs.pull-request-number && steps.create-pull-request.outputs.pull-request-operation != 'closed'
uses: actions/github-script@v7
with:
github-token: ${{ inputs.github-token }}
script: |
let attemps = 0;
const maxAttemps = 10;
while (attemps < maxAttemps) {
const { data: { mergeable, mergeable_state } } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: ${{ steps.create-pull-request.outputs.pull-request-number }},
});
if (mergeable === true) {
core.setOutput('is-mergeable', true);
return;
}
core.debug(`Pull request is not mergeable, mergeable_state: ${mergeable_state}`);
await new Promise(resolve => setTimeout(resolve, 5000));
attemps++;
}
core.error('Pull request is not mergeable');
- name: Merge pull request
if: steps.wait-for-pull-request-mergeable-by-admin.outputs.is-mergeable
shell: bash
env:
GH_TOKEN: ${{ inputs.github-token }}
run: |
set +e
ATTEMPS=0
MAX_ATTEMPS=10
REQUIRED_WORKFLOWS_ERROR="Required workflow"
while [ $ATTEMPS -lt $MAX_ATTEMPS ]; do
echo "::debug::Merging pull request #${{ steps.create-pull-request.outputs.pull-request-number }} for repository ${{ github.repository }}..."
MERGE_OUTPUTS=$(gh pr merge -R "${{ github.repository }}" --rebase --admin "${{ steps.create-pull-request.outputs.pull-request-number }}" 2>&1)
MERGE_EXIT_CODE=$?
echo "::debug::Merge outputs: $MERGE_OUTPUTS"
echo "::debug::Merge exit code: $MERGE_EXIT_CODE"
if [ "$MERGE_EXIT_CODE" = "0" ]; then
exit 0
fi
if [[ "$MERGE_OUTPUTS" != *"$REQUIRED_WORKFLOWS_ERROR"* ]]; then
echo "::error::Failed to merge pull request: $MERGE_OUTPUTS"
exit 1
fi
echo "::debug::Pull request is not mergeable yet because some of required workflow check issues, retrying in 5 seconds..."
sleep 5
ATTEMPS=$((ATTEMPS+1))
done
echo "::error::Failed to merge pull request after $MAX_ATTEMPS attemps: $MERGE_OUTPUTS"
exit 1