Skip to content

Instantly share code, notes, and snippets.

@daneden
Created March 8, 2017 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daneden/2668b2771771f1546f60f4baa204e056 to your computer and use it in GitHub Desktop.
Save daneden/2668b2771771f1546f60f4baa204e056 to your computer and use it in GitHub Desktop.
GitHub Commenters
{
"presets": ["es2015"],
"plugins": ["transform-object-rest-spread"]
}

This JS snippet will find the first 2,000 comments in a given GitHub repo, then list the authors of those comments sorted by number of comments in ascending order.

Installation/setup

  1. Clone or copy-paste this gist somewhere
  2. yarn or npm install
  3. Make sure you have babel-cli globally available: npm i -g babel-cli
  4. Choose your repo and adjust line #7 of index.js as necessary
  5. Run babel-node index.js
  6. Party 🎉
// Get the commenters from a particular github repo,
// then log them to the console in ascending order
// (top commenter last)
import request from 'request-promise-native'
const repo = 'daneden/animate.css' // change me!
const commentsUri = `https://api.github.com/repos/${repo}/issues/comments?per_page=100`
const options = {
headers: {
'User-Agent': 'Request-Promise',
},
qs: {
// access_token is optional but increases the API rate limit
access_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
},
json: true,
}
const promises = [] // this will store the requests we want to get data from
const authors = {} // this will track the number of comments per username
// This will only get 2,000 comments from the repo,
// so your mileage may vary. (This also obviously generates
// 20 API requests, so beware of rate limiting. Yep, this is
// lazy and hacky, but it gets the job done)
for(let i = 0; i < 20; i++) {
promises.push(request({
...options,
uri: commentsUri + `&page=${i+1}`,
}))
}
Promise.all(promises)
// [].concat.apply([], responses) takes `responses`, an array
// of arrays, and flattens to a single array
.then(responses => [].concat.apply([], responses))
.then(comments => comments.map(comment => {
// populate the authors object with number of comments per author
const name = comment.user.login
if(authors.hasOwnProperty(name)) {
authors[name]++
} else {
authors[name] = 1
}
return name
}))
.then(commenters => commenters.filter((val, i, self) => {
// remove duplicate author entries from our array
return self.indexOf(val) === i
}))
.then(filteredCommenters => filteredCommenters.sort(
// sort by number of comments from least to most
// (switch a and b around the operator to order descending)
(a, b) => authors[a] - authors[b])
)
.then(sortedCommenters => sortedCommenters.map((commenter) => {
// log it all to the console
console.log(commenter, authors[commenter])
}))
.catch(err => console.error('error in request', err))
{
"name": "gh-api",
"version": "1.0.0",
"main": "index.js",
"repository": {},
"license": "MIT",
"dependencies": {
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-es2015": "^6.22.0",
"request-promise-native": "^1.0.3"
},
"devDependencies": {
"babel-cli": "^6.23.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment