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
70 lines (61 loc) · 1.83 KB
/
githubServer.ts
File metadata and controls
70 lines (61 loc) · 1.83 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
import fetch, { RequestInit } from 'node-fetch';
import * as vscode from 'vscode';
import Logger from '../common/logger';
import { agent } from '../env/node/net';
import { HostHelper } from './configuration';
export class GitHubManager {
private _servers: Map<string, boolean> = new Map().set('github.com', true);
public async isGitHub(host: vscode.Uri): Promise<boolean> {
if (host === null) {
return false;
}
// .wiki/.git repos are not supported
if (host.path.endsWith('.wiki') || host.authority.match(/gist[.]github[.]com/)) {
return false;
}
if (this._servers.has(host.authority)) {
return !!this._servers.get(host.authority);
}
const [uri, options] = await GitHubManager.getOptions(host, 'HEAD', '/rate_limit');
let isGitHub = false;
try {
const response = await fetch(uri.toString(), options);
const gitHubHeader = response.headers.get('x-github-request-id');
isGitHub = ((gitHubHeader !== undefined) && (gitHubHeader !== null));
return isGitHub;
} catch (ex) {
Logger.appendLine(`No response from host ${host}: ${ex.message}`, 'GitHubServer');
return isGitHub;
} finally {
Logger.debug(`Host ${host} is associated with GitHub: ${isGitHub}`, 'GitHubServer');
this._servers.set(host.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));
return [
uri,
{
hostname: uri.authority,
port: 443,
method,
headers,
agent,
},
];
}
}