|
| 1 | +import { RequestError } from "@octokit/request-error"; |
| 2 | +import { Octokit } from "@octokit/rest"; |
| 3 | +import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods"; |
| 4 | +import { showNeverAskAgainDialog } from "../common/vscode/dialog"; |
| 5 | +import { GitHubDatabaseConfig } from "../config"; |
| 6 | +import { Credentials } from "../common/authentication"; |
| 7 | +import { AppOctokit } from "../common/octokit"; |
| 8 | + |
| 9 | +export type CodeqlDatabase = |
| 10 | + RestEndpointMethodTypes["codeScanning"]["listCodeqlDatabases"]["response"]["data"][number]; |
| 11 | + |
| 12 | +/** |
| 13 | + * Ask the user if they want to connect to GitHub to download CodeQL databases. |
| 14 | + * This should be used when the user does not have an access token and should |
| 15 | + * be followed by an access token prompt. |
| 16 | + */ |
| 17 | +async function askForGitHubConnect( |
| 18 | + config: GitHubDatabaseConfig, |
| 19 | +): Promise<boolean> { |
| 20 | + const answer = await showNeverAskAgainDialog( |
| 21 | + "This repository has an origin (GitHub) that may have one or more CodeQL databases. Connect to GitHub and download any existing databases?", |
| 22 | + false, |
| 23 | + "Connect", |
| 24 | + "Not now", |
| 25 | + "Never", |
| 26 | + ); |
| 27 | + |
| 28 | + if (answer === "Not now" || answer === undefined) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + if (answer === "Never") { |
| 33 | + await config.setDownload("never"); |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +export type ListDatabasesResult = { |
| 41 | + /** |
| 42 | + * Whether the user has been prompted for credentials. This can be used to determine |
| 43 | + * follow-up actions based on whether the user has already had any feedback. |
| 44 | + */ |
| 45 | + promptedForCredentials: boolean; |
| 46 | + databases: CodeqlDatabase[]; |
| 47 | + octokit: Octokit; |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * List CodeQL databases for a GitHub repository. |
| 52 | + * |
| 53 | + * This will first try to fetch the CodeQL databases for the repository with |
| 54 | + * existing credentials (or none if there are none). If that fails, it will |
| 55 | + * prompt the user to connect to GitHub and try again. |
| 56 | + * |
| 57 | + * If the user does not want to connect to GitHub, this will return `undefined`. |
| 58 | + */ |
| 59 | +export async function listDatabases( |
| 60 | + owner: string, |
| 61 | + repo: string, |
| 62 | + credentials: Credentials, |
| 63 | + config: GitHubDatabaseConfig, |
| 64 | +): Promise<ListDatabasesResult | undefined> { |
| 65 | + const hasAccessToken = !!(await credentials.getExistingAccessToken()); |
| 66 | + |
| 67 | + let octokit = hasAccessToken |
| 68 | + ? await credentials.getOctokit() |
| 69 | + : new AppOctokit(); |
| 70 | + |
| 71 | + let promptedForCredentials = false; |
| 72 | + |
| 73 | + let databases: CodeqlDatabase[]; |
| 74 | + try { |
| 75 | + const response = await octokit.rest.codeScanning.listCodeqlDatabases({ |
| 76 | + owner, |
| 77 | + repo, |
| 78 | + }); |
| 79 | + databases = response.data; |
| 80 | + } catch (e) { |
| 81 | + // If we get a 404 when we don't have an access token, it might be because |
| 82 | + // the repository is private/internal. Therefore, we should ask the user |
| 83 | + // whether they want to connect to GitHub and try again. |
| 84 | + if (e instanceof RequestError && e.status === 404 && !hasAccessToken) { |
| 85 | + // Check whether the user wants to connect to GitHub |
| 86 | + if (!(await askForGitHubConnect(config))) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Prompt for credentials |
| 91 | + octokit = await credentials.getOctokit(); |
| 92 | + |
| 93 | + promptedForCredentials = true; |
| 94 | + |
| 95 | + const response = await octokit.rest.codeScanning.listCodeqlDatabases({ |
| 96 | + owner, |
| 97 | + repo, |
| 98 | + }); |
| 99 | + databases = response.data; |
| 100 | + } else { |
| 101 | + throw e; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return { |
| 106 | + promptedForCredentials, |
| 107 | + databases, |
| 108 | + octokit, |
| 109 | + }; |
| 110 | +} |
0 commit comments