-
Notifications
You must be signed in to change notification settings - Fork 3
/
cloneall.sh
65 lines (55 loc) · 1.73 KB
/
cloneall.sh
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
#!/bin/bash
# requires jq -> https://stedolan.github.io/jq/download/;
# create oath token -> https://github.com/settings/tokens;
# GitHub configuration
githubToken="<oathToken>"
githubUser="<username>"
githubOrganization="<organisation>"
# Script configuration
targetDir="."
cloneOrgRepos=false
cloneUserRepos=false
cloneType="https" # https / ssh / ghcli
# Script
cd $targetDir
echo "PWD: $(pwd)"
if [[ $cloneType == "https" ]]
then targetField="clone_url"
elif [[ $cloneType == "ssh" ]]
then targetField="ssh_url"
elif [[ $cloneType == "ghcli" ]]
then targetField="full_name"
else
echo "Invalid \`cloneType\` option. Acceptable: 'https', 'ssh', 'ghcli'. Exiting..."
exit 1
fi
# https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-organization-repositories
listOrgReposUrl="https://api.github.com/orgs/$githubOrganization/repos?per_page=100"
if $cloneOrgRepos
then
orgRepositories=$(curl $listOrgReposUrl -u ${githubUser}:${githubToken} | jq -r .[].${targetField})
else
orgRepositories=""
fi
# https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repositories-for-the-authenticated-user
listUserRepoUrl="https://api.github.com/user/repos?per_page=100&type=owner"
if $cloneUserRepos
then
userRepositories=$(curl $listUserRepoUrl -u ${githubUser}:${githubToken} | jq -r .[].${targetField})
else
userRepositories=""
fi
# Clone all repositories
repositories="$orgRepositories $userRepositories"
for repository in $repositories
do
printf "\nRepository found: $repository\n"
if [[ $cloneType == "ghcli" ]]
then
commandPattern="gh repo clone"
else
commandPattern="git clone"
fi
finalCommand="$commandPattern $repository"
eval $finalCommand
done