githuboctokitoctokit-js

Using Octokit to fetch pull request within a date range


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',
          },

Solution

  • 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.