|
| 1 | +import * as sourcegraph from 'sourcegraph' |
| 2 | +import gql from 'tagged-template-noop' |
| 3 | + |
| 4 | +interface AccessTokenResponse { |
| 5 | + currentUser: { |
| 6 | + accessTokens: { |
| 7 | + nodes: { note: string }[] |
| 8 | + pageInfo: { |
| 9 | + hasNextPage: boolean |
| 10 | + } |
| 11 | + } |
| 12 | + } |
| 13 | + errors: string[] |
| 14 | +} |
| 15 | + |
| 16 | +async function userHasAccessTokenWithNote(note: string): Promise<boolean> { |
| 17 | + const response: AccessTokenResponse = await queryGraphQL(` |
| 18 | + query { |
| 19 | + currentUser { |
| 20 | + accessTokens(first: 1000) { |
| 21 | + nodes { |
| 22 | + note |
| 23 | + }, |
| 24 | + pageInfo { |
| 25 | + hasNextPage |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + `) |
| 31 | + |
| 32 | + if ( |
| 33 | + !response || |
| 34 | + !response.currentUser || |
| 35 | + !response.currentUser.accessTokens || |
| 36 | + !response.currentUser.accessTokens.nodes || |
| 37 | + !Array.isArray(response.currentUser.accessTokens.nodes) |
| 38 | + ) { |
| 39 | + return false |
| 40 | + } |
| 41 | + if ( |
| 42 | + response.currentUser.accessTokens.pageInfo && |
| 43 | + response.currentUser.accessTokens.pageInfo.hasNextPage === true |
| 44 | + ) { |
| 45 | + throw new Error('You have too many access tokens (over 1000).') |
| 46 | + } |
| 47 | + return response.currentUser.accessTokens.nodes.some( |
| 48 | + token => token.note === note |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +async function queryGraphQL(query: string, variables: any = {}): Promise<any> { |
| 53 | + const { data, errors } = await sourcegraph.commands.executeCommand( |
| 54 | + 'queryGraphQL', |
| 55 | + query, |
| 56 | + variables |
| 57 | + ) |
| 58 | + if (errors) { |
| 59 | + throw Object.assign( |
| 60 | + new Error(errors.map((err: any) => err.message).join('\n')), |
| 61 | + { errors } |
| 62 | + ) |
| 63 | + } |
| 64 | + return data |
| 65 | +} |
| 66 | + |
| 67 | +const NOTE_FOR_PYTHON_ACCESS_TOKEN = 'python' |
| 68 | + |
| 69 | +// Undefined means the current user is anonymous. |
| 70 | +let accessTokenPromise: Promise<string | undefined> |
| 71 | +export async function getOrTryToCreateAccessToken(): Promise< |
| 72 | + string | undefined |
| 73 | +> { |
| 74 | + const hasToken = await userHasAccessTokenWithNote( |
| 75 | + NOTE_FOR_PYTHON_ACCESS_TOKEN |
| 76 | + ) |
| 77 | + const setting = sourcegraph.configuration |
| 78 | + .get<Settings>() |
| 79 | + .get('python.accessToken') |
| 80 | + if (hasToken && setting) { |
| 81 | + return setting |
| 82 | + } else { |
| 83 | + return ( |
| 84 | + accessTokenPromise || |
| 85 | + (accessTokenPromise = tryToCreateAccessToken()) |
| 86 | + ) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +async function tryToCreateAccessToken(): Promise<string | undefined> { |
| 91 | + const { currentUser } = await queryGraphQL(gql` |
| 92 | + query { |
| 93 | + currentUser { |
| 94 | + id |
| 95 | + } |
| 96 | + } |
| 97 | + `) |
| 98 | + if (!currentUser) { |
| 99 | + return undefined |
| 100 | + } else { |
| 101 | + const currentUserId: string = currentUser.id |
| 102 | + const result = await queryGraphQL( |
| 103 | + gql` |
| 104 | + mutation CreateAccessToken( |
| 105 | + $user: ID! |
| 106 | + $scopes: [String!]! |
| 107 | + $note: String! |
| 108 | + ) { |
| 109 | + createAccessToken( |
| 110 | + user: $user |
| 111 | + scopes: $scopes |
| 112 | + note: $note |
| 113 | + ) { |
| 114 | + id |
| 115 | + token |
| 116 | + } |
| 117 | + } |
| 118 | + `, |
| 119 | + { |
| 120 | + user: currentUserId, |
| 121 | + scopes: ['user:all'], |
| 122 | + note: NOTE_FOR_PYTHON_ACCESS_TOKEN, |
| 123 | + } |
| 124 | + ) |
| 125 | + const token: string = result.createAccessToken.token |
| 126 | + await sourcegraph.configuration |
| 127 | + .get<Settings>() |
| 128 | + .update('python.accessToken', token) |
| 129 | + return token |
| 130 | + } |
| 131 | +} |
0 commit comments