Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Last active September 5, 2024 05:09
Show Gist options
  • Save kentcdodds/c7d19944d667164a2d8ed3424164a3c0 to your computer and use it in GitHub Desktop.
Save kentcdodds/c7d19944d667164a2d8ed3424164a3c0 to your computer and use it in GitHub Desktop.
Calculate your total lifetime github contributions
// ⚠️ warning: This script was written by ChatGPT, not entirely reviewed, and is completely unmodified
// This counts your lifetime contributions so you can decide
// whether to block Twitter: https://x.com/mjackson/status/1831554887706169674
const GITHUB_API_URL = 'https://api.github.com/graphql'
// scope needed: "user:read" for public contributions and "repo" for private
// https://github.com/settings/tokens/new
const GITHUB_TOKEN = process.env.GITHUB_TOKEN
async function fetchGraphQL(query, variables = {}) {
const response = await fetch(GITHUB_API_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
query,
variables
})
})
const responseBody = await response.json()
if (responseBody.errors) {
throw new Error(JSON.stringify(responseBody.errors, null, 2))
}
return responseBody.data
}
async function getCreatedAt() {
const query = `
{
viewer {
createdAt
}
}
`
const data = await fetchGraphQL(query)
return new Date(data.viewer.createdAt)
}
async function getTotalCommitsForPeriod(from, to) {
const query = `
query($from: DateTime!, $to: DateTime!) {
viewer {
contributionsCollection(from: $from, to: $to) {
totalCommitContributions
}
}
}
`
const variables = { from: from.toISOString(), to: to.toISOString() }
const data = await fetchGraphQL(query, variables)
return data.viewer.contributionsCollection.totalCommitContributions
}
async function getTotalCommits() {
const createdAt = await getCreatedAt()
const now = new Date()
let totalCommits = 0
let from = createdAt
while (from < now) {
const to = new Date(Math.min(from.getTime() + 365 * 24 * 60 * 60 * 1000, now.getTime())) // 1 year chunk or up to now
const commits = await getTotalCommitsForPeriod(from, to)
totalCommits += commits
from = new Date(to.getTime() + 1) // Move to the day after the current `to` date
}
console.log(`Total commits from ${createdAt.toISOString()} to now: ${totalCommits}`)
}
getTotalCommits().catch(err => console.error(err))
@kentcdodds
Copy link
Author

kentcdodds commented Sep 5, 2024

𝕏 says @kentcdodds has 150k posts. This script's output for me:

Total commits from 2012-03-04T22:32:01.000Z to now: 13177

😱 Goodbye 𝕏!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment