Created
August 30, 2020 12:34
-
-
Save waldekmastykarz/6250896151f09db88150596593d5bee0 to your computer and use it in GitHub Desktop.
Get GitHub repo contributors from last month
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios').default; | |
// define date ranges to process commits | |
const now = new Date(); | |
const pastMonthStart = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - 1, 1)); | |
const currentMonthStart = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)); | |
console.log('Retrieving PRs...'); | |
axios | |
.get(`https://api.github.com/repos/pnp/cli-microsoft365/pulls?state=closed&sort=updated&direction=desc&per_page=100`) | |
.then(res => { | |
var contributors = []; | |
console.log(`Found ${res.data.length} PRs. Processing...`); | |
for (let i = 0; i < res.data.length; i++) { | |
const pr = res.data[i]; | |
const prClosedDate = new Date(pr.closed_at); | |
const prUpdatedDate = new Date(pr.updated_at); | |
console.log(`${i + 1} Processing PR${pr.number}...`); | |
if (prClosedDate >= currentMonthStart) { | |
// commit done in the current month, skip | |
console.log(` PR closed on ${prClosedDate}. Skipping`); | |
continue; | |
} | |
if (prUpdatedDate < pastMonthStart) { | |
// commit older than last month, stop processing | |
console.log(` PR updated on ${prUpdatedDate}. Reached too old commits. Ending processing`); | |
break; | |
} | |
if (pr.labels.length === 0) { | |
console.log(' No labels on the PR. Skipping...'); | |
} | |
for (let j = 0; j < pr.labels.length; j++) { | |
console.log(` Checking label ${pr.labels[j].name}...`); | |
if (pr.labels[j].name === 'pr-merged') { | |
if (contributors.indexOf(pr.user.login) < 0) { | |
console.log(` Contributor ${pr.user.login} not in the list. Adding...`); | |
contributors.push(pr.user.login); | |
} | |
else { | |
console.log(` Contributor ${pr.user.login} already on the list`); | |
} | |
break; | |
} | |
console.log(` PR closed but not merged. Skipping...`); | |
} | |
} | |
console.log('Found contributors:'); | |
console.log(contributors); | |
}) | |
.catch(err => { | |
console.error(err); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "gh-contributors", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"axios": "^0.20.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment