PGS
PGS

Reputation: 1194

Is there any way to get all the pull request based on creation date

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.

Upvotes: 0

Views: 3846

Answers (2)

Kristy Hughes
Kristy Hughes

Reputation: 596

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

Upvotes: 3

daveruinseverything
daveruinseverything

Reputation: 5187

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

Upvotes: 0

Related Questions