-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitbucket-rest-api-libexec
executable file
·132 lines (111 loc) · 3.4 KB
/
bitbucket-rest-api-libexec
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
#!/bin/sh
#
# Libexec: Bitbucket REST API.
export API=${API:-${api:-bitbucket-rest-api}}
# Get all open prs.
f_get_open_prs() (
local CACHE=${TMPDIR:-/tmp}/bitbucket-get-open-prs
export CACHE
command -- mkdir -p -- "$CACHE"
command -- rm -r -- "$CACHE/"*
exec "$API" get repos |
exec jq -r --unbuffered '
.values[] |
select(.project.key | contains("~") | not) |
.project.key, .slug
' |
command -- xargs -L 1 -n 2 -P 0 -- sh -c '
exec "$API" get \
"/rest/api/latest/projects/$1/repos/$2/pull-requests?state=open&limit=999" |
command -- jq -rc "select((.values | length) > 0)" > "$CACHE/$1-$2";
' --
exec cat -- "$CACHE"/* |
command -- jq -rc '[inputs]'
)
# Get all open and overdue prs.
f_get_overdue_prs() {
local sec=${1:-$((60 * 60 * 24 * 14))}
\f_get_open_prs |
command -- jq -rc --arg sec "$sec" '
[
.[].values[] |
select(now - (.updatedDate / 1000) >= ($sec | tonumber))
]
'
}
# Get all branches along with repo slug and project key.
f_get_branches() (
local CACHE=${TMPDIR:-/tmp}/bitbucket-get-branches
export CACHE
command -- mkdir -p -- "$CACHE"
command -- rm -r -- "$CACHE/"*
exec "$API" get repos |
exec jq -r --unbuffered '
.values[] |
select(.project.key | contains("~") | not) |
.project.key, .slug
' |
command -- xargs -L 1 -n 2 -P 0 -- sh -c '
exec "$API" get \
"/rest/api/latest/projects/$1/repos/$2/branches?limit=999" |
command -- jq -rc --arg project "$1" --arg repo "$2" "
. += {project_key: \$project, repo_slug: \$repo}
" > "$CACHE/$1-$2";
' --
exec cat -- "$CACHE"/* |
command -- jq -rc '[inputs]'
)
# Get all repo slugs and their project keys.
f_get_repo_slugs() {
exec "$API" get repos |
command -- jq -rc '
[
.values[] |
select(.project.key | contains("~") | not) |
{project_key: .project.key, repo_slug: .slug}
]
'
}
# Get all repo hrefs.
f_get_repo_urls() {
exec "$API" get repos |
command -- jq -rc --arg p "${1:-http}" '
.values[].links.clone[] |
select((.name | contains($p)) and (.href | contains("~") | not)) |
.href
'
}
# Get all tags along with repo slug and project key.
f_get_tags() {
local CACHE=${TMPDIR:-/tmp}/get-all-tags
export CACHE
command -- mkdir -p -- "$CACHE"
command -- rm -r -- "$CACHE/"*
exec "$API" get repos |
exec jq -r --unbuffered '
.values[] |
select(.project.key | contains("~") | not) |
.project.key, .slug
' |
command -- xargs -L 1 -n 2 -P 0 -- sh -c '
exec "$API" get \
"/rest/api/latest/projects/$1/repos/$2/tags?limit=999" |
command -- jq -rc --arg project "$1" --arg repo "$2" "
. += {project_key: \$project, repo_slug: \$repo}
" > "$CACHE/$1-$2";
' --
exec cat -- "$CACHE"/* |
command -- jq -rc '[inputs]'
}
# (Un-)Watch all git repositories.
f_watch_repos() {
local method=${1:-'error: method missing: delete|post'}
\f_get_repo_slugs |
exec jq -r '
.[] |
"/rest/api/latest/projects/\(.project_key)/repos/\(.repo_slug)/watch"
' |
command -- xargs -L 1 -n 1 -P 0 -- "$API" "$method"
}
${1:+"f_$@"}
# vim: set ft=sh :