Last active
September 12, 2021 07:31
-
-
Save nabsha/4bca9d7ea0fe985029f5a2d81252e98f to your computer and use it in GitHub Desktop.
Get links to all repositories in a workspace using Atlassian Bitbucket Python API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Installation | |
## pip3 install atlassian-python-api | |
## Running the script, do | |
## python3 main.py <bitbucket_email> <workspace> [0|1] > output.txt | |
## 0 for https, 1 for git protocol to get link of | |
## The above command generates links in output.txt | |
## To say clone all the repos do, | |
## while read in; do git clone "$in"; done < output.txt | |
import sys | |
import getpass | |
from atlassian.bitbucket.cloud import Cloud | |
def get_all_repository_links(username, password, workspace, link_type): | |
''' | |
Prints list of all repositories under all projects in given workspace | |
:param username: username of account | |
:param password: password of account | |
:param workspace: workspace to look for projects | |
:param link_type: 0 for https, 1 for git | |
:return: nothing | |
''' | |
cloud = Cloud( | |
url='https://api.bitbucket.org', | |
username=username, | |
password=password, | |
cloud=True) | |
cloud.workspaces.get(workspace) | |
w = cloud.workspaces.get(workspace) | |
for p in w.projects.each(): | |
# print(" Project:" + p.name) | |
for r in p.repositories.each(): | |
# print(" Repository:" + r.name) | |
link = r.data['links']['clone'][link_type]['href'] | |
print(link) | |
def usage(appName): | |
return appName + " <username> <workspace>" | |
# Press the green button in the gutter to run the script. | |
if __name__ == '__main__': | |
if (len(sys.argv) < 3): | |
print(usage(sys.argv[0])) | |
exit(-1) | |
pw = getpass.getpass() | |
get_all_repository_links(sys.argv[1], pw, sys.argv[2], int(sys.argv[3])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment