Migrating a repository from bitbucket to Azure Repos -using powershell

Picking up from where I left in the previous note, here are the steps involved in migrating a code repository from bitbucket to Azure Repos using powershell.

I’m going to spend some time noting how bitbucket team, project and repos are related to each other. If you want to go the code directly, here is the link to the github repository. However, I would advise reading this note (5 mins or less) to understand the internal working of the tool.

What is a team in bitbucket?
Per bitbucket -bitbucket Teams makes it simple to create a shared account to consolidate your team-owned repositories and organize your group’s work.

What is a project in bitbucket from a repository perspective?
Per bitbucket support -a project is a place where you organize your repositories. They add the ability to categorize and group repositories.
Loosely stated -a project can be imagined as a host for the repositories. A repository can belong to only one project.

What is a repository in bitbucket?
Apart from the obvious, a repository in bitbucket is associated to bitbucket project and bitbucket team.

So you can imagine the relationship between team, project and repositories to be something like:21. MigratingFromBBtoAR -image5

On the Azure Devops side, we have a similar structure -organization, project and repository. And (like bitbucket), a team project is associated to only one organization and can host multiple repositories.

In this case the requirement was to move all the git repositories in all the projects from one team in Bitbucket to one team project of an organization in Azure Devops.

Prerequisites:
-I had access to all projects and all repositories in bitbucket
-I had admin access in AzureDevops -able to create repositories and push
In order to work with creating Azure repos from the commandline I had to install Azure CLI tool. Accessible here.

Algorithm:
Create local folder for clone work
Login to Bitbucket
Get a list of bitbucket teams
For each bitbucket team, get a list of projects associated with that team
-For each projects, get a list of repositories associated with that project
-For each repository,
-clone the repo to local
-check if a repository exists in Azure Repos, if not create one
-push code from local to repo in Azure Repos
continue until all repositories in particular project are migrated
continue until all projects in particular bitbucket team are migrated
continue until all teams in particular bitbucket login are migrated
Logout of bitbucket
Delete local folder
Delete AzureDevps PAT from environment

Conclusion:
Before you give the powershell script a try, please understand the internal working and logic used to migrate the repositories. If needed, before using it, place appropriate checks in place like – just create a list of repositories in bitbucket and on Azure Devops and review if the list is complete. Once you are confident and certain, go ahead and use it to migrate ONLY ONE repository. You can manage that by proper exit statements.

All in all, I’ve found this script quite robust and have used it to migrate all the repositories and (after multiple fixes) worked perfectly fine. I am storing the final version in github for our community to use it.

Some useful links:
https://community.atlassian.com/t5/Bitbucket-questions/What-is-the-difference-between-a-repository-and-a-project/qaq-p/399774
https://confluence.atlassian.com/bitbucket/group-repositories-into-projects-792497956.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s