import type { Repository } from "../../../packages/shared/src/types"; export async function createGitLabMergeRequest( repo: Repository, sourceBranch: string, targetBranch: string, title: string, description: string, ): Promise { if (!repo.gitlab_host) { throw new Error("Repository missing gitlab_host"); } if (!repo.access_token) { throw new Error("Repository missing access_token"); } const encodedId = encodeURIComponent(repo.full_name); const url = `https://${repo.gitlab_host}/api/v4/projects/${encodedId}/merge_requests`; const res = await fetch(url, { method: "POST", headers: { "PRIVATE-TOKEN": repo.access_token, "Content-Type": "application/json", }, body: JSON.stringify({ source_branch: sourceBranch, target_branch: targetBranch, title, description, }), }); if (!res.ok) { const text = await res.text(); throw new Error(`GitLab create MR failed: ${res.status} ${text}`); } const data = (await res.json()) as { web_url: string }; try { const urlObj = new URL(data.web_url); urlObj.hostname = repo.gitlab_host; return urlObj.toString(); } catch { return data.web_url; } }