@@ -8,6 +8,7 @@ import { WithRequired } from "./types.js"
88import { marshalBool } from "./utils.js" ;
99import { GerritConnectionConfig , GiteaConnectionConfig , GitlabConnectionConfig } from '@sourcebot/schemas/v3/connection.type' ;
1010import { RepoMetadata } from './types.js' ;
11+ import path from 'path' ;
1112
1213export type RepoData = WithRequired < Prisma . RepoCreateInput , 'connections' > ;
1314
@@ -29,10 +30,13 @@ export const compileGithubConfig = async (
2930 const notFound = gitHubReposResult . notFound ;
3031
3132 const hostUrl = config . url ?? 'https://github.com' ;
32- const hostname = new URL ( hostUrl ) . hostname ;
33+ const repoNameRoot = new URL ( hostUrl )
34+ . toString ( )
35+ . replace ( / ^ h t t p s ? : \/ \/ / , '' ) ;
3336
3437 const repos = gitHubRepos . map ( ( repo ) => {
35- const repoName = `${ hostname } /${ repo . full_name } ` ;
38+ const repoDisplayName = repo . full_name ;
39+ const repoName = path . join ( repoNameRoot , repoDisplayName ) ;
3640 const cloneUrl = new URL ( repo . clone_url ! ) ;
3741
3842 const record : RepoData = {
@@ -42,6 +46,7 @@ export const compileGithubConfig = async (
4246 cloneUrl : cloneUrl . toString ( ) ,
4347 webUrl : repo . html_url ,
4448 name : repoName ,
49+ displayName : repoDisplayName ,
4550 imageUrl : repo . owner . avatar_url ,
4651 isFork : repo . fork ,
4752 isArchived : ! ! repo . archived ,
@@ -67,6 +72,7 @@ export const compileGithubConfig = async (
6772 'zoekt.archived' : marshalBool ( repo . archived ) ,
6873 'zoekt.fork' : marshalBool ( repo . fork ) ,
6974 'zoekt.public' : marshalBool ( repo . private === false ) ,
75+ 'zoekt.display-name' : repoDisplayName ,
7076 } ,
7177 branches : config . revisions ?. branches ?? undefined ,
7278 tags : config . revisions ?. tags ?? undefined ,
@@ -93,13 +99,16 @@ export const compileGitlabConfig = async (
9399 const notFound = gitlabReposResult . notFound ;
94100
95101 const hostUrl = config . url ?? 'https://gitlab.com' ;
96- const hostname = new URL ( hostUrl ) . hostname ;
97-
102+ const repoNameRoot = new URL ( hostUrl )
103+ . toString ( )
104+ . replace ( / ^ h t t p s ? : \/ \/ / , '' ) ;
105+
98106 const repos = gitlabRepos . map ( ( project ) => {
99107 const projectUrl = `${ hostUrl } /${ project . path_with_namespace } ` ;
100108 const cloneUrl = new URL ( project . http_url_to_repo ) ;
101109 const isFork = project . forked_from_project !== undefined ;
102- const repoName = `${ hostname } /${ project . path_with_namespace } ` ;
110+ const repoDisplayName = project . path_with_namespace ;
111+ const repoName = path . join ( repoNameRoot , repoDisplayName ) ;
103112
104113 const record : RepoData = {
105114 external_id : project . id . toString ( ) ,
@@ -108,6 +117,7 @@ export const compileGitlabConfig = async (
108117 cloneUrl : cloneUrl . toString ( ) ,
109118 webUrl : projectUrl ,
110119 name : repoName ,
120+ displayName : repoDisplayName ,
111121 imageUrl : project . avatar_url ,
112122 isFork : isFork ,
113123 isArchived : ! ! project . archived ,
@@ -130,7 +140,8 @@ export const compileGitlabConfig = async (
130140 'zoekt.gitlab-forks' : ( project . forks_count ?? 0 ) . toString ( ) ,
131141 'zoekt.archived' : marshalBool ( project . archived ) ,
132142 'zoekt.fork' : marshalBool ( isFork ) ,
133- 'zoekt.public' : marshalBool ( project . private === false )
143+ 'zoekt.public' : marshalBool ( project . private === false ) ,
144+ 'zoekt.display-name' : repoDisplayName ,
134145 } ,
135146 branches : config . revisions ?. branches ?? undefined ,
136147 tags : config . revisions ?. tags ?? undefined ,
@@ -157,11 +168,14 @@ export const compileGiteaConfig = async (
157168 const notFound = giteaReposResult . notFound ;
158169
159170 const hostUrl = config . url ?? 'https://gitea.com' ;
160- const hostname = new URL ( hostUrl ) . hostname ;
171+ const repoNameRoot = new URL ( hostUrl )
172+ . toString ( )
173+ . replace ( / ^ h t t p s ? : \/ \/ / , '' ) ;
161174
162175 const repos = giteaRepos . map ( ( repo ) => {
163176 const cloneUrl = new URL ( repo . clone_url ! ) ;
164- const repoName = `${ hostname } /${ repo . full_name ! } ` ;
177+ const repoDisplayName = repo . full_name ! ;
178+ const repoName = path . join ( repoNameRoot , repoDisplayName ) ;
165179
166180 const record : RepoData = {
167181 external_id : repo . id ! . toString ( ) ,
@@ -170,6 +184,7 @@ export const compileGiteaConfig = async (
170184 cloneUrl : cloneUrl . toString ( ) ,
171185 webUrl : repo . html_url ,
172186 name : repoName ,
187+ displayName : repoDisplayName ,
173188 imageUrl : repo . owner ?. avatar_url ,
174189 isFork : repo . fork ! ,
175190 isArchived : ! ! repo . archived ,
@@ -191,6 +206,7 @@ export const compileGiteaConfig = async (
191206 'zoekt.archived' : marshalBool ( repo . archived ) ,
192207 'zoekt.fork' : marshalBool ( repo . fork ! ) ,
193208 'zoekt.public' : marshalBool ( repo . internal === false && repo . private === false ) ,
209+ 'zoekt.display-name' : repoDisplayName ,
194210 } ,
195211 branches : config . revisions ?. branches ?? undefined ,
196212 tags : config . revisions ?. tags ?? undefined ,
@@ -212,35 +228,41 @@ export const compileGerritConfig = async (
212228 orgId : number ) => {
213229
214230 const gerritRepos = await getGerritReposFromConfig ( config ) ;
215- const hostUrl = ( config . url ?? 'https://gerritcodereview.com' ) . replace ( / \/ $ / , '' ) ; // Remove trailing slash
216- const hostname = new URL ( hostUrl ) . hostname ;
231+ const hostUrl = config . url ;
232+ const repoNameRoot = new URL ( hostUrl )
233+ . toString ( )
234+ . replace ( / ^ h t t p s ? : \/ \/ / , '' ) ;
217235
218236 const repos = gerritRepos . map ( ( project ) => {
219- const repoId = `${ hostname } /${ project . name } ` ;
220- const cloneUrl = new URL ( `${ config . url } /${ encodeURIComponent ( project . name ) } ` ) ;
237+ const cloneUrl = new URL ( path . join ( hostUrl , encodeURIComponent ( project . name ) ) ) ;
238+ const repoDisplayName = project . name ;
239+ const repoName = path . join ( repoNameRoot , repoDisplayName ) ;
221240
222- let webUrl = "https://www.gerritcodereview.com/" ;
223- // Gerrit projects can have multiple web links; use the first one
224- if ( project . web_links ) {
225- const webLink = project . web_links [ 0 ] ;
226- if ( webLink ) {
227- webUrl = webLink . url ;
241+ const webUrl = ( ( ) => {
242+ if ( ! project . web_links || project . web_links . length === 0 ) {
243+ return null ;
228244 }
229- }
230245
231- // Handle case where webUrl is just a gitiles path
232- // https://github.com/GerritCodeReview/plugins_gitiles/blob/5ee7f57/src/main/java/com/googlesource/gerrit/plugins/gitiles/GitilesWeblinks.java#L50
233- if ( webUrl . startsWith ( '/plugins/gitiles/' ) ) {
234- webUrl = `${ hostUrl } ${ webUrl } ` ;
235- }
246+ const webLink = project . web_links [ 0 ] ;
247+ const webUrl = webLink . url ;
248+
249+ // Handle case where webUrl is just a gitiles path
250+ // https://github.com/GerritCodeReview/plugins_gitiles/blob/5ee7f57/src/main/java/com/googlesource/gerrit/plugins/gitiles/GitilesWeblinks.java#L50
251+ if ( webUrl . startsWith ( '/plugins/gitiles/' ) ) {
252+ return path . join ( hostUrl , webUrl ) ;
253+ } else {
254+ return webUrl ;
255+ }
256+ } ) ( ) ;
236257
237258 const record : RepoData = {
238259 external_id : project . id . toString ( ) ,
239260 external_codeHostType : 'gerrit' ,
240261 external_codeHostUrl : hostUrl ,
241262 cloneUrl : cloneUrl . toString ( ) ,
242263 webUrl : webUrl ,
243- name : project . name ,
264+ name : repoName ,
265+ displayName : repoDisplayName ,
244266 isFork : false ,
245267 isArchived : false ,
246268 org : {
@@ -256,11 +278,12 @@ export const compileGerritConfig = async (
256278 metadata : {
257279 gitConfig : {
258280 'zoekt.web-url-type' : 'gitiles' ,
259- 'zoekt.web-url' : webUrl ,
260- 'zoekt.name' : repoId ,
281+ 'zoekt.web-url' : webUrl ?? '' ,
282+ 'zoekt.name' : repoName ,
261283 'zoekt.archived' : marshalBool ( false ) ,
262284 'zoekt.fork' : marshalBool ( false ) ,
263285 'zoekt.public' : marshalBool ( true ) ,
286+ 'zoekt.display-name' : repoDisplayName ,
264287 } ,
265288 } satisfies RepoMetadata ,
266289 } ;
0 commit comments