@leitwerk-dev/gitlab 0.5.11 → 0.5.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -107,6 +107,19 @@ conflict observations remain available.
107
107
 
108
108
  The extension contains no Renovate selection or repair policy. `./testing` exports a persistent `LocalGitLabAdapter` with real local repository ancestry for integration tests.
109
109
 
110
+ ## Merge request templates
111
+
112
+ When creating a new merge request, Leitwerk reads
113
+ `.gitlab/merge_request_templates/` from the target project's default branch.
114
+ `Default.md` wins; without it, Leitwerk uses the sole Markdown template. If
115
+ several exist with no default, delivery reports the ambiguity and uses the
116
+ normal description. Templates from other projects or GitLab settings are not
117
+ used. A repository read error is retryable, not interpreted as an absent template.
118
+
119
+ The initial description keeps the template's Markdown structure and appends the
120
+ workflow's links and recovery marker. Template quick actions are not executed.
121
+ Reconciliation and later updates leave existing descriptions unchanged.
122
+
110
123
  ## Shared MR maintenance
111
124
 
112
125
  Opt in during `setupServer`. Declare external edges using
@@ -14,9 +14,9 @@ async function preflightGitLabRepository(input) {
14
14
  const url = repositoryHttpsUrl(input.url);
15
15
  if (url.origin !== input.origin)
16
16
  throw new Error("GitLab repository origin does not match its profile");
17
- const directory = await mkdtemp(join(tmpdir(), "leitwerk-gitlab-preflight-"));
17
+ const directory2 = await mkdtemp(join(tmpdir(), "leitwerk-gitlab-preflight-"));
18
18
  try {
19
- const askpass = join(directory, "askpass");
19
+ const askpass = join(directory2, "askpass");
20
20
  await writeFile(
21
21
  askpass,
22
22
  '#!/bin/sh\ncase "$1" in *Username*) printf "%s\\n" oauth2;; *) printf "%s\\n" "$LEITWERK_GITLAB_PREFLIGHT_TOKEN";; esac\n',
@@ -37,10 +37,10 @@ async function preflightGitLabRepository(input) {
37
37
  await git(["check-ref-format", `refs/heads/${input.baseBranch}`]);
38
38
  await git(["check-ref-format", `refs/heads/${input.workBranch}`]);
39
39
  await git(["ls-remote", "--exit-code", input.url, `refs/heads/${input.baseBranch}`]);
40
- await git(["init", "--quiet", directory]);
40
+ await git(["init", "--quiet", directory2]);
41
41
  await git([
42
42
  "-C",
43
- directory,
43
+ directory2,
44
44
  "fetch",
45
45
  "--quiet",
46
46
  "--depth=1",
@@ -49,7 +49,7 @@ async function preflightGitLabRepository(input) {
49
49
  ]);
50
50
  await git([
51
51
  "-C",
52
- directory,
52
+ directory2,
53
53
  "push",
54
54
  "--dry-run",
55
55
  input.url,
@@ -60,7 +60,7 @@ async function preflightGitLabRepository(input) {
60
60
  "GitLab HTTPS read/write admission failed; check repository access and write_repository scope"
61
61
  );
62
62
  } finally {
63
- await rm(directory, { recursive: true, force: true });
63
+ await rm(directory2, { recursive: true, force: true });
64
64
  }
65
65
  }
66
66
 
@@ -1979,6 +1979,45 @@ import {
1979
1979
  projectParameters as projectParameters2,
1980
1980
  stringArg as stringArg2
1981
1981
  } from "@leitwerk-dev/process-sdk";
1982
+
1983
+ // src/merge-request-template.ts
1984
+ var directory = ".gitlab/merge_request_templates";
1985
+ async function mergeRequestBody(client, projectId, body, ctx) {
1986
+ if (!client.getRepositoryFile || !client.listRepositoryTree)
1987
+ throw new Error("GitLab repository template lookup is unavailable");
1988
+ const { default_branch: branch } = await client.getProject(projectId, ctx.signal);
1989
+ if (!branch) throw new Error("GitLab repository has no default branch for template lookup");
1990
+ const { commit } = await client.getBranch(projectId, branch, ctx.signal);
1991
+ const list = client.listRepositoryTree.bind(client, projectId, commit.id);
1992
+ const hasTree = (entries, path) => entries.some((entry) => entry.type === "tree" && entry.path === path);
1993
+ if (!hasTree(await list("", ctx.signal), ".gitlab") || !hasTree(await list(".gitlab", ctx.signal), directory))
1994
+ return body;
1995
+ const candidates = (await list(directory, ctx.signal)).filter(
1996
+ ({ path, type, mode }) => type === "blob" && mode !== "120000" && path.startsWith(`${directory}/`) && /^[^/\r\n\0]+\.md$/i.test(path.slice(directory.length + 1))
1997
+ );
1998
+ const template = candidates.find(({ path }) => path === `${directory}/Default.md`) ?? (candidates.length === 1 ? candidates[0] : void 0);
1999
+ if (!template) {
2000
+ if (candidates.length > 1)
2001
+ ctx.reportProgress({
2002
+ title: "Creating GitLab merge request without a template",
2003
+ summary: "Multiple repository templates exist and none is Default.md.",
2004
+ steps: []
2005
+ });
2006
+ return body;
2007
+ }
2008
+ const file = await client.getRepositoryFile(projectId, commit.id, template.path, ctx.signal);
2009
+ if (file.file_path !== template.path || file.encoding !== "base64")
2010
+ throw new Error("Invalid GitLab merge request template response");
2011
+ const markdown = new TextDecoder("utf-8", { fatal: true }).decode(
2012
+ Buffer.from(file.content, "base64")
2013
+ );
2014
+ const description = markdown.replace(/^(\s*)\/(?=[a-z])/gim, "$1\\/").trimEnd();
2015
+ return description ? `${description}
2016
+
2017
+ ${body}` : body;
2018
+ }
2019
+
2020
+ // src/delivery-tools.ts
1982
2021
  function registerGitLabDeliveryTools(api, integration, projects) {
1983
2022
  api.tool({
1984
2023
  name: "gitlab_ensure_merge_request",
@@ -2009,18 +2048,27 @@ function registerGitLabDeliveryTools(api, integration, projects) {
2009
2048
  );
2010
2049
  const request = await ctx.externalWrites.ensure(identity, {
2011
2050
  reconcile: async () => await find() ?? null,
2012
- execute: () => client.createMergeRequest(
2013
- binding.projectId,
2014
- {
2015
- title: stringArg2(args, "title"),
2016
- description: `${stringArg2(args, "body")}
2051
+ execute: async () => {
2052
+ const title = stringArg2(args, "title");
2053
+ const body = await mergeRequestBody(
2054
+ client,
2055
+ binding.projectId,
2056
+ stringArg2(args, "body"),
2057
+ ctx
2058
+ );
2059
+ return client.createMergeRequest(
2060
+ binding.projectId,
2061
+ {
2062
+ title,
2063
+ description: `${body}
2017
2064
 
2018
2065
  ${marker}`,
2019
- source_branch: workBranch,
2020
- target_branch: project.baseBranch
2021
- },
2022
- ctx.signal
2023
- ),
2066
+ source_branch: workBranch,
2067
+ target_branch: project.baseBranch
2068
+ },
2069
+ ctx.signal
2070
+ );
2071
+ },
2024
2072
  toMetadata: (mr) => ({ iid: mr.iid, url: mr.web_url })
2025
2073
  });
2026
2074
  const latest = projects.getByInstanceAndKey(ctx.process.id, project.key);
@@ -2424,4 +2472,4 @@ export {
2424
2472
  createGitLabRepositoryCatalog,
2425
2473
  setupGitLabIntegration
2426
2474
  };
2427
- //# sourceMappingURL=chunk-KW4UOHAM.js.map
2475
+ //# sourceMappingURL=chunk-JPPNVWMZ.js.map