Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/configuration-editing/src/importExportProfiles.ts
3291 views
1
/*---------------------------------------------------------------------------------------------
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 { Octokit } from '@octokit/rest';
7
import * as vscode from 'vscode';
8
import { basename } from 'path';
9
import { agent } from './node/net';
10
11
class GitHubGistProfileContentHandler implements vscode.ProfileContentHandler {
12
13
readonly name = vscode.l10n.t('GitHub');
14
readonly description = vscode.l10n.t('gist');
15
16
private _octokit: Promise<Octokit> | undefined;
17
private getOctokit(): Promise<Octokit> {
18
if (!this._octokit) {
19
this._octokit = (async () => {
20
const session = await vscode.authentication.getSession('github', ['gist', 'user:email'], { createIfNone: true });
21
const token = session.accessToken;
22
23
const { Octokit } = await import('@octokit/rest');
24
25
return new Octokit({
26
request: { agent },
27
userAgent: 'GitHub VSCode',
28
auth: `token ${token}`
29
});
30
})();
31
}
32
return this._octokit;
33
}
34
35
async saveProfile(name: string, content: string): Promise<{ readonly id: string; readonly link: vscode.Uri } | null> {
36
const octokit = await this.getOctokit();
37
const result = await octokit.gists.create({
38
public: false,
39
files: {
40
[name]: {
41
content
42
}
43
}
44
});
45
if (result.data.id && result.data.html_url) {
46
const link = vscode.Uri.parse(result.data.html_url);
47
return { id: result.data.id, link };
48
}
49
return null;
50
}
51
52
private _public_octokit: Promise<Octokit> | undefined;
53
private getPublicOctokit(): Promise<Octokit> {
54
if (!this._public_octokit) {
55
this._public_octokit = (async () => {
56
const { Octokit } = await import('@octokit/rest');
57
return new Octokit({ request: { agent }, userAgent: 'GitHub VSCode' });
58
})();
59
}
60
return this._public_octokit;
61
}
62
63
async readProfile(id: string): Promise<string | null>;
64
async readProfile(uri: vscode.Uri): Promise<string | null>;
65
async readProfile(arg: string | vscode.Uri): Promise<string | null> {
66
const gist_id = typeof arg === 'string' ? arg : basename(arg.path);
67
const octokit = await this.getPublicOctokit();
68
try {
69
const gist = await octokit.gists.get({ gist_id });
70
if (gist.data.files) {
71
return gist.data.files[Object.keys(gist.data.files)[0]]?.content ?? null;
72
}
73
} catch (error) {
74
// ignore
75
}
76
return null;
77
}
78
79
}
80
81
vscode.window.registerProfileContentHandler('github', new GitHubGistProfileContentHandler());
82
83