1 | |
2 |
|
3 | |
4 | * Create merge request |
5 | * Open a merge request from source_branch into target_branch (the project's default branch when empty), with an optional description, labels, assignees and reviewers. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Gitlab, |
9 | project: string, |
10 | source_branch: string, |
11 | title: string, |
12 | target_branch: string | undefined, |
13 | description: string | undefined, |
14 | labels: string[] | undefined, |
15 | assignee_ids: number[] | undefined, |
16 | reviewer_ids: number[] | undefined, |
17 | remove_source_branch: boolean = false, |
18 | squash: boolean = false |
19 | ) { |
20 | const api = `${(auth.baseUrl || "https://gitlab.com").replace(/\/+$/, "")}/api/v4/projects/${encodeURIComponent(project)}` |
21 | const headers = { |
22 | Authorization: `Bearer ${auth.token}`, |
23 | Accept: "application/json", |
24 | } |
25 |
|
26 | let target = target_branch |
27 | if (!target) { |
28 | const response = await fetch(api, { headers }) |
29 | if (!response.ok) { |
30 | throw new Error(`${response.status} ${await response.text()}`) |
31 | } |
32 | target = (await response.json()).default_branch as string |
33 | if (!target) { |
34 | throw new Error( |
35 | `${project} has no default branch (empty repository): set target_branch` |
36 | ) |
37 | } |
38 | } |
39 |
|
40 | const response = await fetch(`${api}/merge_requests`, { |
41 | method: "POST", |
42 | headers: { ...headers, "Content-Type": "application/json" }, |
43 | body: JSON.stringify({ |
44 | source_branch, |
45 | target_branch: target, |
46 | title, |
47 | description: description || undefined, |
48 | labels: labels?.length ? labels.join(",") : undefined, |
49 | assignee_ids: assignee_ids?.length ? assignee_ids : undefined, |
50 | reviewer_ids: reviewer_ids?.length ? reviewer_ids : undefined, |
51 | |
52 | remove_source_branch: remove_source_branch || undefined, |
53 | squash: squash || undefined, |
54 | }), |
55 | }) |
56 |
|
57 | if (!response.ok) { |
58 | throw new Error(`${response.status} ${await response.text()}`) |
59 | } |
60 |
|
61 | return await response.json() |
62 | } |
63 |
|