Also include private repos in user mode

This commit is contained in:
Maximilian Kratz 2023-01-15 10:32:35 +01:00
parent 5d53a5a259
commit c4ebc8eaf0
2 changed files with 29 additions and 2 deletions

View file

@ -8,7 +8,7 @@ pipeline:
image: debian:latest
commands:
- apt-get update -q && apt-get install -yq curl jq
- /bin/bash github2gitea-mirror -m repo -r https://github.com/maxkratz/docker_texlive -u maxkratz
- /bin/bash github2gitea-mirror -m user -u maxkratz
secrets:
- GITEA_URL
- ACCESS_TOKEN

View file

@ -5,6 +5,7 @@
#
# Modes:
# - Mirror a public/private repo
# - Mirror all public/private repos of a user
# - Mirror all starred repos by a user
# - Mirror all public/private repos of an organization
#
@ -52,7 +53,7 @@ done
# Prints a message on how to use the script with exit 1
fail_print_usage () {
echo -e "Usage: $0"
echo -e " -m, --mode {org,star,repo} Mode to use; either mirror an organization or mirror all starred repositories."
echo -e " -m, --mode {org,star,repo,user} Mode to use; either mirror an organization or mirror all starred repositories."
echo -e " -o, --org \$organization GitHub organization to mirror and/or the target organization in Gitea."
echo -e " -u, --user \$github_user GitHub user to gather the starred repositories from."
echo -e " -v, --visibility {public,private} Visibility for the created Gitea organization."
@ -87,6 +88,11 @@ elif [ "${mode}" == "repo" ]; then
echo -e "Repo URL or GitHub user not set."
fail_print_usage
fi
elif [ "${mode}" == "user" ]; then
if [[ -z "${github_user}" ]]; then
echo -e "GitHub user not set."
fail_print_usage
fi
else
echo -e "Mode not found."
fail_print_usage
@ -106,6 +112,11 @@ set_uid() {
uid=$($CURL "${header_options[@]}" $GITEA_URL/api/v1/orgs/${gitea_organization} | jq .id)
}
# Sets the uid to the specified Gitea user
set_uid_user() {
uid=$($CURL "${header_options[@]}" $GITEA_URL/api/v1/users/${github_user} | jq .id)
}
# Fetches all starred repos of the given user to JSON files
fetch_starred_repos() {
log "Fetch starred repos."
@ -128,6 +139,17 @@ fetch_orga_repos() {
done
}
# Fetches all public/private repos of the given GitHub user to JSON files
fetch_user_repos() {
log "Fetch user repos."
i=1
# GitHub API just returns empty arrays instead of 404
while $CURL "https://api.github.com/user/repos?affiliation=owner&page=${i}&per_page=100" -u "${github_user}:${GITHUB_TOKEN}" >${jsonoutput}/${i}.json \
&& (( $(jq <${jsonoutput}/${i}.json '. | length') > 0 )) ; do
(( i++ ))
done
}
# Fetches one public/private GitHub repo to a JSON file
fetch_one_repo() {
log "Fetch one repo."
@ -220,6 +242,11 @@ elif [ "${mode}" == "star" ]; then
set_uid
fetch_starred_repos
repos_to_migration
elif [ "${mode}" == "user" ]; then
log "Mode = user"
set_uid_user
fetch_user_repos
repos_to_migration
fi
log "Finished."