-
Notifications
You must be signed in to change notification settings - Fork 226
Use credentials for database download in non-canary mode #3072
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
Merged
koesie10
merged 4 commits into
main
from
koesie10/download-github-database-authentication
Nov 20, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5d42cbc
Also use credentials in non-canary mode
koesie10 feca898
Merge remote-tracking branch 'origin/main' into koesie10/download-git…
koesie10 9dd061b
Rename github-database-prompt to github-database-download
koesie10 b83ef4e
Split up listing of databases to separate function
koesie10 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
110 changes: 110 additions & 0 deletions
110
extensions/ql-vscode/src/databases/github-database-api.ts
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,110 @@ | ||
| import { RequestError } from "@octokit/request-error"; | ||
| import { Octokit } from "@octokit/rest"; | ||
| import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods"; | ||
| import { showNeverAskAgainDialog } from "../common/vscode/dialog"; | ||
| import { GitHubDatabaseConfig } from "../config"; | ||
| import { Credentials } from "../common/authentication"; | ||
| import { AppOctokit } from "../common/octokit"; | ||
|
|
||
| export type CodeqlDatabase = | ||
| RestEndpointMethodTypes["codeScanning"]["listCodeqlDatabases"]["response"]["data"][number]; | ||
|
|
||
| /** | ||
| * Ask the user if they want to connect to GitHub to download CodeQL databases. | ||
| * This should be used when the user does not have an access token and should | ||
| * be followed by an access token prompt. | ||
| */ | ||
| async function askForGitHubConnect( | ||
| config: GitHubDatabaseConfig, | ||
| ): Promise<boolean> { | ||
| const answer = await showNeverAskAgainDialog( | ||
| "This repository has an origin (GitHub) that may have one or more CodeQL databases. Connect to GitHub and download any existing databases?", | ||
| false, | ||
| "Connect", | ||
| "Not now", | ||
| "Never", | ||
| ); | ||
|
|
||
| if (answer === "Not now" || answer === undefined) { | ||
| return false; | ||
| } | ||
|
|
||
| if (answer === "Never") { | ||
| await config.setDownload("never"); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| export type ListDatabasesResult = { | ||
| /** | ||
| * Whether the user has been prompted for credentials. This can be used to determine | ||
| * follow-up actions based on whether the user has already had any feedback. | ||
| */ | ||
| promptedForCredentials: boolean; | ||
| databases: CodeqlDatabase[]; | ||
| octokit: Octokit; | ||
| }; | ||
|
|
||
| /** | ||
| * List CodeQL databases for a GitHub repository. | ||
| * | ||
| * This will first try to fetch the CodeQL databases for the repository with | ||
| * existing credentials (or none if there are none). If that fails, it will | ||
| * prompt the user to connect to GitHub and try again. | ||
| * | ||
| * If the user does not want to connect to GitHub, this will return `undefined`. | ||
| */ | ||
| export async function listDatabases( | ||
| owner: string, | ||
| repo: string, | ||
| credentials: Credentials, | ||
| config: GitHubDatabaseConfig, | ||
| ): Promise<ListDatabasesResult | undefined> { | ||
| const hasAccessToken = !!(await credentials.getExistingAccessToken()); | ||
|
|
||
| let octokit = hasAccessToken | ||
| ? await credentials.getOctokit() | ||
| : new AppOctokit(); | ||
|
|
||
| let promptedForCredentials = false; | ||
|
|
||
| let databases: CodeqlDatabase[]; | ||
| try { | ||
| const response = await octokit.rest.codeScanning.listCodeqlDatabases({ | ||
| owner, | ||
| repo, | ||
| }); | ||
| databases = response.data; | ||
| } catch (e) { | ||
| // If we get a 404 when we don't have an access token, it might be because | ||
| // the repository is private/internal. Therefore, we should ask the user | ||
| // whether they want to connect to GitHub and try again. | ||
| if (e instanceof RequestError && e.status === 404 && !hasAccessToken) { | ||
| // Check whether the user wants to connect to GitHub | ||
| if (!(await askForGitHubConnect(config))) { | ||
| return; | ||
| } | ||
|
|
||
| // Prompt for credentials | ||
| octokit = await credentials.getOctokit(); | ||
|
|
||
| promptedForCredentials = true; | ||
|
|
||
| const response = await octokit.rest.codeScanning.listCodeqlDatabases({ | ||
| owner, | ||
| repo, | ||
| }); | ||
| databases = response.data; | ||
| } else { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| promptedForCredentials, | ||
| databases, | ||
| octokit, | ||
| }; | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.