Share profiles (#166898)

* Share profiles #159891
- Share profile in GitHub
- Profile resource quick pick
- Import profile from vscode link

* remove duplicate code
This commit is contained in:
Sandeep Somavarapu
2022-11-21 21:03:02 +01:00
committed by GitHub
parent ebb77a7dfd
commit e43bf31ab1
19 changed files with 659 additions and 200 deletions

View File

@@ -12,6 +12,7 @@ import { DisposableStore, repositoryHasGitHubRemote } from './util';
import { GithubPushErrorHandler } from './pushErrorHandler';
import { GitBaseExtension } from './typings/git-base';
import { GithubRemoteSourcePublisher } from './remoteSourcePublisher';
import './importExportProfiles';
export function activate(context: ExtensionContext): void {
context.subscriptions.push(initializeGitBaseExtension());

View File

@@ -0,0 +1,80 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Octokit } from '@octokit/rest';
import * as vscode from 'vscode';
import { httpsOverHttp } from 'tunnel';
import { Agent, globalAgent } from 'https';
import { basename } from 'path';
import { URL } from 'url';
class GitHubGistProfileContentHandler implements vscode.ProfileContentHandler {
readonly name = vscode.l10n.t('GitHub');
private _octokit: Promise<Octokit> | undefined;
private getOctokit(): Promise<Octokit> {
if (!this._octokit) {
this._octokit = (async () => {
const session = await vscode.authentication.getSession('github', ['gist', 'user:email'], { createIfNone: true });
const token = session.accessToken;
const agent = this.getAgent();
const { Octokit } = await import('@octokit/rest');
return new Octokit({
request: { agent },
userAgent: 'GitHub VSCode',
auth: `token ${token}`
});
})();
}
return this._octokit;
}
private getAgent(url: string | undefined = process.env.HTTPS_PROXY): Agent {
if (!url) {
return globalAgent;
}
try {
const { hostname, port, username, password } = new URL(url);
const auth = username && password && `${username}:${password}`;
return httpsOverHttp({ proxy: { host: hostname, port, proxyAuth: auth } });
} catch (e) {
vscode.window.showErrorMessage(`HTTPS_PROXY environment variable ignored: ${e.message}`);
return globalAgent;
}
}
async saveProfile(name: string, content: string): Promise<vscode.Uri | null> {
const octokit = await this.getOctokit();
const result = await octokit.gists.create({
public: true,
files: {
[name]: {
content
}
}
});
return result.data.html_url ? vscode.Uri.parse(result.data.html_url) : null;
}
async readProfile(uri: vscode.Uri): Promise<string | null> {
const gist_id = basename(uri.path);
const octokit = await this.getOctokit();
try {
const gist = await octokit.gists.get({ gist_id });
if (gist.data.files) {
return gist.data.files[Object.keys(gist.data.files)[0]]?.content ?? null;
}
} catch (error) {
// ignore
}
return null;
}
}
vscode.window.registerProfileContentHandler('github', new GitHubGistProfileContentHandler());