Reputation: 93
I am developing an extension for github. I need search a commit. I'm using github api, firstly i tried get all commits on api but max value of perpage is 100. Secondly i tried https://api.github.com/repos/{owner}/{repo}/commits?q={commit_message}
but it doesn't work. So how can I find a commit i'm looking for in github api?
Upvotes: 3
Views: 1544
Reputation: 1196
You can use the search commit API. It's flexible enough to search by different qualifiers, like committer username, author, repository, organization, etc.
Take into account, there is a time limit for running your search, in case you exceed the time limit, the API returns the matches that were already found prior to the timeout, and the response has the incomplete_results property set to true, read more about it here
Here's an example using Octokit that searches inside GitHub org the test string
Search GitHub commit message | Run locally |
---|
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}`);
Upvotes: 6
Reputation: 41
Read this: GitHub API Docs
In the original documentation it says to use get get /repos/{owner}/{repo}/commits
. The error may be caused by this. You should go through the documentation again.
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"
}
If you examine the Respons you can see the message column, maybe this will help. I also found this in another question GET https://api.github.com/repos/izuzak/pmrpc/commits?path=examples&page=1&per_page=1
Upvotes: 0