-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
109 lines (91 loc) · 3.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Octokit } from '@octokit/rest';
const codeBlock = (text, lang = '') => '```' + lang + '\n' + text + '\n```';
const unorderedList = (items) => items.map(item => '- ' + item).join('\n');
function 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);
}
/**
* @typedef {Object} Env
*/
export default {
/**
* @param {Request} request
* @param {Env} env
* @param {ExecutionContext} ctx
* @returns {Promise<Response>}
*/
async fetch(request, env, _) {
const respHeaders = new Headers();
respHeaders.set('Access-Control-Allow-Origin', '*');
respHeaders.set('Access-Control-Allow-Methods', 'POST, OPTIONS');
respHeaders.set('Access-Control-Allow-Headers', '*');
respHeaders.set('Allow', 'POST, OPTIONS');
if (request.method === "OPTIONS") {
return new Response(null, { status: 204, headers: respHeaders });
}
if (request.method !== "POST" || request.body === null) {
return new Response(null, { status: 400, headers: respHeaders });
}
const parsedBody = await request.json();
if (parsedBody === null) {
return new Response(null, { status: 400, headers: respHeaders });
}
const { venue, room, latlng, floor, reporterEmail, debug = false } = parsedBody;
let currentVenue = null;
let currentVenueError = null;
try {
const response = await fetch('https://github.nusmods.com/venues');
const currentVenues = await response.json();
currentVenue = currentVenues[venue];
} catch (e) {
currentVenueError = e;
}
const data = {
roomName: room,
floor,
};
if (latlng) {
// TODO: Check latlng param validity
const [y, x] = latlng;
data.location = { x, y };
}
const paragraphs = [];
if (reporterEmail) {
paragraphs.unshift(`Reporter: ${reporterEmail}`);
}
paragraphs.push(toDataList(data));
if (currentVenue) {
const json = JSON.stringify(currentVenue, null, 2);
paragraphs.push('**Current version:**');
paragraphs.push(codeBlock(json, 'json'));
} else if (currentVenueError) {
paragraphs.push('**Error fetching current version**');
paragraphs.push(codeBlock(currentVenueError.stack));
} else {
paragraphs.push('**Venue does not exist in current version**');
}
paragraphs.push('**Update proposed:**');
paragraphs.push(codeBlock(`"${venue}": ${JSON.stringify(data, null, 2)}`, 'json'));
const body = paragraphs.join('\n\n');
console.log(body);
if (!env.MOCK_GITHUB && !debug) {
const octokit = new Octokit({ auth: env.GITHUB_TOKEN });
await octokit.issues.create({
owner: env.GITHUB_ORG,
repo: env.GITHUB_REPO,
title: `Venue data update for ${venue}`,
body,
labels: ['venue data'],
});
}
return new Response(null, { status: 202, headers: respHeaders });
},
};