-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbapi
More file actions
executable file
·81 lines (68 loc) · 1.9 KB
/
bbapi
File metadata and controls
executable file
·81 lines (68 loc) · 1.9 KB
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
#!/bin/bash
# Command-line tool for grabbing data from the Bitbucket API
# set BB_TEAM to the team name; set BB_API_AUTH_TOKEN to auth
# credentials (e.g. username:password)
main() {
if (( ! $# )); then
echo >&2 "Usage: $0 subcommand [arguments]"
exit 1
fi
: "${BB_TEAM:=vgtf}"
[[ -n $BB_API_AUTH_TOKEN ]] || {
echo >&2 "$0: need to set \$BB_API_AUTH_TOKEN"
exit 1
}
for need in curl jq; do
type -P "$need" >/dev/null || {
echo >&2 "$0: $need not found."
exit 1
}
done
local cmd=$1 func=bbapi_$1
shift
if [[ $(type -t "$func") == function ]]; then
"$func" "$@"
else
echo >&2 "$0: Unknown subcommand '$cmd'"
exit 1
fi
}
# use paging api to retrieve all values from an API endpoint
_bbcat() {
local data='{ "next": "'"$1"'" }' q
while next=$(jq -r -e .next <<<"$data"); do
data=$(curl -sSu "$BB_API_AUTH_TOKEN" "$next")
case "$data" in
'['*) q=. ;;
'{'*) q='.values[]'
esac
jq -r "$q" <<<"$data"
done
}
# list all repositories
bbapi_lsrepos() {
_bbcat https://bitbucket.org/api/2.0/repositories/"${1:-$BB_TEAM}" | jq -r .name | sort
}
# list all groups associated with a team
bbapi_lsgroups() {
_bbcat https://bitbucket.org/api/1.0/groups/"${1:-$BB_TEAM}" | jq -r '.[]|.name' | sort
}
# list all members of a specific group
bbapi_lsgroup() {
curl -sSu "$BB_API_AUTH_TOKEN" https://bitbucket.org/api/1.0/groups/$BB_TEAM/"$1" | jq -r '.members[]|.username'
}
# list pull requests
bbapi_lsprs() {
curl -sSu "$BB_API_AUTH_TOKEN" https://api.bitbucket.org/1.0/user/pullrequests/dashboard
}
# list all members of a team
bbapi_lsteam() {
_bbcat https://bitbucket.org/api/2.0/teams/"${1:-$BB_TEAM}"/members | jq -r .username | sort
}
# list subcommands
bbapi_help() {
printf 'Usage: %s subcommand [arguments]\n' "$0"
printf ' Available subcommands:\n'
declare -F | sed -ne $'s/^declare -f bbapi_/ /p'
}
main "$@"