Home  >  Q&A  >  body text

How do I list open PRs across all repositories in my organization using octokit?

<p>I want to write a script in Javascript to query all open github PRs in all repositories in my organization. I can do this in the browser using this URL: <code>https://my.github.server/pulls?q=is:open is:pr org:my-org-name</code>. </p> <p>But using octokit, I need to provide the name of the repository to search. It looks like the github API requires it as well, but as I said, the URL above doesn't provide the repository name, but it works just fine. </p> <p>The document also has <code>/repos</code> at the beginning, but mine doesn't have it above. I can't find the one I'm using in the github API documentation. If I try <code>octokit.request( 'GET /pulls?q=...' )</code> as above, I get a 404. </p> <p>I'm sure there is a way to list the repositories and run the above search on each one, but I have dozens of repositories so this will probably be much slower. Is there any way to do it in one request? </p>
P粉966335669P粉966335669383 days ago556

reply all(2)I'll reply

  • P粉107991030

    P粉1079910302023-09-02 16:21:43

    It does support filtering by organization. use:

    await octokit.request("GET /search/issues", {
        q: `is:pr is:open org:ORGANIZATION`,
    });
    

    reply
    0
  • P粉713866425

    P粉7138664252023-09-02 13:30:20

    There is no direct way to use GitHub's API or Octokit to get all open PRs in all repositories within an organization in a single request. The Search API can search for PRs, but does not support filtering by organization.

    You can get a list of all repositories in your organization and get all pull requests for each repository using the repository list.

    Example:

    const { Octokit } = require("@octokit/core");
    
    const octokit = new Octokit({ auth: `your_auth_token` });
    
    async function fetchAllRepos(org) {
        const repos = [];
        let page = 1;
        while (true) {
            const result = await octokit.request('GET /orgs/{org}/repos', {
                org: org,
                type: 'public',
                per_page: 100,
                page: page
            });
    
            if (result.data.length === 0) break;
            repos.push(...result.data);
            page++;
        }
        return repos;
    }
    
    async function fetchAllPRs(org) {
        const repos = await fetchAllRepos(org);
    
        const prPromises = repos.map(repo =>
            octokit.request('GET /repos/{owner}/{repo}/pulls', {
                owner: org,
                repo: repo.name,
                state: 'open'
            })
        );
    
        const prResults = await Promise.all(prPromises);
        const prs = prResults.flatMap(result => result.data);
        return prs;
    }
    
    fetchAllPRs('my-org-name')
        .then(prs => console.log(prs))
        .catch(err => console.error(err));

    Not sure how slow this would be in your case. Anyway, I hope this helps.

    reply
    0
  • Cancelreply