Retreive所有github存储库包含在存储库Id的数组中

时间:2017-10-06 14:56:37

标签: javascript github-api

有什么办法可以将所有存储库都包含在存储库ID数组中吗?

像这样......

const githubRepoIds = [ 1,2,3 ];
fetch(`/search/repositories/q=${githubRepoIds.join(',')} in:id`)

2 个答案:

答案 0 :(得分:1)

这是一个概念验证,每个存储库ID生成1个请求(请参阅rate limiting)。

function getRepositories(ids) {
    var promises = ids.map(id => getRepository(id).catch(() => {}));
    return Promise.all(promises).then(repos => repos.filter(repo => repo !== undefined));

    function getRepository(id) {
        return new Promise((resolve, reject) => {
            const url = "https://api.github.com/repositories/" + id;
            const xhr = new XMLHttpRequest();
            xhr.open("GET", url);
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        resolve(JSON.parse(xhr.response));
                    } else {
                        reject();
                    }
                }
            };
            xhr.send();
        });
    }
}

用法:

const githubRepoIds = [ 1,2,3 ];
getRepositories(githubRepoIds).then(repos => console.log(repos));

答案 1 :(得分:0)

我认为in限定符仅允许您将搜索限制为存储库名称,描述,自述文件及其组合。请参阅:https://help.github.com/articles/searching-repositories/#scope-the-search-fields