Skip to content

Commit

Permalink
fix: char % in file path break blob api & bad sourcegraph lsif api pa…
Browse files Browse the repository at this point in the history
…rameters (#440)
  • Loading branch information
conwnet authored Sep 6, 2022
1 parent 460d7b2 commit f3250ae
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 26 deletions.
6 changes: 5 additions & 1 deletion extensions/github1s/src/adapters/sourcegraph/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author netcon
*/

import { ApolloClient, ApolloQueryResult, createHttpLink, InMemoryCache } from '@apollo/client/core';
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client/core';
import { isNil, trimEnd, trimStart } from '@/helpers/util';

const sourcegraphLink = createHttpLink({
Expand Down Expand Up @@ -49,3 +49,7 @@ export const combineGlobsToRegExp = (globs: string[]) => {
};

export const escapeRegexp = (text: string): string => text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');

export const buildRepoPattern = (repo: string) => {
return `^${escapeRegexp(repo)}$`;
};
20 changes: 5 additions & 15 deletions extensions/github1s/src/adapters/sourcegraph/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ import { getTextSearchResults } from './search';

type SupportedPlatfrom = 'github' | 'gitlab' | 'bitbucket';

const escapeRegexp = (text: string): string => text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');

export class SourcegraphDataSource extends DataSource {
private static instanceMap: Map<SupportedPlatfrom, SourcegraphDataSource> = new Map();
private refsPromiseMap: Map<string, Promise<{ branches: Branch[]; tags: Tag[] }>> = new Map();
Expand Down Expand Up @@ -71,10 +69,6 @@ export class SourcegraphDataSource extends DataSource {
return repo;
}

buildRepoPattern(repo: string) {
return `^${escapeRegexp(this.buildRepository(repo))}$`;
}

async provideDirectory(repo: string, ref: string, path: string, recursive = false): Promise<Directory> {
const directories = await readDirectory(this.buildRepository(repo), ref, path, recursive);
directories.entries.forEach((entry) => {
Expand Down Expand Up @@ -109,7 +103,7 @@ export class SourcegraphDataSource extends DataSource {
// sourcegraph api break binary files and text coding, so we use github api first here
if (this.platform === 'github') {
try {
return await fetch(`https://raw.githubusercontent.com/${repo}/${ref}/${path}`)
return await fetch(encodeURI(`https://raw.githubusercontent.com/${repo}/${ref}/${path}`))
.then((response) => response.arrayBuffer())
.then((buffer) => ({ content: new Uint8Array(buffer) }));
} catch (e) {}
Expand Down Expand Up @@ -177,8 +171,7 @@ export class SourcegraphDataSource extends DataSource {
query: TextSearchQuery,
options: TextSearchOptions
): Promise<TextSearchResults> {
const repoPattern = this.buildRepoPattern(repo);
return getTextSearchResults(repoPattern, ref, query, options);
return getTextSearchResults(this.buildRepository(repo), ref, query, options);
}

async provideCommits(repo: string, options?: CommitsQueryOptions): Promise<(Commit & { files?: ChangedFile[] })[]> {
Expand Down Expand Up @@ -216,8 +209,7 @@ export class SourcegraphDataSource extends DataSource {
character: number,
symbol: string
): Promise<SymbolDefinitions> {
const repoPattern = this.buildRepoPattern(repo);
return getSymbolDefinitions(repoPattern, ref, path, line, character, symbol);
return getSymbolDefinitions(this.buildRepository(repo), ref, path, line, character, symbol);
}

async provideSymbolReferences(
Expand All @@ -228,8 +220,7 @@ export class SourcegraphDataSource extends DataSource {
character: number,
symbol: string
): Promise<SymbolReferences> {
const repoPattern = this.buildRepoPattern(repo);
return getSymbolReferences(repoPattern, ref, path, line, character, symbol);
return getSymbolReferences(this.buildRepository(repo), ref, path, line, character, symbol);
}

async provideSymbolHover(
Expand All @@ -240,8 +231,7 @@ export class SourcegraphDataSource extends DataSource {
character: number,
_symbol: string
): Promise<SymbolHover | null> {
const repoPattern = this.buildRepoPattern(repo);
return getSymbolHover(repoPattern, ref, path, line, character);
return getSymbolHover(this.buildRepository(repo), ref, path, line, character);
}

provideUserAvatarLink(user: string): string {
Expand Down
2 changes: 1 addition & 1 deletion extensions/github1s/src/adapters/sourcegraph/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const getLSIFHover = async (
const response = await sourcegraphClient.query({
query: LSIFHoverQuery,
variables: {
repository: repository,
repository,
ref,
path: path.slice(1),
line,
Expand Down
5 changes: 3 additions & 2 deletions extensions/github1s/src/adapters/sourcegraph/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { gql } from '@apollo/client/core';
import { escapeRegexp, sourcegraphClient } from './common';
import { buildRepoPattern, escapeRegexp, sourcegraphClient } from './common';
import { CodeLocation } from '../types';

const searchPositionsQuery = gql`
Expand Down Expand Up @@ -48,7 +48,8 @@ const searchPositionsQuery = gql`
// get symbol position information base on search,
// used by definition, reference and hover
export const getSymbolPositions = async (repository: string, ref: string, symbol: string): Promise<CodeLocation[]> => {
const repoRefString = ref.toUpperCase() === 'HEAD' ? `repo:${repository}` : `repo:${repository}@${ref}`;
const repoPattern = buildRepoPattern(repository);
const repoRefString = ref.toUpperCase() === 'HEAD' ? `repo:${repoPattern}` : `repo:${repoPattern}@${ref}`;
const optionsString = ['context:global', 'type:symbol', 'patternType:regexp', 'case:yes'].join(' ');
const patternString = `^${escapeRegexp(symbol)}$`;
const query = [repoRefString, optionsString, patternString].join(' ');
Expand Down
9 changes: 5 additions & 4 deletions extensions/github1s/src/adapters/sourcegraph/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

import { gql } from '@apollo/client/core';
import { TextSearchOptions, TextSearchQuery, TextSearchResults } from '../types';
import { escapeRegexp, sourcegraphClient, combineGlobsToRegExp } from './common';
import { escapeRegexp, sourcegraphClient, combineGlobsToRegExp, buildRepoPattern } from './common';

// build text search query for sourcegraph graphql request
export const buildTextSearchQueryString = (
repo: string,
repository: string,
ref: string,
query: TextSearchQuery,
options: TextSearchOptions
): string => {
const repoPattern = buildRepoPattern(repository);
const countString = `count:${options.pageSize ? (options.page || 1) * options.pageSize : 100}`;
const repoRefQueryString = ref.toUpperCase() === 'HEAD' ? `repo:${repo}` : `repo:${repo}@${ref}`;
const repoRefString = ref.toUpperCase() === 'HEAD' ? `repo:${repoPattern}` : `repo:${repoPattern}@${ref}`;
// the string may looks like `case:yse file:src -file:node_modules`
const optionsString = [
query.isCaseSensitive ? `case:yes` : '',
Expand All @@ -37,7 +38,7 @@ export const buildTextSearchQueryString = (
return `/\b${patternString}\b/`;
}

return [repoRefQueryString, optionsString, patternString, countString].filter(Boolean).join(' ');
return [repoRefString, optionsString, patternString, countString].filter(Boolean).join(' ');
};

const textSearchQuery = gql`
Expand Down
2 changes: 1 addition & 1 deletion extensions/github1s/src/providers/text-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class GitHub1sTextSearchProvider implements vscode.TextSearchProvider, vs
return router.getAuthority().then(async (authority) => {
const [repo, ref] = authority.split('+');
const dataSource = await adapterManager.getCurrentAdapter().resolveDataSource();
const searchOptions = { page: 1, pageSize: 30, includes: options.includes, excludes: options.excludes };
const searchOptions = { page: 1, pageSize: 100, includes: options.includes, excludes: options.excludes };
const searchResults = await dataSource.provideTextSearchResults(repo, ref, query, searchOptions);
const currentScheme = adapterManager.getCurrentScheme();

Expand Down
4 changes: 2 additions & 2 deletions extensions/github1s/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ export class Router extends EventEmitter<RouterState> {
public async push(path: string) {
await this._barrier.wait();
const emptyState = { pathname: '', search: '', hash: '' };
return this._history!.push({ ...emptyState, ...parsePath(path) });
return this._history!.push({ ...emptyState, ...parsePath(encodeURI(path)) });
}

// replace the url with current history
public async replace(path: string) {
await this._barrier.wait();
const emptyState = { pathname: '', search: '', hash: '' };
return this._history!.replace({ ...emptyState, ...parsePath(path) });
return this._history!.replace({ ...emptyState, ...parsePath(encodeURI(path)) });
}

public async resolveParser(): Promise<RouterParser> {
Expand Down

0 comments on commit f3250ae

Please sign in to comment.