我必须开发一个功能,其中我必须在 linux 服务器上使用 python 代码从 bitbucket 存储库中提取文件。文件位于 bitbucket 存储库本身
你能建议我怎么做吗以及最好的方法吗?我尝试使用 API - http:///rest/api/1.0/projects//repos//browse - 它给了我组件级数据,即只有文件名,但没有实际文件内容
谢谢
最佳答案
有一个 python 库封装了其余的 api:
https://github.com/cosmin/stashy
或者你可以使用 urllib2:
#!/usr/bin/python
import os
import tempfile
import sys
import urllib2
import json
import base64
import logging
import re
import pprint
import requests
import subprocess
projectKey= "FW"
repoKey = "fw"
branch = "master"
pathToVersionProperties = "core/CruiseControl/CI_version.properties"
localVersionProperties = "CI_version.properties"
bitbucketBaseUrl = "https://bitbucket.company.com/rest/api/latest"
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
def checkPersonalAccessToken():
try:
os.environ["PAT"]
logging.info("Detected Personal Access Token")
except KeyError:
logging.error("Personal Access Token: $PAT env variable not set, update Jenkins master with correct environment variable")
sys.exit(1)
def getJenkinsPropertiesFile():
restEndpoint = "{}/projects/{}/repos/{}/raw/{}".format(bitbucketBaseUrl, projectKey, repoKey, pathToVersionProperties)
logging.info("REST endpoint : {}".format(restEndpoint))
request = urllib2.Request(restEndpoint)
request.add_header("Authorization", "Bearer %s" % os.environ["PAT"])
result = urllib2.urlopen(request).read()
return result
checkPersonalAccessToken()
propertiesString = getJenkinsPropertiesFile()
此示例从 bitbucket 中检索属性文件。我不确定您使用的是哪个版本的 Bitbucket。上面的示例使用个人访问 token 进行身份验证(在 Bitbucket 5.5 中添加)。您也可以使用标准用户名/密码。
关于linux - 使用 python 代码从 bitbucket 存储库中提取数据的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52883511/