Skip to content

Commit

Permalink
Improve logs for changes made
Browse files Browse the repository at this point in the history
  • Loading branch information
hernandoagf committed Jun 9, 2021
1 parent 7f6232b commit abf0367
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
1 change: 0 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -12,13 +12,12 @@ 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.
- `BRANCH` = The branch you want to commit to.

### 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.
59 changes: 44 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import connectDB from './config/db'
import LedgerUpdate from './models/LedgerUpdate'
import User from './models/User'
import {
fetchModifiedUsers,
manager,
Expand All @@ -14,44 +15,72 @@ const main = async (): Promise<void> => {

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() })
}
}
}
Expand All @@ -64,7 +93,7 @@ const main = async (): Promise<void> => {

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')
}
}
Expand Down

0 comments on commit abf0367

Please sign in to comment.