我如何找到我在 github api 上搜索的提交?

问题描述 投票:0回答:2

我正在开发 github 的扩展。我需要搜索一个提交。我正在使用 github api,首先我尝试获取 api 上的所有提交,但每页的最大值是 100。其次我尝试了

https://api.github.com/repos/{owner}/{repo}/commits?q={commit_message}
但它不起作用。那么我怎样才能在 github api 中找到我正在寻找的提交呢?

javascript github fetch github-api
2个回答
6
投票

您可以使用搜索提交API。它足够灵活,可以按不同的限定符进行搜索,例如提交者用户名、作者、存储库、组织等。

请考虑到,运行搜索有时间限制,如果超过时间限制,API 将返回超时之前已找到的匹配项,并且响应将 incomplete_results 属性设置为 true,阅读更多相关信息这里

这是一个使用 Octokit 的示例,它在 GitHub org 内搜索 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}`);


0
投票

阅读此内容:GitHub API 文档 在原始文档中,它说使用 get

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": "[email protected]",
        "date": "2011-04-14T16:00:49Z"
      },
      "committer": {
        "name": "Monalisa Octocat",
        "email": "[email protected]",
        "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

中发现了这一点
© www.soinside.com 2019 - 2024. All rights reserved.