Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit b5d8290

Browse files
authored
fix: more robust scraping to support org level repos (#219)
1 parent e401726 commit b5d8290

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/libs/gitlab/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface DiffVersionsResponse {
3434
type GetBaseCommitIDInput = Pick<GitLabDiffInfo, 'owner' | 'repoName' | 'mergeRequestID' | 'diffID'>
3535

3636
const buildURL = (owner: string, repoName: string, path: string) =>
37-
`${window.location.origin}/api/v4/projects/${owner}%2f${repoName}${path}`
37+
`${window.location.origin}/api/v4/projects/${encodeURIComponent(owner)}%2f${repoName}${path}`
3838

3939
const get = <T>(url: string): Observable<T> => ajax.get(url).pipe(map(({ response }) => response as T))
4040

src/libs/gitlab/scrape.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { last } from 'lodash'
1+
import { last, take } from 'lodash'
22

33
import { FileInfo } from '../code_intelligence'
44

@@ -18,11 +18,6 @@ export interface GitLabInfo {
1818
repoName: string
1919

2020
repoPath: string
21-
22-
/**
23-
* The parts of the URL following the repo name.
24-
*/
25-
urlParts: string[]
2621
}
2722

2823
/**
@@ -39,10 +34,17 @@ export interface GitLabFileInfo extends Pick<GitLabInfo, 'repoPath'> {
3934
export function getPageInfo(): GitLabInfo {
4035
const host = window.location.hostname
4136

42-
const parts = window.location.pathname.slice(1).split('/')
37+
const projectLink = document.querySelector<HTMLAnchorElement>('.context-header a')
38+
if (!projectLink) {
39+
throw new Error('Unable to determine project name')
40+
}
41+
42+
const projectName = new URL(projectLink.href).pathname.slice(1)
4343

44-
const owner = parts[0]
45-
const repoName = parts[1]
44+
const parts = projectName.split('/')
45+
46+
const owner = take(parts, parts.length - 1).join('/')
47+
const repoName = last(parts)!
4648

4749
let pageKind: GitLabPageKind
4850
if (window.location.pathname.includes(`${owner}/${repoName}/commit`)) {
@@ -57,7 +59,6 @@ export function getPageInfo(): GitLabInfo {
5759
owner,
5860
repoName,
5961
repoPath: [host, owner, repoName].join('/'),
60-
urlParts: parts.slice(2),
6162
pageKind,
6263
}
6364
}
@@ -66,12 +67,15 @@ export function getPageInfo(): GitLabInfo {
6667
* Gets information about a file view page.
6768
*/
6869
export function getFilePageInfo(): GitLabFileInfo {
69-
const { repoPath } = getPageInfo()
70+
const { repoPath, owner, repoName } = getPageInfo()
7071

71-
const parts = window.location.pathname.slice(1).split('/')
72+
const matches = window.location.pathname.match(new RegExp(`${owner}\/${repoName}\/blob\/(.*?)\/(.*)`))
73+
if (!matches) {
74+
throw new Error('Unable to determine revision or file path')
75+
}
7276

73-
const rev = parts[3]
74-
const filePath = parts.slice(4).join('/')
77+
const rev = matches[1]
78+
const filePath = matches[2]
7579

7680
return {
7781
repoPath,
@@ -96,15 +100,20 @@ export interface GitLabDiffInfo extends Pick<GitLabFileInfo, 'repoPath'>, Pick<G
96100
* Scrapes the DOM for the repo path and revision information.
97101
*/
98102
export function getDiffPageInfo(): GitLabDiffInfo {
99-
const { repoPath, owner, repoName, urlParts } = getPageInfo()
103+
const { repoPath, owner, repoName } = getPageInfo()
100104

101105
const query = new URLSearchParams(window.location.search)
102106

107+
const matches = window.location.pathname.match(/merge_requests\/(.*?)\/diffs/)
108+
if (!matches) {
109+
throw new Error('Unable to determine merge request ID')
110+
}
111+
103112
return {
104113
repoPath,
105114
owner,
106115
repoName,
107-
mergeRequestID: urlParts[1],
116+
mergeRequestID: matches[1],
108117
diffID: query.get('diff_id') || undefined,
109118
baseCommitID: query.get('start_sha') || undefined,
110119
}
@@ -148,9 +157,12 @@ export function getHeadCommitIDFromCodeView(codeView: HTMLElement): FileInfo['co
148157
}
149158

150159
const commitAnchor = commitSHA.closest('a')! as HTMLAnchorElement
151-
const hrefParts = new URL(commitAnchor.href).pathname.slice(1).split('/')
160+
const revMatch = new URL(commitAnchor.href).pathname.match(/blob\/(.*?)\//)
161+
if (!revMatch) {
162+
throw new Error('Unable to determine head revision from code view')
163+
}
152164

153-
return hrefParts[3]
165+
return revMatch[1]
154166
}
155167

156168
interface GitLabCommitPageInfo extends Pick<GitLabFileInfo, 'repoPath'>, Pick<GitLabInfo, 'owner' | 'repoName'> {

0 commit comments

Comments
 (0)