Skip to content

Commit 47aedca

Browse files
refactor: extract resolveOrEnsureForkRemote to deduplicate remote resolution logic
Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
1 parent 1d00304 commit 47aedca

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

apps/server/src/git/Layers/GitManager.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -250,31 +250,44 @@ export const makeGitManager = Effect.gen(function* () {
250250
const gitHubCli = yield* GitHubCli;
251251
const textGeneration = yield* TextGeneration;
252252

253-
const configurePullRequestHeadUpstream = (
253+
const resolveOrEnsureForkRemote = (
254254
cwd: string,
255-
pullRequest: ResolvedPullRequest & PullRequestHeadRemoteInfo,
255+
repositoryNameWithOwner: string,
256+
headRepositoryOwnerLogin: string | null | undefined,
256257
) =>
257258
Effect.gen(function* () {
258-
const repositoryNameWithOwner = pullRequest.headRepositoryNameWithOwner?.trim() ?? "";
259-
if (repositoryNameWithOwner.length === 0) {
260-
return;
261-
}
262-
263259
const cloneUrls = yield* gitHubCli.getRepositoryCloneUrls({
264260
cwd,
265261
repository: repositoryNameWithOwner,
266262
});
267263
const originRemoteUrl = yield* gitCore.readConfigValue(cwd, "remote.origin.url");
268264
const remoteUrl = shouldPreferSshRemote(originRemoteUrl) ? cloneUrls.sshUrl : cloneUrls.url;
269265
const preferredRemoteName =
270-
pullRequest.headRepositoryOwnerLogin?.trim() ||
266+
headRepositoryOwnerLogin?.trim() ||
271267
repositoryNameWithOwner.split("/")[0]?.trim() ||
272268
"fork";
273-
const remoteName = yield* gitCore.ensureRemote({
269+
return yield* gitCore.ensureRemote({
274270
cwd,
275271
preferredName: preferredRemoteName,
276272
url: remoteUrl,
277273
});
274+
});
275+
276+
const configurePullRequestHeadUpstream = (
277+
cwd: string,
278+
pullRequest: ResolvedPullRequest & PullRequestHeadRemoteInfo,
279+
) =>
280+
Effect.gen(function* () {
281+
const repositoryNameWithOwner = pullRequest.headRepositoryNameWithOwner?.trim() ?? "";
282+
if (repositoryNameWithOwner.length === 0) {
283+
return;
284+
}
285+
286+
const remoteName = yield* resolveOrEnsureForkRemote(
287+
cwd,
288+
repositoryNameWithOwner,
289+
pullRequest.headRepositoryOwnerLogin,
290+
);
278291

279292
yield* gitCore.setBranchUpstream({
280293
cwd,
@@ -306,21 +319,11 @@ export const makeGitManager = Effect.gen(function* () {
306319
return;
307320
}
308321

309-
const cloneUrls = yield* gitHubCli.getRepositoryCloneUrls({
322+
const remoteName = yield* resolveOrEnsureForkRemote(
310323
cwd,
311-
repository: repositoryNameWithOwner,
312-
});
313-
const originRemoteUrl = yield* gitCore.readConfigValue(cwd, "remote.origin.url");
314-
const remoteUrl = shouldPreferSshRemote(originRemoteUrl) ? cloneUrls.sshUrl : cloneUrls.url;
315-
const preferredRemoteName =
316-
pullRequest.headRepositoryOwnerLogin?.trim() ||
317-
repositoryNameWithOwner.split("/")[0]?.trim() ||
318-
"fork";
319-
const remoteName = yield* gitCore.ensureRemote({
320-
cwd,
321-
preferredName: preferredRemoteName,
322-
url: remoteUrl,
323-
});
324+
repositoryNameWithOwner,
325+
pullRequest.headRepositoryOwnerLogin,
326+
);
324327

325328
yield* gitCore.fetchRemoteBranch({
326329
cwd,

0 commit comments

Comments
 (0)