From ed63ba9958ab96b61d099b5f058e38df072b791a Mon Sep 17 00:00:00 2001 From: Chris Pinkney Date: Tue, 6 Apr 2021 00:39:30 -0400 Subject: [PATCH] initial implementation of planet feed wiki migration tool Co-authored-by: Josue --- src/api/users/.gitignore | 1 + src/api/users/env.local | 1 + src/api/users/tools/migrate/README.md | 4 ++ src/api/users/tools/migrate/migrate.js | 68 ++++++++++++++++++++++++ src/api/users/tools/migrate/package.json | 19 +++++++ 5 files changed, 93 insertions(+) create mode 100644 src/api/users/tools/migrate/README.md create mode 100644 src/api/users/tools/migrate/migrate.js create mode 100644 src/api/users/tools/migrate/package.json diff --git a/src/api/users/.gitignore b/src/api/users/.gitignore index c816fafba8..e3dca5fe1f 100644 --- a/src/api/users/.gitignore +++ b/src/api/users/.gitignore @@ -2,3 +2,4 @@ firebase-debug.log firestore-debug.log ui-debug.log +legacy_users.json diff --git a/src/api/users/env.local b/src/api/users/env.local index 6d53ee1187..b68bd618e6 100644 --- a/src/api/users/env.local +++ b/src/api/users/env.local @@ -5,3 +5,4 @@ FIRESTORE_EMULATOR_HOST=localhost:8088 USERS_PORT=6666 USERS_URL=http://localhost:6666 +FEED_URL=https://wiki.cdot.senecacollege.ca/wiki/Planet_CDOT_Feed_List diff --git a/src/api/users/tools/migrate/README.md b/src/api/users/tools/migrate/README.md new file mode 100644 index 0000000000..5a93e62043 --- /dev/null +++ b/src/api/users/tools/migrate/README.md @@ -0,0 +1,4 @@ +TODO: + +- talk about why this is needed +- talk about env.local diff --git a/src/api/users/tools/migrate/migrate.js b/src/api/users/tools/migrate/migrate.js new file mode 100644 index 0000000000..f13f1e7540 --- /dev/null +++ b/src/api/users/tools/migrate/migrate.js @@ -0,0 +1,68 @@ +const fetch = require('node-fetch'); +const jsdom = require('jsdom'); +const { isWebUri } = require('valid-url'); +const fs = require('fs'); +const { logger } = require('@senecacdot/satellite'); + +const { JSDOM } = jsdom; +const { FEED_URL } = process.env; + +const FILE = 'legacy_users.json'; + +const getWikiText = async (url) => { + try { + const response = await fetch(url); + const data = await response.text(); + + const dom = new JSDOM(data); + return dom.window.document.querySelector('pre').textContent; + } catch { + throw new Error(`Unable to download wiki feed data from url ${url}`); + } +}; + +(async () => { + const commentRegex = /^\s*#/; + let wikiText; + let firstName; + let lastName; + const users = []; + + // Try to fetch the feed list from 'FEED_URL' + try { + wikiText = await getWikiText( + FEED_URL || 'https://wiki.cdot.senecacollege.ca/wiki/Planet_CDOT_Feed_List' + ); + } catch (error) { + logger.error(error); + process.exit(1); + } + + // store every line in an array + const lines = wikiText.split(/\r\n|\r|\n/); + + // Iterate through all lines and find url/name pairs, then parse them. + lines.forEach((line, index) => { + const feeds = []; + + if (!commentRegex.test(line) && line.startsWith('[')) { + feeds.push(line.replace(/[[\]']/g, '')); + + if (feeds.length && isWebUri(feeds[0])) { + [firstName, lastName] = lines[index + 1].replace(/^\s*name\s*=\s*/, '').split(' '); + users.push({ firstName, lastName, feeds }); + } else { + logger.error(`Skipping invalid wiki feed url ${feeds} for author ${firstName} ${lastName}`); + } + } + }); + + try { + fs.writeFileSync(`${FILE}`, JSON.stringify(users)); + logger.info( + `Processed ${users.length} records. Legacy users were successfully written to file: ${FILE}.` + ); + } catch (err) { + logger.error(err); + } +})(); diff --git a/src/api/users/tools/migrate/package.json b/src/api/users/tools/migrate/package.json new file mode 100644 index 0000000000..f9751f03e6 --- /dev/null +++ b/src/api/users/tools/migrate/package.json @@ -0,0 +1,19 @@ +{ + "name": "migrate", + "version": "1.0.0", + "description": "Migrates users from the planet feed wiki list to Firebase", + "main": "migrate.js", + "scripts": { + "start": "env-cmd -f ../../env.local node migrate.js" + }, + "author": "", + "license": "ISC", + "dependencies": { + "@senecacdot/satellite": "^1.x.0", + "env-cmd": "^10.1.0", + "jsdom": "^16.5.2", + "node-fetch": "^2.6.1", + "set-interval-async": "^2.0.1", + "valid-url": "^1.0.9" + } +}