1- import { last } from 'lodash'
1+ import { last , take } from 'lodash'
22
33import { 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'> {
3934export 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 */
6869export 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 */
98102export 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 ( / m e r g e _ r e q u e s t s \/ ( .* ?) \/ d i f f s / )
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 ( / b l o b \/ ( .* ?) \/ / )
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
156168interface GitLabCommitPageInfo extends Pick < GitLabFileInfo , 'repoPath' > , Pick < GitLabInfo , 'owner' | 'repoName' > {
0 commit comments