【发布时间】:2017-09-01 18:22:12
【问题描述】:
Github API 文档向filter repositories by topics 提供说明。 有没有办法使用 API 从特定 repo 中获取主题?
【问题讨论】:
标签: github repository github-api
Github API 文档向filter repositories by topics 提供说明。 有没有办法使用 API 从特定 repo 中获取主题?
【问题讨论】:
标签: github repository github-api
我不知道有什么方法可以只获取存储库的主题,但是如果您执行get for a repository,则返回的存储库 json 对象将具有一个主题属性,该属性是该存储库的数组主题。
在该文档页面的顶部,您会注意到为了返回主题,您需要在您的 GET 请求中添加特定的标头:"Accept":"application/vnd.github.mercy-preview+json"
希望这会有所帮助!
【讨论】:
您可以使用Github GraphQL API 来做到这一点
查询:
{
repository(owner: "twbs", name: "bootstrap") {
repositoryTopics(first: 10) {
edges {
node {
topic {
name
}
}
}
}
}
}
这将返回前 10 个主题和每个主题的名称,如下所示。
回应:
{
"data": {
"repository": {
"repositoryTopics": {
"edges": [
{
"node": {
"topic": {
"name": "css"
}
}
},
{
"node": {
"topic": {
"name": "bootstrap"
}
}
},
{
"node": {
"topic": {
"name": "javascript"
}
}
},
{
"node": {
"topic": {
"name": "html"
}
}
}
]
}
}
}
}
在GitHub GraphQL Explorer中测试一下
【讨论】:
我遇到了类似的问题,所以我做了一个节点模块,只需要一行代码就可以做到
var github_topics = require('github-topics');
var topics = github_topics.gettopics('url_of_repo');
例如
var topics = github_topics.gettopics('https://github.com/Aniket965/blog');
它将返回该github存储库的主题数组,该节点模块的链接是NPM
【讨论】:
您可以使用 Github API 轻松完成此操作(目前处于“预览模式”):
curl -H "Accept: application/vnd.github.mercy-preview+json" https://api.github.com/repos/twbs/bootstrap/topics
{
"names": [
"css",
"bootstrap",
"javascript",
"html",
"jekyll-site",
"scss",
"css-framework",
"sass"
]
}
您需要包含额外的标头Accept: application/vnd.github.mercy-preview+json。
有一个“但是”,因为它处于“预览模式”,所以不支持生产使用(请阅读下面链接中的“注意”和“警告”部分)。
另见:
【讨论】:
我使用 Accept Headers 添加了 fetch:
fetch("https://api.github.com/users/lucksp/repos",
{
method: "GET",
headers: {
Accept: "application/vnd.github.mercy-preview+json"
}
})
【讨论】: