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

使用Python从Bitbucket提取源(分支)列表,python,bitbucket,bitbucket-api,Python,Bitbucket,Bitbucket Api,我是python新手,正在尝试更新其他人的代码 我需要从Bitbucket中提取一个源(分支)列表,以允许用户从该列表中进行选择。现有代码成功地使用URL请求从项目和存储库的Bitbucket中检索列表,但我找不到访问源位置的方法,无法将默认的“Master”更改为用户选择的分支。作为参考,此代码摘录用于提取存储库信息: @app.route("/initial3" , methods=['GET', 'POST']) def initial3(): selected_git_proje

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

我需要从Bitbucket中提取一个源(分支)列表,以允许用户从该列表中进行选择。现有代码成功地使用URL请求从项目和存储库的Bitbucket中检索列表,但我找不到访问源位置的方法,无法将默认的“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 = 'https://git.ourcompanyname.com/rest/api/1.0/projects/'+project+'/repos/'+repository+'/files?limit=10000&at='+branch
我认为我可以重复使用相同的代码,但要使用修改过的URL(docs.atlassian上的一些引用似乎表明这是可行的):


如果您有任何建议,我将不胜感激——我第一次看python是在两天前。

如果您不必通过http请求来完成,我建议您使用GitPython库。您可以使用它访问任何存储库


经过大量的尝试和错误,我发现了有效的语法。分支引用应用于HTTP请求的末尾

检索分支信息:

@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 = '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

如果不想使用GitPython库,我真的需要在依赖http请求的现有代码的基础上进行构建。您可以查看它是如何构建的,并模仿该方法<代码>https://github.com/gitpython-developers/GitPython或浏览Bitbucket API。希望其他人会有更具体的答案。