generated from GuillaumeFalourd/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.yml
240 lines (225 loc) · 9.66 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
name: Pull Request Action (Github CLI)
description: Github Actions to create pull request (all os supported) using Github CLI ⤵️
inputs:
source_branch:
description: Branch name to pull from, default is triggered branch
required: false
destination_branch:
description: Branch name to sync to in this repo, default is main
required: false
default: main
pr_title:
description: Pull request title
required: false
pr_body:
description: Pull request body
required: false
pr_reviewer:
description: Pull request reviewers, comma-separated list (no spaces)
required: false
pr_assignee:
description: Pull request assignees, comma-separated list (no spaces)
required: false
pr_label:
description: Pull request labels, comma-separated list (no spaces)
required: false
pr_milestone:
description: Pull request milestone
required: false
pr_draft:
description: Draft pull request
required: false
pr_allow_empty:
description: Create PR even if no changes
required: false
outputs:
pr_url:
description: 'Pull request URL'
value: ${{ steps.pr-creation.outputs.pr_url }}
pr_number:
description: 'Pull request number'
value: ${{ steps.pr-creation.outputs.pr_number }}
has_changed_files:
description: 'Boolean string indicating whether any file has been changed'
value: ${{ steps.pr-creation.outputs.has_changed_files }}
pr_created:
description: 'Boolean string indicating whether a PR was created'
value: ${{ steps.pr-creation.outputs.pr_created }}
runs:
using: "composite"
steps:
- name: Create Pull Request ⤵️
env:
INPUT_SOURCE_BRANCH: ${{ inputs.source_branch }}
INPUT_DESTINATION_BRANCH: ${{ inputs.destination_branch }}
INPUT_PR_TITLE: ${{ inputs.pr_title }}
INPUT_PR_BODY: ${{ inputs.pr_body }}
INPUT_PR_REVIEWER: ${{ inputs.pr_reviewer }}
INPUT_PR_ASSIGNEE: ${{ inputs.pr_assignee }}
INPUT_PR_LABEL: ${{ inputs.pr_label }}
INPUT_PR_MILESTONE: ${{ inputs.pr_milestone }}
INPUT_PR_DRAFT: ${{ inputs.pr_draft }}
INPUT_PR_ALLOW_EMPTY: ${{ inputs.pr_allow_empty }}
id: pr-creation
shell: bash
run: |
set -e
set -o pipefail
# HELPER SYNTAX:
# catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]]
catch() {
{
IFS=$'\n' read -r -d '' "${1}";
IFS=$'\n' read -r -d '' "${2}";
(IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_});
} < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1)
}
reset_color="\\e[0m"
color_red="\\e[31m"
color_green="\\e[32m"
color_yellow="\\e[33m"
color_blue="\\e[36m"
color_gray="\\e[37m"
echo_blue() { printf "%b\n" "${color_blue}$(printf "%s\n" "$*")${reset_color}"; }
echo_green() { printf "%b\n" "${color_green}$(printf "%s\n" "$*")${reset_color}"; }
echo_red() { printf "%b\n" "${color_red}$(printf "%s\n" "$*")${reset_color}"; }
echo_yellow() { printf "%b\n" "${color_yellow}$(printf "%s\n" "$*")${reset_color}"; }
echo_gray() { printf "%b\n" "${color_gray}$(printf "%s\n" "$*")${reset_color}"; }
echo_grey() { printf "%b\n" "${color_gray}$(printf "%s\n" "$*")${reset_color}"; }
echo_info() { printf "%b\n" "${color_blue}ℹ $(printf "%s\n" "$*")${reset_color}"; }
echo_error() { printf "%b\n" "${color_red}✖ $(printf "%s\n" "$*")${reset_color}"; }
echo_warning() { printf "%b\n" "${color_yellow}✔ $(printf "%s\n" "$*")${reset_color}"; }
echo_success() { printf "%b\n" "${color_green}✔ $(printf "%s\n" "$*")${reset_color}"; }
echo_fail() { printf "%b\n" "${color_red}✖ $(printf "%s\n" "$*")${reset_color}"; }
enable_debug() {
if [[ "${RUNNER_DEBUG}" == "1" ]]; then
echo_info "Enabling debug mode."
set -x
fi
}
disable_debug() {
if [[ "${RUNNER_DEBUG}" != "1" ]]; then
set +x
fi
}
enable_debug
if [[ $SHELLOPTS == *xtrace* ]]; then
env
fi
# init bool var to check if PR creation should be skipped
skip_pr_creation=false
pr_created=false
echo "::group::Gather Inputs"
if [[ ! -z "${INPUT_SOURCE_BRANCH}" ]]; then
SOURCE_BRANCH="${INPUT_SOURCE_BRANCH}"
elif [[ ! -z "$GITHUB_HEAD_REF" && "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
SOURCE_BRANCH="$GITHUB_HEAD_REF"
elif [[ ! -z "$GITHUB_REF" ]]; then
SOURCE_BRANCH=${GITHUB_REF/refs\/heads\//} # Remove branch prefix
else
echo_fail "Set the inputs.source_branch parameter or trigger from a branch.
exit 1
fi
echo_info "SOURCE_BRANCH=$SOURCE_BRANCH"
if [[ ! -z "${INPUT_DESTINATION_BRANCH}" ]]; then
DESTINATION_BRANCH="${INPUT_DESTINATION_BRANCH}"
else
DESTINATION_BRANCH="main"
fi
echo_info "DESTINATION_BRANCH=$DESTINATION_BRANCH"
echo "::endgroup::"
echo "::group::Configure git"
# Github actions no longer auto set the username and GITHUB_TOKEN
git remote set-url origin "https://$GITHUB_ACTOR:$GITHUB_TOKEN@${GITHUB_SERVER_URL#https://}/$GITHUB_REPOSITORY"
# Pull all branches references down locally so subsequent commands can see them
git fetch origin '+refs/heads/*:refs/heads/*' --update-head-ok
# Print out all branches
git --no-pager branch -a -vv
echo "::endgroup::"
echo "::group::Ensure pull-request contains differences"
SOURCE_REV=$(git rev-parse --revs-only "$SOURCE_BRANCH")
DESTINATION_REV=$(git rev-parse --revs-only "$DESTINATION_BRANCH")
if [ "$SOURCE_REV" = "$DESTINATION_REV" ]; then
echo_info "Source and destination branches are the same. Source rev: $SOURCE_REV, Destination rev: $DESTINATION_REV. Skipping PR creation."
skip_pr_creation=true
fi
# skip pr creation if there are no file differences, this avoids PRs with just a merge commit and no content
LINES_CHANGED=$(git diff --name-only "$DESTINATION_BRANCH" "$SOURCE_BRANCH" -- | wc -l | awk '{print $1}')
if [[ "$LINES_CHANGED" = "0" ]] && [[ ! "${INPUT_PR_ALLOW_EMPTY}" == "true" ]]; then
echo_info "No file changes detected between source and destination branches and pr_allow_empty is not set to true. Skipping PR creation."
skip_pr_creation=true
fi
if [[ "$LINES_CHANGED" = "0" ]]; then
has_changed_files=false
else
has_changed_files=true
fi
echo "::endgroup::"
echo "::group::Assemble gh pr parameters"
# Workaround for `hub` auth error https://github.com/github/hub/issues/2149#issuecomment-513214342
export GITHUB_USER="$GITHUB_ACTOR"
declare -a COMMAND
COMMAND+=(gh pr create --base $DESTINATION_BRANCH --head $SOURCE_BRANCH --no-maintainer-edit)
if [[ ! -z "${INPUT_PR_TITLE}" ]]; then
COMMAND+=(--title "${INPUT_PR_TITLE}")
else
COMMAND+=(--fill)
fi
if [[ ! -z "${INPUT_PR_BODY}" ]]; then
COMMAND+=(--body "${INPUT_PR_BODY}")
fi
if [[ ! -z "${INPUT_PR_REVIEWER}" ]]; then
COMMAND+=(--reviewer "${INPUT_PR_REVIEWER}")
fi
if [[ ! -z "${INPUT_PR_ASSIGNEE}" ]]; then
COMMAND+=(--assignee "${INPUT_PR_ASSIGNEE}")
fi
if [[ ! -z "${INPUT_PR_LABEL}" ]]; then
COMMAND+=(--label "${INPUT_PR_LABEL}")
fi
if [[ ! -z "${INPUT_PR_MILESTONE}" ]]; then
COMMAND+=(--milestone "${INPUT_PR_MILESTONE}")
fi
if [[ "${INPUT_PR_DRAFT}" == "true" ]]; then
COMMAND+=(--draft)
fi
echo "::endgroup::"
echo "::group::Create pull request $SOURCE_BRANCH -> $DESTINATION_BRANCH"
pr_command_exit_code=0
pr_already_exists=-1
if [[ "$skip_pr_creation" = "false" ]]; then
echo_info "Command that will be executed: ${COMMAND[@]}"
set +e
catch pr_command_output pr_command_error "${COMMAND[@]}"
pr_command_exit_code=$?
pr_already_exists=$(echo "${pr_command_error}" | grep -c "already exists:" || true)
set -e
echo_info "Output of github cli command: " ${pr_command_output} ${pr_command_error}
if [[ "$pr_command_exit_code" -eq 0 ]];
then
pr_url="${pr_command_output}"
pr_number="${pr_command_output##*/}"
elif [[ "$pr_already_exists" -eq 1 ]];
then
pr_url=$(echo "$pr_command_error" | grep -o 'https://github.com[^ ]*')
pr_number=${pr_url##*/}
fi
if [[ "$pr_command_exit_code" != "0" && "$pr_already_exists" -eq 0 ]]; then
echo_error "Failed to create pull request. Exit code: $pr_command_exit_code. Error: $pr_command_error"
else
# assume that if we got here, a PR was created or exists
echo_success "Pull request created or exists: $pr_url"
pr_created=true
fi
fi
echo "::endgroup::"
echo "::group::Set outputs"
echo "pr_created=${pr_created}" >> "$GITHUB_OUTPUT"
echo "pr_url=$pr_url" >> "$GITHUB_OUTPUT"
echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT"
echo "has_changed_files=${has_changed_files}" >> "$GITHUB_OUTPUT"
cat "$GITHUB_OUTPUT"
echo "::endgroup::"
branding:
icon: 'git-pull-request'
color: 'black'