You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于创建日期获取Bitbucket所有Pull Request?求更优方案

好问题!我来帮你梳理下Bitbucket Server(也就是你用的Stash)的API方案,以及更优的实现方式:

1. Bitbucket是否支持按createdDate筛选Pull Request的API?

当然支持!Bitbucket Server的Pull Request REST API提供了直接按日期范围过滤的参数,完全不用拉取全量数据后再本地筛选,能大幅减少请求次数和本地处理开销。

你可以用这两个核心查询参数:

  • createdStart:筛选创建时间大于等于该时间的PR,格式必须是ISO 8601标准(比如2024-01-01T00:00:00.000Z,Z代表UTC时区)
  • createdEnd:筛选创建时间小于等于该时间的PR,格式同上

举个调整后的API请求示例:

https://stash.net/rest/api/1.0/projects/{PROJECT_KEY}/repos/{REPO_SLUG}/pull-requests?at=refs/heads/release-18&state=MERGED&order=NEWEST&withAttributes=false&withProperties=true&limit=100&createdStart=2024-01-01T00:00:00.000Z
2. 优化后的代码实现

利用API的内置过滤能力,你的代码可以简化很多,而且效率更高:

def get_pullrequest_date_based(date_arg):
    """
    Get pull requests filtered by createdDate using Bitbucket API's built-in filtering
    :param date_arg: ISO 8601 formatted start date (e.g., "2024-01-01T00:00:00.000Z")
    :return: List of pull request IDs
    """
    pull_request_ids = []
    start = 0
    is_last_page = False

    while not is_last_page:
        # Build URL with createdStart parameter to let API do the filtering
        url = (
            f"{STASH_REST_API_URL}/pull-requests?"
            "state=MERGED&order=NEWEST&withAttributes=false&withProperties=true"
            f"&limit=100&start={start}&createdStart={date_arg}"
        )
        result = make_request(url)
        
        # No need for local filtering anymore - API returns only matching PRs
        pull_request_ids.extend([value['id'] for value in result['values']])
        
        is_last_page = result['isLastPage']
        if not is_last_page:
            start += 100

    print(f"Size : {len(pull_request_ids)}")
    return pull_request_ids
3. 额外的小优化建议
  • 如果你的date_arg不是ISO 8601格式,可以用Python的datetime模块转换:
    from datetime import datetime
    # 示例:把2024年1月1日转换为符合要求的格式
    date_arg = datetime(2024, 1, 1).isoformat() + "Z"
    
  • 如果需要同时筛选结束日期,只需在URL中追加&createdEnd={你的结束日期}参数即可
  • 保持分页逻辑,但因为API已经帮你过滤了数据,分页的次数会比原来少很多,性能提升明显

内容的提问来源于stack exchange,提问作者PGS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:32:21