Sid*_*mal 3 javascript github-api
我正在寻找获取提交的作者/贡献者的方法。我对 github-api 真的很陌生,这给我带来的麻烦比我想象的要多。
问题描述
如果我有这样的文件的 URL ,是否有 github API 可以向我显示已提交该文件的贡献者列表?
或者,我是否需要使用多个 API 调用的结果(例如)
I'm thinking of cross-referencing the outputs of those two^ if everything else fails.
这应该返回 Pratik855
我找到了这个答案,但这并不是我想要的。虽然满足了所有要求,但我不确定如何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。
要获取存储库中特定文件的所有贡献者的完整数据,请调用
存储库端点上的 List commits,并将 file\xe2\x80\x99s 存储库路径作为path参数\xe2\x80\x99s 值:
也就是说,一般形式为:
\nGET /repos/:owner/:repo/commits?path=:path-to-file\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\x99 将返回一个 JSON 对象,其中包含该文件的所有提交的数组。要获取每个贡献者的姓名,您可以选择使用commit.author.name或commit.committer.name(取决于您真正想要的)或author.login或committer.login。
因此,它\xe2\x80\x99 是一个API 调用\xe2\x80\x94,但要仅获取名称,请处理返回的JSON 数据。
\nHere\xe2\x80\x99s 是一个在 JavaScript 中执行此操作的简单示例:
\nGET /repos/:owner/:repo/commits?path=:path-to-file\nRun Code Online (Sandbox Code Playgroud)\r\n这里\xe2\x80\x99s 是一个如何跳过任何重复名称并以一组唯一名称结尾的示例:
\nconst githubAPI = "https://api.github.com"\nconst commitsEndpoint = "/repos/csitauthority/CSITauthority.github.io/commits"\nconst commitsURL = githubAPI + commitsEndpoint\nconst filepath = "HUGO/content/post/vlan-101.md"\nfetch(commitsURL + "?path=" + filepath)\n .then(response => response.json())\n .then(commits => {\n for (var i = 0; i < commits.length; i++) {\n console.log(commits[i].commit.author.name)\n }\n })Run Code Online (Sandbox Code Playgroud)\r\n| 归档时间: |
|
| 查看次数: |
2395 次 |
| 最近记录: |