Skip to content

migrate github-extension to ESM #246726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions build/lib/extensions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions build/lib/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { Stream } from 'stream';
import File from 'vinyl';
import { createStatsStream } from './stats';
import * as util2 from './util';
const vzip = require('gulp-vinyl-zip');
import filter from 'gulp-filter';
import rename from 'gulp-rename';
import fancyLog from 'fancy-log';
Expand All @@ -26,6 +25,7 @@ import { getProductionDependencies } from './dependencies';
import { IExtensionDefinition, getExtensionStream } from './builtInExtensions';
import { getVersion } from './getVersion';
import { fetchUrls, fetchGithub } from './fetch';
const vzip = require('gulp-vinyl-zip');

const root = path.dirname(path.dirname(__dirname));
const commit = getVersion(root);
Expand Down Expand Up @@ -62,7 +62,12 @@ function updateExtensionPackageJSON(input: Stream, update: (data: any) => any):
}

function fromLocal(extensionPath: string, forWeb: boolean, disableMangle: boolean): Stream {
const webpackConfigFileName = forWeb ? 'extension-browser.webpack.config.js' : 'extension.webpack.config.js';

const esm = JSON.parse(fs.readFileSync(path.join(extensionPath, 'package.json'), 'utf8')).type === 'module';

const webpackConfigFileName = forWeb
? `extension-browser.webpack.config.${!esm ? 'js' : 'cjs'}`
: `extension.webpack.config.${!esm ? 'js' : 'cjs'}`;

const isWebPacked = fs.existsSync(path.join(extensionPath, webpackConfigFileName));
let input = isWebPacked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ module.exports = withDefaults({
context: __dirname,
entry: {
extension: './src/extension.ts'
},
output: {
libraryTarget: 'module',
chunkFormat: 'module',
},
experiments: {
outputModule: true
}
});
362 changes: 141 additions & 221 deletions extensions/github/package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions extensions/github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"vscode.git-base"
],
"main": "./out/extension.js",
"type": "module",
"capabilities": {
"virtualWorkspaces": false,
"untrustedWorkspaces": {
Expand Down Expand Up @@ -227,9 +228,9 @@
"watch": "gulp watch-extension:github"
},
"dependencies": {
"@octokit/graphql": "5.0.5",
"@octokit/graphql": "8.2.0",
"@octokit/graphql-schema": "14.4.0",
"@octokit/rest": "19.0.4",
"@octokit/rest": "21.1.0",
"tunnel": "^0.0.6",
"@vscode/extension-telemetry": "^0.9.8"
},
Expand Down
4 changes: 2 additions & 2 deletions extensions/github/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { AuthenticationSession, authentication, window } from 'vscode';
import { Agent, globalAgent } from 'https';
import { graphql } from '@octokit/graphql/dist-types/types';
import { graphql } from '@octokit/graphql/types';
import { Octokit } from '@octokit/rest';
import { httpsOverHttp } from 'tunnel';
import { URL } from 'url';
Expand Down Expand Up @@ -71,7 +71,7 @@ export async function getOctokitGraphql(): Promise<graphql> {
const token = session.accessToken;
const { graphql } = await import('@octokit/graphql');

return graphql.defaults({
return graphql({
headers: {
authorization: `token ${token}`
},
Expand Down
10 changes: 5 additions & 5 deletions extensions/github/src/branchProtection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import { authentication, EventEmitter, LogOutputChannel, Memento, Uri, workspace } from 'vscode';
import { Repository as GitHubRepository, RepositoryRuleset } from '@octokit/graphql-schema';
import { AuthenticationError, getOctokitGraphql } from './auth';
import { API, BranchProtection, BranchProtectionProvider, BranchProtectionRule, Repository } from './typings/git';
import { DisposableStore, getRepositoryFromUrl } from './util';
import { AuthenticationError, getOctokitGraphql } from './auth.js';
import { API, BranchProtection, BranchProtectionProvider, BranchProtectionRule, Repository } from './typings/git.js';
import { DisposableStore, getRepositoryFromUrl } from './util.js';
import TelemetryReporter from '@vscode/extension-telemetry';

const REPOSITORY_QUERY = `
Expand Down Expand Up @@ -74,7 +74,7 @@ export class GitHubBranchProtectionProviderManager {
private readonly gitAPI: API,
private readonly globalState: Memento,
private readonly logger: LogOutputChannel,
private readonly telemetryReporter: TelemetryReporter) {
private readonly telemetryReporter: TelemetryReporter.default) {
this.disposables.add(this.gitAPI.onDidOpenRepository(repository => {
if (this._enabled) {
this.providerDisposables.add(gitAPI.registerBranchProtectionProvider(repository.rootUri, new GitHubBranchProtectionProvider(repository, this.globalState, this.logger, this.telemetryReporter)));
Expand Down Expand Up @@ -113,7 +113,7 @@ export class GitHubBranchProtectionProvider implements BranchProtectionProvider
private readonly repository: Repository,
private readonly globalState: Memento,
private readonly logger: LogOutputChannel,
private readonly telemetryReporter: TelemetryReporter) {
private readonly telemetryReporter: TelemetryReporter.default) {
// Restore branch protection from global state
this.branchProtection = this.globalState.get<BranchProtection[]>(this.globalStateKey, []);

Expand Down
2 changes: 1 addition & 1 deletion extensions/github/src/canonicalUriProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { CancellationToken, CanonicalUriProvider, CanonicalUriRequestOptions, Disposable, ProviderResult, Uri, workspace } from 'vscode';
import { API } from './typings/git';
import { API } from './typings/git.js';

const SUPPORTED_SCHEMES = ['ssh', 'https', 'file'];

Expand Down
8 changes: 4 additions & 4 deletions extensions/github/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { API as GitAPI, RefType, Repository } from './typings/git';
import { publishRepository } from './publish';
import { DisposableStore, getRepositoryFromUrl } from './util';
import { LinkContext, getCommitLink, getLink, getVscodeDevHost } from './links';
import { API as GitAPI, RefType, Repository } from './typings/git.js';
import { publishRepository } from './publish.js';
import { DisposableStore, getRepositoryFromUrl } from './util.js';
import { LinkContext, getCommitLink, getLink, getVscodeDevHost } from './links.js';

async function copyVscodeDevLink(gitAPI: GitAPI, useSelection: boolean, context: LinkContext, includeRange = true) {
try {
Expand Down
4 changes: 2 additions & 2 deletions extensions/github/src/credentialProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { CredentialsProvider, Credentials, API as GitAPI } from './typings/git';
import { CredentialsProvider, Credentials, API as GitAPI } from './typings/git.js';
import { workspace, Uri, Disposable } from 'vscode';
import { getSession } from './auth';
import { getSession } from './auth.js';

const EmptyDisposable: Disposable = { dispose() { } };

Expand Down
30 changes: 15 additions & 15 deletions extensions/github/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

import { commands, Disposable, ExtensionContext, extensions, l10n, LogLevel, LogOutputChannel, window } from 'vscode';
import TelemetryReporter from '@vscode/extension-telemetry';
import { GithubRemoteSourceProvider } from './remoteSourceProvider';
import { API, GitExtension } from './typings/git';
import { registerCommands } from './commands';
import { GithubCredentialProviderManager } from './credentialProvider';
import { DisposableStore, repositoryHasGitHubRemote } from './util';
import { GithubPushErrorHandler } from './pushErrorHandler';
import { GitBaseExtension } from './typings/git-base';
import { GithubRemoteSourcePublisher } from './remoteSourcePublisher';
import { GitHubBranchProtectionProviderManager } from './branchProtection';
import { GitHubCanonicalUriProvider } from './canonicalUriProvider';
import { VscodeDevShareProvider } from './shareProviders';
import { GitHubSourceControlHistoryItemDetailsProvider } from './historyItemDetailsProvider';
import { GithubRemoteSourceProvider } from './remoteSourceProvider.js';
import { API, GitExtension } from './typings/git.js';
import { registerCommands } from './commands.js';
import { GithubCredentialProviderManager } from './credentialProvider.js';
import { DisposableStore, repositoryHasGitHubRemote } from './util.js';
import { GithubPushErrorHandler } from './pushErrorHandler.js';
import { GitBaseExtension } from './typings/git-base.js';
import { GithubRemoteSourcePublisher } from './remoteSourcePublisher.js';
import { GitHubBranchProtectionProviderManager } from './branchProtection.js';
import { GitHubCanonicalUriProvider } from './canonicalUriProvider.js';
import { VscodeDevShareProvider } from './shareProviders.js';
import { GitHubSourceControlHistoryItemDetailsProvider } from './historyItemDetailsProvider.js';

export function activate(context: ExtensionContext): void {
const disposables: Disposable[] = [];
Expand All @@ -31,8 +31,8 @@ export function activate(context: ExtensionContext): void {
disposables.push(logger.onDidChangeLogLevel(onDidChangeLogLevel));
onDidChangeLogLevel(logger.logLevel);

const { aiKey } = require('../package.json') as { aiKey: string };
const telemetryReporter = new TelemetryReporter(aiKey);
const { aiKey } = context.extension.packageJSON as { aiKey: string };
const telemetryReporter = new TelemetryReporter.default(aiKey);
disposables.push(telemetryReporter);

disposables.push(initializeGitBaseExtension());
Expand Down Expand Up @@ -84,7 +84,7 @@ function setGitHubContext(gitAPI: API, disposables: DisposableStore) {
}
}

function initializeGitExtension(context: ExtensionContext, telemetryReporter: TelemetryReporter, logger: LogOutputChannel): Disposable {
function initializeGitExtension(context: ExtensionContext, telemetryReporter: TelemetryReporter.default, logger: LogOutputChannel): Disposable {
const disposables = new DisposableStore();

let gitExtension = extensions.getExtension<GitExtension>('vscode.git');
Expand Down
8 changes: 4 additions & 4 deletions extensions/github/src/historyItemDetailsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import { authentication, Command, l10n, LogOutputChannel, workspace } from 'vscode';
import { Commit, Repository as GitHubRepository, Maybe } from '@octokit/graphql-schema';
import { API, AvatarQuery, AvatarQueryCommit, Repository, SourceControlHistoryItemDetailsProvider } from './typings/git';
import { DisposableStore, getRepositoryDefaultRemote, getRepositoryDefaultRemoteUrl, getRepositoryFromUrl, groupBy, sequentialize } from './util';
import { AuthenticationError, getOctokitGraphql } from './auth';
import { getAvatarLink } from './links';
import { API, AvatarQuery, AvatarQueryCommit, Repository, SourceControlHistoryItemDetailsProvider } from './typings/git.js';
import { DisposableStore, getRepositoryDefaultRemote, getRepositoryDefaultRemoteUrl, getRepositoryFromUrl, groupBy, sequentialize } from './util.js';
import { AuthenticationError, getOctokitGraphql } from './auth.js';
import { getAvatarLink } from './links.js';

const ISSUE_EXPRESSION = /(([A-Za-z0-9_.\-]+)\/([A-Za-z0-9_.\-]+))?(#|GH-)([1-9][0-9]*)($|\b)/g;

Expand Down
4 changes: 2 additions & 2 deletions extensions/github/src/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { API as GitAPI, RefType, Repository } from './typings/git';
import { getRepositoryFromUrl, repositoryHasGitHubRemote } from './util';
import { API as GitAPI, RefType, Repository } from './typings/git.js';
import { getRepositoryFromUrl, repositoryHasGitHubRemote } from './util.js';

export function isFileInRepo(repository: Repository, file: vscode.Uri): boolean {
return file.path.toLowerCase() === repository.rootUri.path.toLowerCase() ||
Expand Down
6 changes: 3 additions & 3 deletions extensions/github/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { API as GitAPI, Repository } from './typings/git';
import { getOctokit } from './auth';
import { API as GitAPI, Repository } from './typings/git.js';
import { getOctokit } from './auth.js';
import { TextEncoder } from 'util';
import { basename } from 'path';
import { Octokit } from '@octokit/rest';
import { isInCodespaces } from './pushErrorHandler';
import { isInCodespaces } from './pushErrorHandler.js';

function sanitizeRepositoryName(value: string): string {
return value.trim().replace(/[^a-z0-9_.]/ig, '-');
Expand Down
10 changes: 6 additions & 4 deletions extensions/github/src/pushErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

import { TextDecoder } from 'util';
import { commands, env, ProgressLocation, Uri, window, workspace, QuickPickOptions, FileType, l10n, Disposable, TextDocumentContentProvider } from 'vscode';
import TelemetryReporter from '@vscode/extension-telemetry';
import { getOctokit } from './auth';
import { GitErrorCodes, PushErrorHandler, Remote, Repository } from './typings/git';
import { getOctokit } from './auth.js';
import { GitErrorCodes, PushErrorHandler, Remote, Repository } from './typings/git.js';
import * as path from 'path';
import TelemetryReporter from '@vscode/extension-telemetry';



type Awaited<T> = T extends PromiseLike<infer U> ? Awaited<U> : T;

Expand Down Expand Up @@ -100,7 +102,7 @@ export class GithubPushErrorHandler implements PushErrorHandler {
private disposables: Disposable[] = [];
private commandErrors = new CommandErrorOutputTextDocumentContentProvider();

constructor(private readonly telemetryReporter: TelemetryReporter) {
constructor(private readonly telemetryReporter: TelemetryReporter.default) {
this.disposables.push(workspace.registerTextDocumentContentProvider('github-output', this.commandErrors));
}

Expand Down
8 changes: 4 additions & 4 deletions extensions/github/src/remoteSourceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*--------------------------------------------------------------------------------------------*/

import { Uri, env, l10n, workspace } from 'vscode';
import { RemoteSourceProvider, RemoteSource, RemoteSourceAction } from './typings/git-base';
import { getOctokit } from './auth';
import { RemoteSourceProvider, RemoteSource, RemoteSourceAction } from './typings/git-base.js';
import { getOctokit } from './auth.js';
import { Octokit } from '@octokit/rest';
import { getRepositoryFromQuery, getRepositoryFromUrl } from './util';
import { getBranchLink, getVscodeDevHost } from './links';
import { getRepositoryFromQuery, getRepositoryFromUrl } from './util.js';
import { getBranchLink, getVscodeDevHost } from './links.js';

function asRemoteSource(raw: any): RemoteSource {
const protocol = workspace.getConfiguration('github').get<'https' | 'ssh'>('gitProtocol');
Expand Down
4 changes: 2 additions & 2 deletions extensions/github/src/remoteSourcePublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { publishRepository } from './publish';
import { API as GitAPI, RemoteSourcePublisher, Repository } from './typings/git';
import { publishRepository } from './publish.js';
import { API as GitAPI, RemoteSourcePublisher, Repository } from './typings/git.js';

export class GithubRemoteSourcePublisher implements RemoteSourcePublisher {
readonly name = 'GitHub';
Expand Down
6 changes: 3 additions & 3 deletions extensions/github/src/shareProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { API } from './typings/git';
import { getRepositoryFromUrl, repositoryHasGitHubRemote } from './util';
import { encodeURIComponentExceptSlashes, ensurePublished, getRepositoryForFile, notebookCellRangeString, rangeString } from './links';
import { API } from './typings/git.js';
import { getRepositoryFromUrl, repositoryHasGitHubRemote } from './util.js';
import { encodeURIComponentExceptSlashes, ensurePublished, getRepositoryForFile, notebookCellRangeString, rangeString } from './links.js';

export class VscodeDevShareProvider implements vscode.ShareProvider, vscode.Disposable {
readonly id: string = 'copyVscodeDevLink';
Expand Down
2 changes: 1 addition & 1 deletion extensions/github/src/test/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import 'mocha';
import * as assert from 'assert';
import { workspace, extensions, Uri, commands } from 'vscode';
import { findPullRequestTemplates, pickPullRequestTemplate } from '../pushErrorHandler';
import { findPullRequestTemplates, pickPullRequestTemplate } from '../pushErrorHandler.js';

suite('github smoke test', function () {
const cwd = workspace.workspaceFolders![0].uri;
Expand Down
4 changes: 2 additions & 2 deletions extensions/github/src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import * as path from 'path';
import * as testRunner from '../../../../test/integration/electron/testrunner';
import * as testRunner from '../../../../test/integration/electron/testrunner.js';

const suite = 'Github Tests';

Expand All @@ -27,4 +27,4 @@ if (process.env.BUILD_ARTIFACTSTAGINGDIRECTORY) {

testRunner.configure(options);

export = testRunner;
export default testRunner;
2 changes: 1 addition & 1 deletion extensions/github/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { Repository } from './typings/git';
import { Repository } from './typings/git.js';

export class DisposableStore {

Expand Down
4 changes: 4 additions & 0 deletions extensions/github/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "./out",
"skipLibCheck": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"typeRoots": [
"./node_modules/@types"
]
Expand Down
Loading