From 8e594766f8700597bda750391a6684a662403c9d Mon Sep 17 00:00:00 2001 From: Isaac Snow Date: Mon, 1 Oct 2018 14:41:01 -0700 Subject: [PATCH] fix: more robust scraping to support org level repos --- src/libs/gitlab/api.ts | 2 +- src/libs/gitlab/scrape.ts | 48 ++++++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/libs/gitlab/api.ts b/src/libs/gitlab/api.ts index f1717f8a..ee910ba4 100644 --- a/src/libs/gitlab/api.ts +++ b/src/libs/gitlab/api.ts @@ -34,7 +34,7 @@ interface DiffVersionsResponse { type GetBaseCommitIDInput = Pick const buildURL = (owner: string, repoName: string, path: string) => - `${window.location.origin}/api/v4/projects/${owner}%2f${repoName}${path}` + `${window.location.origin}/api/v4/projects/${encodeURIComponent(owner)}%2f${repoName}${path}` const get = (url: string): Observable => ajax.get(url).pipe(map(({ response }) => response as T)) diff --git a/src/libs/gitlab/scrape.ts b/src/libs/gitlab/scrape.ts index 23e253f3..6e0d1c50 100644 --- a/src/libs/gitlab/scrape.ts +++ b/src/libs/gitlab/scrape.ts @@ -1,4 +1,4 @@ -import { last } from 'lodash' +import { last, take } from 'lodash' import { FileInfo } from '../code_intelligence' @@ -18,11 +18,6 @@ export interface GitLabInfo { repoName: string repoPath: string - - /** - * The parts of the URL following the repo name. - */ - urlParts: string[] } /** @@ -39,10 +34,17 @@ export interface GitLabFileInfo extends Pick { export function getPageInfo(): GitLabInfo { const host = window.location.hostname - const parts = window.location.pathname.slice(1).split('/') + const projectLink = document.querySelector('.context-header a') + if (!projectLink) { + throw new Error('Unable to determine project name') + } + + const projectName = new URL(projectLink.href).pathname.slice(1) - const owner = parts[0] - const repoName = parts[1] + const parts = projectName.split('/') + + const owner = take(parts, parts.length - 1).join('/') + const repoName = last(parts)! let pageKind: GitLabPageKind if (window.location.pathname.includes(`${owner}/${repoName}/commit`)) { @@ -57,7 +59,6 @@ export function getPageInfo(): GitLabInfo { owner, repoName, repoPath: [host, owner, repoName].join('/'), - urlParts: parts.slice(2), pageKind, } } @@ -66,12 +67,15 @@ export function getPageInfo(): GitLabInfo { * Gets information about a file view page. */ export function getFilePageInfo(): GitLabFileInfo { - const { repoPath } = getPageInfo() + const { repoPath, owner, repoName } = getPageInfo() - const parts = window.location.pathname.slice(1).split('/') + const matches = window.location.pathname.match(new RegExp(`${owner}\/${repoName}\/blob\/(.*?)\/(.*)`)) + if (!matches) { + throw new Error('Unable to determine revision or file path') + } - const rev = parts[3] - const filePath = parts.slice(4).join('/') + const rev = matches[1] + const filePath = matches[2] return { repoPath, @@ -96,15 +100,20 @@ export interface GitLabDiffInfo extends Pick, Pick, Pick {