-
-
Save JamesDLD/fc15dd24971f9e18ee0ac84ddea2e0fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ------------------------------------------------------------------ | |
# Set Azure DevOps Default Branch on All Repo | |
# If branch doesn't exist it will | |
# create it from the current default branch | |
# previous default branch will be locked | |
#Ref : https://devblogs.microsoft.com/devops/azure-repos-default-branch-name/ | |
# ------------------------------------------------------------------ | |
#Variable | |
set -e #stop on error | |
organization="https://dev.azure.com/YourOrganizationName/" | |
targetBranch="main" | |
iteration=1 | |
#Ensuring that az and jq cmdlet are available | |
if [ $(which az) ] && [ $(which jq) ] ; then | |
echo "az and jq are available" | |
else | |
echo "az or jq are not not available" | |
exit 1 | |
fi | |
#Ensure that the Azure DevOps extension is installed https://docs.microsoft.com/en-us/azure/devops/cli/?view=azure-devops | |
if az extension show --name azure-devops ; then | |
echo "The Azure DevOps CLI extension is installed" | |
else | |
echo "Installing the Azure DevOps CLI extension" | |
az extension add --name azure-devops | |
fi | |
#Connect with Azure AD | |
#az login | |
#list ADO projects | |
if projects=$(az devops project list --organization $organization | jq -r '.value[] | @base64') ; then | |
echo "az login authentication worked" | |
else | |
echo "az login authentication didn't worked, fill in your PAT using those minimum privileges : Projet and Team = read & Code = Full" | |
az devops login | |
fi | |
echo "" | |
for project in $projects; do | |
_jqproject() { | |
echo ${project} | base64 --decode | jq -r ${1} | |
} | |
project_name=$(_jqproject '.name') | |
project_id=$(_jqproject '.id') | |
echo "[$iteration] Working in project $project_name having the Id $project_id" | |
#list ADO repos | |
repos=$(az repos list --organization $organization --project $project_id | jq -r '.[] | @base64') | |
for repo in $repos; do | |
_jqrepo() { | |
echo ${repo} | base64 --decode | jq -r ${1} | |
} | |
repo_defaultBranch=$(_jqrepo '.defaultBranch') | |
repo_id=$(_jqrepo '.id') | |
repo_name=$(_jqrepo '.name') | |
echo "[$iteration] Repo $repo_name with id $repo_id has the default branch $repo_defaultBranch" | |
if [ $repo_defaultBranch != "refs/heads/$targetBranch" ] && [ "$repo_defaultBranch" != "null" ] ; then | |
refs=$(az repos ref list --organization $organization --project $project_id --repository $repo_id | jq -r '.[].name') | |
if [[ "$refs" == *"refs/heads/$targetBranch"* ]] ; then | |
echo "[$iteration] Ref refs/heads/$targetBranch already exist on repo $repo_name having the id $repo_id" | |
else | |
echo "[$iteration] Creating ref refs/heads/$targetBranch on repo $repo_name having the id $repo_id" | |
refFrom=$(az repos ref list --organization $organization --project $project_id --repository $repo_id | jq -r '.[] | select( .name == "'"$repo_defaultBranch"'" ) | .objectId') | |
az repos ref create --name "heads/$targetBranch" --object-id $refFrom --organization $organization --project $project_id --repository $repo_id | |
fi | |
echo "[$iteration] Setting the refs/heads/$targetBranch as default branch of repo $repo_name having the id $repo_id" | |
az repos update --organization $organization --project $project_id --repository $repo_id --default-branch "refs/heads/$targetBranch" | |
echo "[$iteration] Locking the $repo_defaultBranch of repo $repo_name having the id $repo_id" | |
az repos ref lock --organization $organization --project $project_id --repository $repo_id --name "$(echo $repo_defaultBranch | cut -c 6-)" | |
else | |
echo "[$iteration] Default branch ok for repo $repo_name having the id $repo_id" | |
fi | |
iteration=$((iteration+1)) | |
#exit; | |
echo "" | |
done | |
echo "" | |
#exit; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment