-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
executable file
·220 lines (185 loc) · 5.85 KB
/
script.sh
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
#!/usr/bin/env bash
# Setup default values for variables
VERSION="1.0.0"
BB_PR_SOURCE=AUTO_MERGE
BB_PR_DESTINATION=
BB_USER=sunsbot
BB_PASSWORD=
BB_REPO_OWNER=
BB_REPO_SLUG=
CURL_BIN=$(which curl)
JQ_BIN=$(which jq)
function usage() {
cat <<EOM
Required arguments:
-s | --source Source git branch to create pull request from.
-d | --destination Destination git branch to merge pull request to.
Optional arguments:
-u | --user BitBucket username. If not set, uses BITBUCKET_AUTOMERGE_USER.
-p | --password BitBucket user password. If not set, uses BITBUCKET_AUTOMERGE_PASS.
--repo-owner The repository owner. If not set, uses BITBUCKET_REPO_OWNER.
--repo-slug The slug of the repository. If not set, uses BITBUCKET_REPO_SLUG.
--version Display the version of this script
Examples:
Create and automatically merge a pull request from master to develop:
script.sh -s AUTO_MERGE -d qa
All options:
script.sh -s AUTO_MERGE -d qa -u sunsbot -p xyz --repo-owner NBAFrontEndDev --repo-slug nba-teams-static-suns
EOM
exit 3
}
# Check requirements
function require() {
command -v "$1" > /dev/null 2>&1 || {
echo "Some of the required software is not installed:"
echo " please install $1" >&2;
exit 4;
}
}
function assertRequiredArgumentsSet() {
if [ -z ${BITBUCKET_AUTOMERGE_USER+x} ];
then unset BITBUCKET_AUTOMERGE_USER
else
BB_USER=$BITBUCKET_AUTOMERGE_USER
fi
if [ -z ${BITBUCKET_AUTOMERGE_PASS+x} ];
then unset BITBUCKET_AUTOMERGE_PASS
else
BB_PASSWORD=$BITBUCKET_AUTOMERGE_PASS
fi
if [ -z ${BITBUCKET_REPO_OWNER+x} ];
then unset BITBUCKET_REPO_OWNER
else
BB_REPO_OWNER=$BITBUCKET_REPO_OWNER
fi
if [ -z ${BITBUCKET_REPO_SLUG+x} ];
then unset BITBUCKET_REPO_SLUG
else
BB_REPO_SLUG=$BITBUCKET_REPO_SLUG
fi
if [ BB_PR_SOURCE == false ]; then
echo "Source is required. You can pass the value using -s, --source"
exit 7
fi
if [ BB_PR_DESTINATION == false ]; then
echo "Destination is required. You can pass the value using -d, --destination"
exit 8
fi
}
function createCommit() {
TIMESTAMP=$(date +%s)
RESULT=`$CURL_BIN -X POST "https://api.bitbucket.org/2.0/repositories/$BB_REPO_OWNER/$BB_REPO_SLUG/src?ref=$BB_PR_SOURCE" \
--silent \
--user $BB_USER:$BB_PASSWORD \
-H 'application/x-www-form-urlencoded' \
--data-urlencode "/auto_merge=AUTO $TIMESTAMP" \
--data-urlencode "message=AutoMerge Commit and Merge" \
--data-urlencode "branch=$BB_PR_SOURCE" \
--data-urlencode "author=Automerge Bot <[email protected]>"
`
echo $RESULT
}
function sendCommand() {
RESULT=`$CURL_BIN -X POST "https://api.bitbucket.org/2.0/repositories/$BB_REPO_OWNER/$BB_REPO_SLUG/pullrequests" \
--silent \
--user $BB_USER:$BB_PASSWORD \
-H 'content-type: application/json' \
-d '{
"title": "Auto Merge: '$BB_PR_SOURCE' -> '$BB_PR_DESTINATION'",
"description": "Automatic pull request created.",
"state": "OPEN",
"destination": {
"branch": {
"name": "'$BB_PR_DESTINATION'"
}
},
"source": {
"branch": {
"name": "'$BB_PR_SOURCE'"
}
}
}'`
# Check for error messages
ERR_MSG=`echo $RESULT | $JQ_BIN -r '.error.message' || true`
if [ "$ERR_MSG" == 'null' ]; then
# No errors, continue
BB_MERGE_URL=`echo $RESULT | $JQ_BIN -r '.links.merge.href'`
RESULT=`$CURL_BIN -X POST "$BB_MERGE_URL" \
--fail --show-error --silent \
--user $BB_USER:$BB_PASSWORD \
-H 'content-type: application/json' \
-d '{
"close_source_branch": false,
"merge_strategy": "merge_commit"
}'`
echo ""
echo "Success"
elif [ "$ERR_MSG" == 'There are no changes to be pulled' ]; then
# Do we have changes that need to be merged?
echo "Nothing to do. All changes are already merged."
else
echo "BitBucket returned an error: $ERR_MSG"
echo $RESULT
exit 9
fi
}
if [ "$BASH_SOURCE" == "$0" ]; then
set -o errexit
set -o pipefail
set -u
set -e
# If no args are provided, display usage information
if [ $# == 0 ]; then usage; fi
# Make sure we have out dependancies
require curl
require jq
# Loop through arguments, two at a time for key and value
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-s|--source)
BB_PR_SOURCE="$2"
shift # past argument
;;
-d|--destination)
BB_PR_DESTINATION="$2"
shift # past argument
;;
-u|--user)
BB_USER="$2"
shift # past argument
;;
-p|--password)
BB_PASSWORD="$2"
shift # past argument
;;
--repo-owner)
BB_REPO_OWNER="$2"
shift # past argument
;;
--repo-slug)
BB_REPO_SLUG="$2"
shift # past argument
;;
--version)
echo ${VERSION}
exit 0
;;
*)
usage
exit 2
;;
esac
shift # past argument or value
done
# Check that required arguments are provided
assertRequiredArgumentsSet
# create and commit new file
echo "Committing change to branch $BB_PR_SOURCE from user $BB_USER"
createCommit
# Send command
echo "Creating and merging pull request from $BB_PR_SOURCE to $BB_PR_DESTINATION"
sendCommand
exit 0
fi