Skip to content

Commit

Permalink
Merge pull request #409 from conwnet/master
Browse files Browse the repository at this point in the history
release 0.7.3
  • Loading branch information
conwnet authored May 26, 2022
2 parents 8ac2f3b + 06b1e71 commit d9d7c95
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 43 deletions.
13 changes: 7 additions & 6 deletions api/vscode-unpkg/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ module.exports = async (req, res) => {
const publisher = matches[1];
const restPartsPath = matches[2];
const requestUrl = `https://${publisher}.vscode-unpkg.net/${publisher}/${restPartsPath}`;
const responsePromise = got(requestUrl);
const bufferPromise = responsePromise.buffer();
const [response, buffer] = await Promise.all([responsePromise, bufferPromise]);
const response = await got(requestUrl).catch((error) => {
return error.response || { statusCode: 500, headers: {}, body: error.message };
});

res.status(response.statusCode);
res.setHeader('cache-control', response.headers['cache-control']);
res.setHeader('content-type', response.headers['content-type']);
return res.send(buffer);
['cache-control', 'content-type'].forEach((headerKey) => {
response.headers[headerKey] && res.setHeader(headerKey, response.headers[headerKey]);
});
return res.send(response.body);
};
6 changes: 5 additions & 1 deletion extensions/github1s/src/adapters/github1s/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ export class GitHub1sDataSource extends DataSource {
const { owner, repo } = parseRepoFullName(repoFullName);
const requestParams = { owner, repo, ref };
const { data } = await fetcher.request('GET /repos/{owner}/{repo}/git/matching-refs/{ref}', requestParams);
return data.map((item) => ({ name: item.ref.slice(ref === 'heads' ? 11 : 10), commitSha: item.object.sha }));
return data.map((item) => ({
name: item.ref.slice(ref === 'heads' ? 11 : 10),
commitSha: item.object.sha,
description: `${ref === 'heads' ? 'Branch' : 'Tag'} at ${item.object.sha.slice(0, 8)}`,
}));
}
);

Expand Down
2 changes: 2 additions & 0 deletions extensions/github1s/src/adapters/sourcegraph/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ export const getAllRefs = async (repository: string): Promise<{ branches: Branch
const branches = (repositoryData.branches?.nodes || []).map?.((branch) => ({
name: branch.displayName,
commitSha: branch.target?.commit?.oid,
description: `Branch at ${branch.target?.commit?.oid?.slice(0, 8)}`,
}));
const tags = (repositoryData.tags?.nodes || []).map?.((tag) => ({
name: tag?.displayName,
commitSha: tag?.target?.commit?.oid,
description: `Tag at ${tag?.target?.commit?.oid?.slice(0, 8)}`,
}));
return { branches, tags };
};
6 changes: 4 additions & 2 deletions extensions/github1s/src/adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ export interface SymbolicLink {

export interface Branch {
name: string;
commitSha: string;
commitSha?: string;
description?: string;
}

export interface Tag {
name: string;
commitSha: string;
commitSha?: string;
description?: string;
}

export type CommitsQueryOptions = { from?: string; author?: string; path?: string } & CommonQueryOptions;
Expand Down
58 changes: 36 additions & 22 deletions extensions/github1s/src/commands/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,52 @@ import router from '@/router';
import { adapterManager } from '@/adapters';
import { Repository } from '@/repository';

const loadMorePickerItem: vscode.QuickPickItem = {
label: '$(more) Load More',
alwaysShow: true,
};

const checkoutToItem: vscode.QuickPickItem = {
label: '$(debug-disconnect) Checkout detached',
alwaysShow: true,
};

// check out to branch/tag/commit
const commandCheckoutTo = async () => {
const routerParser = await router.resolveParser();
const routeState = await router.getState();

const scheme = adapterManager.getCurrentScheme();
const repository = Repository.getInstance(scheme, routeState.repo);
const [branchRefs, tagRefs] = await Promise.all([repository.getBranchList(), repository.getTagList()]);
const branchPickerItems: vscode.QuickPickItem[] = branchRefs.map((branchRef) => ({
label: branchRef.name,
description: (branchRef.commitSha || '').slice(0, 8),
}));
const tagPickerItems: vscode.QuickPickItem[] = tagRefs.map((tagRef) => ({
label: tagRef.name,
description: `Tag at ${(tagRef.commitSha || '').slice(0, 8)}`,
}));

const quickPick = vscode.window.createQuickPick();
quickPick.placeholder = routeState.ref;
quickPick.items = [...branchPickerItems, ...tagPickerItems];

const loadMoreRefPickerItems = async () => {
quickPick.busy = true;
const scheme = adapterManager.getCurrentScheme();
const repository = Repository.getInstance(scheme, routeState.repo);
await Promise.all([repository.loadMoreBranches(), repository.loadMoreTags()]);
const [branchRefs, tagRefs] = await Promise.all([repository.getBranchList(), repository.getTagList()]);
const refPickerItems = [...branchRefs, ...tagRefs].map((ref) => ({
label: ref.name,
description: ref.description,
}));
const hasMore = (await Promise.all([repository.hasMoreBranches(), repository.hasMoreTags()])).some(Boolean);
quickPick.items = [...refPickerItems, hasMore ? loadMorePickerItem : null!, checkoutToItem].filter(Boolean);
quickPick.busy = false;
};

quickPick.placeholder = 'Input a ref to checkout';
quickPick.items = [checkoutToItem];
loadMoreRefPickerItems();
quickPick.show();
const choice = await new Promise<vscode.QuickPickItem | undefined>((resolve) =>
quickPick.onDidAccept(() => resolve(quickPick.activeItems[0]))
);
quickPick.hide();

const selectedRef = choice?.label || quickPick.value;
if (selectedRef) {
quickPick.onDidAccept(async () => {
const choice = quickPick.activeItems[0];
if (choice === loadMorePickerItem) {
return loadMoreRefPickerItems();
}
const selectedRef = choice === checkoutToItem ? quickPick.value : choice?.label;
const targetRef = selectedRef.toUpperCase() !== 'HEAD' ? selectedRef : undefined;
router.push(await routerParser.buildTreePath(routeState.repo, targetRef));
}
quickPick.hide();
});
};

export const registerRefCommands = (context: vscode.ExtensionContext) => {
Expand Down
7 changes: 2 additions & 5 deletions extensions/github1s/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,10 @@ const initialVSCodeState = async () => {
let documentShowOptions: vscode.TextDocumentShowOptions = {};
if (startLine || endLine) {
const startPosition = new vscode.Position((startLine || endLine)! - 1, 0);
const endPosition = new vscode.Position((endLine || startLine)! - 1, 999999);
const endPosition = new vscode.Position((endLine || startLine)! - 1, 1 << 20);
documentShowOptions = { selection: new vscode.Range(startPosition, endPosition) };
}
// TODO: the selection of the opening file may be cleared
// when editor try to restore previous state in the same file
vscode.commands.executeCommand(
'vscode.open',
vscode.window.showTextDocument(
vscode.Uri.parse('').with({ scheme, path: `/${routerState.filePath}` }),
documentShowOptions
);
Expand Down
6 changes: 3 additions & 3 deletions extensions/github1s/src/listeners/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const handleRouterOnTextEditorSelectionChange = async (editor: vscode.TextEditor
const routerParser = await router.resolveParser();

// only add the line number anchor when pageType is PageType.Blob
if (pageType !== PageType.Blob || !editor.selection) {
if (pageType !== PageType.Blob || !editor?.selection) {
return;
}

Expand All @@ -63,11 +63,11 @@ const handleRouterOnTextEditorSelectionChange = async (editor: vscode.TextEditor
repo,
ref,
activeFileUri.path.slice(1),
editor.selection.start.line + 1,
!editor.selection.isEmpty ? editor.selection.start.line + 1 : undefined,
editor.selection.end.line !== editor.selection.start.line ? editor.selection.end.line + 1 : undefined
);

router.replace(browserPath);
browserPath !== (await router.getPath()) && router.replace(browserPath);
};

// refresh file history view if active editor changed
Expand Down
6 changes: 6 additions & 0 deletions extensions/github1s/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export class Router extends EventEmitter<RouterState> {
return this._history!;
}

public async getPath() {
await this._barrier.wait();
const { pathname, search, hash } = this._history!.location;
return `${pathname}${search}${hash}`;
}

// push the url with current history
public async push(path: string) {
await this._barrier.wait();
Expand Down
2 changes: 1 addition & 1 deletion extensions/github1s/src/views/code-review-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const statusIconMap = {

export const getCodeReviewTreeItemLabel = (codeReview: adapterTypes.CodeReview) => {
const statusIcon = statusIconMap[getCodeReviewStatus(codeReview)];
return `${statusIcon} #${codeReview.id} ${codeReview.title}`;
return `${statusIcon} #${codeReview.id} ${codeReview.title.split(/[\r\n]/)[0]}`;
};

export const getCodeReviewTreeItemDescription = (codeReview: adapterTypes.CodeReview) => {
Expand Down
8 changes: 5 additions & 3 deletions extensions/github1s/src/views/commit-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { getChangedFileDiffCommand, getCommitChangedFiles } from '@/changes/file
import { GitHub1sSourceControlDecorationProvider } from '@/providers/decorations/source-control';

export const getCommitTreeItemDescription = (commit: adapterTypes.Commit): string => {
return [commit.sha.slice(0, 7), commit.author, relativeTimeTo(commit.createTime)].join(', ');
const shortCommitSha = commit.sha.slice(0, 7);
const relativeTimeStr = commit.createTime ? relativeTimeTo(commit.createTime) : null;
return [shortCommitSha, commit.author, relativeTimeStr].filter(Boolean).join(', ');
};

export interface CommitTreeItem extends vscode.TreeItem {
Expand Down Expand Up @@ -91,9 +93,9 @@ export class CommitTreeDataProvider implements vscode.TreeDataProvider<vscode.Tr
const repository = Repository.getInstance(currentAdapter.scheme, repo);
const repositoryCommits = await repository.getCommitList(ref, filePath, this._forceUpdate);
const commitTreeItems = repositoryCommits.map((commit) => {
const label = `${commit.message}`;
const label = commit.message.split(/[\r\n]/)[0];
const description = getCommitTreeItemDescription(commit);
const tooltip = `${label} (${description})`;
const tooltip = `${commit.message}\n(${description})`;
const iconPath = vscode.Uri.parse(commit.avatarUrl || '');
const contextValue = 'github1s:viewItems:commitListItem';

Expand Down

1 comment on commit d9d7c95

@vercel
Copy link

@vercel vercel bot commented on d9d7c95 May 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.