Skip to content

Instantly share code, notes, and snippets.

@houstondapaz
Created October 13, 2021 17:47
Show Gist options
  • Save houstondapaz/451d6c6a2911971c03d3ec3f8590fb22 to your computer and use it in GitHub Desktop.
Save houstondapaz/451d6c6a2911971c03d3ec3f8590fb22 to your computer and use it in GitHub Desktop.
Recreate branch at all owner repos
#!/usr/bin/env bash
RESET_BRANCH=development
GITHUB_USER_NAME=houstondapaz
GITHUB_OWNER=houstondapaz
GITHUB_TOKEN=
declare repositories=(`
for ((i=0; ; i++)); do
current_repositories=$(curl -u $GITHUB_USER_NAME:$GITHUB_TOKEN https://api.github.com/orgs/$GITHUB_OWNER/repos\?page\=$i\&per_page\=100\&type\=private | jq -c 'map( { "name": .name , "main_branch": .default_branch } )')
if [ ${#current_repositories} -lt 1 ]; then
break;
fi
echo $current_repositories
done
`)
repositories_concatenated='[]'
for line in ${repositories[@]}; do
repositories_concatenated=`jq --argjson arr1 "$repositories_concatenated" --argjson arr2 "$line" -n '$arr1 + $arr2 | group_by(.name) | map(.[-1])'`
done
echo "Total Repositories => `jq length <<< $repositories_concatenated`"
jq -c '.[]' <<< $REPOSITORIES | while read repository; do
repo_name=$(jq -r '.name' <<< $repository)
main_branch=$(jq -r '.main_branch' <<< $repository)
echo "Deleting $repository branch $RESET_BRANCH"
curl -s -X DELETE -u $GITHUB_USER_NAME:$GITHUB_TOKEN https://api.github.com/repos/$GITHUB_OWNER/$repository/git/refs/heads/$RESET_BRANCH
echo "Recreating $repository branch $RESET_BRANCH"
SHA=$(curl -s -u $GITHUB_USER_NAME:$GITHUB_TOKEN https://api.github.com/orgs/$GITHUB_OWNER/repos/$repository/git/refs/heads/$main_branch | jq -r .object.sha)
curl -s -X POST -u $GITHUB_USER_NAME:$GITHUB_TOKEN -H "Content-Type: application/json" -d '{ "ref": "refs/heads/$RESET_BRANCH", "sha": "'$SHA'"}' https://api.github.com/orgs/$GITHUB_OWNER/repos/$repository/git/refs
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment