Hello,
I came across this article showing how to importing GIT repositories from other hosting services. I was wondering, if there's a way to automate this with scripting?
Thank you and best,
G.W.
The steps necessary to "import" a repo to any platform, Bitbucket included, is to:
These steps would need to know what credentials to use for the initial clone as well as the creds for pushing into the new remote (since they'll likely differ) as well as knowing the URLs for the source and destination. All of this should be possible in bash or really any scripting language that can interact with CLI for the purpose of calling git operations.
Example:
source_username = ""source_password = ""source_repo_url = ""destination_username = ""destination_password = ""destination_repo_name = ""destination_project_key = ""destination_workspace_slug = ""# clone the repogit clone --mirror https://$source_username:$source_password@source_repo_url temp# create empty placeholder in destination workspacecurl -u "$destination_username:$destination_password" -X POST -H "Content-Type: application/json" -d '{ "scm": "git", "project": { "key": $destination_project_key } "https://api.bitbucket.org/2.0/repositories/$destination_workspace_slug/$destination_repo_name"# explore into repo dircd temp# add new remotegit remote add cloud https://$destination_username:$destination_password@bitbucket.org/$destination_workspace_slug/$destination_repo_name.git# push to new remotegit push -u cloud --allgit push -u cloud --tags
Then you'd just need to loop it if there's multiple repos that you want to migrate, passing in the source_url and the repo name/project key for each itteration.
You can see an example of this in python for migrating from GitHub to Bitbucket Cloud here
I think we could replicate the steps one way or another.
Operations like creating the repository are supported by the Bitbucket API, and moving the content would be a series of git operations.
Since you have to check out the repository first, it's not as direct as importing with Bitbucket cloud, but workable.
Hi Michael,
This is really helpful. Thank you! I have a question about the username format in the curl command: should it just be my email address verbatim?
Best,
Thank you very much for the info!
For the Bitbucket (destination) side of things, you'll need your username (not email address)
For the password, I'd recommend just using an app password as they can be used for git operations (clone/push) as well as interacting with the REST API (the curl command). You could also use an access token but that's probably an unnecessary added complication since app passwords work great for this sort of thing.
Thank you Michael!
It looks like you're new here. Sign in or register to get started.