Skip to content

Instantly share code, notes, and snippets.

@Richard-Barrett
Created May 15, 2025 00:54
Show Gist options
  • Select an option

  • Save Richard-Barrett/6e31e5fd327c7f19df6f3820c2648df3 to your computer and use it in GitHub Desktop.

Select an option

Save Richard-Barrett/6e31e5fd327c7f19df6f3820c2648df3 to your computer and use it in GitHub Desktop.
Change GitHub Repositories From Flat List
#!/bin/bash
set -euo pipefail
ORG=""
NEW_VISIBILITY=""
REPO_LIST="repos.txt"
show_help() {
cat <<EOF
Usage: $0 <org_name> <visibility> [--repos-file <path>]
Change the visibility of a list of repositories in a GitHub organization.
Arguments:
<org_name> The GitHub organization name
<visibility> Desired visibility: public | private | internal
Options:
--repos-file <path> Path to the repository list file (default: ./repos.txt)
--help Show this help message and exit
Example:
$0 my-org private --repos-file ./lists/my_repos.txt
EOF
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--help)
show_help
exit 0
;;
--repos-file)
shift
REPO_LIST="$1"
;;
*)
if [[ -z "$ORG" ]]; then
ORG="$1"
elif [[ -z "$NEW_VISIBILITY" ]]; then
NEW_VISIBILITY="$1"
else
echo "❌ Unknown argument: $1"
show_help
exit 1
fi
;;
esac
shift
done
# Validate arguments
if [[ -z "$ORG" || -z "$NEW_VISIBILITY" ]]; then
echo "❌ Missing required arguments."
show_help
exit 1
fi
if [[ "$NEW_VISIBILITY" != "public" && "$NEW_VISIBILITY" != "private" && "$NEW_VISIBILITY" != "internal" ]]; then
echo "❌ Invalid visibility: $NEW_VISIBILITY. Must be 'public', 'private', or 'internal'."
exit 1
fi
if [[ ! -f "$REPO_LIST" ]]; then
echo "❌ Repositories file not found: $REPO_LIST"
exit 1
fi
# Loop through repos
while read -r REPO; do
[[ -z "$REPO" ]] && continue
echo "🔄 Changing visibility of '$REPO' in org '$ORG' to '$NEW_VISIBILITY'..."
gh api \
-X PATCH \
"repos/$ORG/$REPO" \
-f visibility="$NEW_VISIBILITY" \
>/dev/null && echo "✅ Updated $REPO" || echo "⚠️ Failed to update $REPO"
done < "$REPO_LIST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment