Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NUSMods GitHub Venues Hook
const git = require('isomorphic-git');
const axios = require('axios');
const Octokit = require('@octokit/rest');
const octokit = new Octokit();
const codeBlock = (text, lang = '') =>
'```' + lang + '\n' + text + '\n```';
const unorderedList = (items) =>
items.map(item => '- ' + item).join('\n');
const toDataList = (data) => {
const dataList = [
`Room Name: ${data.roomName}`,
`Floor: ${data.floor}`,
];
if (data.location) {
const { x, y } = data.location;
dataList.push(`Location: [${y}, ${x}](https://www.openstreetmap.org/?mlat=${y}&mlon=${x}#map=19/${y}/${x})`);
}
return unorderedList(dataList);
}
/**
* @param {string} venue
* @param {string} room
* @param {array} latlng
* @param {integer} floor
* @param {string} comment
* @param {string} reporterName
* @param {string} reporterEmail
*/
module.exports = async (venue, room, latlng = null, floor, comment = null, reporterName = null, reporterEmail = null, context) => {
octokit.authenticate({
type: 'oauth',
token: process.env.GITHUB_TOKEN,
});
console.log({ venue, room, latlng, floor, comment, reporterName, reporterEmail });
// Get current version of the venue
const currentVenues = await axios.get('https://raw.githubusercontent.com/nusmodifications/nusmods/master/www/src/js/data/venues.json')
.then(response => response.data);
const currentVenue = currentVenues[venue];
const data = {
roomName: room,
floor,
};
if (latlng) {
// TODO: Check latlng param validity
const [y, x] = latlng;
data.location = { x, y };
}
const paragraphs = [
toDataList(data),
];
if (comment) {
paragraphs.push('**Reporter comment:**');
paragraphs.push(comment);
}
if (currentVenue) {
paragraphs.push('**Current version:**')
paragraphs.push(codeBlock(JSON.stringify(currentVenue, null, 2)));
} else {
paragraphs.push('**Venue does not exist in current version**');
}
paragraphs.push('**Update proposed:**');
paragraphs.push(codeBlock(`"${venue}": ${JSON.stringify(data, null, 2)}`, 'json'));
if (reporterName || reporterEmail) {
if (reporterName && reporterEmail) {
paragraphs.unshift(`Reporter: ${reporterName} (${reporterEmail})`);
} else {
paragraphs.unshift(`Reporter: ${reporterName || reporterEmail}`);
}
}
const body = paragraphs.join('\n\n');
console.log(body);
if (!process.env.MOCK_GITHUB) {
await octokit.issues.create({
owner: process.env.GITHUB_ORG,
repo: process.env.GITHUB_REPO,
title: `Venue data update for ${venue}`,
body,
labels: ['venue data'],
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.