我如何在 GitHub API 上查找我搜索的提交记录?

3

我正在为Github开发一个扩展程序,需要搜索一个提交(commit)。我使用Github API进行开发。首先,我尝试从API获取所有提交,但每页最大值是100。其次,我尝试了https://api.github.com/repos/{owner}/{repo}/commits?q={commit_message},但它不起作用。那么我该如何在Github API中找到我要寻找的提交(commit)呢?

2个回答

6
你可以使用搜索提交API。它足够灵活,可以通过不同的限定符进行搜索,比如提交者用户名、作者、仓库、组织等。
请注意,运行搜索有一个时间限制,如果超过时间限制,API会返回在超时之前已经找到的匹配项,并且响应中的incomplete_results属性设置为true,了解更多信息,请点击这里
下面是一个使用Octokit的示例,它在GitHub组织中搜索test字符串。
搜索 GitHub 提交信息 本地运行 
const text = 'test';
const org = 'github';
const query =`${text} org:${org}`;
const searchResult = await github.rest.search.commits({
  q: query
});
  
// In case there is a timeout running the query, you can use incomplete_results to check if there are likely more results
// for your query, read more here https://docs.github.com/en/rest/reference/search#timeouts-and-incomplete-results
const { items, total_count} = searchResult.data;
console.log(items.map(commit => commit.commit));
console.log(`Found ${total_count} commits for ${text} at GitHub org called ${org}`);


谢谢,那个可行。 - Furkan Karakuzu

0
请阅读:GitHub API文档 在原始文档中,它说要使用 get /repos/{owner}/{repo}/commits。错误可能是由此引起的。您应该重新阅读文档。
await octokit.request('GET /repos/{owner}/{repo}/commits', {
  owner: 'octocat',
  repo: 'hello-world'
})

[
  {
    "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
    "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
    "node_id": "MDY6Q29tbWl0NmRjYjA5YjViNTc4NzVmMzM0ZjYxYWViZWQ2OTVlMmU0MTkzZGI1ZQ==",
    "html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e",
    "comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments",
    "commit": {
      "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
      "author": {
        "name": "Monalisa Octocat",
        "email": "support@github.com",
        "date": "2011-04-14T16:00:49Z"
      },
      "committer": {
        "name": "Monalisa Octocat",
        "email": "support@github.com",
        "date": "2011-04-14T16:00:49Z"
      },
      "message": "Fix all the bugs",
      "tree": {
        "url": "https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e",
        "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e"
      }

如果您检查响应,可以看到消息列,也许这会有所帮助。我还在另一个问题中找到了这个GET https://api.github.com/repos/izuzak/pmrpc/commits?path=examples&page=1&per_page=1


它对我不起作用,因为我需要获取所有提交或按提交消息搜索而不带每页参数。 - Furkan Karakuzu

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