-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfillbucket.sh
executable file
·38 lines (27 loc) · 1.16 KB
/
fillbucket.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
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
trap "echo 'Missing parameter'; exit 1" INT TERM EXIT
username="$1"
password="$2"
reponame="$3"
trap - INT TERM EXIT
spacename="$username"
if [ $# -ge 4 ]; then
spacename="$4"
fi
CURL_OPTS=(-u "$username:$password" --silent)
echo "Validating BitBucket credentials..."
curl --fail "${CURL_OPTS[@]}" "https://api.bitbucket.org/2.0/user" > /dev/null || (
echo "... failed. Most likely, the provided credentials are invalid. Terminating..."
exit 1
)
reponame=$(echo $reponame | tr '[:upper:]' '[:lower:]')
echo "Checking if BitBucket repository \"$spacename/$reponame\" exists..."
curl "${CURL_OPTS[@]}" "https://api.bitbucket.org/2.0/repositories/$spacename/$reponame" | grep "error" > /dev/null && (
echo "BitBucket repository \"$spacename/$reponame\" does NOT exist, creating it..."
curl -X POST --fail "${CURL_OPTS[@]}" "https://api.bitbucket.org/2.0/repositories/$spacename/$reponame" -H "Content-Type: application/json" -d '{"scm": "git", "is_private": "true"}' > /dev/null
)
echo "Pushing to remote..."
git push https://"$username:$password"@bitbucket.org/$spacename/$reponame.git --all --force