Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Importing a GIT repository with script

G. W. July 17, 2024

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.

2 answers

1 accepted

1 vote
Answer accepted
Michael Walker
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 17, 2024

The steps necessary to "import" a repo to any platform, Bitbucket included, is to:

  1. Clone the original repo
  2. Create a new empty placeholder in the desired destination and copy the git URL
  3. Explore into the local clone's directory
  4. Set a new "remote" targeting the desired destination (the git URL from step 2) within the local repo's config
  5. Push to the new remote

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 repo
git clone --mirror https://$source_username:$source_password@source_repo_url temp
# create empty placeholder in destination workspace
curl -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 dir
cd temp
# add new remote
git remote add cloud https://$destination_username:$destination_password@bitbucket.org/$destination_workspace_slug/$destination_repo_name.git
# push to new remote
git push -u cloud --all
git 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

G. W. July 17, 2024

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,

G.W.

Michael Walker
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 18, 2024

For the Bitbucket (destination) side of things, you'll need your username (not email address)

  1. Log in to Bitbucket cloud
  2. Select settings (cog) in the top right corner
  3. Choose "Personal Settings"
  4. Copy your "username"

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.

Like G. W. likes this
G. W. July 18, 2024

Thank you Michael!

Like Michael Walker likes this
0 votes
Jim Knepley - ReleaseTEAM
Atlassian Partner
July 17, 2024

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.

G. W. July 17, 2024

Thank you very much for the info!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events