Skip to content

Commit 2018ed6

Browse files
Add repo display name + some cleanup
1 parent 7a33bec commit 2018ed6

File tree

8 files changed

+64
-39
lines changed

8 files changed

+64
-39
lines changed

packages/backend/src/repoCompileUtils.ts

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { WithRequired } from "./types.js"
88
import { marshalBool } from "./utils.js";
99
import { GerritConnectionConfig, GiteaConnectionConfig, GitlabConnectionConfig } from '@sourcebot/schemas/v3/connection.type';
1010
import { RepoMetadata } from './types.js';
11+
import path from 'path';
1112

1213
export 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(/^https?:\/\//, '');
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(/^https?:\/\//, '');
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(/^https?:\/\//, '');
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(/^https?:\/\//, '');
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 `${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
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "Repo" ADD COLUMN "displayName" TEXT;

packages/db/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum StripeSubscriptionStatus {
3838
model Repo {
3939
id Int @id @default(autoincrement())
4040
name String
41+
displayName String?
4142
createdAt DateTime @default(now())
4243
updatedAt DateTime @updatedAt
4344
indexedAt DateTime?

packages/web/src/actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ export const getRepos = async (domain: string, filter: { status?: RepoIndexingSt
429429
codeHostType: repo.external_codeHostType,
430430
repoId: repo.id,
431431
repoName: repo.name,
432+
repoDisplayName: repo.displayName ?? undefined,
432433
repoCloneUrl: repo.cloneUrl,
433434
webUrl: repo.webUrl ?? undefined,
434435
linkedConnections: repo.connections.map(({ connection }) => ({

packages/web/src/app/[domain]/components/repositoryCarousel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const RepositoryBadge = ({
7373

7474
return {
7575
repoIcon: <FileIcon className="w-4 h-4" />,
76-
displayName: repo.repoName.split('/').slice(-2).join('/'),
76+
displayName: repo.repoName,
7777
repoLink: undefined,
7878
}
7979
})();

packages/web/src/app/[domain]/repos/repositoryTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const RepositoryTable = ({ isAddNewRepoButtonVisible }: RepositoryTablePr
3939

4040
if (!repos) return [];
4141
return repos.map((repo): RepositoryColumnInfo => ({
42-
name: repo.repoName.split('/').length > 2 ? repo.repoName.split('/').slice(-2).join('/') : repo.repoName,
42+
name: repo.repoDisplayName ?? repo.repoName,
4343
imageUrl: repo.imageUrl,
4444
connections: repo.linkedConnections,
4545
repoIndexingStatus: repo.repoIndexingStatus as RepoIndexingStatus,

packages/web/src/lib/schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export const repositoryQuerySchema = z.object({
168168
codeHostType: z.string(),
169169
repoId: z.number(),
170170
repoName: z.string(),
171+
repoDisplayName: z.string().optional(),
171172
repoCloneUrl: z.string(),
172173
webUrl: z.string().optional(),
173174
linkedConnections: z.array(z.object({

packages/web/src/lib/utils.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,19 @@ export const getRepoCodeHostInfo = (repo?: Repository): CodeHostInfo | undefined
4747
return undefined;
4848
}
4949

50-
const webUrlType = repo.RawConfig ? repo.RawConfig['web-url-type'] : undefined;
51-
if (!webUrlType) {
50+
if (!repo.RawConfig) {
5251
return undefined;
5352
}
5453

55-
const url = new URL(repo.URL);
56-
const displayName = url.pathname.slice(1);
54+
// @todo : use zod to validate config schema
55+
const webUrlType = repo.RawConfig['web-url-type']!;
56+
const displayName = repo.RawConfig['display-name'] ?? repo.RawConfig['name']!;
57+
5758
return _getCodeHostInfoInternal(webUrlType, displayName, repo.URL);
5859
}
5960

60-
export const getRepoQueryCodeHostInfo = (repo?: RepositoryQuery): CodeHostInfo | undefined => {
61-
if (!repo) {
62-
return undefined;
63-
}
64-
65-
const displayName = repo.repoName.split('/').slice(-2).join('/');
61+
export const getRepoQueryCodeHostInfo = (repo: RepositoryQuery): CodeHostInfo | undefined => {
62+
const displayName = repo.repoDisplayName ?? repo.repoName;
6663
return _getCodeHostInfoInternal(repo.codeHostType, displayName, repo.webUrl ?? repo.repoCloneUrl);
6764
}
6865

0 commit comments

Comments
 (0)