-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-pr
executable file
·141 lines (118 loc) · 3.83 KB
/
create-pr
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
#!/usr/bin/env bash
#
#
# Creates a GitHub pull request with the given properties.
#
# Usage:
# create-pr [-b <base-branch>] [<label> ...]
# create-pr -h
#
# Arguments:
# <label> Label to add to the pull request.
#
# Options:
# -b <base-branch> Name of the branch from which to pull changes.
# Defaults to the repository's default branch.
#
# -h Display help and exit.
#
#
# Exit codes:
# 0 - success
# 1 - incorrect usage
# 2 - GitHub error
# exit codes
SUCCESS=0
INCORRECT_USAGE=1
GITHUB_ERROR=2
#######################################
# Display help message.
# Arguments:
# None
# Outputs:
# Writes help message to stdout
#######################################
showhelp() {
# Get list of labels for the current repository
label_options=$(bkt --discard-failures -- gh label list --json name --template '{{range .}}{{"\t\t\t - "}}{{.name}}{{"\n"}}{{end}}' 2> /dev/null)
if [[ -n "${label_options}" ]]; then
label_options=$'Available labels:\n'$label_options
fi
cat << EOF
Usage:
create-pr [-b <base-branch>] [<label> ...]
create-pr -h
Creates a GitHub pull request with the given properties.
Arguments:
<label> Label to add to the pull request.
${label_options}
Options:
-b <base-branch> Name of the branch from which to pull changes.
Defaults to the repository's default branch.
-h Display help and exit.
EOF
}
#######################################
# Asserts the given command runs successfully.
# Arguments:
# $1 - message to print before running command
# $2 - command to run
# $3 - error message to print on failure
# $4 - exit code to use on failure
# $5 - success message to print on success
# Outputs:
# Writes error message to stdout
#######################################
assert_success() {
echo $1
eval $2
if [[ $? -ne 0 ]]; then
echo "$3"
echo -e "\nGoodbye..."
exit $4
fi
echo -e $5
}
# assign branch variable according to given argument
while getopts "b: h" flag; do
case "${flag}" in
b) base="${OPTARG}" ;;
h) showhelp; exit ${SUCCESS} ;;
?) showhelp; exit ${INCORRECT_USAGE} ;;
esac
done
shift $((OPTIND - 1))
if [[ $# -gt 0 ]]; then
labels="--label '$1"
shift
fi
while [[ $# -gt 0 ]]; do
labels="${labels},$1"
shift
done
if [[ -n "${labels}" ]]; then
labels="${labels}'"
fi
if [[ -z "${base}" ]]; then
assert_success "Determining base branch..." \
"base=$(bkt --discard-failures -- gh api /repos/$(git remote get-url origin | sed 's/^git@github.com://' | sed 's/.git$//g')/branches?protected=true --jq '.[0].name')" \
"Error determining base branch" \
${GITHUB_ERROR} \
"Base branch determined\n"
fi
title=$(git branch --show-current)
title=$(echo ${title} | sed -e 's|\(.*\)/|\u\1:-|') # Replace '<type>/' prefix with '<Type>:-'
title=$(echo ${title} | sed -e 's|-\(.\)| \u\1|g') # convert kebab-case to Title Case
assert_success "Creating pull request..." \
"gh pr create --assignee @me --base '${base}' --fill ${labels} --title '${title}'" \
"Error creating pull request" \
${GITHUB_ERROR} \
"Pull request created\n"
repo=$(gh repo view --json name --template {{.name}})
number=$(gh pr view --json number --template {{.number}})
assert_success "Requesting review from 'Employees' team..." \
"gh api --silent --method POST --header 'Accept: application/vnd.github+json' --header 'X-GitHub-Api-Version: 2022-11-28' /repos/SituDevelopment/${repo}/pulls/${number}/requested_reviewers --field team_reviewers[]=employees" \
"Error requesting review" \
${GITHUB_ERROR} \
"Review requested\n"
exit ${SUCCESS}