Skip to content

Commit e71fc4d

Browse files
committed
unify github url parsing into one function
1 parent 7fb0b78 commit e71fc4d

4 files changed

Lines changed: 240 additions & 374 deletions

File tree

apps/code/src/main/services/folders/service.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "node:fs";
22
import path from "node:path";
33
import { getRemoteUrl, isGitRepository } from "@posthog/git/queries";
44
import { InitRepositorySaga } from "@posthog/git/sagas/init";
5-
import { parseGitHubUrl } from "@posthog/git/utils";
5+
import { parseGithubUrl } from "@posthog/git/utils";
66
import { WorktreeManager } from "@posthog/git/worktree";
77
import type { IDialog } from "@posthog/platform/dialog";
88
import { normalizeRepoKey } from "@shared/utils/repo";
@@ -237,14 +237,15 @@ export class FoldersService {
237237
folderPath: string,
238238
overrideRemoteUrl: string | undefined,
239239
): Promise<string | null> {
240+
const slug = (url: string | null | undefined) => {
241+
const parsed = parseGithubUrl(url);
242+
return parsed ? `${parsed.owner}/${parsed.repo}` : null;
243+
};
240244
if (overrideRemoteUrl) {
241-
return (
242-
parseGitHubUrl(overrideRemoteUrl)?.path ??
243-
normalizeRepoKey(overrideRemoteUrl)
244-
);
245+
return slug(overrideRemoteUrl) ?? normalizeRepoKey(overrideRemoteUrl);
245246
}
246247
const localRemoteUrl = await getRemoteUrl(folderPath);
247-
return parseGitHubUrl(localRemoteUrl)?.path ?? null;
248+
return slug(localRemoteUrl);
248249
}
249250

