generated from sourcecred/template-instance
-
Notifications
You must be signed in to change notification settings - Fork 5
Work in progress, case sensitivity is going to be tricky #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
johnmark13
wants to merge
45
commits into
main
Choose a base branch
from
automated-ledger-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
a80eb41
Work in progress, case sensitivity is going to be tricky
johnmark13 975be71
8712370
a64464e
0d54657
OK, quite a hacky way to match, but, matches
johnmark13 36a5a38
Merge branch 'automated-ledger-update' of github.com:nation3/nationcr…
johnmark13 529ff2e
9cad903
a4aef76
Can create GitHub identity, have to decide if we want it to?
johnmark13 08b4eba
Merge branch 'automated-ledger-update' of github.com:nation3/nationcr…
johnmark13 b27c6ca
6f9b674
56e2f55
4bdf91f
9e7321d
c379766
3edaa7d
69e2304
cfb7e02
714081e
717eed4
OK, it is messy but Discord in as well, will split across files
johnmark13 c9da7c3
Merge branch 'automated-ledger-update' of github.com:nation3/nationcr…
johnmark13 a43e0cb
3d5fa83
d41b17d
ca117c3
Discourse
johnmark13 bc70695
f79fc08
Put things back
johnmark13 fc0b557
Put things back
johnmark13 10b1944
d58e184
c268424
6c1108c
Rest ledger to Main
johnmark13 c7b6cf7
b3038b3
1a22a13
7589f32
962aaf0
e4e40d0
Dont update ledger if no need
johnmark13 0f49782
Merge branch 'automated-ledger-update' of github.com:nation3/nationcr…
johnmark13 3f7a7ec
Change config property name to discordGuildId
johnmark13 e8c55fc
Update scripts/github-utils.js
johnmark13 e51fe60
Fix Dicord typo
johnmark13 d3fa718
Fix Disuorce typo
johnmark13 97276a8
Merge branch 'automated-ledger-update' of github.com:nation3/nationcr…
johnmark13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module.exports = { | ||
| discordGuildId: "690584551239581708", | ||
| repo: "nation3/sourcecred-instance", | ||
| branch: "automated-ledger-update", | ||
| chainId: "1", | ||
| tokenAddress: "0x333A4823466879eeF910A04D473505da62142069" | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| const { Client, GatewayIntentBits } = require('discord.js'); | ||
| const sc = require('sourcecred').sourcecred; | ||
| const scUtils = require('./sourcecred-utils'); | ||
|
|
||
| module.exports = { | ||
| getDiscordMembers: async (guildId) => { | ||
| const token = process.env.SOURCECRED_DISCORD_TOKEN; | ||
| const client = new Client({ | ||
| intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] | ||
| }); | ||
|
|
||
| client.once('ready', () => { | ||
| console.log('Discord Bot is Go!'); | ||
| }); | ||
|
|
||
| await client.login(token); | ||
|
|
||
| const discordMembers = new Map(); | ||
|
|
||
| const guild = client.guilds.resolve(guildId); | ||
|
|
||
| const rawMembers = await guild.members.fetch(); | ||
|
|
||
| rawMembers.forEach((member) => { | ||
| discordMembers.set(`${member.user.username}#${member.user.discriminator}`, member.id); | ||
| }); | ||
|
|
||
| client.destroy(); | ||
|
|
||
| return discordMembers; | ||
| }, | ||
|
|
||
| testAndUpdateDiscordAccount: async (ledgerManager, lowerAccountToIdentityMap, discordMemberMap, passportData, chainId, tokenAddress) => { | ||
| const {passport_id, owner_address, discord_username,discord_username_ens} = passportData; | ||
|
|
||
| console.info(`Reading Discord data for: ${passport_id}`); | ||
|
|
||
| if ((!discord_username) && (!discord_username_ens)) { | ||
| console.info(`No Discord username for passport ${passport_id}`) | ||
| return; | ||
| } | ||
|
|
||
| //get just the username from one of the two columns | ||
| let discordUsernameAndDiscriminator = cleanDiscordUsername(discord_username); | ||
| if (!discordUsernameAndDiscriminator) { | ||
| discordUsernameAndDiscriminator = cleanDiscordUsername(discord_username_ens); | ||
| } | ||
|
|
||
| if (!discordUsernameAndDiscriminator) { | ||
| console.info(`Invalid Discord username ${discord_username}/${discord_username_ens} provided, skipping Discord for passport ${passport_id}`) | ||
| return; | ||
| } | ||
|
|
||
| //Discord idenitity is stored using the ID of the account, which we do not have, so have to have a Bot which can find it | ||
| //N\u0000sourcecred\u0000discord\u0000MEMBER\u0000user\u0000976148126308122644\u0000 | ||
|
|
||
| const discordUserId = discordMemberMap.get(`${discordUsernameAndDiscriminator[0]}#${discordUsernameAndDiscriminator[1]}`); | ||
|
|
||
| if(!discordUserId) { | ||
| console.log(`No Discord Member found in Nation3 Discord with Username ${discordUsernameAndDiscriminator[0]}#${discordUsernameAndDiscriminator[1]} not setting Discord Identity for ${passport_id}`); | ||
| return; | ||
| } | ||
|
|
||
| const dAddress = scUtils.createDiscordIdentity(discordUsernameAndDiscriminator, discordUserId); | ||
|
|
||
| const hasAccount = lowerAccountToIdentityMap.has(dAddress.toLowerCase()); | ||
| if(hasAccount) { | ||
| scUtils.testAndUpdateAccountPayoutAddress( | ||
| ledgerManager, | ||
| lowerAccountToIdentityMap, | ||
| dAddress, | ||
| 'discord', | ||
| discordUsernameAndDiscriminator, | ||
| passport_id, | ||
| owner_address, | ||
| chainId, | ||
| tokenAddress); | ||
| } | ||
| else { | ||
| addDiscordIdentityAndSetPayoutAddress( | ||
| ledgerManager, | ||
| dAddress, | ||
| discordUsernameAndDiscriminator, | ||
| passport_id, | ||
| owner_address, | ||
| chainId, | ||
| tokenAddress); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function addDiscordIdentityAndSetPayoutAddress(ledgerManager, scAddress, dUsername, passport_id, owner_address, chainId, tokenAddress) { | ||
| console.log(`Creating a new SourceCred identity for ${dUsername} for passport ${passport_id}`) | ||
|
|
||
| const baseIdentityProposal = scUtils.createDiscordIdentityProposal(scAddress, dUsername); | ||
|
|
||
| const baseIdentityId = sc.ledger.utils.ensureIdentityExists( | ||
| ledgerManager.ledger, | ||
| baseIdentityProposal, | ||
| ); | ||
|
|
||
| console.log(`Base Identity ID ${JSON.stringify(baseIdentityId)}`); | ||
|
|
||
| console.log(`Setting payout address for Discord for passport ${passport_id} - discordUsername ${dUsername}`) | ||
|
|
||
| ledgerManager.ledger.setPayoutAddress(baseIdentityId, owner_address , chainId, tokenAddress); | ||
|
|
||
| console.log(`Updated payout address to ${owner_address} for passport ${passport_id} - discordUsername ${dUsername}`) | ||
| } | ||
|
|
||
| function cleanDiscordUsername(dUsername) { | ||
| //https://discord.gg/grbsArn | ||
| //Adelaide Isla#3410 | ||
| let processing = dUsername; | ||
|
|
||
| if(!processing || processing.indexOf('#') < 0) { | ||
| return null; | ||
| } | ||
|
|
||
| if(dUsername.indexOf('/') >= 0) { | ||
| const regex = /.*\/(.*)/gm; | ||
| processing = processing.replace(regex, '$1'); | ||
| } | ||
|
|
||
| if(processing.startsWith('@')) { | ||
| processing = processing.substring(0); | ||
| } | ||
|
|
||
| const hash = processing.lastIndexOf('#'); | ||
| const username = processing.substring(0, hash); | ||
| const discriminator = processing.substring(hash + 1); | ||
|
|
||
| return [username, discriminator]; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| const sc = require('sourcecred').sourcecred; | ||
| const scUtils = require('./sourcecred-utils'); | ||
|
|
||
| module.exports = { | ||
| //exists - {"action":{"alias":{"address":"N\u0000sourcecred\u0000discourse\u0000user\u0000https://forum.nation3.org\u0000aahna\u0000","description":"discourse/[@aahna](https://forum.nation3.org/u/aahna/)"} | ||
| //new - {"action":{"alias":{"address":"N\u0000sourcecred\u0000dicourse\u0000user\u0000https://forum.nation3.org\u0000aahna\u0000","description":"discourse/[@aahna](https://forum.nation3.org/u/aahna)"} | ||
| testAndUpdateDiscourseAccount: async (ledgerManager, lowerAccountToIdentityMap, passportData, chainId, tokenAddress) => { | ||
| const {passport_id, owner_address, discourse_username} = passportData; | ||
|
|
||
| console.info(`Reading Discourse data for: ${passport_id}`); | ||
|
|
||
| if ((!discourse_username)) { | ||
| console.info(`No Discourse username for passport ${passport_id}`) | ||
| return; | ||
| } | ||
|
|
||
| //construct identity | ||
| const discourseAddress = scUtils.createDiscourseIdentity(discourse_username); | ||
|
|
||
| const hasAccount = lowerAccountToIdentityMap.has(discourseAddress.toLowerCase()); | ||
| if(hasAccount) { | ||
| scUtils.testAndUpdateAccountPayoutAddress( | ||
| ledgerManager, | ||
| lowerAccountToIdentityMap, | ||
| discourseAddress, | ||
| 'discourse', | ||
| discourse_username, | ||
| passport_id, | ||
| owner_address, | ||
| chainId, | ||
| tokenAddress); | ||
| } | ||
| else { | ||
| addDiscourseIdentityAndSetPayoutAddress( | ||
| ledgerManager, | ||
| discourseAddress, | ||
| discourse_username, | ||
| passport_id, | ||
| owner_address, | ||
| chainId, | ||
| tokenAddress); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function addDiscourseIdentityAndSetPayoutAddress(ledgerManager, scAddress, dUsername, passport_id, owner_address, chainId, tokenAddress) { | ||
| console.log(`Creating a new SourceCred identity for ${dUsername} for passport ${passport_id}`) | ||
|
|
||
| const baseIdentityProposal = scUtils.createDiscourseIdentityProposal(scAddress, dUsername); | ||
|
|
||
| const baseIdentityId = sc.ledger.utils.ensureIdentityExists( | ||
| ledgerManager.ledger, | ||
| baseIdentityProposal, | ||
| ); | ||
|
|
||
| console.log(`Base Identity ID ${JSON.stringify(baseIdentityId)}`); | ||
|
|
||
| console.log(`Setting payout address for GitHub for passport ${passport_id} - gitHubUsername ${dUsername}`) | ||
|
|
||
| ledgerManager.ledger.setPayoutAddress(baseIdentityId, owner_address ,chainId, tokenAddress); | ||
|
|
||
| console.log(`Updated payout address to ${owner_address} for passport ${passport_id} - gitHubUsername ${dUsername}`) | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| const sc = require('sourcecred').sourcecred; | ||
| const scUtils = require('./sourcecred-utils'); | ||
|
|
||
| module.exports = { | ||
| testAndUpdateGithubAccount: async (ledgerManager, lowerAccountToIdentityMap, passportData, chainId, tokenAddress) => { | ||
| const {passport_id, owner_address, github_username, github_username_ens} = passportData; | ||
|
|
||
| console.info(`Reading GitHub data for: ${passport_id}`); | ||
|
|
||
| if ((!github_username) && (!github_username_ens)) { | ||
| console.info(`No GitHub username for passport ${passport_id}`) | ||
| return; | ||
| } | ||
|
|
||
| //get just the username from one of the two columns | ||
| const tidiedGithubUsername = github_username && github_username.length > 0 | ||
| ? cleanGithubUsername(github_username) | ||
| : cleanGithubUsername(github_username_ens); | ||
|
|
||
| //construct identity | ||
| const ghAddress = scUtils.createGitHubIdentity(tidiedGithubUsername); | ||
|
|
||
| const hasAccount = lowerAccountToIdentityMap.has(ghAddress.toLowerCase()); | ||
| if(hasAccount) { | ||
| scUtils.testAndUpdateAccountPayoutAddress( | ||
| ledgerManager, | ||
| lowerAccountToIdentityMap, | ||
| ghAddress, | ||
| 'github', | ||
| tidiedGithubUsername, | ||
| passport_id, | ||
| owner_address, | ||
| chainId, | ||
| tokenAddress); | ||
| } | ||
| else { | ||
| addGitHubIdentityAndSetPayoutAddress( | ||
| ledgerManager, | ||
| ghAddress, | ||
| tidiedGithubUsername, | ||
| passport_id, | ||
| owner_address, | ||
| chainId, | ||
| tokenAddress); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function addGitHubIdentityAndSetPayoutAddress(ledgerManager, scAddress, ghUsername, passport_id, owner_address, chainId, tokenAddress) { | ||
| console.log(`Creating a new SourceCred identity for ${ghUsername} for passport ${passport_id}`) | ||
|
|
||
| const baseIdentityProposal = scUtils.createGitHubIdentityProposal(scAddress, ghUsername); | ||
|
|
||
| const baseIdentityId = sc.ledger.utils.ensureIdentityExists( | ||
| ledgerManager.ledger, | ||
| baseIdentityProposal, | ||
| ); | ||
|
|
||
| console.log(`Base Identity ID ${JSON.stringify(baseIdentityId)}`); | ||
|
|
||
| console.log(`Setting payout address for GitHub for passport ${passport_id} - gitHubUsername ${ghUsername}`) | ||
|
|
||
| ledgerManager.ledger.setPayoutAddress(baseIdentityId, owner_address, chainId, tokenAddress); | ||
|
|
||
| console.log(`Updated payout address to ${owner_address} for passport ${passport_id} - gitHubUsername ${ghUsername}`) | ||
| } | ||
|
|
||
| function cleanGithubUsername(ghUsername) { | ||
| //https://github.com/luisivan | ||
| //@adelaideisla | ||
| let processing = ghUsername; | ||
| if(ghUsername.indexOf('/') >= 0) { | ||
| const regex = /.*\/(.*)/gm; | ||
| processing = processing.replace(regex, '$1'); | ||
| } | ||
|
|
||
| if(processing.startsWith('@')) { | ||
| processing = processing.substring(0); | ||
| } | ||
|
|
||
| return processing; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.