From abf0367ea3726c04dbc98867cc1ea350795430b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hernando=20Guzm=C3=A1n?= Date: Wed, 9 Jun 2021 19:20:54 -0300 Subject: [PATCH] Improve logs for changes made --- .env.sample | 1 - README.md | 5 ++--- src/index.ts | 59 +++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/.env.sample b/.env.sample index 125b1f9..846ab50 100644 --- a/.env.sample +++ b/.env.sample @@ -1,4 +1,3 @@ -REPO_AND_BRANCH = "https://raw.githubusercontent.com/1Hive/pollen/gh-pages/" MONGODB_URI = "Your MongoDB connection URI" GITHUB_API_TOKEN = "Your GitHub API Token (it needs write permissions)" REPO = "Your instance repo" diff --git a/README.md b/README.md index e393d79..7c529ac 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # pollen-onboarding -A script to add, merge and activate new users in the ledger every 6 hours +A script to add, merge and activate new users in the Pollen instance. ## Developer setup @@ -12,7 +12,6 @@ A script to add, merge and activate new users in the ledger every 6 hours Copy the content of your `.env.sample` file into a new `.env` file and add the corresponding variables: -- `REPO_AND_BRANCH` = The repo and branch of your sourcecred instance. In this case it's `https://raw.githubusercontent.com/1Hive/pollen/gh-pages/` - `MONGODB_URI` = Your MongoDB connection URI. - `GITHUB_API_TOKEN` = GitHub token with commit permissions to the repo. - `REPO` = The repo you want to commit to. @@ -20,5 +19,5 @@ Copy the content of your `.env.sample` file into a new `.env` file and add the c ### Run the script! -- `npm run dev` will execute the script in developer mode, with hot reloading. - `npm start` will execute the script in production mode. +- `npm run dev` will execute the script in developer mode, with hot reloading. diff --git a/src/index.ts b/src/index.ts index 18441af..cd60eae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import connectDB from './config/db' import LedgerUpdate from './models/LedgerUpdate' +import User from './models/User' import { fetchModifiedUsers, manager, @@ -14,44 +15,72 @@ const main = async (): Promise => { await manager.reloadLedger() const ledger = manager.ledger + + const runStartTime = Date.now() + + log(`Attempting to update ledger entries for users: \n- ${ + modifiedUsers.map(user => user.username).join('\n- ') + }`) for (const user of modifiedUsers) { // Start of Ledger modifications logic const { discordId, username, discourse, github } = user - log(`Updating ledger entry for ${username}`) + log(`Checking ledger entry for ${username}`) - const discordIdentityId = ledger - .accountByAddress(`N\u0000sourcecred\u0000discord\u0000MEMBER\u0000user\u0000${discordId}\u0000`) - .identity.id + const discordAccount = ledger.accountByAddress( + `N\u0000sourcecred\u0000discord\u0000MEMBER\u0000user\u0000${discordId}\u0000` + ) + + if (!discordAccount) { + log(`Cannot find Discord identity for user ${username}, skipping to next user...`) + User.findOneAndUpdate({ discordId }, { modifiedAt: Date.now() }) + continue + } + + const discordIdentityId = discordAccount.identity.id if (discourse) { - const discourseAccount = ledger - .accountByAddress(`N\u0000sourcecred\u0000discourse\u0000user\u0000https://forum.1hive.org\u0000${discourse}\u0000`) + const discourseAccount = ledger.accountByAddress( + `N\u0000sourcecred\u0000discourse\u0000user\u0000https://forum.1hive.org\u0000${discourse}\u0000` + ) - const discourseIdentityId = discourseAccount ? discourseAccount.identity.id : createDiscourseIdentity(discourse, ledger) + let discourseIdentityId + if (discourseAccount) discourseIdentityId = discourseAccount.identity.id + else { + log(`Could not find a Discourse identity for ${discourse}, creating a new Discourse identity...`) + discourseIdentityId = createDiscourseIdentity(discourse, ledger) + } if (discordIdentityId !== discourseIdentityId) { try { ledger.mergeIdentities({ base: discordIdentityId, target: discourseIdentityId }) - log(`Merged identity ${discourseIdentityId} into ${discordIdentityId}`) + log(`Merged Discourse identity ${discourse} into Discord identity ${username}`) } catch (err) { - log(`An error occurred when trying to merge identity ${discourseIdentityId} into ${discordIdentityId}: ${err}`) + log(`An error occurred when trying to merge Discourse identity ${discourse} into Discord identity ${username}: ${err}`) + User.findOneAndUpdate({ discordId }, { modifiedAt: Date.now() }) } } } if (github) { - const githubAccount = ledger - .accountByAddress(`N\u0000sourcecred\u0000github\u0000USERLIKE\u0000USER\u0000${github}\u0000`) + const githubAccount = ledger.accountByAddress( + `N\u0000sourcecred\u0000github\u0000USERLIKE\u0000USER\u0000${github}\u0000` + ) - const githubIdentityId = githubAccount ? githubAccount.identity.id : createGithubIdentity(github, ledger) + let githubIdentityId + if (githubAccount) githubIdentityId = githubAccount.identity.id + else { + log(`Could not find a GitHub identity for ${github}, creating a new GitHub identity...`) + githubIdentityId = createGithubIdentity(github, ledger) + } if (discordIdentityId !== githubIdentityId) { try { ledger.mergeIdentities({ base: discordIdentityId, target: githubIdentityId }) - log(`Merged identity ${githubIdentityId} into ${discordIdentityId}`) + log(`Merged GitHub identity ${github} into Discord identity ${username}`) } catch (err) { - log(`An error occurred when trying to merge identity ${githubIdentityId} into ${discordIdentityId}: ${err}`) + log(`An error occurred when trying to merge GitHub identity ${github} into Discord identity ${username}: ${err}`) + User.findOneAndUpdate({ discordId }, { modifiedAt: Date.now() }) } } } @@ -64,7 +93,7 @@ const main = async (): Promise => { if(persistRes.error) log(`An error occurred when trying to commit the new ledger: ${persistRes.error}`) else { - await LedgerUpdate.create({ modifiedAt: Date.now() }) + await LedgerUpdate.create({ modifiedAt: runStartTime }) log('Accounts successfully modified') } }