本文总结如何调用Altassian的Bitbucket API及相关任务自动化的实现。
Updated: 2022 / 6 / 22
Altassian | Bitbucket Python API使用及相关任务自动化的实现
Bitbucket Python API
stashy
根据用户名和密码登录Bitbucket Server
from stashy
stash = stashy.connect()
url = 'http://YourServerAddr:7990',
username = 'YourUsername',
password = 'YourPassword')
atlassian-python-api
Bitbucket Module
根据用户名和密码登录Bitbucket Server
from atlassian import Bitbucket
bitbucket = Bitbucket(
url = 'http://YourServerAddr:7990',
username = 'YourUsername',
password = 'YourPassword')
Manage Projects
-
bitbucket.project.list()
bitbucket.project.list()
# {'key': ..., 'id': ..., 'name': ..., 'description': ..., 'public': ..., 'type': ..., 'links': {'self': [{'href': 'http://YourServerAdd:7990/projects/'key''}]}}
-
bitbucket.repo_list()
bitbucket.repo_list('YourProjectKey')
# {'slug': ..., 'id': ..., 'name': ..., 'description': ..., 'scmId': ..., 'state': ..., 'statusMessage': ..., 'forkable': ..., 'project': {'key': ..., 'id': ..., 'name': ..., 'description': ..., 'public': ..., 'type': ..., 'links': {'self': [{'href': 'http://YourServerAddr:7990/projects/YourProjectKey'}]}}, 'public': ..., 'links': {'clone': ..., [{'href': 'ssh://git@YourServerAddr: 7999/YourProjectKey/YourRepoSlug.git', 'name': 'ssh'}, {'href: 'http://YourServerAddr:7990/scm/YourProjectKey/YourReposlug.git', 'name': 'http'}], 'self': [{'href': 'http://YourServerAddr:7990/projects/YourProjectKey/repos/YourReposlug/browse'}]}
- [x]
Repository
File
- bitbucket.get_file_list(project_key, repository_slug, sub_folder, query, start, limit)
FileList = bitbucket.get_file_list(project_key = YourProjectKey, repository_slug = YourRepoSlug, sub_folder = YourFilePath, query = YourBranchName, start = FromwhereStart, limit=None)
# list(FileList)可列出在该Project该Repo该Branch该Subfolder下从start开始的所有file name
- bitbucket.get_content_file(project, repository, filename, at=None, markup=None)
FileCont = bitbucket.get_content_file(YourProjectKey, YourRepoSlug, YourFilePath&Name, YourBranch, markup=None)
type(FileCont)
# <class 'bytes'>
FILE = open('./FILE.csv', 'wb').write(FileCont)
- bitbucket.download_repo_archive(project_key, repository_slug, dest_fd, at, filename, format, path, prefix, chunk_size)
with open('./File', mode='wb') as Yourfd:
bitbucket.download_repo_archive(
project_key = YourProjectKey,
repository_slug = YourRepoSlug,
dest_fd = Yourfd,
at = YourBranch,
path = YourFilePath&Name,
format = zip)
# 或者
# fd = open('./File', mode = 'wb')
# bitbucket.download_repo_archive(
# project_key = YourProjectKey,
# repository_slug = YourRepoSlug,
# dest_fd = Yourfd,
# at = YourBranch,
# path = YourFilePath&Name,
# format = zip)
# dest_fd.close()