Skip to content

Instantly share code, notes, and snippets.

@Richard-Barrett
Created February 4, 2025 23:32
Show Gist options
  • Select an option

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

Select an option

Save Richard-Barrett/a5830c8ad4677b40d6cd2ec577981d84 to your computer and use it in GitHub Desktop.
Rename GitHub Organization Names as Used in Conjunction with migrate_orgs.sh
#!/bin/bash
# Ensure GitHub CLI (gh) is installed
if ! command -v gh &>/dev/null; then
echo "GitHub CLI (gh) is not installed. Please install it first."
exit 1
fi
# Usage function
usage() {
echo "Usage: $0 --source-token <SOURCE_GH_TOKEN> --target-token <TARGET_GH_TOKEN> [OPTIONS]"
echo ""
echo "Options:"
echo " --source-token GitHub Token for the source account (Required unless --dry-run is used)"
echo " --target-token GitHub Token for the target account (Required unless --dry-run is used)"
echo " --rename-source Rename SOURCE orgs (append -old)"
echo " --rename-target Rename TARGET_NEW orgs (remove -new)"
echo " --dry-run Show what changes would be made without executing them"
echo " --help, -h Show this help message and exit"
echo ""
echo "Examples:"
echo " # Rename SOURCE organizations to append '-old'"
echo " $0 --source-token ghp_xxxxxxx --target-token ghp_yyyyyyy --rename-source"
echo ""
echo " # Rename TARGET_NEW organizations (remove '-new')"
echo " $0 --source-token ghp_xxxxxxx --target-token ghp_yyyyyyy --rename-target"
echo ""
echo " # Rename both SOURCE and TARGET_NEW organizations"
echo " $0 --source-token ghp_xxxxxxx --target-token ghp_yyyyyyy --rename-source --rename-target"
echo ""
echo " # Dry run (show changes without executing them)"
echo " $0 --rename-source --rename-target --dry-run"
echo ""
exit 0
}
# Parse command-line arguments
SOURCE_GH_TOKEN=""
TARGET_GH_TOKEN=""
RENAME_SOURCE=false
RENAME_TARGET=false
DRY_RUN=false
while [[ $# -gt 0 ]]; do
case "$1" in
--source-token)
SOURCE_GH_TOKEN="$2"
shift 2
;;
--target-token)
TARGET_GH_TOKEN="$2"
shift 2
;;
--rename-source)
RENAME_SOURCE=true
shift
;;
--rename-target)
RENAME_TARGET=true
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
--help|-h)
usage
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
# Check if at least one rename flag is set
if [[ "$RENAME_SOURCE" == false && "$RENAME_TARGET" == false ]]; then
echo "Error: You must specify --rename-source or --rename-target."
usage
fi
# If not in dry-run mode, check for required tokens
if [[ "$DRY_RUN" == false ]]; then
if [[ -z "$SOURCE_GH_TOKEN" || -z "$TARGET_GH_TOKEN" ]]; then
echo "Error: Both --source-token and --target-token are required unless --dry-run is used."
usage
fi
fi
ORG_LIST="orgs.txt"
# Process each organization
while IFS= read -r ORG_NAME; do
if [[ "$RENAME_SOURCE" == true ]]; then
OLD_NAME="${ORG_NAME}-old"
if [[ "$DRY_RUN" == true ]]; then
echo "[DRY RUN] Would rename source org: $ORG_NAME → $OLD_NAME"
else
echo "Renaming source org: $ORG_NAME → $OLD_NAME"
GITHUB_TOKEN="$SOURCE_GH_TOKEN" gh api --method PATCH -H "Accept: application/vnd.github.v3+json" "/orgs/$ORG_NAME" -f name="$OLD_NAME"
fi
fi
if [[ "$RENAME_TARGET" == true ]]; then
TARGET_NAME_NEW="${ORG_NAME}-new"
if [[ "$DRY_RUN" == true ]]; then
echo "[DRY RUN] Would rename target org: $TARGET_NAME_NEW → $ORG_NAME"
else
echo "Renaming target org: $TARGET_NAME_NEW → $ORG_NAME"
GITHUB_TOKEN="$TARGET_GH_TOKEN" gh api --method PATCH -H "Accept: application/vnd.github.v3+json" "/orgs/$TARGET_NAME_NEW" -f name="$ORG_NAME"
fi
fi
done < "$ORG_LIST"
if [[ "$DRY_RUN" == true ]]; then
echo "[DRY RUN] No actual changes were made."
else
echo "Organization renaming completed!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment