Github API:获取Github存储库的主题

14

Github API文档提供了筛选主题的指示,搜索存储库。 有没有一种方法可以使用API从特定仓库获取主题?

5个回答

12
你可以使用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中测试


9
您可以轻松使用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
但是,由于它处于“预览模式”,不建议在生产环境中使用(请阅读下方链接中的“注意”和“警告”部分)。
参见:

请注意,此内容已不再处于预览状态:https://github.blog/changelog/2021-10-14-rest-api-preview-promotions/ - rethab

8
我不知道有什么方法可以只获取存储库的主题,但是如果您执行 get for a repository,返回的存储库JSON对象将具有一个名为topics的属性,该属性是该存储库主题的数组。
在文档页面顶部,您会注意到为了返回主题,需要在您的GET请求中添加特定的标题:"Accept":"application/vnd.github.mercy-preview+json" 希望能对您有所帮助!

2

我添加了带有Accept Headers的fetch请求:

fetch("https://api.github.com/users/lucksp/repos", 
   {
    method: "GET",
    headers: {
    Accept: "application/vnd.github.mercy-preview+json"
   }
})

2

我曾经遇到类似的问题,所以我制作了一个Node模块,只需要一行代码即可完成:

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

4
将微依赖带到一个全新的层次! ;) - Radon Rosborough

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接