0

Currently I am using the below API to get the pull requests raised for a branch.

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

I need to get all the pull requests created based on createdDate. Does bitbucket provide any API?

Currently I am checking the created date and filtering it.

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

Any other better way to do it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
PGS
  • 1,046
  • 2
  • 17
  • 38
  • Your `is_last_page` variable seems poorly named - `is_last_page` is logically false until you get to the last page, at which point it becomes true. The code would still work, it might just be confusing to someone else reading it. A simpler approach might be to do away with and run an infinite loop instead, `while True`, then `if result['isLastPage']: break` – daveruinseverything May 15 '18 at 10:26

2 Answers2

3

You can't filter by creation date. You can find the full REST API documentation for pull requests here

What you are doing can be improved though because you are ordering the pull requests by creation date. Once you find a pull request that was created before your cut-off you can bail and not continue to page through pull requests that you know you aren't going to want.

Somtehing like this maybe (my Python is rusty though and I haven't tested this code, so apologies for any typos)

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
Kristy Hughes
  • 586
  • 1
  • 6
  • 10
0

Short answer: no, that API resource does not provide a built in date filter. You'd need to apply whatever other filters (if any) are relevant (e.g. the branch involved, direction, state, etc.) and then apply any further desired filtering in your own code logic.

Do you have sample code that you're using to page through the API? If you have a code snippet you can share perhaps we can help you achieve your requirements

daveruinseverything
  • 4,775
  • 28
  • 40
  • Thanks for your comment. I have updated my sample code for filtering the date. Could you please check it – PGS May 14 '18 at 15:01