从Github API中查找用户的总贡献

Ank*_*itG 12 github github-api

我试图从github提取任何用户的以下信息.

在此输入图像描述

github-api中是否有一种方式/ api 可以直接获取此信息?

小智 15

要加载包含所有贡献的 svg,您可以在 html 页面中使用此代码

<img src="https://ghchart.rshah.org/username" alt="Name Your Github chart">
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

要自定义颜色,你可以这样做

 <img src="https://ghchart.rshah.org/HEXCOLORCODE/username" alt="Name Your Github chart">
Run Code Online (Sandbox Code Playgroud)

十六进制颜色代码 = 17A2B8 在此输入图像描述


Mei*_*eiK 11

2019 年的答案,使用GitHub API V4

先去GitHub申请token:https : //help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line。第 7 步,仅选择范围read:user

卷曲

curl -H "Authorization: bearer token" -X POST -d '{"query":"query {\n  user(login: \"MeiK2333\") {\n    name\n    contributionsCollection {\n      contributionCalendar {\n        colors\n        totalContributions\n        weeks {\n          contributionDays {\n            color\n            contributionCount\n            date\n            weekday\n          }\n          firstDay\n        }\n      }\n    }\n  }\n}"}' https://api.github.com/graphql
Run Code Online (Sandbox Code Playgroud)

JavaScript

curl -H "Authorization: bearer token" -X POST -d '{"query":"query {\n  user(login: \"MeiK2333\") {\n    name\n    contributionsCollection {\n      contributionCalendar {\n        colors\n        totalContributions\n        weeks {\n          contributionDays {\n            color\n            contributionCount\n            date\n            weekday\n          }\n          firstDay\n        }\n      }\n    }\n  }\n}"}' https://api.github.com/graphql
Run Code Online (Sandbox Code Playgroud)


Ber*_*tel 7

https://github.com/users/<USER>/contributions您可以使用URL 参数获取 svg 日历,to例如:

https://github.com/users/bertrandmartel/contributions?to=2016-12-31

您可以使用基本的 xml 解析器来汇总 svg 的所有贡献。

2016 年的示例:

curl -s "https://github.com/users/bertrandmartel/contributions?to=2016-12-31" | \
     xmlstarlet sel -t -v "sum(//svg/g/g/rect/@data-count)"
Run Code Online (Sandbox Code Playgroud)


HaN*_*riX 5

您可以使用github events api来实现:

示例(node.js)

const got = require('got')

async function getEvents(username) {
  const events = []
  let page = 1

  do {
    const url = `https://api.github.com/users/${username}/events?page=${page}`
    var { body } = await got(url, {
      json: true
    })
    page++
    events.push(...body)
  } while(!body.length)

  return events
}

(async () => {
  const events = await getEvents('handtrix')

  console.log('Overall Events', events.length)
  console.log('PullRequests', events.filter(event => event.type === 'PullRequestEvent').length)
  console.log('Forks', events.filter(event => event.type === 'ForkEvent').length)
  console.log('Issues', events.filter(event => event.type === 'IssuesEvent').length)
  console.log('Reviews', events.filter(event => event.type === 'PullRequestReviewEvent').length)
})()

Run Code Online (Sandbox Code Playgroud)

示例(javascript)

async function getEvents(username) {
  const events = []
  let page = 1

  do {
    const url = `https://api.github.com/users/${username}/events?page=${page}`
    var body = await fetch(url).then(res => res.json())
    page++
    events.push(...body)
  } while(!body.length)

  return events
}

(async () => {
  const events = await getEvents('handtrix')

  console.log('Overall Events', events.length)
  console.log('PullRequests', events.filter(event => event.type === 'PullRequestEvent').length)
  console.log('Forks', events.filter(event => event.type === 'ForkEvent').length)
  console.log('Issues', events.filter(event => event.type === 'IssuesEvent').length)
  console.log('Reviews', events.filter(event => event.type === 'PullRequestReviewEvent').length)
})()
Run Code Online (Sandbox Code Playgroud)

文档

  • 使用此功能时请记住,它仅返回过去 90 天的数据 (7认同)

Que*_*est 5

是的,您可以使用新的 graphql API 轻松完成此操作

查看资源管理器:https : //developer.github.com/v4/explorer/

在那里您可以看到作为用户边缘的贡献集合。您可以获得重建日历所需的所有信息。

我已经包含了一个完整的示例,并且资源管理器文档可以进一步指导您。

专门回答你的问题,这query.user.contributionsCollection.contributionsCalendar.totalContributions 就是你要找的

继续并将以下内容复制/粘贴到资源管理器中,您将看到我去年的贡献历史

query { 
  user(login: "qhenkart") {
    email
    createdAt
    contributionsCollection(from: "2019-09-28T23:05:23Z", to: "2020-09-28T23:05:23Z") {
      contributionCalendar {
        totalContributions
        weeks {
          contributionDays {
            weekday
            date 
            contributionCount 
            color
          }
        }
        months  {
          name
            year
            firstDay 
          totalWeeks 
          
        }
      }
    }
  }
  
}
Run Code Online (Sandbox Code Playgroud)


Sim*_*mon 0

您可以使用此函数提取去年(客户)的贡献:

function getContributions(){
    const svgGraph = document.getElementsByClassName('js-calendar-graph')[0];
    const daysRects = svgGraph.getElementsByClassName('day');
    const days = [];

    for (let d of daysRects){
        days.push({
           date: d.getAttribute('data-date'),
           count: d.getAttribute('data-count')
        }); 
    }

    return days;    
}
Run Code Online (Sandbox Code Playgroud)

我还编写了一个小节点模块,可以“提取”贡献
@simonwep/github-contributions

也许这会对你有帮助(即使我晚了四年)