如何在Github API的分页中获取用户回购中的问题数量?

时间:2019-09-14 11:18:38

标签: reactjs github-api

所以我的任务是从用户的存储库中获取所有问题。

为此,我正在这样做

axios.get(`https://api.github.com/repos/${user}/${repo}/issues?state=all`)
      .then((response) => {
        console.log(response);
      })
      .catch((err) => console.log(err));
  };

问题是我还需要对这些问题进行分页,但是response没有有关问题数量的任何信息。它只有一系列问题。我认为只有30个头衔

我试图为此使用不同的路径,例如搜索一个

axios.get(`https://api.github.com/search/issues?q=${user}+${repo}&per_page=10`)

尽管它确实返回总计数,但是它给了我完全不同的结果问题。

所有帮助将不胜感激。

为了轻松地进行api请求,您可以将Facebook用作用户参数,将React用作回购参数

2 个答案:

答案 0 :(得分:0)

好像github提供了一个Link标头,它将为您提供该信息: https://developer.github.com/v3/#pagination

  

链接标头包含分页信息:

Link: <https://api.github.com/user/repos?page=3&per_page=100>; rel="next",
  <https://api.github.com/user/repos?page=50&per_page=100>; rel="last"

答案 1 :(得分:0)

示例react js repo: https://api.github.com/repos/facebook/react/issues 分页: https://api.github.com/repos/facebook/react/issues?page=2

阅读docs了解更多选项

axios.get(`https://api.github.com/repos/${user}/${repo}/issues?page=${page}&per_page=10`)
   .then((response) => {
     console.log(response);
   })
   .catch((err) => console.log(err));
};

带有GraphQL的v​​4

阅读此article

GraphQL查询:

query { 
  repository(owner:"facebook", name:"react") { 
    issues(states:OPEN) {
      totalCount
    }
  }
}