2

I'm using Octokit/rest js to fetch pull requests from a repository.

Ideally, I'd like to have pull requests from x date to y date depending on the creation date.

Is there a way to do this?

Right now I'm fetching all the pull requests and then iterating over them.

Ideally, I would like to passe some parameters on the request. This is what I'm doing now:

 const pullRequests = await octokit.paginate(
          '/repos/{owner}/{repo}/pulls',
          {
            owner: organization.providerLogin,
            repo: repository.name,
            state: 'all',
          },

This is what I would like to do:

 const pullRequests = await octokit.paginate(
          '/repos/{owner}/{repo}/pulls?q=createdat2022-04-01..2022-04-07',
          {
            owner: organization.providerLogin,
            repo: repository.name,
            state: 'all',
          },
Sydney C.
  • 950
  • 10
  • 21

1 Answers1

4

I would use @octokit/rest for this job with GitHub's Search API.

const { Octokit } = require("@octokit/rest");

const octokit = new Octokit();

const prs = await octokit.rest.search.issuesAndPullRequests({
  q: `type:pr+repo:${organization.providerLogin}/${repository.name}+created:>=2022-01-01`,
  per_page: 100, // use the number you like
});

console.log(prs.data.items)

I created a RunKit where you can play with: https://runkit.com/dominguezcelada/625035fc20b0c4000842fde9


You can wrap the previous code snippet with octokit.paginate() to get all the paginated results.

Octokit Docs recommend to use octokit.paginate.iterator so you can run the iterator asynchronously


Let me know if this works or not. I would be happy to help you on that if necessary.

OscarDOM
  • 716
  • 5
  • 15
  • 1
    That helps a lot thank you. This raises another concern: For this type of search, Octokit does not populate relations such as requested_reviewers. I can't find anything related to pull request relations in the GitHub search API documentation. Do you know if that's possible? – Sydney C. Apr 12 '22 at 13:20
  • 1
    For that, I think you will need the power of GraphQL. If you want, you can open a new issue or update the current one, and then I can give you a working example you can get inspired from. – OscarDOM Apr 14 '22 at 16:40