linux - 使用 python 代码从 bitbucket 存储库中提取数据的最佳方法

标签 linux python-3.x git bitbucket bitbucket-api

我必须开发一个功能,其中我必须在 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/

相关文章:

python - 将数字序列折叠成范围

git - 使用 gcloud 的凭证助手访问 Google 源代码库会阻止 osxkeychain 工作

git - 如何优化我的gitlab-ci.yml任何想法?

linux - 如何将前 10 个最近的文件从一个目录复制到另一个目录?

linux - 使用 Jenkins、CMake 和 pkg-config 构建多个项目的首选方式?

linux - 向 SVN 文件添加注释

Linux、GNU GCC、ld、版本脚本和 ELF 二进制格式——它是如何工作的?

python - urwid 中是否有焦点改变事件?

arrays - 解析 JSON 来做数学题?

Git: merge 到master,同时自动选择用分支覆盖master文件