首页 > 解决方案 > 有什么方法可以根据创建日期获取所有拉取请求

问题描述

目前我正在使用下面的 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

任何其他更好的方法来做到这一点。

标签: pull-requestbitbucket-server

解决方案


您不能按创建日期过滤。您可以在此处找到拉取请求的完整 REST API 文档

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

可能是这样的(虽然我的 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

推荐阅读