-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-repos.sh
More file actions
executable file
·89 lines (77 loc) · 2.55 KB
/
fetch-repos.sh
File metadata and controls
executable file
·89 lines (77 loc) · 2.55 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
82
83
84
85
86
87
88
89
#!/usr/bin/env bash
# TotallySafePR: Fetch all GitHub repos (across orgs), store them, confirm with user.
set -euo pipefail
GITHUB_TOKEN="${GITHUB_TOKEN:-}"
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "GITHUB_TOKEN is not set."
printf 'Paste your GitHub PAT: '
read -rs GITHUB_TOKEN
echo
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "No token provided. Exiting."
exit 1
fi
fi
REPOS_FILE="${REPOS_FILE:-./repos.txt}"
TMP_REPOS=$(mktemp)
trap 'rm -f "$TMP_REPOS"' EXIT
# Slurp body from curl -D - output (headers then blank line then body)
body() { echo "$1" | awk 'BEGIN{skip=1} /^[[:space:]]*$/{skip=0;next} !skip'; }
# Next page URL from Link header
next_url() { echo "$1" | tr -d '\r' | awk -F'[:<>; ]+' '/^Link:/{for(i=1;i<=NF;i++) if($i=="rel=\"next\"") {print $(i-1); exit}}'; }
echo "Fetching your repositories (user repos you own)..."
url="https://api.github.com/user/repos?per_page=100&type=owner"
while [[ -n "$url" ]]; do
resp=$(curl -sS -D - -H "Authorization: Bearer $GITHUB_TOKEN" "$url")
body=$(body "$resp")
if echo "$body" | jq -e '.message' &>/dev/null; then
echo "API error: $(echo "$body" | jq -r '.message')" >&2
exit 1
fi
echo "$body" | jq -r '.[].full_name' >> "$TMP_REPOS"
url=$(next_url "$resp")
done
echo "Fetching organizations..."
orgs=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/user/orgs" | jq -r '.[].login')
while IFS= read -r org; do
[[ -z "$org" ]] && continue
echo "Fetching repos for org: $org"
url="https://api.github.com/orgs/$org/repos?per_page=100"
while [[ -n "$url" ]]; do
resp=$(curl -sS -D - -H "Authorization: Bearer $GITHUB_TOKEN" "$url")
body=$(body "$resp")
if echo "$body" | jq -e '.message' &>/dev/null; then
echo "API error for org $org: $(echo "$body" | jq -r '.message')" >&2
break
fi
echo "$body" | jq -r '.[].full_name' >> "$TMP_REPOS"
url=$(next_url "$resp")
done
done <<< "$orgs"
# Deduplicate and sort
sort -u "$TMP_REPOS" | grep -v '^$' > "$TMP_REPOS.sorted"
mv "$TMP_REPOS.sorted" "$TMP_REPOS"
count=$(wc -l < "$TMP_REPOS" | tr -d ' ')
if [[ "$count" -eq 0 ]]; then
echo "No repositories found."
exit 1
fi
echo ""
echo "Found $count repository/repositories:"
echo "----------------------------------------"
cat "$TMP_REPOS"
echo "----------------------------------------"
echo ""
printf 'Use all of them? [y/N]: '
read -r answer
case "${answer:-n}" in
[yY]|[yY][eE][sS])
cp "$TMP_REPOS" "$REPOS_FILE"
echo "Stored $count repos in $REPOS_FILE"
;;
*)
echo "Aborted. No repos stored."
exit 0
;;
esac