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.
我们从..开始
- 我有 list of contributors
- 我可以在 github 网站上使用
?author=
过滤贡献者的提交,例如 this - 也可以在文件提交中看到贡献者
应该可以
- 所有这些让我觉得应该也可以通过 API 找到文件的贡献者。
Problem 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应该返回 Pratik855
*编辑
我找到了这个 SO answer但这不是我想要的。虽然提出了所有要求,但我不确定 https://api.github.com/repos/csitauthority/csitauthority.github.io/commits?=README
是如何转换为 https://api.github.com/repos/csitauthority/csitauthority.github.io/commits?=HUGO/content/page/vlan-101.md
基于https://github.com/csitauthority/CSITauthority.github.io/blob/master/HUGO/content/post/vlan-101.md
因为HUGO只能生成第三种规范的URL。
我正在使用
- 雨果
- Github 页面
最佳答案
要获取存储库中特定文件的所有贡献者的完整数据,请调用
List commits on a repository以文件的 repo 路径作为 path
参数值的端点:
即一般形式为:
GET /repos/:owner/:repo/commits?path=:path-to-file
这将返回一个 JSON 对象,其中包含该文件的所有提交的数组。要从每个人那里获取贡献者姓名,您可以选择使用 commit.author.name
或 commit.committer.name
(取决于您实际想要的)或author.login
或 committer.login
。
所以这是一个单一的 API 调用——但是为了只获取名称,处理你返回的 JSON 数据。
这是一个在 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)
}
})
下面是一个如何跳过任何重复名称并以一组唯一名称结尾的示例:
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"));
})
关于javascript - 获取已提交特定文件的贡献者列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46762160/