Skip to content

Commit

Permalink
[Code] Support case insensitive repository uri in files Apis and LSP …
Browse files Browse the repository at this point in the history
…requests (#41665) (#41849)

* [Code] Support case insensitive repository uri in files Apis and LSP requests.

Use the original repository uri stored in the meta object corresponding to the given uri
rather than the given uri directly.

* [Code] refined returned type of getRepoUriFromMeta
  • Loading branch information
Yang Yang authored Jul 24, 2019
1 parent b0dfc80 commit a8cbd1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class LspIndexerFactory implements IndexerFactory {
this.log.info(`Create indexer to index ${repoUri} from ${indexedRevision} to ${revision}`);
// Create the indexer to index only the diff between these 2 revisions.
return new LspIncrementalIndexer(
repoUri,
repo.uri,
revision,
indexedRevision,
this.lspService,
Expand All @@ -52,7 +52,7 @@ export class LspIndexerFactory implements IndexerFactory {
this.log.info(`Create indexer to index ${repoUri} at ${revision}`);
// Create the indexer to index the entire repository.
return new LspIndexer(
repoUri,
repo.uri,
revision,
this.lspService,
this.options,
Expand Down
56 changes: 29 additions & 27 deletions x-pack/legacy/plugins/code/server/routes/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ import { TEXT_FILE_LIMIT } from '../../common/file';
import { decodeRevisionString } from '../../common/uri_util';

export function fileRoute(server: CodeServerRouter, gitOps: GitOperations) {
async function repoExists(req: hapi.Request, repoUri: string) {
async function getRepoUriFromMeta(
req: hapi.Request,
repoUri: string
): Promise<string | undefined> {
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(req));

try {
// Check if the repository already exists
await repoObjectClient.getRepository(repoUri);
return true;
const repo = await repoObjectClient.getRepository(repoUri);
return repo.uri;
} catch (e) {
return false;
return undefined;
}
}

Expand All @@ -43,13 +45,13 @@ export function fileRoute(server: CodeServerRouter, gitOps: GitOperations) {
const skip = queries.skip ? parseInt(queries.skip as string, 10) : 0;
const withParents = 'parents' in queries;
const flatten = 'flatten' in queries;
const repoExist = await repoExists(req, uri);
if (!repoExist) {
const repoUri = await getRepoUriFromMeta(req, uri);
if (!repoUri) {
return Boom.notFound(`repo ${uri} not found`);
}

try {
return await gitOps.fileTree(uri, path, revision, skip, limit, withParents, flatten);
return await gitOps.fileTree(repoUri, path, revision, skip, limit, withParents, flatten);
} catch (e) {
if (e.isBoom) {
return e;
Expand All @@ -66,12 +68,12 @@ export function fileRoute(server: CodeServerRouter, gitOps: GitOperations) {
async handler(req: hapi.Request, h: hapi.ResponseToolkit) {
const { uri, path, ref } = req.params;
const revision = decodeRevisionString(ref);
const repoExist = await repoExists(req, uri);
if (!repoExist) {
const repoUri = await getRepoUriFromMeta(req, uri);
if (!repoUri) {
return Boom.notFound(`repo ${uri} not found`);
}
try {
const blob = await gitOps.fileContent(uri, path, decodeURIComponent(revision));
const blob = await gitOps.fileContent(repoUri, path, decodeURIComponent(revision));
if (blob.isBinary()) {
const type = fileType(blob.content());
if (type && type.mime && type.mime.startsWith('image/')) {
Expand Down Expand Up @@ -126,12 +128,12 @@ export function fileRoute(server: CodeServerRouter, gitOps: GitOperations) {
async handler(req, h: hapi.ResponseToolkit) {
const { uri, path, ref } = req.params;
const revision = decodeRevisionString(ref);
const repoExist = await repoExists(req, uri);
if (!repoExist) {
const repoUri = await getRepoUriFromMeta(req, uri);
if (!repoUri) {
return Boom.notFound(`repo ${uri} not found`);
}
try {
const blob = await gitOps.fileContent(uri, path, revision);
const blob = await gitOps.fileContent(repoUri, path, revision);
if (blob.isBinary()) {
return h.response(blob.content()).type('application/octet-stream');
} else {
Expand Down Expand Up @@ -166,12 +168,12 @@ export function fileRoute(server: CodeServerRouter, gitOps: GitOperations) {
const count = queries.count ? parseInt(queries.count as string, 10) : 10;
const after = queries.after !== undefined;
try {
const repoExist = await repoExists(req, uri);
if (!repoExist) {
const repoUri = await getRepoUriFromMeta(req, uri);
if (!repoUri) {
return Boom.notFound(`repo ${uri} not found`);
}
const repository = await gitOps.openRepo(uri);
const commit = await gitOps.getCommitInfo(uri, revision);
const repository = await gitOps.openRepo(repoUri);
const commit = await gitOps.getCommitInfo(repoUri, revision);
if (commit === null) {
throw Boom.notFound(`commit ${revision} not found in repo ${uri}`);
}
Expand Down Expand Up @@ -206,12 +208,12 @@ export function fileRoute(server: CodeServerRouter, gitOps: GitOperations) {
method: 'GET',
async handler(req) {
const uri = req.params.uri;
const repoExist = await repoExists(req, uri);
if (!repoExist) {
const repoUri = await getRepoUriFromMeta(req, uri);
if (!repoUri) {
return Boom.notFound(`repo ${uri} not found`);
}
try {
return await gitOps.getBranchAndTags(uri);
return await gitOps.getBranchAndTags(repoUri);
} catch (e) {
if (e.isBoom) {
return e;
Expand All @@ -227,12 +229,12 @@ export function fileRoute(server: CodeServerRouter, gitOps: GitOperations) {
method: 'GET',
async handler(req) {
const { uri, revision } = req.params;
const repoExist = await repoExists(req, uri);
if (!repoExist) {
const repoUri = await getRepoUriFromMeta(req, uri);
if (!repoUri) {
return Boom.notFound(`repo ${uri} not found`);
}
try {
const diff = await gitOps.getCommitDiff(uri, decodeRevisionString(revision));
const diff = await gitOps.getCommitDiff(repoUri, decodeRevisionString(revision));
return diff;
} catch (e) {
if (e.isBoom) {
Expand All @@ -249,13 +251,13 @@ export function fileRoute(server: CodeServerRouter, gitOps: GitOperations) {
method: 'GET',
async handler(req) {
const { uri, path, revision } = req.params;
const repoExist = await repoExists(req, uri);
if (!repoExist) {
const repoUri = await getRepoUriFromMeta(req, uri);
if (!repoUri) {
return Boom.notFound(`repo ${uri} not found`);
}
try {
const blames = await gitOps.blame(
uri,
repoUri,
decodeRevisionString(decodeURIComponent(revision)),
path
);
Expand Down

0 comments on commit a8cbd1f

Please sign in to comment.