forked from hozzjss/pollen-onboarding
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
MONGODB_URI = "Your MongoDB connection URI" | ||
GITHUB_API_TOKEN = "Your GitHub API Token (it needs write permissions)" | ||
GH_API_TOKEN = "Your GitHub API Token (it needs write permissions)" | ||
REPO = "Your instance repo" | ||
BRANCH = "The branch you want to work on" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,161 +1,190 @@ | ||
const chalk = require('chalk') | ||
|
||
import connectDB from './config/db' | ||
import LedgerUpdate from './models/LedgerUpdate' | ||
import User from './models/User' | ||
import { | ||
fetchModifiedUsers, | ||
manager, | ||
createDiscourseIdentity, | ||
createGithubIdentity | ||
} from './utils' | ||
const chalk = require("chalk"); | ||
|
||
import connectDB from "./config/db"; | ||
import LedgerUpdate from "./models/LedgerUpdate"; | ||
import User from "./models/User"; | ||
import { | ||
fetchModifiedUsers, | ||
manager, | ||
createDiscourseIdentity, | ||
createGithubIdentity, | ||
} from "./utils"; | ||
|
||
const main = async (): Promise<void> => { | ||
await connectDB() | ||
const modifiedUsers = await fetchModifiedUsers() | ||
await connectDB(); | ||
const modifiedUsers = await fetchModifiedUsers(); | ||
|
||
if (!modifiedUsers.length) { | ||
console.log(chalk.yellow('No recent modified users found, exitting...')) | ||
process.exit() | ||
console.log(chalk.yellow("No recent modified users found, exitting...")); | ||
process.exit(); | ||
} | ||
|
||
await manager.reloadLedger() | ||
const ledger = manager.ledger | ||
await manager.reloadLedger(); | ||
const ledger = manager.ledger; | ||
|
||
const runStartTime = Date.now() | ||
const runStartTime = Date.now(); | ||
|
||
console.log( | ||
`${new Date()}. Attempting to update ledger entries for users: \n- ${modifiedUsers | ||
.map((user) => user.username) | ||
.join("\n- ")}` | ||
); | ||
|
||
console.log(`${new Date()}. 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, address } = user | ||
let { discourse, github } = user | ||
console.log(`\nChecking ledger entry for ${username}...`) | ||
const { discordId, username, address } = user; | ||
let { discourse, github } = user; | ||
console.log(`\nChecking ledger entry for ${username}...`); | ||
|
||
// Find account by Discord ID | ||
console.log(ledger.accountByName("hz")); | ||
const discordAccount = ledger.accountByAddress( | ||
`N\u0000sourcecred\u0000discord\u0000MEMBER\u0000user\u0000${discordId}\u0000` | ||
) | ||
); | ||
|
||
if (!discordAccount) { | ||
console.log( | ||
chalk.yellow(` - Cannot find Discord identity for user ${username}, skipping to next user`) | ||
) | ||
User.findOneAndUpdate({ discordId }, { modifiedAt: Date.now() }) | ||
continue | ||
chalk.yellow( | ||
` - Cannot find Discord identity for user ${username}, skipping to next user` | ||
) | ||
); | ||
User.findOneAndUpdate({ discordId }, { modifiedAt: Date.now() }); | ||
continue; | ||
} | ||
|
||
const discordIdentityId = discordAccount.identity.id | ||
const discordIdentityId = discordAccount.identity.id; | ||
// Regex to test invalid characters in Discourse and GitHub usernames | ||
const regex = /[._]/g | ||
const regex = /[._]/g; | ||
|
||
// Merge Discourse identity if specified | ||
if (discourse) { | ||
if (regex.test(discourse)) { | ||
console.log(` - ${discourse}: Invalid Discourse name for identity, replacing...`) | ||
discourse = discourse.replace(regex, '-') | ||
console.log(` - New Discourse name: ${discourse}`) | ||
console.log( | ||
` - ${discourse}: Invalid Discourse name for identity, replacing...` | ||
); | ||
discourse = discourse.replace(regex, "-"); | ||
console.log(` - New Discourse name: ${discourse}`); | ||
} | ||
|
||
const discourseAccount = ledger.accountByAddress( | ||
`N\u0000sourcecred\u0000discourse\u0000user\u0000https://forum.1hive.org\u0000${discourse}\u0000` | ||
) | ||
); | ||
|
||
let discourseIdentityId | ||
if (discourseAccount) discourseIdentityId = discourseAccount.identity.id | ||
let discourseIdentityId; | ||
if (discourseAccount) discourseIdentityId = discourseAccount.identity.id; | ||
else { | ||
console.log(` - Could not find a Discourse identity for ${discourse}, created a new one`) | ||
discourseIdentityId = createDiscourseIdentity(discourse, ledger) | ||
console.log( | ||
` - Could not find a Discourse identity for ${discourse}, created a new one` | ||
); | ||
discourseIdentityId = createDiscourseIdentity(discourse, ledger); | ||
} | ||
|
||
if (discordIdentityId !== discourseIdentityId) { | ||
try { | ||
ledger.mergeIdentities({ base: discordIdentityId, target: discourseIdentityId }) | ||
console.log(` - Merged Discourse identity ${discourse} into Discord identity ${username}`) | ||
ledger.mergeIdentities({ | ||
base: discordIdentityId, | ||
target: discourseIdentityId, | ||
}); | ||
console.log( | ||
` - Merged Discourse identity ${discourse} into Discord identity ${username}` | ||
); | ||
} catch (err) { | ||
console.log( | ||
chalk.red(` - An error occurred when trying to merge Discourse identity ${discourse} into Discord identity ${username}: ${err}`) | ||
chalk.red( | ||
` - An error occurred when trying to merge Discourse identity ${discourse} into Discord identity ${username}: ${err}` | ||
) | ||
User.findOneAndUpdate({ discordId }, { modifiedAt: Date.now() }) | ||
); | ||
User.findOneAndUpdate({ discordId }, { modifiedAt: Date.now() }); | ||
} | ||
} | ||
} | ||
|
||
// Merge GitHub identity if specified | ||
if (github) { | ||
if (regex.test(github)) { | ||
console.log(` - ${github}: Invalid GitHub name for identity, replacing...`) | ||
github = github.replace(regex, '-') | ||
console.log(` - New GitHub name: ${github}`) | ||
console.log( | ||
` - ${github}: Invalid GitHub name for identity, replacing...` | ||
); | ||
github = github.replace(regex, "-"); | ||
console.log(` - New GitHub name: ${github}`); | ||
} | ||
|
||
const githubAccount = ledger.accountByAddress( | ||
`N\u0000sourcecred\u0000github\u0000USERLIKE\u0000USER\u0000${github}\u0000` | ||
) | ||
); | ||
|
||
let githubIdentityId | ||
if (githubAccount) githubIdentityId = githubAccount.identity.id | ||
let githubIdentityId; | ||
if (githubAccount) githubIdentityId = githubAccount.identity.id; | ||
else { | ||
console.log(` - Could not find a GitHub identity for ${github}, created a new one`) | ||
githubIdentityId = createGithubIdentity(github, ledger) | ||
console.log( | ||
` - Could not find a GitHub identity for ${github}, created a new one` | ||
); | ||
githubIdentityId = createGithubIdentity(github, ledger); | ||
} | ||
|
||
if (discordIdentityId !== githubIdentityId) { | ||
try { | ||
ledger.mergeIdentities({ base: discordIdentityId, target: githubIdentityId }) | ||
console.log(` - Merged GitHub identity ${github} into Discord identity ${username}`) | ||
ledger.mergeIdentities({ | ||
base: discordIdentityId, | ||
target: githubIdentityId, | ||
}); | ||
console.log( | ||
` - Merged GitHub identity ${github} into Discord identity ${username}` | ||
); | ||
} catch (err) { | ||
console.log( | ||
chalk.red(` - An error occurred when trying to merge GitHub identity ${github} into Discord identity ${username}: ${err}`) | ||
) | ||
User.findOneAndUpdate({ discordId }, { modifiedAt: Date.now() }) | ||
chalk.red( | ||
` - An error occurred when trying to merge GitHub identity ${github} into Discord identity ${username}: ${err}` | ||
) | ||
); | ||
User.findOneAndUpdate({ discordId }, { modifiedAt: Date.now() }); | ||
} | ||
} | ||
} | ||
|
||
// Add wallet address | ||
if ( | ||
!discordAccount.payoutAddresses.size || | ||
!discordAccount.payoutAddresses.size || | ||
discordAccount.payoutAddresses.values().next().value !== address | ||
) { | ||
ledger.setPayoutAddress( | ||
discordAccount.identity.id, | ||
address, | ||
'100', | ||
'0x71850b7E9Ee3f13Ab46d67167341E4bDc905Eef9' | ||
) | ||
"100", | ||
"0x71850b7E9Ee3f13Ab46d67167341E4bDc905Eef9" | ||
); | ||
} | ||
|
||
// Activate account | ||
if (!discordAccount.active) { | ||
try { | ||
ledger.activate(discordIdentityId) | ||
console.log(` - Discord identity for user ${username} activated`) | ||
ledger.activate(discordIdentityId); | ||
console.log(` - Discord identity for user ${username} activated`); | ||
} catch (err) { | ||
console.log( | ||
chalk.red(` - An error occurred when trying to activate Discord identity for user ${discourse}: ${err}`) | ||
) | ||
chalk.red( | ||
` - An error occurred when trying to activate Discord identity for user ${discourse}: ${err}` | ||
) | ||
); | ||
} | ||
|
||
} | ||
// End of Ledger modifications logic | ||
} | ||
const persistRes = await manager.persist() | ||
if(persistRes.error) { | ||
|
||
const persistRes = await manager.persist(); | ||
|
||
if (persistRes.error) { | ||
console.log( | ||
chalk.red(`\nAn error occurred when trying to commit the new ledger: ${persistRes.error}`) | ||
) | ||
process.exit(1) | ||
} | ||
else { | ||
await LedgerUpdate.create({ modifiedAt: runStartTime }) | ||
console.log(chalk.green('\nAccounts successfully modified')) | ||
process.exit() | ||
chalk.red( | ||
`\nAn error occurred when trying to commit the new ledger: ${persistRes.error}` | ||
) | ||
); | ||
process.exit(1); | ||
} else { | ||
await LedgerUpdate.create({ modifiedAt: runStartTime }); | ||
console.log(chalk.green("\nAccounts successfully modified")); | ||
process.exit(); | ||
} | ||
} | ||
}; | ||
|
||
main() | ||
main(); |
Oops, something went wrong.