2017-10-16 98 views
2

我正在尋找獲取提交的作者/貢獻者的方法。我對github-api非常陌生,this給了我比我想象中更多的麻煩。獲取對特定文件進行提交的貢獻者列表

我們開始用..

  • 我有list of contributors
  • 我可以?author=this
  • 這是可以看到的貢獻者過濾由貢獻者提交GitHub的網站上in file commitits Github displays contributor of a file

它應該是可能的

  • 所有這一切使我認爲它應該有可能通過API找到文件的貢獻者。

問題描述

如果我有一個文件such as this的網址,有沒有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的頁面

回答

2

您可以通過撥打電話給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")); 
 
    })

+0

當我對'const filepath =「HUGO/content/post/grandfather-problem.md」'運行它時,它返回兩次相同的提交者。我如何篩選出獨特的提交者?謝謝! –

+1

有關如何跳過重複項並以一組唯一名稱結尾的示例,請參閱最新答案。 – sideshowbarker

+0

哇,這真是一個很好的解決方案!我爲什麼沒有想到它? xD我花了我所有的時間瀏覽文檔以獲取提交者列表(而不是提交),就像它在gh-dashboard中顯示的那樣。非常感謝! +1 –