Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Python code to clone bitbucket repository using subprocess

kompal.sithta May 24, 2023

Hi All,

 

I am trying to clone a python repository using the following code. I am trying to automate the process of cloning for certain automation tasks required for code analysis:

 

import subprocess

def clone_bitbucket_repo(username, password, repository_url,destination_path):
    command = ['git' ,'clone', repository_url, destination_path]


    # Set up environment variables for authentication
    env_variables = {
        'BITBUCKET_USERNAME': username,
        'BITBUCKET_PASSWORD': password
    }

    # Execute the git clone command
    try:
        subprocess.check_output(command, shell=True, env=env_variables)
        print("Repository cloned successfully!")
    except subprocess.CalledProcessError as e:
        print(f"Error cloning repository: {e}")

# Example usage
bitbucket_username = 'username'
bitbucket_password = 'password'
destination_path = 'path/of/folder/'

clone_bitbucket_repo(bitbucket_username, bitbucket_password, repository_url,destination_path)
the error being generated by running this script is :fatal: unable to access 'https://bitbucket.org/{workspace-name}/{repo-name}.git/': getaddrinfo() thread failed to start
Please let me know if can have a solution to reolve this error.
Thanks,
Kompal

1 answer

0 votes
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 24, 2023

The problem

The error message

unable to access 'https://bitbucket.org/{workspace-name}/{repo-name}.git/': getaddrinfo()

...makes it clear that those two placeholders in the URL weren't replaced.

The fix

Change this:

repository_url = 'https://username@bitbucket.org/{workspace-name}/{repo-name}.git'

to the URL where you enter your actual workspacename and repository name:

repository_url = 'https://username@bitbucket.org/foo/bar.git'

To me it seems that the "username" part in the URL should also be replaced. At least I don't think that this is what you really want to use.

jake November 10, 2023

hi Aron. 

i m trying to do the similar thing and getting the same error. 

so the question is, where to get the exact url i need to pass as repository_url?
should we go to our repo -> clone -> and copy that link? 

or just copy the link from browser?

also, why do we need to pass username in the base url? is it because it different when we clone manually vs when we do from python using subprocess? 

ex: i see this when i go to bitbucket and click on clone and copy the link in ssh

ssh://git@bitbucketdc-ssh.mycompnay.net:1111/myproject/myrepo.git and can see in http like this
https://bitbucketdc.mycompnay.net/scm/myproject/myrepo.git
appreciate for any help.

jake November 10, 2023

ok. i got it fixed. not sure what was the issue, but now its working 

Like # people like this
jake November 13, 2023

i though it might worth to put my solution here. 

so when you try to add your url make sure to use f when you pass username and password as object. 

like you have this 

'https://{username}:{password}bitbucket.org/{workspace-name}/{repo-name}.git'

so if you have something like above, make sure to add f, otherwise it cant be readed and  will throw error of :fatal: unable to access

so like this: 

f"https://{username}:{password}bitbucket.org/{workspace-name}/{repo-name}.git"

Suggest an answer

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

Atlassian Community Events