fix(gitlab): resolve headOwnerLogin and local branch name for fork merge requests

This commit is contained in:
quickwind
2026-10-29 12:03:10 +11:00
parent 0feaf0d987
commit ea3f2ccd4a
2 changed files with 44 additions and 22 deletions
@@ -463,9 +463,8 @@ describe("createGitLabService", () => {
it("maps a same-repo merge request view to a checkout target", async () => {
const { service, calls } = makeService(() => ok(JSON.stringify(OPEN_MR)));
await expect(
service.getPullRequestCheckoutTarget?.({ cwd: "/repo", number: 14 }),
).resolves.toEqual({
const target = await service.getPullRequestCheckoutTarget?.({ cwd: "/repo", number: 14 });
expect(target).toEqual({
number: 14,
baseRefName: "main",
headRefName: "release/v0.4.0",
@@ -479,6 +478,12 @@ describe("createGitLabService", () => {
isCrossRepository: false,
headRefKind: "branch",
});
expect(
service.buildPrLocalBranchName?.({
headRef: target!.headRefName,
checkoutTarget: target!,
}),
).toBe("release/v0.4.0");
expect(calls[0]).toEqual(["mr", "view", "14", "-F", "json"]);
});
@@ -489,20 +494,26 @@ describe("createGitLabService", () => {
...OPEN_MR,
source_project_id: 202,
target_project_id: 101,
author: { username: "contributor" },
}),
),
);
await expect(
service.getPullRequestCheckoutTarget?.({ cwd: "/repo", number: 14 }),
).resolves.toMatchObject({
const target = await service.getPullRequestCheckoutTarget?.({ cwd: "/repo", number: 14 });
expect(target).toMatchObject({
number: 14,
headRefName: "release/v0.4.0",
isCrossRepository: true,
headOwnerLogin: null,
headOwnerLogin: "contributor",
headRepositorySshUrl: null,
headRepositoryUrl: null,
});
expect(
service.buildPrLocalBranchName?.({
headRef: target!.headRefName,
checkoutTarget: target!,
}),
).toBe("contributor/release/v0.4.0");
});
it("creates a merge request and parses the URL and iid from glab output", async () => {
+26 -15
View File
@@ -60,6 +60,7 @@ import {
} from "@getpaseo/protocol/check-traits";
import { GITLAB_ACTIVE_PIPELINE_STATUS_SET } from "@getpaseo/protocol/gitlab-pipeline";
import { isGitLabStatusFacts, type GitLabStatusFacts } from "./gitlab-facts.js";
import { buildForkLocalBranchName } from "../utils/change-request-checkout.js";
const GLAB_ENV = {
GIT_TERMINAL_PROMPT: "0",
@@ -144,6 +145,15 @@ const GitLabPipelineDetailsSchema = z
})
.passthrough();
const GitLabAuthorSchema = z
.object({
username: z.string().optional(),
name: z.string().optional(),
web_url: z.string().nullable().optional(),
avatar_url: z.string().nullable().optional(),
})
.passthrough();
const GitLabMergeRequestSchema = z
.object({
iid: z.number(),
@@ -155,6 +165,7 @@ const GitLabMergeRequestSchema = z
sha: z.string().optional(),
source_project_id: z.number().nullable().optional(),
target_project_id: z.number().nullable().optional(),
author: GitLabAuthorSchema.nullable().optional(),
draft: z.boolean().optional(),
work_in_progress: z.boolean().optional(),
has_conflicts: z.boolean().optional(),
@@ -186,14 +197,7 @@ const GitLabIssueSchema = z
})
.passthrough();
const GitLabNoteAuthorSchema = z
.object({
username: z.string().optional(),
name: z.string().optional(),
web_url: z.string().nullable().optional(),
avatar_url: z.string().nullable().optional(),
})
.passthrough();
const GitLabNoteAuthorSchema = GitLabAuthorSchema;
const GitLabNoteLineRangeEndpointSchema = z
.object({
@@ -1183,10 +1187,22 @@ export function createGitLabService(options: CreateGitLabServiceOptions = {}): F
return mr.source_branch;
},
buildPrLocalBranchName({ headRef, checkoutTarget }) {
return buildForkLocalBranchName({ headRef, ...checkoutTarget });
},
async getPullRequestCheckoutTarget(
input: GetPullRequestOptions,
): Promise<PullRequestCheckoutTarget> {
const mr = await viewMergeRequest(input.cwd, String(input.number));
const isCrossRepository =
mr.source_project_id !== undefined &&
mr.source_project_id !== null &&
mr.target_project_id !== undefined &&
mr.target_project_id !== null &&
mr.source_project_id !== mr.target_project_id;
const headOwnerLogin =
isCrossRepository && mr.author?.username?.trim() ? mr.author.username.trim() : null;
return {
number: mr.iid,
baseRefName: mr.target_branch,
@@ -1195,15 +1211,10 @@ export function createGitLabService(options: CreateGitLabServiceOptions = {}): F
{ remoteName: "origin", remoteRef: `refs/merge-requests/${mr.iid}/head` },
{ remoteName: "origin", remoteRef: `refs/heads/${mr.source_branch}` },
],
headOwnerLogin: null,
headOwnerLogin,
headRepositorySshUrl: null,
headRepositoryUrl: null,
isCrossRepository:
mr.source_project_id !== undefined &&
mr.source_project_id !== null &&
mr.target_project_id !== undefined &&
mr.target_project_id !== null &&
mr.source_project_id !== mr.target_project_id,
isCrossRepository,
headRefKind: "branch",
};
},