How to manage a repository in Azure Repos using REST API?
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In today’s blog post, we will discuss how to create, update, delete, interact, and restore an Azure Repos programmatically using REST API.
How to manage a repository in Azure Repos using REST API?
Prerequisites:
Whenever you create a new Azure DevOps project in your organization, a default Azure Repo repository is automatically created inside your project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
## Define variables ORGANIZATION=" PROJECT=" REPOSITORY=" USER=$(az account show | jq -r .user.name) PAT=' ## Test if authentication working (List projects in your organization) curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 | jq . ## Create a new Azure DevOps project curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "name": "'"$PROJECT"'", "description": "Azure DevOps Demo Project", "capabilities": { "versioncontrol": { "sourceControlType": "Git" }, "processTemplate": { "templateTypeId": "6b724908-ef14-45cf-84f8-768b5384da45" } } }' \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 | jq . ## Get project default repository curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=6.0 | jq . |
How to create a new repository in Azure Repos using REST API?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## Create a new repository PROJECT_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 \ | jq -r '.value[] | select(.name == "'"$PROJECT"'") | .id') && curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "name": "'"$REPOSITORY"'", "project": { "id": "'"$PROJECT_ID"'" } }' \ https://dev.azure.com/$ORGANIZATION/_apis/git/repositories?api-version=6.0 | jq . ## List Azure repo for your project curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=6.0 | jq . |
How to get details of a repository in Azure Repos using REST API?
1 2 3 4 5 6 |
## Get Azure repo details REPOSITORY_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=6.0 \ | jq -r '.value[] | select(.name == "'"$REPOSITORY"'") | .id') && curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories/$REPOSITORY_ID?api-version=6.0 | jq . |
How to get Azure Repo repository clone URL using REST API?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## Clone Azure repo REPO_HTTPS_CLONE_URL=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories/$REPOSITORY_ID?api-version=6.0 \ | jq -r .webUrl) && B64_PAT=$(printf "%s"":$PAT" | base64) && git -c http.extraHeader="Authorization: Basic ${B64_PAT}" \ clone $REPO_HTTPS_CLONE_URL ## Create your 1st commit cd $REPOSITORY echo "hello world" > README.md git add README.md git commit -m "README.md added" ## Push your changes to Azure repo git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push cd .. |
How to get commit details of a repository in Azure Repos using REST API?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## List all commits in your Azure Repo curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories/$REPOSITORY_ID/commits?api-version=6.0 | jq . ## Get a particular commit details in Azure Repo COMMIT_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git\ /repositories/$REPOSITORY_ID/commits?api-version=6.0 | jq -r .value[].commitId) && curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/\ repositories/$REPOSITORY_ID/commits/$COMMIT_ID?api-version=6.0 | jq . ## Get changes in a particular commit in Azure Repo curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/\ repositories/$REPOSITORY_ID/commits/$COMMIT_ID/changes?api-version=6.0 | jq . |
How to update a repository in Azure Repos using REST API?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## Update Azure repo name or default branch curl -s --request PATCH \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "name": "MyNewRepo" }' \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/\ repositories/$REPOSITORY_ID?api-version=6.0 | jq . ## Get Azure repo details curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/\ git/repositories/$REPOSITORY_ID?api-version=6.0 | jq . |
How to delete a repository in Azure Repos using REST API?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
## Delete Azure Repo curl -s --request DELETE \ -u $USER:$PAT \ --header "Content-Type: application/json" \ https://dev.azure.com/$ORGANIZATION/_apis/git\ /repositories/$REPOSITORY_ID?api-version=6.0 | jq . ## List Azure repo for your project curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/\ git/repositories?api-version=6.0 | jq . ## Retrieve soft-deleted git repositories from the recycle bin. ## Warning: Preview feature, may change curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis\ /git/recycleBin/repositories?api-version=6.0-preview.1 | jq . ## Delete Azure repo from recycle bin (Parmanent Delete) ## Warning: Preview feature, may change ## Do not delete if you want to restore back (next step) curl -s --request DELETE \ -u $USER:$PAT \ --header "Content-Type: application/json" \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/recycleBin/\ repositories/$REPOSITORY_ID?api-version=6.0-preview.1 |
How to restore a repository in Azure Repos using REST API?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
## Restore Azure Repo from recycle bin curl -s --request PATCH \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "deleted": "false" }' \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/\ git/recycleBin/repositories/$REPOSITORY_ID?api-version=6.0-preview.1 | jq . ## List Azure repo for your project curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/\ git/repositories?api-version=6.0 | jq . ## Delete the project (Cleanup) curl -s --request DELETE \ -u $USER:$PAT \ --header "Content-Type: application/json" \ https://dev.azure.com/$ORGANIZATION/_apis/projects/$PROJECT_ID?api-version=6.0 |
Hope you have enjoyed this article. To know more about Azure DevOps, please refer below official documentation
https://docs.microsoft.com/en-us/azure/devops/?view=azure-devops