Skip to content

Commit bbb5c34

Browse files
committed
Initial organization repo list export script
1 parent 4a7e2bf commit bbb5c34

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

api/bash/repo-list-export.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
#
3+
# set GITHUB_TOKEN to your GitHub or GHE access token
4+
# set GITHUB_API_ENDPOINT to your GHE API endpoint (defaults to https://api.github.com)
5+
6+
if [ -n "$GITHUB_API_ENDPOINT" ]
7+
then
8+
url=$GITHUB_API_ENDPOINT
9+
else
10+
url="https://api.github.com"
11+
fi
12+
13+
token=$GITHUB_TOKEN
14+
15+
dependency_test()
16+
{
17+
for dep in curl jq ; do
18+
command -v $dep &>/dev/null || { echo -e "\n${_error}Error:${_reset} I require the ${_command}$dep${_reset} command but it's not installed.\n"; exit 1; }
19+
done
20+
}
21+
22+
token_test()
23+
{
24+
if [ -n "$token" ]
25+
then
26+
token_cmd="Authorization: token $token"
27+
else
28+
echo "You must set a Personal Access Token to the GITHUB_TOKEN environment variable"
29+
exit 1
30+
fi
31+
}
32+
33+
usage()
34+
{
35+
echo -e "Usage: $0 [options] <orgname>...\n"
36+
echo "Options:"
37+
echo " -h | --help Help"
38+
echo ""
39+
}
40+
41+
get_repos()
42+
{
43+
last_repo_page=$( curl -s --head -H "$token_cmd" "$url/orgs/$org/repos?per_page=100" | sed -nE 's/^Link:.*per_page=100.page=([0-9]+)>; rel="last".*/\1/p' )
44+
45+
if [ "$last_repo_page" == "" ]
46+
then
47+
all_repos=$( curl -s -H "$token_cmd" "$url/orgs/$org/repos?per_page=100" | jq '.[].name' | sed 's/\"//g' )
48+
49+
total_repos=$( echo $all_repos | sed 's/\"//g' | wc -w | sed 's/ //g' )
50+
echo "Fetching repository list for '$org' organization on GitHub.com"
51+
for (( i=1; i<=$total_repos; i++ ))
52+
do
53+
54+
repo=$( echo ${all_repos[0]} | cut -f $i -d " " )
55+
echo "$repo"
56+
57+
done > repos.txt
58+
echo "Total # of repositories in "\'$org\'": $total_repos"
59+
echo "List saved to $org.txt"
60+
else
61+
for (( j=1; j<=$last_repo_page; j++ ))
62+
do
63+
all_repos=$( curl -s -H "$token_cmd" "$url/orgs/$org/repos?per_page=100&page=$j" | jq '.[].name' | sed 's/\"//g' )
64+
65+
total_repos=$( echo $all_repos | sed 's/\"//g' | wc -w | sed 's/ //g' )
66+
echo "Fetching repository list for '$org' organization on GitHub.com"
67+
68+
for (( i=1; i<=$total_repos; i++ ))
69+
do
70+
71+
repo=$( echo ${all_repos[0]} | cut -f $i -d " " )
72+
echo "$repo"
73+
74+
done
75+
done | sort > $org.txt
76+
grand_total_repos=$(wc -l $org.txt | sed -nE 's/ +([0-9]+) .+/\1/p')
77+
echo "Total # of repositories in "\'$org\'": $grand_total_repos"
78+
echo "List saved to $org.txt"
79+
fi #end last_repo_page == ""
80+
}
81+
82+
#### MAIN
83+
84+
dependency_test
85+
86+
token_test
87+
88+
if [[ $# -eq 0 ]] ; then
89+
echo "Error: no organization name entered" 1>&2
90+
echo
91+
usage
92+
exit 1
93+
fi
94+
95+
while [ "$1" != "" ]; do
96+
case $1 in
97+
-h | --help ) usage
98+
exit ;;
99+
-* ) echo "Error: invalid argument: '$1'" 1>&2
100+
echo
101+
usage
102+
exit 1;;
103+
* ) org="$1"
104+
get_repos
105+
esac
106+
shift
107+
done
108+
109+
exit 0

0 commit comments

Comments
 (0)