src/vs/platform/agentHost/common/githubEndpoints.ts

125 LOC · 121 covered · 4 uncovered · 20 ranges · 2609 concepts · 10 introducers · 1277 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- githubEndpoints.ts ×4
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { URI } from '../../../base/common/uri.js';
7 > import { ProtectedResourceMetadata } from './state/protocol/state.js';
8 >
9 > /**
10 > * The GitHub endpoints an agent host talks to, derived from an optional
11 > * GitHub Enterprise base URI. All values are string URIs with no trailing slash.
12 > */
13 > export interface IGitHubEndpoints {
14 > /** REST API base (e.g. `https://api.github.com`), used as the resource identifier and the REST host. */
15 > readonly apiBaseUri: string;
16 > /** GraphQL endpoint (distinct from `apiBaseUri` for on-prem: `/api/graphql`, not `/api/v3/graphql`). */
17 > readonly graphQlUri: string;
18 > /** OAuth authorization server URI, advertised in `authorization_servers`. */
19 > readonly oauthServer: string;
20 > /**
21 > * The configured GitHub Enterprise host (authority only, e.g. `acme.ghe.com`),
22 > * or `undefined` for github.com. Used to point the Copilot CLI at an enterprise
23 > * host via `COPILOT_GH_HOST`.
24 > */
25 > readonly enterpriseHost: string | undefined;
26 > }
27 >
28 > const GITHUB_DOT_COM_COPILOT_API_BASE_URI = 'https://api.githubcopilot.com';
29 >
30 > /** Canonical github.com endpoints, used when no enterprise URI is configured. */
31 > const GITHUB_DOT_COM_ENDPOINTS: IGitHubEndpoints = {
32 > apiBaseUri: 'https://api.github.com',
33 > graphQlUri: 'https://api.github.com/graphql',
34 > oauthServer: 'https://github.com/login/oauth',
35 > enterpriseHost: undefined,
36 > };
37 >
38 > /**
39 > * Derives the {@link IGitHubEndpoints} for a GitHub Enterprise base URI, mirroring
40 > * the URL derivation in the built-in `github-authentication` extension
41 > * (`githubServer.ts` / `common/env.ts`):
42 > *
43 > * - unset / empty / unparseable → github.com defaults (byte-for-byte, preserving
44 > * the resource identifiers used by every non-enterprise install).
45 > * - GitHub Enterprise **Cloud** (authority ends in `.ghe.com`) → API on an `api.`
46 > * subdomain: `https://api.<authority>`.
47 > * - GitHub Enterprise **Server** (on-prem) → API under `/api/v3`, GraphQL under
48 > * `/api/graphql`.
49 > *
50 > * The OAuth server is always `<scheme>://<authority>/login/oauth` for enterprise.
51 > */
52 > export function deriveGitHubEndpoints(enterpriseUri: string | undefined): IGitHubEndpoints {
53 > if (!enterpriseUri) { githubEndpoints.ts ×3
54 > return GITHUB_DOT_COM_ENDPOINTS; githubEndpoints.ts ×1
55 > }
57 > let uri: URI;
58 > try {
59 > uri = URI.parse(enterpriseUri);
60 > } catch {
61 return GITHUB_DOT_COM_ENDPOINTS;
62 }
64 > const authority = uri.authority;
65 > if (!authority) {
66 > return GITHUB_DOT_COM_ENDPOINTS; githubEndpoints.ts ×2
67 > }
69 > // A github.com authority is never a GitHub Enterprise host — treat it as the
70 > // default rather than deriving a nonsensical `github.com/api/v3`. Guards the
71 > // case where the enterprise host can't be resolved and falls back to github.com.
72 > if (authority === 'github.com' || authority === 'www.github.com' || authority === 'api.github.com') { githubEndpoints.ts ×3
73 > return GITHUB_DOT_COM_ENDPOINTS; githubEndpoints.ts ×2
74 > }
76 > const scheme = uri.scheme || 'https';
77 > const isCloud = /\.ghe\.com$/.test(authority); githubEndpoints.ts ×3
78 > return {
79 > apiBaseUri: isCloud ? `${scheme}://api.${authority}` : `${scheme}://${authority}/api/v3`,
80 > graphQlUri: isCloud ? `${scheme}://api.${authority}/graphql` : `${scheme}://${authority}/api/graphql`,
81 > oauthServer: `${scheme}://${authority}/login/oauth`,
82 > enterpriseHost: authority,
83 > };
84 > }
86 > /**
87 > * Derives the official GitHub MCP server URL from the per-user Copilot API
88 > * endpoint returned by `/copilot_internal/user`.
89 > */
90 > export function gitHubMcpServerUrl(copilotApiBaseUri: string | undefined): string | undefined {
92 > const uri = URI.parse(copilotApiBaseUri ?? GITHUB_DOT_COM_COPILOT_API_BASE_URI, true);
93 > if (!uri.authority) {
94 return undefined;
95 }
96 > return uri.with({ path: '/mcp', query: null, fragment: null }).toString(true); githubEndpoints.ts ×3
97 > } catch {
98 > return undefined; githubEndpoints.ts ×1
99 > }
102 > /**
103 > * The GitHub Copilot protected resource for the given endpoints. Shared by the
104 > * endpoint service and tests so the resource identity is defined once.
105 > */
106 > export function gitHubCopilotResource(endpoints: IGitHubEndpoints): ProtectedResourceMetadata {
107 > return { githubEndpoints.ts ×1
108 > resource: endpoints.apiBaseUri,
109 > resource_name: 'GitHub Copilot',
110 > authorization_servers: [endpoints.oauthServer],
111 > scopes_supported: ['read:user', 'user:email'],
112 > required: true,
113 > };
114 > }
116 > /** The GitHub repository protected resource for the given endpoints. */
117 > export function gitHubRepoResource(endpoints: IGitHubEndpoints): ProtectedResourceMetadata {
118 > return { githubEndpoints.ts ×1
119 > resource: `${endpoints.apiBaseUri}/repos`,
120 > resource_name: 'GitHub Repository',
121 > authorization_servers: [endpoints.oauthServer],
122 > scopes_supported: ['repo'],
123 > required: false,
124 > };
125 > }