Home > Back-end >  gh API - filter Pull Requests older then certain days ago
gh API - filter Pull Requests older then certain days ago

Time:12-05

Is there a way to filter ids of PRs based on the timeago information when they were created/updated: ie. 1 day ago? The only available field in json is createdAt/updatedAt which has following format: 2022-11-29T12:59:17Z I would like to list PRs older than certain amount of days.

I am using command

gh pr list

The output should be ids of the PRs like this:

MDExOlB1bGxSZXF1ZXN0NTE1OOI4Jg==
MDExOlB1bGxSZXF1ZXN0NTE1OOI4Jg==

CodePudding user response:

To filter pull requests on GitHub based on their age, you can use the state and created_at parameters in the /pulls endpoint of the GitHub API. The state parameter can be used to specify whether you want to include open or closed pull requests, while the created parameter can be used to filter pull requests based on the date they were created.

Here is an example of how you can use these parameters to filter pull requests that are older than 30 days:

GET /repos/{owner}/{repo}/pulls?state=all&created_at:<=2022-11-01T00:00:00Z

In this example, the state parameter is set to all to include both open and closed pull requests, and the created_at parameter is set to <=2022-11-01T00:00:00Z to filter pull requests that were created before November 1, 2020. This will return a list of all pull requests that are older than 30 days.

You can adjust the created_at parameter as needed to filter pull requests based on their age.

Keep in mind that the GitHub API has strict rate limiting, so you may need to use pagination to retrieve all of the pull requests that match your filter criteria. You can use the page and per_page parameters to control pagination, as well as the Link header in the API response to navigate to the next or previous page of results.

  • Related