Finding total contributions of a user from GitHub API

Answers for 2019, Use GitHub API V4.

First go to GitHub to apply for a token: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line. step 7, scopes select only 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)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)