你可以使用 Octokit 来列出组织中所有仓库中的开放式 PR。

huangapple go评论83阅读模式
英文:

How can I use octokit to list open PRs from all repos in my org?

问题

我想用JavaScript编写一个脚本来查询我组织中所有仓库中的所有打开的GitHub拉取请求。我可以在浏览器中使用以下URL来执行此操作:https://my.github.server/pulls?q=is%3Aopen+is%3Apr+org%3Amy-org-name

但是,使用octokit,我需要提供要搜索的仓库名称。看起来GitHub API也需要它,但像我所说,上面的URL没有提供仓库名称,但仍然可以正常工作。

文档中的方法还在开头加上了/repos,而我的方法没有。我在GitHub API文档中找不到我正在使用的这种方法。如果我像上面这样尝试octokit.request( 'GET /pulls?q=...' ),我会得到一个404错误。

我确信有一种方法可以列出仓库并在每个仓库上运行上面的搜索,但我有数十个仓库,所以那可能会慢得多。有没有一种方法可以通过一个请求来完成?

英文:

I would like to write a script in Javascript to query all open github PRs in all repos in my org. I can use this URL to do it in a browser: https://my.github.server/pulls?q=is%3Aopen+is%3Apr+org%3Amy-org-name.

But using octokit, I need to supply the name of the repo in which to search. It looks as if the github API also requires it, but like I said, the URL above doesn't supply a repo name and it works just fine.

The documented one also has /repos at the beginning, which mine above does not. I can't find the one I'm using anywhere in the github API docs. If I try octokit.request( 'GET /pulls?q=...' ) as above, I get a 404.

I'm sure there's a way to list the repos and run the above search on each one, but I have dozens of repos, so that's likely to be much slower. Is there a way to do it in one request?

答案1

得分: 1

在GitHub的API或Octokit中,没有直接的方法可以在单个请求中获取组织中所有存储库的所有打开的PR。搜索API可以搜索PR,但不支持按组织进行筛选。

您可以获取组织中所有存储库的列表,然后使用存储库列表来获取每个存储库的所有拉取请求。

示例:

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));

不确定这在您的情况下会有多慢。但希望这能帮助您。

英文:

There is no direct way to fetch all open PRs across all repositories within an organization in a single request using GitHub's API or Octokit. The Search API can search for PRs but it doesn't support filtering by organization.

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

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 will be in your case. I hope this helps anyway.

答案2

得分: 1

它支持按组织进行筛选。使用:

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

It does support filtering by org. Use:

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

huangapple
  • 本文由 发表于 2023年6月22日 08:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定