Skip to content

Instantly share code, notes, and snippets.

@tedivm
Last active January 12, 2022 14:56
Show Gist options
  • Save tedivm/f3d170bab6d2f6e040925380ad2fbb6d to your computer and use it in GitHub Desktop.
Save tedivm/f3d170bab6d2f6e040925380ad2fbb6d to your computer and use it in GitHub Desktop.
Github Private to Internal
#!/usr/bin/env bash
# The "Internal" visibility is still in preview in the API.
# We cannot filter private and internal, so this script will
# convert internal to internal.
CONVERT_TO="internal"
if [[ -z "$1" ]]; then
echo "Organization required"
exit 1
fi
if ! gh auth status -h github.com > /dev/null 2>&1; then
if ! gh auth login ; then
echo "Login failed."
exit 1
fi
fi
# Returns private and internal repositories.
# Filters archived repositories.
REPO_LIST=$(gh repo list $1 --private -L 999 | grep -v archived | cut -f1)
while IFS= read -r repo; do
# Exclude client repositories.
if [[ $repo == "$1/client*" ]]; then
continue
fi
echo "Converting $repo to $CONVERT_TO."
echo gh api -X PATCH "/repos/$repo" -fvisibility=$CONVERT_TO --preview nebula
if gh api -X PATCH "/repos/$repo" -fvisibility=$CONVERT_TO --preview nebula > /dev/null ; then
echo "$(tput setaf 2)$repo converted successfully.$(tput sgr 0)"
else
echo "$(tput setaf 1)$repo failed to convert.$(tput sgr 0)"
fi
# Avoid rate limits and give people a chance to bail on the script if things go wrong
sleep 0.2
done <<< "$REPO_LIST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment