-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget-all-cc-repos
executable file
·131 lines (103 loc) · 2.59 KB
/
get-all-cc-repos
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
#!/usr/bin/env bash
# Get all repositories in an organization that match a prefix
# (default: CalConnect/cc-)
VERSION="0.0.1"
ORG="${ORG:-CalConnect}"
PREFIX="${PREFIX:-cc-}"
log() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*"
} >&2
err() {
printf "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: \e[1;31mError:\e[m %b\n" "$*"
} >&2
ecdo() {
log "Running: $*"
"$@"
}
get-page() {
local page="${1:-1}"
# The header and the `--include` flag are for pagination support.
command curl \
--include \
-s \
"https://api.github.com/orgs/$ORG/repos?page=${page}" \
--header "Accept: application/vnd.github+json" | \
process-pagination
}
# Parse curl headers, look for link: <http://...?page=2>; rel="next",
# and get next page if it exists.
process-pagination() {
local next_page=
local is_past_headers=
while read -r line
do
if [[ -n $is_past_headers ]]
then
echo "$line"
continue
fi
if [[ "$line" =~ ^link:.*page=([0-9]+).*\>\;\ rel=\"next\" ]]
then
next_page="${BASH_REMATCH[1]}"
continue
fi
# Start of actual JSON, flag it.
if [[ "$line" =~ ^\[ ]]
then
is_past_headers=1
echo "$line"
fi
continue
done
if [[ -n $next_page ]]
then
get-page "$next_page"
fi
}
# Paginated results are in separate arrays,
# so slurp them into one for a more uniform experience.
slurp-into-one-array() {
command jq -s '[.[][]]'
}
# Filter out only repositories that are prefixed with $PREFIX
filter-prefix() {
local prefix="$PREFIX"
# command jq "[.[] | select(.name | startswith(\"$PREFIX\"))]"
command jq --arg prefix "$prefix" 'map(select(.name | startswith($prefix)))'
}
# Filter out only repositories that have a non-zero size
# Repos with no commits have zero size.
filter-size() {
command jq 'map(select(.size > 0))'
}
show_usage() {
local prog_name="\e[1m${0}\e[22m"
printf "%b" "Usage: $prog_name SUBDIR TARGET_YAML
$prog_name [-h|--help]
$prog_name --version
Get all repositories in an organization that match a prefix
\e[4mFlags:\e[24m
--version Print the version and exit
--help Print this help message and exit
\e[4mMandatory environment variables:\e[24m
ORG The organization to search for repositories
PREFIX The prefix (of a repository name) to search repositories by
"
}
main() {
if [[ "$1" == "-h" || "$1" == "--help" ]]
then
show_usage
exit 0
elif [[ "$1" == "--version" ]]
then
echo "${0} v${VERSION}"
exit 0
fi
# Start from page 1.
get-page 1 \
| slurp-into-one-array \
| filter-prefix \
| filter-size
}
main "$@"