1
+ import * as storage from './storage.js' ;
2
+ import * as github from './github.js' ;
3
+ import * as discord from './discord.js' ;
4
+
5
+ /**
6
+ * Shared utility function. For a given Github UserId, fetch profile metadata,
7
+ * transform it, and push it to the Discord metadata endpoint.
8
+ */
9
+ export async function updateMetadata ( userId : string ) {
10
+ const githubTokens = await storage . getGithubTokens ( userId ) ;
11
+ const discordTokens = await storage . getDiscordTokens (
12
+ githubTokens . discord_user_id
13
+ ) ;
14
+
15
+ // Fetch the user profile data from Github
16
+ let metadata : Record < string , string > ;
17
+ try {
18
+ const profile = await github . getProfile ( userId , githubTokens ) ;
19
+ // Transform the data from the profile, and grab only the bits of data used by Discord.
20
+ metadata = {
21
+ iscoder : profile . user . iscoder ,
22
+ isdocumentation : profile . user . iscoder ,
23
+ } ;
24
+ } catch ( e ) {
25
+ e . message = `Error fetching Github profile data: ${ e . message } ` ;
26
+ console . error ( e ) ;
27
+ // If fetching the profile data for the Github user fails for any reason,
28
+ // ensure metadata on the Discord side is nulled out. This prevents cases
29
+ // where the user revokes the Github app permissions, and is left with
30
+ // stale verified role data.
31
+ metadata = {
32
+ iscoder : undefined ,
33
+ isdocumentation : undefined ,
34
+ } ;
35
+ }
36
+
37
+ // Push the data to Discord.
38
+ await discord . pushMetadata ( userId , discordTokens , metadata ) ;
39
+ }
0 commit comments