Skip to content

Instantly share code, notes, and snippets.

@mnapoli

mnapoli/app.js Secret

Created May 29, 2018 13:38
Show Gist options
  • Save mnapoli/e344b243b378cbe36a7ee71e18582c47 to your computer and use it in GitHub Desktop.
Save mnapoli/e344b243b378cbe36a7ee71e18582c47 to your computer and use it in GitHub Desktop.
'use strict';
let GithubGraphQLApi = require('node-github-graphql');
let github = new GithubGraphQLApi({
token: '<your token here>'
});
module.exports.hello = (event, context, callback) => {
github.query(`
{
repository(owner: "laravel", name: "blog-contest-may-mayhem") {
issues(first: 100, states: OPEN) {
nodes {
title
reactions(first: 100, content: THUMBS_UP) {
totalCount
nodes {
user {
company
location
createdAt
}
}
}
}
}
}
}
`, null, (res, err) => {
let issues = res.data.repository.issues.nodes.map((issue) => {
return {
title: issue.title,
votes: issue.reactions.totalCount,
voters: issue.reactions.nodes.map(user => user.user),
};
});
issues.sort((a, b) => {
if (a.votes < b.votes) return -1;
if (a.votes > b.votes) return 1;
return 0;
});
issues.reverse();
let output = '';
let position = 1;
for (let issue of issues) {
// Locations
let locations = issue.voters.map(voter => voter.location);
locations = locations.filter(location => location !== null);
// Split locations on `,` and `-` and `/`
locations = locations.map(location => location.split(/[-,\/]+/).slice(-1)[0].trim());
// Lowercase
locations = locations.map(location => location.toLowerCase());
// Cities to countries
locations = locations.map(location => {
const cities = {
'antwerp': 'belgium',
'amsterdam': 'netherlands',
'the netherlands': 'netherlands',
'rotterdam': 'netherlands',
'nederland': 'netherlands',
'the hague': 'netherlands',
'pune': 'india',
'vadodara': 'india',
'dublin': 'ireland',
'copenhagen': 'denmark',
'nairobi kenya': 'kenya',
'brasil': 'brazil',
'kerala': 'india',
'ahmedabad': 'india',
'chandigarh': 'india',
'berlin': 'germany',
'paris': 'france',
'lyon': 'france',
'strasbourg': 'france',
'beijing china': 'china',
'new dametta_dametta': 'egypt',
'damitta': 'egypt',
'mahalla': 'egypt',
'esfahan': 'iran',
'baharestan': 'iran',
'chennai': 'india',
'rajkot': 'india',
'gurgaon': 'india',
'mons': 'belgium',
'kraków': 'poland',
'krakow': 'poland',
'cracow': 'poland',
'mielec': 'poland',
'deutschland': 'germany',
'zurich': 'switzerland',
'tirana': 'albania',
'shanghai.jiangsu.china': 'china',
'wuxi jiangsu': 'china',
'mons': 'belgium',
'kuala lumpur': 'malaysia',
'jakarta': 'indonesia',
'united states': 'usa',
'san diego': 'usa',
'arizona': 'usa',
'phoenix': 'usa',
'california': 'usa',
'az': 'usa',
'mi': 'usa',
'mx': 'usa',
'wa': 'usa',
'nc': 'usa',
'il': 'usa',
'tx': 'usa',
'sydney': 'australia',
'sydney australia': 'australia',
'london': 'uk',
'united kingdom': 'uk',
'aberdeen': 'uk',
'essex': 'uk',
};
if (location in cities) {
return cities[location];
}
return location;
});
// Count and sort
var locationsCount = {};
locations.forEach(function(i) { locationsCount[i] = (locationsCount[i]||0) + 1;});
var sortedLocations = [];
for (let location in locationsCount) {
sortedLocations.push([location, locationsCount[location]]);
}
sortedLocations.sort(function(a, b) {
return b[1] - a[1];
});
// Creation date
let creationDates = issue.voters.map(voter => new Date(voter.createdAt));
creationDates = creationDates.filter(date => date >= new Date('2018-05-22'));
output += '#' + position + ' - ';
position++;
output += issue.votes;
output += ' - ' + issue.title + "\n";
if (sortedLocations.length > 0) {
output += "\t";
for (let location of sortedLocations) {
output += location[0] + ' (' + location[1] + '), ';
}
output += "\n";
}
if (creationDates.length > 0) {
output += "\t" + creationDates.length + ' accounts created after May 22nd: ';
for (let date of creationDates) {
output += date.toISOString().slice(0, 10) + ', ';
}
output += "\n";
}
output += "\n";
}
callback(null, {
statusCode: 200,
body: output,
});
});
};
@Quezler
Copy link

Quezler commented Jun 1, 2018

Your example link at https://github.com/laravel/blog-contest-may-mayhem/issues/57 returns the wrong content type, application/json instead of text/plain 🤔

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