forked from CodingCatDev/codingcat.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
522 lines (474 loc) · 14.6 KB
/
Copy pathindex.ts
File metadata and controls
522 lines (474 loc) · 14.6 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { Octokit } from 'octokit';
import * as crypto from 'crypto';
import { sendTopic } from '../utilities/pubsub';
import slugify from 'slugify';
// @ts-ignore
import * as matter from 'gray-matter';
const updatetopic = 'GitHubUpdateFirestore';
const webhookupdatetopic = 'GitHubUpdateFirestoreFromWebhook';
const webhookdeletetopic = 'GitHubRemoveFirestoreFromWebhook';
const OWNER = 'codingcatdev';
const REPO = 'v2-codingcat.dev';
// Initialize Firebase admin
admin.initializeApp();
/**
* Uncomment to test locally
*/
// export const test = functions
// .runWith({ secrets: ["GH_TOKEN"] })
// .https.onRequest(async (request, response) => {
// if (!process.env.GH_TOKEN) {
// throw new functions.https.HttpsError(
// "failed-precondition",
// "Missing GitHub Personal Token"
// );
// }
// try {
// await updateContentFromGitHub();
// } catch (error) {
// throw new functions.https.HttpsError("unknown");
// }
// response.status(200).send();
// });
/**
* GitHub Webhook for push events
*/
export const webhook = functions
.runWith({ secrets: ['GH_WEBHOOK_SECRET'] })
.https.onRequest(async (request, response) => {
if (!process.env.GH_WEBHOOK_SECRET) {
response.status(401).send('Missing GitHub Webhook Secret');
return;
}
const method = request.method;
if (method !== 'POST') {
throw new functions.https.HttpsError('unimplemented', 'Wrong method');
}
const payload = request.body;
const xHubSignature256 = request.get('X-Hub-Signature-256');
if (!xHubSignature256) {
response.status(401).send('Missing xHubSignature256');
return;
}
const sig = Buffer.from(xHubSignature256);
const hmac = crypto.createHmac('sha256', process.env.GH_WEBHOOK_SECRET);
const digest = Buffer.from(
'sha256=' + hmac.update(request.rawBody).digest('hex'),
'utf8'
);
if (sig.length !== digest.length || !crypto.timingSafeEqual(digest, sig)) {
throw new functions.https.HttpsError(
'permission-denied',
'Signature of digest did not match'
);
}
functions.logger.debug('gh:payload', payload);
const owner = payload.repository.owner.name;
const repo = payload.repository.name;
//TODO: maybe have more included in secrets array?
if (owner !== 'CodingCatDev' || repo !== 'v2-codingcat.dev') {
throw new functions.https.HttpsError(
'permission-denied',
'Incorrect Owner/Repo'
);
}
/**
* Loop through changed files and collect from all commits
*/
let changed;
let removed;
for (const commit of payload.commits) {
changed = [...commit.added, ...commit.modified];
removed = [...commit.removed];
}
//Remove dups
changed = [...new Set(changed)];
removed = [...new Set(removed)];
/**
* Send Topic to lookup each files data and update Firestore
*/
for (const path of changed) {
const splitPath = path.split('/');
const content = splitPath.at(0);
// Skip if this file is not in the content directory
if (content !== 'content') {
continue;
}
await sendTopic(webhookupdatetopic, { path, owner, repo });
}
/**
* Send Topic to lookup each files data and remove from Firestore
*/
for (const path of removed) {
const splitPath = path.split('/');
const content = splitPath.at(0);
// Skip if this file is not in the content directory
if (content !== 'content') {
continue;
}
await sendTopic(webhookdeletetopic, { path });
}
response.status(200).send();
});
/*
* Adds file data to firestore from GitHub content
*/
export const addItemToFirestoreFromWebhook = functions
.runWith({ secrets: ['GH_TOKEN'] })
.pubsub.topic(webhookupdatetopic)
.onPublish(async (message) => {
if (!process.env.GH_TOKEN) {
functions.logger.error('Missing GitHub Personal Token');
return;
}
/** @type {{type: string, content: Object}} payload */
const payload = JSON.parse(JSON.stringify(message.json));
functions.logger.debug('payload', payload);
const { path, owner, repo } = payload;
const splitPath = path.split('/');
const type = splitPath.at(1);
if (!path || !type || !owner || !repo) {
functions.logger.debug(`Missing Data in Payload`);
return;
}
/**
* Send Topic for single path object
*/
const octokit = createOctokit();
const { data, headers } = await octokit.rest.repos.getContent({
owner,
repo,
path,
});
functions.logger.debug(headers['x-ratelimit-remaining']);
await sendTopic(updatetopic, { type, content: data });
});
/*
* Adds file data to firestore from GitHub content
*/
export const removeItemFromFirestoreFromWebhook = functions
.runWith({ secrets: ['GH_TOKEN'] })
.pubsub.topic(webhookdeletetopic)
.onPublish(async (message) => {
if (!process.env.GH_TOKEN) {
functions.logger.error('Missing GitHub Personal Token');
return;
}
/** @type {{type: string, content: Object}} payload */
const payload = JSON.parse(JSON.stringify(message.json));
functions.logger.debug('payload', payload);
const { path } = payload;
const splitPath = path.split('/');
const type = splitPath.at(1);
const name = splitPath.at(2);
const lesson = splitPath.at(3);
const lessonName = splitPath.at(4);
if (lesson && lessonName) {
const ref = admin
.firestore()
.collection(type)
.doc(slugify(name))
.collection(lesson)
.doc(slugify(lessonName));
await ref.delete();
functions.logger.debug(`removed: ${lessonName}`);
} else {
const ref = admin.firestore().collection(type).doc(slugify(name));
await ref.delete();
functions.logger.debug(`removed: ${name}`);
}
});
/**
* Allows for authenticated user to run update
* TODO: only allow admins
*/
export const addContent = functions
.runWith({ secrets: ['GH_TOKEN'] })
.https.onCall(async (data, context) => {
if (!process.env.GH_TOKEN) {
throw new functions.https.HttpsError(
'failed-precondition',
'Missing GitHub Personal Token'
);
}
// Checking that the user is authenticated.
if (!context.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError(
'failed-precondition',
'The function must be called while authenticated.'
);
}
try {
await updateContentFromGitHub();
functions.logger.info(`Successfully added content for pubsub`);
} catch (error) {
functions.logger.error(`Failed to load cron data`);
}
});
/**
* Runs nightly update for GitHub content
* This checks all content so we might not want this after initial load.
* TODO: Should this run more/less often?
*/
export const addContentNightly = functions
.runWith({ secrets: ['GH_TOKEN'] })
.pubsub.schedule('0 0 * * *')
.timeZone('America/New_York')
.onRun(async (context) => {
if (!process.env.GH_TOKEN) {
throw new functions.https.HttpsError(
'failed-precondition',
'Missing GitHub Personal Token'
);
}
try {
await updateContentFromGitHub();
} catch (error) {
throw new functions.https.HttpsError('unknown', 'Unknown Error');
}
});
/*
* Adds file data to firestore from GitHub content
*/
export const addItemToFirestore = functions
.runWith({ secrets: ['GH_TOKEN'] })
.pubsub.topic(updatetopic)
.onPublish(async (message) => {
if (!process.env.GH_TOKEN) {
functions.logger.error('Missing GitHub Personal Token');
return;
}
/** @type {{type: string, content: Object}} payload */
const payload = JSON.parse(JSON.stringify(message.json));
functions.logger.debug('payload', payload);
const { content } = payload;
const { path } = content;
const splitPath = path.split('/');
const type = splitPath.at(1);
const name = splitPath.at(2);
const lesson = splitPath.at(3);
const lessonName = splitPath.at(4);
if (!type) {
functions.logger.error(`Missing Type for content update.`);
}
let ref;
if (lesson && lessonName) {
ref = admin
.firestore()
.collection(type)
.doc(slugify(name))
.collection(lesson)
.doc(slugify(lessonName));
await updateDocumentFromGitHub(ref, payload);
} else {
ref = admin.firestore().collection(type).doc(createSlug(type, content));
await updateDocumentFromGitHub(ref, payload);
}
/**
* If this is a course there should be lesson data as well.
* Only should run when full run, not webhook
* */
if (type === 'course' && content.lesson) {
functions.logger.debug('lesson', content.lesson);
const lessonSlug = slugify(content.lesson.name);
const lessonRef = ref.collection('lesson').doc(lessonSlug);
await updateDocumentFromGitHub(lessonRef, {
content: content.lesson,
});
}
});
/**
* Set Octokit instance and return */
const createOctokit = () => {
// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
return new Octokit({
auth: process.env.GH_TOKEN,
});
};
const createSlug = (
type: string,
content: { name: string; path: string }
): string => {
let fileSlug = slugify(content.name);
if (type === 'course') {
// Get the Folder name of course for uniqueness
fileSlug = slugify(content.path.split('/').at(-2) || '');
}
return fileSlug;
};
/**
* Returns GitHub's version of content, including encoded file
* @param {string} path */
const getGitHubContent = async (path: string) => {
const octokit = createOctokit();
const { data, headers } = await octokit.rest.repos.getContent({
owner: OWNER,
repo: REPO,
path,
});
functions.logger.debug(data);
functions.logger.debug(
'x-ratelimit-remaining',
headers['x-ratelimit-remaining']
);
return data;
};
/**
* Returns GitHub's version of content, including encoded file
* @param {string} path */
const getGitHubCommit = async (path: string) => {
const octokit = createOctokit();
const { data, headers } = await octokit.rest.repos.listCommits({
owner: OWNER,
repo: REPO,
path,
});
functions.logger.debug(data);
functions.logger.debug(
'x-ratelimit-remaining',
headers['x-ratelimit-remaining']
);
return data;
};
/**
* Triggers the lookup of content from GitHub
* and updates Firestore based on latest content
*/
const updateContentFromGitHub = async () => {
const octokit = createOctokit();
// Find all the content to send to firestore
for (const type of [
'framework',
'language',
'page',
'podcast',
'post',
'tutorial',
]) {
const { data, headers }: { data: any; headers: any } =
await octokit.rest.repos.getContent({
owner: OWNER,
repo: REPO,
path: `content/${type}`,
});
functions.logger.info(`Found ${data?.length} ${type} to check.`);
functions.logger.debug(headers['x-ratelimit-remaining']);
// trigger pubsub to scale this update all at once
// TODO: recursive for directories
for (const d of data) {
await sendTopic(updatetopic, { type, content: d });
}
}
/**
* Find all Course data
*/
const {
data: courseDirs,
headers: courseDirHeaders,
}: { data: any; headers: any } = await octokit.rest.repos.getContent({
owner: OWNER,
repo: REPO,
path: `content/course`,
});
functions.logger.info(`Found ${courseDirs?.length} courses to check.`);
functions.logger.debug(courseDirHeaders['x-ratelimit-remaining']);
// Loop each Course
for (const course of courseDirs) {
// Get Course detail from index
const {
data: indexFile,
headers: indexFileHeaders,
}: { data: any; headers: any } = await octokit.rest.repos.getContent({
owner: OWNER,
repo: REPO,
path: `${course.path}/index.md`,
});
functions.logger.debug(`Found index for course to check.`);
functions.logger.debug(indexFileHeaders['x-ratelimit-remaining']);
// If there is no course, just bail
if (!indexFile?.name) {
continue;
}
// Get Course lessons
const {
data: lessonFiles,
headers: lessonFilesHeaders,
}: { data: any; headers: any } = await octokit.rest.repos.getContent({
owner: OWNER,
repo: REPO,
path: `${course.path}/lesson`,
});
functions.logger.debug(
`Found ${lessonFiles?.length} lessons for ${indexFile?.name} to check.`
);
functions.logger.debug(lessonFilesHeaders['x-ratelimit-remaining']);
for (const lesson of lessonFiles) {
// Send the details for course with each lesson
await sendTopic(updatetopic, {
type: 'course',
content: {
...indexFile,
lesson,
},
});
}
}
};
/**
* Gets raw markdown file and updates Firestore
* @param {admin.firestore.DocumentReference<admin.firestore.DocumentData>} ref
* @param {any} payload
* @returns Promise<void>
*/
const updateDocumentFromGitHub = async (
ref: admin.firestore.DocumentReference<admin.firestore.DocumentData>,
payload: any
) => {
// Check if this file already exists
const doc = await ref.get();
if (doc.exists) {
functions.logger.info(`Document already exists, checking sha...`);
if (doc.data()?.sha == payload?.content?.sha) {
functions.logger.info(`sha matches no need to continue`);
return;
}
}
// Get raw file
const gitHubContent: any = await getGitHubContent(payload.content.path);
functions.logger.info(
`${payload.content.path} github file content`,
gitHubContent
);
// Get commit
const gitHubCommit = await getGitHubCommit(payload.content.path);
// Convert encoded file
const bufferObj = Buffer.from(gitHubContent.content, gitHubContent.encoding);
const decodedFile = bufferObj.toString();
// Decode markdown and get frontmatter
const mdObject = matter(decodedFile);
/**
* Update Firestore with the github data, frontmatter, and markdown.
* Flatten the Author data for easier searches
*/
await ref.set(
{
github: {
content: gitHubContent,
commit: gitHubCommit,
},
content: mdObject.content,
...mdObject.data,
weight: mdObject?.data?.weight ? mdObject?.data?.weight : 0,
published: mdObject?.data?.published
? mdObject?.data?.published
: 'draft',
start: mdObject?.data?.start
? admin.firestore.Timestamp.fromDate(new Date(mdObject?.data?.start))
: admin.firestore.Timestamp.fromDate(new Date('Jan 01, 1900')),
},
{ merge: true }
);
};