【问题标题】:Get list of contributors who have made commits to a particular file获取已提交特定文件的贡献者列表
【发布时间】:2021-12-01 19:13:39
【问题描述】:

我正在寻找获取提交的作者/贡献者的方法。我对 github-api 真的很陌生,this 给我带来的麻烦比我想象的要多。

我们从……开始。

  • 我有list of contributors
  • 我可以在 github 网站上使用 ?author= 过滤贡献者的提交,例如 this
  • 也可以在文件提交中看到贡献者

应该可以

  • 所有这些让我觉得应该也可以通过 API 找到文件的贡献者。

问题描述

如果我有文件 such as this 的 URL,是否有一个 github API 可以显示对该文件进行提交的贡献者列表?

或者,我是否需要使用多个 API 调用的结果,例如(例如)

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://github.com/csitauthority/CSITauthority.github.io/blob/master/HUGO/content/post/vlan-101.md 转换为 https://api.github.com/repos/csitauthority/csitauthority.github.io/commits?=HUGO/content/page/vlan-101.md,因为 HUGO 只能生成第三种规范 URL。


我正在使用

  • 雨果
  • Github 页面

【问题讨论】:

    标签: javascript github-api


    【解决方案1】:

    要获取存储库中特定文件的所有贡献者的完整数据,请调用
    List commits on a repository 端点,并将文件的存储库路径作为path 参数的值:

    https://api.github.com/repos/csitauthority/CSITauthority.github.io/commits?path=HUGO/content/post/vlan-101.md

    即一般形式为:

    GET /repos/:owner/:repo/commits?path=:path-to-file
    

    这将返回一个 JSON 对象,其中包含该文件的所有提交的数组。要从每个人那里获取贡献者姓名,您可以选择使用 commit.author.namecommit.committer.name(取决于您实际需要哪一个)或 author.logincommitter.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"));
      })

    【讨论】:

    • 当我针对 const filepath = "HUGO/content/post/grandfather-problem.md" 运行它时,它会返回同一个提交者两次。如何过滤掉唯一的提交者?谢谢!
    • 有关如何跳过重复项并最终获得一组唯一名称的示例,请参阅更新的答案。
    • 哇,这是一个非常巧妙的解决方案!为什么我没有想到呢? xD 我花了所有时间查看文档以获取提交者列表(不是提交),就像它在 gh-dashboard 中显示的那样。非常感谢! +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 2015-11-16
    • 2018-09-24
    • 2012-07-29
    • 2016-11-24
    • 2011-09-13
    相关资源
    最近更新 更多