I am looking for ways to fetch the author/contributor of a commit. I'm really new to github-api and this is giving me more trouble than I imagined.
?author=
like thisProblem Description
If I have the URL of the file such as this, is there a github API that shows me the list of contributors who have made commits to that file?
Or, Do I need to use results of multiple API calls like(for instance)
I'm thinking of cross-referencing the outputs of those two^ if everything else fails.
This should return Pratik855
I found this SO answer but this is not quite what I'm looking for. While all the requirements are made, I'm unsure how https://api.github.com/repos/csitauthority/csitauthority.github.io/commits?=README
translates to https://api.github.com/repos/csitauthority/csitauthority.github.io/commits?=HUGO/content/page/vlan-101.md
based on https://github.com/csitauthority/CSITauthority.github.io/blob/master/HUGO/content/post/vlan-101.md
because HUGO can only generate the 3rd kind of canonical URL.
To get complete data on all contributors to a particular file in a repository, make a call to the
List commits on a repository endpoint with the file’s repo path as the path
parameter’s value:
That is, the general form is:
GET /repos/:owner/:repo/commits?path=:path-to-file
That’ll return a JSON object with an array of all the commits for that file. To get the contributor name from each, you have a choice of using commit.author.name
or commit.committer.name
(depending on which of those you actually want) or author.login
or committer.login
.
So it’s a single API call — but to get just the names, process the JSON data you get back.
Here’s a simple example of doing it in JavaScript:
const githubAPI = "https://api.github.com"
const commitsEndpoint = "/repos/csitauthority/CSITauthority.github.io/commits"
const commitsURL = githubAPI + commitsEndpoint
const filepath = "HUGO/content/post/vlan-101.md"
fetch(commitsURL + "?path=" + filepath)
.then(response => response.json())
.then(commits => {
for (var i = 0; i < commits.length; i++) {
console.log(commits[i].commit.author.name)
}
})
And here’s an example of how to skip any duplicate names and end with a set of unique names:
const githubAPI = "https://api.github.com"
const commitsEndpoint = "/repos/csitauthority/CSITauthority.github.io/commits"
const commitsURL = githubAPI + commitsEndpoint
const filepath = "HUGO/content/post/grandfather-problem.md"
fetch(commitsURL + "?path=" + filepath)
.then(response => response.json())
.then(commits => {
const names = [];
for (var i = 0; i < commits.length; i++) {
if (!names.includes(commits[i].commit.author.name)) {
names.push(commits[i].commit.author.name);
}
}
console.log(names.join("\n"));
})