-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch
executable file
·221 lines (184 loc) · 6.92 KB
/
branch
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
#!/usr/bin/env bash
#
#
# Creates a new development branch for a GitHub issue.
# If the VS Code command: `code` is available, the project will be opened in VS Code.
#
#
# Usage:
# branch [-r] [-o <owner>] [-b <base-branch>] [-d <dev-branch>] <repository> <issue-number>
# branch [-r] [-o <owner>] [-b <base-branch>] -d <dev-branch> <repository> --no-issue
# branch -h
#
# Options:
# -r Reuse the current VS Code window.
#
# -o <owner> Owner of the repository.
# Defaults to "SituDevelopment".
#
# -b <base-branch> Name of the branch from which to take the new branch.
# Defaults to the repository's default branch.
#
# -d <dev-branch> Name of the new branch to create.
# Defaults to being auto-generated by the issue number and title.
# If `--no-issue` is specified, `dev-branch` is required.
#
# <repository> Name of the repository.
#
# <issue-number> Number of the issue for which the development branch is being created.
#
# --no-issue Create a development branch without an issue.
#
# -h Display help and exit.
#
#
# Exit codes:
# 0 - success
# 1 - incorrect usage
# 2 - GitHub error
# 3 - git error
# 4 - VS Code error
# exit codes
SUCCESS=0
INCORRECT_USAGE=1
GITHUB_ERROR=2
GIT_ERROR=3
VS_CODE_ERROR=4
#######################################
# Display help message.
# Arguments:
# None
# Outputs:
# Writes help message to stdout
#######################################
showhelp() {
cat << EOF
Usage:
branch [-r] [-o <owner>] [-b <base-branch>] [-d <dev-branch>] <repository> <issue-number>
branch [-r] [-o <owner>] [-b <base-branch>] -d <dev-branch> <repository> --no-issue
branch -h
Creates a new development branch for a GitHub issue.
Options:
-r Reuse the current VS Code window.
-o <owner> Owner of the repository.
Defaults to "SituDevelopment".
-b <base-branch> Name of the branch from which to take the new branch.
Defaults to the repository's default branch.
-d <dev-branch> Name of the new branch to create.
Defaults to being auto-generated by the issue number and title.
If \`--no-issue\` is specified, \`dev-branch\` is required.
<repository> Name of the repository.
<issue-number> Number of the issue for which the development branch is being created.
--no-issue Create a development branch without an issue.
-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 repository and branch variables according to given arguments
while getopts ": r o: b: d: h" flag; do
case "${flag}" in
r) reuse=" --reuse-window" ;;
o) owner="${OPTARG}" ;;
b) base="${OPTARG}" ;;
d) branch="${OPTARG}" ;;
h) showhelp; exit ${SUCCESS} ;;
?) showhelp; exit ${INCORRECT_USAGE} ;;
esac
done
shift $((${OPTIND} - 1))
if [[ $# -ne 2 ]]; then
showhelp
exit ${INCORRECT_USAGE}
fi
repo=$1
issue=$2
# set default values for unset variables
reuse=${reuse:=""}
owner=${owner:="SituDevelopment"}
base=${base:=$(bkt --discard-failures -- gh api /repos/${owner}/${repo}/branches?protected=true --jq '.[0].name')}
branch=${branch:=""}
# if not provided, construct branch name
if [[ -z "${branch}" ]]; then
if [[ "${issue}" == "--no-issue" ]]; then
showhelp
exit ${INCORRECT_USAGE}
fi
assert_success "Looking for issue..." \
"gh issue view ${issue} --repo '${owner}/${repo}' --json '' &> /dev/null" \
"Issue not found" \
${INCORRECT_USAGE} \
"Issue found\n"
branch=$(gh issue view ${issue} --repo "${owner}/${repo}" --json title --jq '.title')
branch=$(echo ${branch} | sed -e 's| |-|g') # convert to kebab-case
branch=$(echo ${branch} | sed -e 's|[^a-zA-Z0-9-]||g') # remove non-kebab-case characters
branch=$(echo ${branch} | sed -E 's|[A-Z][a-z]+|\L&|g') # convert non-initialisms to lowercase
prefixes=(bug chore feature)
issue_labels=$(gh issue view ${issue} --repo "${owner}/${repo}" --json labels --jq '.labels[].name')
for prefix in "${prefixes[@]}"; do
if [[ $issue_labels == *"${prefix}"* ]]; then
branch="${prefix}/${branch}"
break
fi
done
fi
directory="/var/www/development/${repo}-${branch/\//-}"
if [[ "${issue}" == "--no-issue" ]]; then
assert_success "Cloning repository..." \
"git clone [email protected]:${owner}/${repo} --branch '${base}' '${directory}'" \
"Error cloning repository" \
${GIT_ERROR} \
"Cloned repository\n"
cd "${directory}"
assert_success "Creating development branch..." \
"git switch --create '${branch}'" \
"Error creating development branch" \
${GIT_ERROR} \
"Created development branch\n"
assert_success "Publishing development branch to remote repository..." \
"git push --set-upstream origin '${branch}'" \
"Error publishing development branch to remote repository" \
${GIT_ERROR} \
"Published development branch to remote repository\n"
else
assert_success "Creating development branch..." \
"gh issue develop '${issue}' --repo '${owner}/${repo}' --base '${base}' --name '${branch}' &> /dev/null" \
"Error creating development branch" \
${GITHUB_ERROR} \
"Created development branch\n"
assert_success "Cloning repository..." \
"git clone [email protected]:${owner}/${repo} --branch '${branch}' '${directory}'" \
"Error cloning repository" \
${GIT_ERROR} \
"Cloned repository\n"
fi
assert_success "Checking for VS Code command..." \
"which code" \
"VS Code command not found" \
${SUCCESS} \
"VS Code command found\n"
assert_success "Opening project in VS Code..." \
"code ${reuse} '${directory}'" \
"Error opening project in VS Code" \
${VS_CODE_ERROR} \
"Opened project in VS Code\n"
exit ${SUCCESS}