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

icnyk63a  于 2023-04-19  发布在  Java
关注(0)|答案(2)|浏览(110)

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

gcxthw6b

gcxthw6b1#

你可以使用search commit API。它足够灵活,可以通过不同的限定词进行搜索,比如提交者用户名、作者、仓库、组织等。
请注意,运行搜索有时间限制,如果超过时间限制,API将返回在超时之前已经找到的匹配项,并且响应的incomplete_results属性设置为true,请在此处了解更多信息
下面是一个使用Octokit在GitHub org中搜索test字符串的示例
| 搜索GitHub提交消息 |在本地运行x1c 0d1x|
| --------------|--------------|

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}`);
pftdvrlh

pftdvrlh2#

阅读这个: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": "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"
      }

如果您检查响应,您可以看到消息列,也许这会有所帮助。

相关问题