【问题标题】:Finding total contributions of a user from Github API从 Github API 查找用户的总贡献
【发布时间】:2013-08-18 05:01:17
【问题描述】:

我正在尝试从 github 为任何用户提取以下信息。

github-api 中是否有公开的方式/api,我们可以直接获取这些信息?

【问题讨论】:

    标签: github github-api


    【解决方案1】:

    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

    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
    

    JavaScript

    async function getContributions(token, username) {
        const headers = {
            'Authorization': `bearer ${token}`,
        }
        const body = {
            "query": `query {
                user(login: "${username}") {
                  name
                  contributionsCollection {
                    contributionCalendar {
                      colors
                      totalContributions
                      weeks {
                        contributionDays {
                          color
                          contributionCount
                          date
                          weekday
                        }
                        firstDay
                      }
                    }
                  }
                }
              }`
        }
        const response = await fetch('https://api.github.com/graphql', { method: 'POST', body: JSON.stringify(body), headers: headers })
        const data = await response.json()
        return data
    }
    
    const data = await getContributions('token', 'MeiK2333')
    console.log(data)
    

    【讨论】:

    • 仅供参考:如果您只想知道认证用户的总贡献,{"query": "query {viewer {contributionsCollection {contributionCalendar {totalContributions}}}}"} 应该足够了。
    • 你能解释一下为什么你使用方法POST,不应该是GET吗?
    • @Maverick 这是 POST b/c graphQL 正在用于此请求。看到这个链接 - stackoverflow.com/questions/59162265/…
    【解决方案2】:

    是的,您可以使用新的 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 
              
            }
          }
        }
      }
      
    }
    

    【讨论】:

      【解决方案3】:

      您可以为此使用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)
      })()
      
      

      示例(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)
      })()
      

      文档

      【讨论】:

      • 使用时请记住,它只返回过去 90 天的数据
      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

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

        <img src="https://ghchart.rshah.org/username" alt="Name Your Github chart">
        

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

         <img src="https://ghchart.rshah.org/HEXCOLORCODE/username" alt="Name Your Github chart">
        

        HEXCOLORCODE = 17A2B8

        【讨论】:

        • 链接现在好像坏了。
        【解决方案6】:

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

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

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

        也许这会对你有所帮助(即使我迟到了 4 年)

        【讨论】:

          【解决方案7】:

          我相信您可以在 Code Climate 的 Velocity git 分析中看到某个时间范围内的贡献计数以及其他个人贡献分析,您可以在此处请求访问:https://go.codeclimate.com/velocity-free-for-teams

          【讨论】:

            【解决方案8】:

            如果您更喜欢npm 包,可以试试这个。 https://github.com/SammyRobensParadise/github-contributions-counter#readme

            您可以获得所有时间的捐款或每年的捐款。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-11-18
              • 1970-01-01
              • 1970-01-01
              • 2014-01-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-01-08
              相关资源
              最近更新 更多