javascript - 获取已提交特定文件的贡献者列表

标签 javascript github-api

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
  • 也可以在文件提交中看到贡献者 Github displays contributor of a file

应该可以

  • 所有这些让我觉得应该也可以通过 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 参数值的端点:

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"));
  })

关于javascript - 获取已提交特定文件的贡献者列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46762160/

相关文章:

javascript - ZeroClipboard/zlip 在 iframe 中导致不安全的 Javascript 警告

github - 检查用户是否在指定日期使用 API 提交了 Github

python - 处理 REST API 的不同响应数据类型

javascript - 选择一个选项时的 jquery addClass 和 removeClass

javascript - JavaScript 中的反射

javascript - 功能适用于移动Safari

jenkins - 如何更新 PR 合并提交的构建状态

git - 如何使用Github Release API 发布没有源代码的版本?

GitHub Api 下载 zip 或 tarball 链接

javascript - 悬停时的 CSS 动画