使用Python从Bitbucket中提取来源(分支)列表

时间:2018-08-09 13:23:54

标签: python bitbucket bitbucket-api

我是python的新手,正在尝试更新其他人的代码。

我需要从Bitbucket中提取源(分支)列表,以允许用户从该列表中进行选择。现有代码成功地使用 URL 请求从Bitbucket中检索了项目和存储库的列表,但是我找不到一种方法来访问Source位置以从默认的“ Master”更改为用户选择的分支。作为参考,此代码摘录用于提取存储库信息:

@app.route("/initial3" , methods=['GET', 'POST'])
def initial3():
    selected_git_project = str(request.form.get('git_project'))
    selected_git_repository = str(request.form.get('git_information'))
    #checkbox_all_selection = str(request.form.get('checkbox_all'))
    confluence_information = [str(request.form.get('confluence_information'))]
    selected_page = request.form.get('page_id')
    returnlistsearch = []
    url = 'https://git.ourcompanyname.com/rest/api/1.0/projects/'+selected_git_project+'/repos/'+selected_git_repository+'/files?limit=1000'
    resources_json = requests.get(url, auth=(git_user, git_password)).json()
    resources_json_dump = (json.dumps(resources_json, indent=4, sort_keys=False))
    decoded = json.loads(resources_json_dump)
    for x in decoded['values']:
        if '.robot' in x:
            location=os.path.dirname(x)
            if location!='':
                returnlistsearch.append(location)
    returnlistsearch =remove_duplicated(returnlistsearch)
    return render_template('initial3.html',git_repository=selected_git_repository,git_project=selected_git_project ,git_information=returnlistsearch)

我认为我可以重用相同的代码,但使用经过修改的URL(docs.atlassian上的某些引用似乎表明可以使用):

url = 'https://git.ourcompanyname.com/rest/api/1.0/projects/'+selected_git_project+'/repos/'+selected_git_repository+'/files?limit=1000'

任何建议都将不胜感激-我第一次看到python是在两天前。

2 个答案:

答案 0 :(得分:0)

如果您不必通过http请求执行此操作,则建议使用GitPython库。您可以使用它来访问任何存储库。

Here is the tutorial, how to use it.

答案 1 :(得分:0)

经过反复试验,我发现了有效的语法。分支引用应用于HTTP请求的末尾。

获取分支信息:

url = 'https://git.ourcompanyname.com/rest/api/1.0/projects/'+project+'/repos/'+repository+'/files?limit=10000&at='+branch

检索分支内的文件列表:

url2 = 'https://git.ourcompanyname.com/rest/api/1.0/projects/'+project+'/repos/'+repository+'/browse/' + results + '?at='+branch
相关问题