forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubServer.ts
More file actions
126 lines (111 loc) · 5.01 KB
/
Copy pathgithubServer.ts
File metadata and controls
126 lines (111 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import fetch from 'cross-fetch';
import * as vscode from 'vscode';
import { HostHelper } from './configuration';
import { GitHubServerType } from '../common/authentication';
import Logger from '../common/logger';
import { agent } from '../env/node/net';
import { getEnterpriseUri } from '../github/utils';
export class GitHubManager {
private static readonly _githubDotComServers = new Set<string>().add('github.com').add('ssh.github.com');
private static readonly _gheServers = new Set<string>().add('ghe.com');
private static readonly _neverGitHubServers = new Set<string>().add('bitbucket.org').add('gitlab.com').add('codeberg.org');
private _knownServers: Map<string, GitHubServerType> = new Map([...Array.from(GitHubManager._githubDotComServers.keys()).map(key => [key, GitHubServerType.GitHubDotCom]), ...Array.from(GitHubManager._gheServers.keys()).map(key => [key, GitHubServerType.Enterprise])] as [string, GitHubServerType][]);
public static isGithubDotCom(host: string): boolean {
return this._githubDotComServers.has(host);
}
public static isNeverGitHub(host: string): boolean {
return this._neverGitHubServers.has(host);
}
public async isGitHub(host: vscode.Uri): Promise<GitHubServerType> {
if (host === null) {
return GitHubServerType.None;
}
const authority = host.authority.toLowerCase();
// .wiki/.git repos are not supported
if (host.path.endsWith('.wiki') || authority.match(/gist[.]github[.]com/)) {
return GitHubServerType.None;
}
if (GitHubManager.isGithubDotCom(authority)) {
return GitHubServerType.GitHubDotCom;
}
const matchingKnownServer = Array.from(this._knownServers.keys()).find(server => authority.endsWith(server));
const knownEnterprise = getEnterpriseUri();
if ((host.authority.toLowerCase() === knownEnterprise?.authority.toLowerCase()) && (!matchingKnownServer || (this._knownServers.get(matchingKnownServer) === GitHubServerType.None))) {
return GitHubServerType.Enterprise;
}
if (matchingKnownServer) {
return this._knownServers.get(matchingKnownServer) ?? GitHubServerType.None;
}
const [uri, options] = await GitHubManager.getOptions(host, 'HEAD', '/rate_limit');
let isGitHub = GitHubServerType.None;
try {
const response = await fetch(uri.toString(), options);
const otherGitHubHeaders: string[] = [];
response.headers.forEach((_value, header) => {
otherGitHubHeaders.push(header);
});
Logger.debug(`All headers: ${otherGitHubHeaders.join(', ')}`, 'GitHubServer');
const gitHubHeader = response.headers.get('x-github-request-id');
const gitHubEnterpriseHeader = response.headers.get('x-github-enterprise-version');
if (!gitHubHeader && !gitHubEnterpriseHeader) {
const [uriFallBack] = await GitHubManager.getOptions(host, 'HEAD', '/status');
const response = await fetch(uriFallBack.toString());
const responseText = await response.text();
if (responseText.startsWith('GitHub lives!')) {
// We've made it this far so it's not github.com
// It's very likely enterprise.
isGitHub = GitHubServerType.Enterprise;
} else {
// Check if we got an enterprise-looking needs auth response:
// { message: 'Must authenticate to access this API.', documentation_url: 'https://docs.github.com/enterprise/3.3/rest'}
Logger.appendLine(`Received fallback response from the server: ${responseText}`, 'GitHubServer');
const parsedResponse = JSON.parse(responseText);
if (parsedResponse.documentation_url && (parsedResponse.documentation_url as string).startsWith('https://docs.github.com/enterprise')) {
isGitHub = GitHubServerType.Enterprise;
}
}
} else {
isGitHub = ((gitHubHeader !== undefined) && (gitHubHeader !== null)) ? (gitHubEnterpriseHeader ? GitHubServerType.Enterprise : GitHubServerType.GitHubDotCom) : GitHubServerType.None;
}
return isGitHub;
} catch (ex) {
Logger.warn(`No response from host ${host}: ${ex.message}`, 'GitHubServer');
return isGitHub;
} finally {
Logger.debug(`Host ${host} is associated with GitHub: ${isGitHub}`, 'GitHubServer');
this._knownServers.set(authority, isGitHub);
}
}
public static async getOptions(
hostUri: vscode.Uri,
method: string = 'GET',
path: string,
token?: string,
): Promise<[vscode.Uri, RequestInit]> {
const headers: {
'user-agent': string;
authorization?: string;
} = {
'user-agent': 'GitHub VSCode Pull Requests',
};
if (token) {
headers.authorization = `token ${token}`;
}
const uri = vscode.Uri.joinPath(await HostHelper.getApiHost(hostUri), HostHelper.getApiPath(hostUri, path));
const requestInit = {
hostname: uri.authority,
port: 443,
method,
headers,
agent
};
return [
uri,
requestInit as RequestInit,
];
}
}