首页 > 解决方案 > Bitbucket Rest API 用于获取项目下的 Repo 名称

问题描述

我正在尝试使用 Bitbucket API 获取项目下的存储库名称。文档上的当前链接说要使用

curl -u 用户名:密码 http://${bitbucket-url}/rest/api/1.0/projects/${projectkey}/repos/

回复:

{ "size": 1, "limit": 25, "isLastPage": true, "values": [ { "slug": "my-repo", "id": 1, "name": "My repo", “scmId”:“git”,“state”:“AVAILABLE”,“statusMessage”:“Available”,“forkable”:true,“project”:{“key”:“PRJ”,“id”:1,“ name": "My Cool Project", "description": "The description for my cool project.", "public": true, "type": "NORMAL", "links": { "self": [ { "href ": "http://link/to/project" } ] } }, "public": true, "links": { "clone": [ { "href": "ssh://git@/PRJ/my-repo.git", "name": "ssh" }, { "href": "https:///scm/PRJ/my-repo.git", "name": " http" } ], "self": [ { "href": "http://link/to/repository" } ] } } ], "start": 0 }

但我只需要响应中的 repo 名称

标签: bitbucket-api

解决方案


from subprocess import call
import configparser
import subprocess
import json
import os

base_dir = os.getcwd()
DETACHED_PROCESS = 0x00000008

cmd = 'curl --url "' + bb_url + '?pagelen=100&page=' + str(page) + '" --user ' + bb_user + ':' + bb_pwd + ' --request GET --header "Accept: application/json"'
output = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=DETACHED_PROCESS).communicate()
datastore = json.loads(output[0].decode("utf-8"))
size = datastore.get("size")
values = datastore.get("values")
if(len(values)) == 0:
    break
for repos in range(size):
    repo_name = values[repos]["values"]["slug"]
    f_initial = open (base_dir+"\\repositoryList.txt", "a+")
    f_initial.write(repo_name)
    f_initial.write("\n")
    f_initial.close()
page = page + 1

此脚本将帮助您获取项目中所有存储库的列表,并将其写入文件 repositoryList.txt 下


推荐阅读