|
| 1 | +import { warn } from 'node:console'; |
| 2 | +import { writeFile } from 'node:fs/promises'; |
| 3 | +import { homedir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { isNativeError } from 'node:util/types'; |
| 6 | + |
| 7 | +import vscode from 'vscode'; |
| 8 | + |
| 9 | +import fetchGHExclusions from './fetchGHExclusions'; |
| 10 | + |
| 11 | +const exclusionPath = join(homedir(), '.appmap', 'navie', 'global-ignore'); |
| 12 | + |
| 13 | +export async function downloadContentExclusions(githubToken: string) { |
| 14 | + const exclusions = await fetchGHExclusions(githubToken, 'all'); |
| 15 | + const paths = exclusions.flatMap(({ rules }) => rules.flatMap(({ paths }) => paths)); |
| 16 | + await writeFile(exclusionPath, paths.join('\n')); |
| 17 | + exclusionsDownloaded = true; |
| 18 | +} |
| 19 | + |
| 20 | +let exclusionsDownloaded = false; |
| 21 | + |
| 22 | +export async function ensureExclusionsDownloaded() { |
| 23 | + if (exclusionsDownloaded) return; |
| 24 | + try { |
| 25 | + const githubToken = await getGHToken(); |
| 26 | + if (!githubToken) throw new Error('Failed to obtain GitHub token'); |
| 27 | + await downloadContentExclusions(githubToken); |
| 28 | + } catch (e) { |
| 29 | + exclusionDownloadError(e); |
| 30 | + throw e; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export async function tryDownloadingExclusions() { |
| 35 | + if (exclusionsDownloaded) return; |
| 36 | + const githubToken = await getGHToken(true); |
| 37 | + if (githubToken) |
| 38 | + try { |
| 39 | + downloadContentExclusions(githubToken); |
| 40 | + } catch { |
| 41 | + // pass, it will be retried |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +export async function getGHToken(silent = false): Promise<string | undefined> { |
| 46 | + const provider = vscode.workspace |
| 47 | + .getConfiguration() |
| 48 | + .get('github.copilot.advanced.authProvider', 'github'); |
| 49 | + const session = await vscode.authentication.getSession(provider, [], { |
| 50 | + silent, |
| 51 | + createIfNone: !silent, |
| 52 | + }); |
| 53 | + if (session) return session.accessToken; |
| 54 | +} |
| 55 | + |
| 56 | +function exclusionDownloadError(error: unknown) { |
| 57 | + const title = 'Failed to download content exclusion policy from GitHub'; |
| 58 | + const errorText = isNativeError(error) ? error.message : String(error); |
| 59 | + |
| 60 | + warn(`Failed to download content exclusions: ${errorText}`); |
| 61 | + if (exclusionDownloadError.wait) return; |
| 62 | + |
| 63 | + const showDetails = () => { |
| 64 | + vscode.window.showErrorMessage(title, { |
| 65 | + detail: `${errorText}\nPlease try again later.`, |
| 66 | + modal: true, |
| 67 | + }); |
| 68 | + }; |
| 69 | + if (exclusionDownloadError.wait === null) showDetails(); // first time always show the details |
| 70 | + else vscode.window.showErrorMessage(title, {}, 'More...').then((x) => x && showDetails()); |
| 71 | + |
| 72 | + exclusionDownloadError.wait = true; |
| 73 | + setTimeout(() => (exclusionDownloadError.wait = false), 5000).unref(); |
| 74 | +} |
| 75 | +exclusionDownloadError.wait = null as boolean | null; |
0 commit comments