【问题标题】:Is there any way to get all the pull request based on creation date有什么方法可以根据创建日期获取所有拉取请求
【发布时间】:2018-05-14 07:11:12
【问题描述】:

目前我正在使用以下 API 来获取分支的拉取请求。

https://stash.net/rest/api/1.0/projects/{}/repos/{}/pull-requests?
at=refs/heads/release-18&state=ALL&order=OLDEST&withAttributes=false
&withProperties=true&limit=100

我需要获取基于 createdDate 创建的所有拉取请求。 bitbucket 是否提供任何 API?

目前我正在检查创建日期并对其进行过滤。

def get_pullrequest_date_based():
    """
    Get all the pull requests raised and filter the based on date
    :return: List of pull request IDs
    """
    pull_request_ids = []
    start = 0
    is_last_page = True
    while is_last_page:
        url = STASH_REST_API_URL + "/pull-requests?state=MERGED&order=NEWEST&withAttributes=false&withProperties=true" + "/results&limit=100&start="+str(start)
        result = make_request(url)
        pull_request_ids.append([value['id'] for value in result['values'] if value['createdDate'] >= date_arg])
        if not result['isLastPage']:
            start += 100
        else:
            is_last_page = False
        print "Size :",len(pull_request_ids)
    return pull_request_ids

任何其他更好的方法。

【问题讨论】:

  • 您的 is_last_page 变量似乎命名不当 - is_last_page 在逻辑上为假,直到您到达最后一页,此时它变为真。该代码仍然可以工作,它可能只是让其他阅读它的人感到困惑。更简单的方法可能是取消并运行一个无限循环,while True,然后是if result['isLastPage']: break

标签: pull-request bitbucket-server


【解决方案1】:

您无法按创建日期进行过滤。您可以找到拉取请求的完整 REST API 文档here

您正在做的事情可以得到改进,因为您是按创建日期排序拉取请求。一旦您发现在您的截止日期之前创建的拉取请求,您可以保释并且不再继续翻阅您知道自己不会想要的拉取请求。

可能是这样的(虽然我的 Python 生锈了,我还没有测试过这段代码,所以对于任何拼写错误表示歉意)

def get_pullrequest_date_based():
    """
    Get all the pull requests raised and filter the based on date
    :return: List of pull request IDs
    """
    pull_request_ids = []
    start = 0
    is_last_page = True
    past_date_arg = False
    while is_last_page and not past_date_arg:
        url = STASH_REST_API_URL +     "/pull-requests?state=MERGED&order=NEWEST&withAttributes=false&withProperties=true" +     "/results&limit=100&start="+str(start)
        result = make_request(url)
        for value in result['values']:
            if value['createdDate'] >= date_arg:
                pull_request_ids.append(value['id'])
            else:
                # This and any subsequent value is going to be too old to care about
                past_date_arg = True
        if not result['isLastPage']:
            start += 100
        else:
            is_last_page = False
        print "Size :",len(pull_request_ids)
    return pull_request_ids

【讨论】:

    【解决方案2】:

    简短回答:不,该 API 资源不提供内置日期过滤器。您需要应用任何其他相关的过滤器(如果有)(例如涉及的分支、方向、状态等),然后在您自己的代码逻辑中应用任何进一步所需的过滤。

    您是否有用于通过 API 进行分页的示例代码?如果您有代码 sn-p 可以分享,也许我们可以帮助您实现您的要求

    【讨论】:

    • 感谢您的评论。我更新了用于过滤日期的示例代码。你能检查一下吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2018-01-11
    • 2019-01-03
    相关资源
    最近更新 更多