Skip to content

Commit c2ddd68

Browse files
committed
Use GHEC-DR URL in Octokit instance
1 parent 9a14896 commit c2ddd68

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed

extensions/ql-vscode/src/common/vscode/authentication.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Octokit } from "@octokit/rest";
33
import type { Credentials } from "../authentication";
44
import { AppOctokit } from "../octokit";
55
import { hasGhecDrUri } from "../../config";
6+
import { getOctokitBaseUrl } from "./octokit";
67

78
// We need 'repo' scope for triggering workflows, 'gist' scope for exporting results to Gist,
89
// and 'read:packages' for reading private CodeQL packages.
@@ -15,24 +16,18 @@ const SCOPES = ["repo", "gist", "read:packages"];
1516
*/
1617
export class VSCodeCredentials implements Credentials {
1718
/**
18-
* A specific octokit to return, otherwise a new authenticated octokit will be created when needed.
19-
*/
20-
private octokit: Octokit | undefined;
21-
22-
/**
23-
* Creates or returns an instance of Octokit.
19+
* Creates or returns an instance of Octokit. The returned instance should
20+
* not be stored and reused, as it may become out-of-date with the current
21+
* authentication session.
2422
*
2523
* @returns An instance of Octokit.
2624
*/
2725
async getOctokit(): Promise<Octokit> {
28-
if (this.octokit) {
29-
return this.octokit;
30-
}
31-
3226
const accessToken = await this.getAccessToken();
3327

3428
return new AppOctokit({
3529
auth: accessToken,
30+
baseUrl: getOctokitBaseUrl(),
3631
});
3732
}
3833

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getGitHubInstanceApiUrl } from "../../config";
2+
3+
/**
4+
* Returns the Octokit base URL to use based on the GitHub instance URL.
5+
*
6+
* This is necessary because the Octokit base URL should not have a trailing
7+
* slash, but this is included by default in a URL.
8+
*/
9+
export function getOctokitBaseUrl(): string {
10+
let apiUrl = getGitHubInstanceApiUrl().toString();
11+
if (apiUrl.endsWith("/")) {
12+
apiUrl = apiUrl.slice(0, -1);
13+
}
14+
return apiUrl;
15+
}

extensions/ql-vscode/src/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export function hasGhecDrUri(): boolean {
127127
* The URI for GitHub.com.
128128
*/
129129
export const GITHUB_URL = new URL("https://github.com");
130+
export const GITHUB_API_URL = new URL("https://api.github.com");
130131

131132
/**
132133
* If the GitHub Enterprise URI is set to something that looks like GHEC-DR, return it.
@@ -148,6 +149,16 @@ export function getGitHubInstanceUrl(): URL {
148149
return GITHUB_URL;
149150
}
150151

152+
export function getGitHubInstanceApiUrl(): URL {
153+
const ghecDrUri = getGhecDrUri();
154+
if (ghecDrUri) {
155+
const url = new URL(ghecDrUri.toString());
156+
url.hostname = `api.${url.hostname}`;
157+
return url;
158+
}
159+
return GITHUB_API_URL;
160+
}
161+
151162
const ROOT_SETTING = new Setting("codeQL");
152163

153164
// Telemetry configuration

extensions/ql-vscode/src/databases/code-search-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { AppOctokit } from "../common/octokit";
77
import type { ProgressCallback } from "../common/vscode/progress";
88
import { UserCancellationException } from "../common/vscode/progress";
99
import type { EndpointDefaults } from "@octokit/types";
10+
import { getOctokitBaseUrl } from "../common/vscode/octokit";
1011

1112
export async function getCodeSearchRepositories(
1213
query: string,
@@ -54,6 +55,7 @@ async function provideOctokitWithThrottling(
5455

5556
const octokit = new MyOctokit({
5657
auth,
58+
baseUrl: getOctokitBaseUrl(),
5759
throttle: {
5860
onRateLimit: (retryAfter: number, options: EndpointDefaults): boolean => {
5961
void logger.log(

extensions/ql-vscode/src/databases/database-fetcher.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
allowHttp,
3131
downloadTimeout,
3232
getGitHubInstanceUrl,
33+
hasGhecDrUri,
3334
isCanary,
3435
} from "../config";
3536
import { showAndLogInformationMessage } from "../common/logging";
@@ -151,9 +152,10 @@ export class DatabaseFetcher {
151152
maxStep: 2,
152153
});
153154

155+
const instanceUrl = getGitHubInstanceUrl();
156+
154157
const options: InputBoxOptions = {
155-
title:
156-
'Enter a GitHub repository URL or "name with owner" (e.g. https://github.com/github/codeql or github/codeql)',
158+
title: `Enter a GitHub repository URL or "name with owner" (e.g. https://github.com/github/codeql or github/codeql)`,
157159
placeHolder: "https://github.com/<owner>/<repo> or <owner>/<repo>",
158160
ignoreFocusOut: true,
159161
};
@@ -187,7 +189,8 @@ export class DatabaseFetcher {
187189
throw new Error(`Invalid GitHub repository: ${githubRepo}`);
188190
}
189191

190-
const credentials = isCanary() ? this.app.credentials : undefined;
192+
const credentials =
193+
isCanary() || hasGhecDrUri() ? this.app.credentials : undefined;
191194

192195
const octokit = credentials
193196
? await credentials.getOctokit()

extensions/ql-vscode/src/databases/github-databases/api.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Octokit } from "@octokit/rest";
33
import type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
44
import { showNeverAskAgainDialog } from "../../common/vscode/dialog";
55
import type { GitHubDatabaseConfig } from "../../config";
6+
import { hasGhecDrUri } from "../../config";
67
import type { Credentials } from "../../common/authentication";
78
import { AppOctokit } from "../../common/octokit";
89
import type { ProgressCallback } from "../../common/vscode/progress";
@@ -67,7 +68,10 @@ export async function listDatabases(
6768
credentials: Credentials,
6869
config: GitHubDatabaseConfig,
6970
): Promise<ListDatabasesResult | undefined> {
70-
const hasAccessToken = !!(await credentials.getExistingAccessToken());
71+
// On GHEC-DR, unauthenticated requests will enver work, so we should always ask
72+
// for authentication.
73+
const hasAccessToken =
74+
!!(await credentials.getExistingAccessToken()) || hasGhecDrUri();
7175

7276
let octokit = hasAccessToken
7377
? await credentials.getOctokit()

0 commit comments

Comments
 (0)