250251
getRepositoryByRemoteUrl(

apps/code/src/main/services/git/service.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ import { CommitSaga } from "@posthog/git/sagas/commit";
3535
import { DiscardFileChangesSaga } from "@posthog/git/sagas/discard";
3636
import { PullSaga } from "@posthog/git/sagas/pull";
3737
import { PushSaga } from "@posthog/git/sagas/push";
38-
import {
39-
parseGitHubUrl,
40-
parseGithubRefUrl,
41-
parsePrUrl,
42-
} from "@posthog/git/utils";
38+
import { parseGithubUrl } from "@posthog/git/utils";
4339
import { inject, injectable } from "inversify";
4440
import { MAIN_TOKENS } from "../../di/tokens";
4541
import { logger } from "../../utils/logger";
@@ -222,15 +218,15 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
222218
const remoteUrl = await getRemoteUrl(directoryPath);
223219
if (!remoteUrl) return null;
224220

225-
const repo = parseGitHubUrl(remoteUrl);
226-
if (!repo) return null;
221+
const parsed = parseGithubUrl(remoteUrl);
222+
if (!parsed) return null;
227223

228224
const branch = await getCurrentBranch(directoryPath);
229225
if (!branch) return null;
230226

231227
return {
232-
organization: repo.organization,
233-
repository: repo.repository,
228+
organization: parsed.owner,
229+
repository: parsed.repo,
234230
remote: remoteUrl,
235231
branch,
236232
};
@@ -430,20 +426,20 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
430426
const remoteUrl = await getRemoteUrl(directoryPath);
431427
if (!remoteUrl) return null;
432428

433-
const parsed = parseGitHubUrl(remoteUrl);
429+
const parsed = parseGithubUrl(remoteUrl);
434430
if (!parsed) return null;
435431

436432
const currentBranch = await getCurrentBranch(directoryPath);
437433
const defaultBranch = await getDefaultBranch(directoryPath);
438434

439435
let compareUrl: string | null = null;
440436
if (currentBranch && currentBranch !== defaultBranch) {
441-
compareUrl = `https://github.com/${parsed.path}/compare/${defaultBranch}...${currentBranch}?expand=1`;
437+
compareUrl = `https://github.com/${parsed.owner}/${parsed.repo}/compare/${defaultBranch}...${currentBranch}?expand=1`;
442438
}
443439

444440
return {
445-
organization: parsed.organization,
446-
repository: parsed.repository,
441+
organization: parsed.owner,
442+
repository: parsed.repo,
447443
currentBranch: currentBranch ?? null,
448444
defaultBranch,
449445
compareUrl,
@@ -823,7 +819,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
823819

824820
try {
825821
const remoteUrl = await getRemoteUrl(directoryPath);
826-
const isGitHubRepo = !!(remoteUrl && parseGitHubUrl(remoteUrl));
822+
const isGitHubRepo = !!(remoteUrl && parseGithubUrl(remoteUrl));
827823
const currentBranch = await getCurrentBranch(directoryPath);
828824
const defaultBranch = await getDefaultBranch(directoryPath).catch(
829825
() => null,
@@ -894,7 +890,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
894890
const remoteUrl = await getRemoteUrl(directoryPath);
895891
if (!remoteUrl) return null;
896892

897-
const parsed = parseGitHubUrl(remoteUrl);
893+
const parsed = parseGithubUrl(remoteUrl);
898894
if (!parsed) return null;
899895

900896
const result = await execGh([
@@ -909,7 +905,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
909905
"--limit",
910906
"1",
911907
"--repo",
912-
parsed.path,
908+
`${parsed.owner}/${parsed.repo}`,
913909
]);
914910

915911
if (result.exitCode !== 0) {
@@ -984,8 +980,8 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
984980
}
985981

986982
public async getPrChangedFiles(prUrl: string): Promise<ChangedFile[]> {
987-
const pr = parsePrUrl(prUrl);
988-
if (!pr) return [];
983+
const pr = parseGithubUrl(prUrl);
984+
if (pr?.kind !== "pr") return [];
989985

990986
const { owner, repo, number } = pr;
991987

@@ -1057,8 +1053,8 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
10571053
public async getPrDetailsByUrl(
10581054
prUrl: string,
10591055
): Promise<PrDetailsByUrlOutput | null> {
1060-
const pr = parsePrUrl(prUrl);
1061-
if (!pr) return null;
1056+
const pr = parseGithubUrl(prUrl);
1057+
if (pr?.kind !== "pr") return null;
10621058

10631059
try {
10641060
const result = await execGh([
@@ -1093,8 +1089,8 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
10931089
prUrl: string,
10941090
action: PrActionType,
10951091
): Promise<UpdatePrByUrlOutput> {
1096-
const pr = parsePrUrl(prUrl);
1097-
if (!pr) {
1092+
const pr = parseGithubUrl(prUrl);
1093+
if (pr?.kind !== "pr") {
10981094
return { success: false, message: "Invalid PR URL" };
10991095
}
11001096

@@ -1127,8 +1123,8 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
11271123
}
11281124

11291125
public async getPrReviewComments(prUrl: string): Promise<PrReviewComment[]> {
1130-
const pr = parsePrUrl(prUrl);
1131-
if (!pr) return [];
1126+
const pr = parseGithubUrl(prUrl);
1127+
if (pr?.kind !== "pr") return [];
11321128

11331129
const { owner, repo, number } = pr;
11341130

@@ -1159,8 +1155,8 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
11591155
commentId: number,
11601156
body: string,
11611157
): Promise<ReplyToPrCommentOutput> {
1162-
const pr = parsePrUrl(prUrl);
1163-
if (!pr) {
1158+
const pr = parseGithubUrl(prUrl);
1159+
if (pr?.kind !== "pr") {
11641160
return { success: false, comment: null };
11651161
}
11661162

@@ -1549,8 +1545,8 @@ ${truncatedDiff || "(no diff available)"}${contextSection}`;
15491545
if (!repoInfo) return [];
15501546

15511547
// Full GitHub URL: look up directly. May target a different repo than the local one.
1552-
const urlRef = parseGithubRefUrl(query);
1553-
if (urlRef && kinds.includes(urlRef.kind)) {
1548+
const urlRef = parseGithubUrl(query);
1549+
if (urlRef && urlRef.kind !== "repo" && kinds.includes(urlRef.kind)) {
15541550
const repoSlug = `${urlRef.owner}/${urlRef.repo}`;
15551551
return this.fetchGhRefs(
15561552
[

0 commit comments

Comments
 (0)