renovate-mcp 1.8.2 → 2.0.0

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.
@@ -0,0 +1,174 @@
1
+ import { validateEndpoint } from "./endpointValidator.js";
2
+ import { classifyRemoteHost, parseRemoteUrl, readOriginRemote } from "./gitRemote.js";
3
+ import { homeScope, matchesHost, PUBLIC_HOST } from "./hostTokens.js";
4
+ import { DEFAULT_WORKER_TIMEOUT_MS, runRenovateWorker } from "./renovateWorker.js";
5
+ import { isLocalPreset } from "./presetResolver.js";
6
+ import { isRecord } from "./util.js";
7
+ /**
8
+ * `dry_run`'s local-preset flattening (ADR-0009). Under `--platform=local`
9
+ * Renovate cannot resolve `local>` presets, so the tool resolves them first
10
+ * with Renovate's own `resolveConfigPresets` in a worker thread, routed through
11
+ * the derived platform/endpoint, and the spawned Renovate gets the result.
12
+ *
13
+ * Same worker-isolation invariant as the other carve-outs: the main process
14
+ * never imports `renovate`. No preview fallback: a worker failure is a refusal.
15
+ */
16
+ // Renovate's `parsePreset` source detection, shared with resolve_config so
17
+ // both tools agree on which entries are local presets.
18
+ export { isLocalPreset };
19
+ /** Keys whose object values Renovate's resolver does not descend into. */
20
+ const UNRESOLVED_KEYS = ["content", "onboardingConfig"];
21
+ /**
22
+ * Every local preset in `config`, at the places `resolveConfigPresets` resolves
23
+ * `extends`: the top level, objects in arrays (`packageRules[]`), and nested
24
+ * objects other than `content` / `onboardingConfig`. A string `extends` counts
25
+ * as a one-entry list, as Renovate's `ExtendsMigration` makes it.
26
+ */
27
+ export function findLocalPresets(config) {
28
+ const found = [];
29
+ const walk = (node) => {
30
+ const list = typeof node.extends === "string" ? [node.extends] : node.extends;
31
+ if (Array.isArray(list)) {
32
+ for (const entry of list)
33
+ if (typeof entry === "string" && isLocalPreset(entry))
34
+ found.push(entry);
35
+ }
36
+ for (const [key, value] of Object.entries(node)) {
37
+ if (Array.isArray(value)) {
38
+ for (const element of value)
39
+ if (isRecord(element))
40
+ walk(element);
41
+ }
42
+ else if (isRecord(value) && !UNRESOLVED_KEYS.includes(key)) {
43
+ walk(value);
44
+ }
45
+ }
46
+ };
47
+ if (isRecord(config))
48
+ walk(config);
49
+ return found;
50
+ }
51
+ /** Overridable like the other worker entries; see `mergeWorker.ts`. */
52
+ function workerEntry() {
53
+ return (process.env.RENOVATE_MCP_PRESET_FLATTEN_WORKER_ENTRY ??
54
+ new URL("./presetFlattenWorkerImpl.js", import.meta.url));
55
+ }
56
+ /**
57
+ * Resolve every preset from the first non-built-in `extends` entry on, plus
58
+ * the nested `extends` of the config's own keys, with Renovate's resolver.
59
+ * Refuses when Renovate throws (its `validationError` is the reason), when
60
+ * the result fails Renovate's repo-config validation, or when a local preset
61
+ * is still left in it.
62
+ */
63
+ export async function flattenLocalPresets(config, options) {
64
+ let reply;
65
+ try {
66
+ reply = await runRenovateWorker(workerEntry(), { config, platform: options.platform, endpoint: options.endpoint, hostRules: options.hostRules }, { timeoutMs: options.timeoutMs ?? DEFAULT_WORKER_TIMEOUT_MS, label: "preset flattening" });
67
+ }
68
+ catch (err) {
69
+ // "Renovate preset flattening failed: <Renovate's validationError>", or the
70
+ // driver's exit / timeout message.
71
+ return { ok: false, reason: err.message };
72
+ }
73
+ if (reply.errors.length) {
74
+ return {
75
+ ok: false,
76
+ reason: `The flattened config fails Renovate's repo-config validation: ${reply.errors.join("; ")}. A platform run would accept this config: it only logs such errors in resolved preset content at debug level (security errors and \`configValidationError: true\` aside). dry_run cannot, because the spawned Renovate reads the flattened result as the repo's own config and would stop with \`config-validation\`. Fix the preset, or run remotely (\`platform\` + \`repository\`).`,
77
+ };
78
+ }
79
+ const left = findLocalPresets(reply.config);
80
+ if (left.length) {
81
+ return { ok: false, reason: `Local presets are left after flattening: ${left.join(", ")}.` };
82
+ }
83
+ return {
84
+ ok: true,
85
+ config: reply.config,
86
+ keptExtends: reply.keptExtends,
87
+ flattenedPresets: reply.flattenedPresets,
88
+ warnings: reply.warnings,
89
+ };
90
+ }
91
+ const NESTED_PRESET_ERROR = /Preset caused unexpected error \((.*?)\)(?:\. Note:|$)/;
92
+ /**
93
+ * A hint for a run that failed on a local preset Renovate met inside another
94
+ * preset: under `--platform=local` it throws "does not support local presets",
95
+ * which surfaces as "Preset caused unexpected error (<preset>)". The tool only
96
+ * flattens on its own when the repo config itself names a local preset.
97
+ */
98
+ export function nestedLocalPresetHint(reportErrors) {
99
+ for (const problem of reportErrors) {
100
+ const preset = typeof problem.validationError === "string" ? NESTED_PRESET_ERROR.exec(problem.validationError)?.[1] : undefined;
101
+ if (preset && isLocalPreset(preset)) {
102
+ return `\`${preset}\` is a local preset that another preset pulls in, and Renovate cannot resolve local presets under \`platform=local\`. Pass \`presetPlatform\` (plus \`endpoint\` for a self-hosted host) to have dry_run flatten every preset after the leading built-ins with Renovate's resolver first.`;
103
+ }
104
+ }
105
+ return undefined;
106
+ }
107
+ /**
108
+ * Where `local>` presets of a local run are fetched from (ADR-0008). Routing
109
+ * only: env tokens never follow it. `presetPlatform`, else the platform of the repo's
110
+ * origin remote (a hosted platform, or the flavour of a self-hosted host),
111
+ * with the endpoint of `homeScope` for that platform. A hosted Gitea / Forgejo
112
+ * other than the public one (codeberg.org) gets `https://<host>/api/v1`.
113
+ * For an origin-derived platform `RENOVATE_ENDPOINT` applies only when it is
114
+ * on the origin's host (or a subdomain, api.github.com for github.com): the
115
+ * env endpoint names where the env platform lives, not where this repo does.
116
+ * An origin whose hostname shows no flavour takes the env platform when
117
+ * `RENOVATE_ENDPOINT` is on its host.
118
+ * Refuses an origin it cannot classify, a self-hosted origin without an
119
+ * endpoint, Bitbucket, and an endpoint the validator blocks.
120
+ */
121
+ export async function resolveLocalPresetRoute(repoPath, input, env = process.env) {
122
+ let platform = input.presetPlatform;
123
+ let derivedEndpoint;
124
+ let selfHostedHost;
125
+ let originHost;
126
+ if (!platform) {
127
+ const url = await readOriginRemote(repoPath);
128
+ const remote = url ? parseRemoteUrl(url) : null;
129
+ const classified = remote ? classifyRemoteHost(remote.host) : null;
130
+ // A hostname that shows no flavour (git.example.com) is the env platform
131
+ // when RENOVATE_ENDPOINT is on that host: the env names it exactly.
132
+ const home = homeScope({}, env);
133
+ const envFlavor = remote && home.platform && home.endpoint && matchesHost(remote.host, home.endpoint) ? home.platform : undefined;
134
+ if (!remote || !classified || (classified.flavor === "unknown" && !envFlavor)) {
135
+ const what = remote ? `the origin host \`${remote.host}\` could not be classified` : "the repository has no parseable origin remote";
136
+ return {
137
+ ok: false,
138
+ reason: `The config extends local presets, which dry_run flattens before a local run, but ${what}. Pass \`presetPlatform\` (github, gitlab, gitea or forgejo) plus \`endpoint\` for a self-hosted host, or set \`RENOVATE_PLATFORM\` and \`RENOVATE_ENDPOINT\` for that host on the MCP server.`,
139
+ };
140
+ }
141
+ originHost = remote.host;
142
+ if (classified.classified === "self-hosted") {
143
+ platform = (classified.flavor === "unknown" ? envFlavor : classified.flavor);
144
+ selfHostedHost = remote.host;
145
+ }
146
+ else {
147
+ platform = classified.classified;
148
+ if ((platform === "gitea" || platform === "forgejo") && remote.host !== PUBLIC_HOST[platform]) {
149
+ derivedEndpoint = `https://${remote.host}/api/v1`;
150
+ }
151
+ }
152
+ }
153
+ if (platform === "bitbucket") {
154
+ return {
155
+ ok: false,
156
+ reason: "dry_run cannot flatten Bitbucket local presets: Renovate reads them through its initialised Bitbucket platform client, which a local run does not have. Reference them with a supported source or run Renovate against Bitbucket itself.",
157
+ };
158
+ }
159
+ const scoped = homeScope({ platform, endpoint: input.endpoint }, env).endpoint;
160
+ const envOnOtherHost = !input.endpoint && originHost && scoped && !matchesHost(originHost, scoped);
161
+ const endpoint = (envOnOtherHost ? undefined : scoped) ?? derivedEndpoint;
162
+ if (!endpoint && selfHostedHost) {
163
+ return {
164
+ ok: false,
165
+ reason: `The origin \`${selfHostedHost}\` is a self-hosted ${platform}, but no API endpoint is known for its local presets. Pass \`endpoint\` (for example \`https://${selfHostedHost}/api/v4\` for GitLab), or set \`RENOVATE_PLATFORM=${platform}\` and \`RENOVATE_ENDPOINT\` on the MCP server.`,
166
+ };
167
+ }
168
+ if (endpoint) {
169
+ const blocked = validateEndpoint(endpoint);
170
+ if (blocked)
171
+ return { ok: false, reason: blocked };
172
+ }
173
+ return { ok: true, scope: endpoint ? { platform, endpoint } : { platform } };
174
+ }
@@ -0,0 +1,79 @@
1
+ import { parentPort, workerData } from "node:worker_threads";
2
+ const { config, platform, endpoint, hostRules } = workerData;
3
+ const format = (m) => `${m.topic ?? "Configuration Error"}: ${m.message ?? ""}`;
4
+ try {
5
+ const { GlobalConfig } = await import("renovate/dist/config/global.js");
6
+ const hostRulesApi = await import("renovate/dist/util/host-rules.js");
7
+ const { resolveConfigPresets } = await import("renovate/dist/config/presets/index.js");
8
+ const { parsePreset } = await import("renovate/dist/config/presets/parse.js");
9
+ const { migrateConfig } = await import("renovate/dist/config/migration.js");
10
+ const { massageConfig } = await import("renovate/dist/config/massage.js");
11
+ const { compile } = await import("renovate/dist/util/template/index.js");
12
+ const { trimTrailingApiPath } = await import("renovate/dist/modules/platform/gitea/utils.js");
13
+ const { validateConfig } = await import("renovate/dist/config/validation.js");
14
+ // A real run's initPlatform trims a Gitea/Forgejo endpoint to its base URL
15
+ // before it lands in the global config; the preset source adds `/api/v1`.
16
+ const globalEndpoint = endpoint && (platform === "gitea" || platform === "forgejo") ? trimTrailingApiPath(endpoint) : endpoint;
17
+ GlobalConfig.set({ platform: platform, endpoint: globalEndpoint });
18
+ for (const rule of hostRules)
19
+ hostRulesApi.add(rule);
20
+ // Renovate migrates and massages the repo config before resolving presets.
21
+ const input = massageConfig(migrateConfig(config).migratedConfig);
22
+ const extendsList = Array.isArray(input.extends) ? input.extends : [];
23
+ // The cut: the leading run of built-in presets stays in `extends` for the
24
+ // spawned Renovate to expand; everything from the first other entry on is
25
+ // resolved here. "Built-in" is Renovate's own parse, after the same
26
+ // template compile its resolver applies.
27
+ const isInternal = (entry) => {
28
+ if (typeof entry !== "string")
29
+ return false;
30
+ try {
31
+ return parsePreset(compile(entry, {})).presetSource === "internal";
32
+ }
33
+ catch {
34
+ return false;
35
+ }
36
+ };
37
+ let cut = extendsList.findIndex((entry) => !isInternal(entry));
38
+ if (cut < 0)
39
+ cut = extendsList.length;
40
+ // In one pass Renovate resolves a nested object's `extends` after merging
41
+ // that object across every preset, kept built-ins included. Resolved from
42
+ // the cut on, the object would be merged over the built-ins' instead, so
43
+ // their values would lose to its presets: flatten everything then.
44
+ const ownNestedExtends = (node) => Object.entries(node).some(([key, value]) => !!value &&
45
+ typeof value === "object" &&
46
+ !Array.isArray(value) &&
47
+ key !== "content" &&
48
+ key !== "onboardingConfig" &&
49
+ ("extends" in value || ownNestedExtends(value)));
50
+ if (ownNestedExtends(input))
51
+ cut = 0;
52
+ const keptExtends = extendsList.slice(0, cut);
53
+ const rootIgnore = Array.isArray(input.ignorePresets) ? input.ignorePresets : [];
54
+ const suffix = { ...input, extends: extendsList.slice(cut) };
55
+ const { config: resolved, visitedPresets } = await resolveConfigPresets(suffix);
56
+ const migrated = migrateConfig(resolved).migratedConfig;
57
+ // Renovate drops ignorePresets after resolving; the kept built-ins still
58
+ // need the root list when the spawned Renovate expands them.
59
+ const flattened = {
60
+ ...(keptExtends.length ? { extends: keptExtends } : {}),
61
+ ...(keptExtends.length && rootIgnore.length ? { ignorePresets: rootIgnore } : {}),
62
+ ...migrated,
63
+ };
64
+ // The spawned Renovate reads this as its repo config and runs
65
+ // migrateAndValidate on it: validate it the same way first.
66
+ const { errors, warnings } = await validateConfig("repo", massageConfig(migrateConfig(flattened).migratedConfig));
67
+ parentPort.postMessage({
68
+ ok: true,
69
+ config: flattened,
70
+ keptExtends,
71
+ flattenedPresets: visitedPresets.merged,
72
+ errors: errors.map(format),
73
+ warnings: warnings.map(format),
74
+ });
75
+ }
76
+ catch (err) {
77
+ const e = err;
78
+ parentPort.postMessage({ ok: false, error: e.validationError ?? e.message ?? String(err) });
79
+ }
@@ -1,6 +1,7 @@
1
1
  import { PRESETS, REMOVED_PRESETS } from "../data/presets.generated.js";
2
2
  import { fetchExternalPreset } from "./externalPresetFetcher.js";
3
3
  import { HOSTED_PLATFORMS } from "./gitRemote.js";
4
+ import { homeScope, parseHostTokens } from "./hostTokens.js";
4
5
  import { runMerge } from "./mergeWorker.js";
5
6
  import { isRecord } from "./util.js";
6
7
  /**
@@ -11,6 +12,12 @@ import { isRecord } from "./util.js";
11
12
  * that historically imported it from there.
12
13
  */
13
14
  export const OWN_SOURCE = "<own>";
15
+ /**
16
+ * `PresetWarning.preset` of a credential-configuration problem (a malformed
17
+ * `RENOVATE_MCP_HOST_TOKENS`). Nothing is fetched while it stands; tools turn
18
+ * it into an `isError`.
19
+ */
20
+ export const AUTH_SOURCE = "<auth>";
14
21
  /**
15
22
  * Platforms `local>` presets can be routed through via the `platform` input.
16
23
  * Same list the git-remote classifier uses, so `check_setup`'s hints and the
@@ -43,7 +50,7 @@ export function classifyExternalSource(source) {
43
50
  case "local":
44
51
  return {
45
52
  fetchable: false,
46
- reason: "local> presets need a platform context. Pass `platform` (one of github, gitlab, gitea, forgejo, bitbucket) plus `endpoint` to resolve_config so they're routed through your platform's API. For full-fidelity merging (hostRules, regexManagers, etc.), run `dry_run` with the same params instead.",
53
+ reason: "local> presets need a platform context. Pass `platform` (one of github, gitlab, gitea, forgejo, bitbucket) plus `endpoint` to resolve_config, or set `RENOVATE_PLATFORM` / `RENOVATE_ENDPOINT` on the MCP server, so they're routed through your platform's API. For full-fidelity merging (hostRules, regexManagers, etc.), run `dry_run` with the same params instead.",
47
54
  };
48
55
  case "npm":
49
56
  return {
@@ -100,6 +107,9 @@ export async function resolveConfig(config, options = {}) {
100
107
  * merge (the worker-isolated faithful merge runs separately, see `mergeWorker`).
101
108
  */
102
109
  export async function collectMergeSteps(config, options = {}) {
110
+ // The `local>` route (ADR-0008), env fallbacks included. Tokens never
111
+ // follow it: they bind to the env scope only.
112
+ const scope = homeScope({ platform: options.platform, endpoint: options.endpoint });
103
113
  const ctx = {
104
114
  steps: [],
105
115
  presetsResolved: [],
@@ -108,15 +118,31 @@ export async function collectMergeSteps(config, options = {}) {
108
118
  stack: [],
109
119
  fetchExternal: options.fetchExternal ?? false,
110
120
  timeoutMs: options.timeoutMs,
111
- endpoint: options.endpoint,
112
- platform: options.platform,
121
+ endpoint: scope.endpoint,
122
+ platform: scope.platform,
123
+ fetchImpl: options.fetchImpl,
124
+ authError: undefined,
113
125
  cache: new Map(),
114
126
  };
115
- await collectSteps(config, null, [], ctx);
127
+ if (ctx.fetchExternal) {
128
+ const hostTokens = parseHostTokens();
129
+ if (!hostTokens.ok) {
130
+ ctx.authError = hostTokens.error;
131
+ ctx.warnings.push({ preset: AUTH_SOURCE, message: hostTokens.error });
132
+ }
133
+ }
134
+ await collectSteps(config, null, [], [], ctx);
116
135
  const { steps, presetsResolved, presetsUnresolved, warnings } = ctx;
117
136
  return { steps, presetsResolved, presetsUnresolved, warnings };
118
137
  }
119
- async function collectSteps(input, ownerName, viaChain, ctx) {
138
+ async function collectSteps(input, ownerName, viaChain, inheritedIgnore, ctx) {
139
+ // Renovate's resolveConfigPresets: a non-empty inherited list (the root's)
140
+ // wins; otherwise this node's own list governs its subtree.
141
+ const ignorePresets = inheritedIgnore.length > 0
142
+ ? inheritedIgnore
143
+ : Array.isArray(input.ignorePresets)
144
+ ? input.ignorePresets
145
+ : [];
120
146
  const rawExtends = input.extends;
121
147
  if (Array.isArray(rawExtends) && rawExtends.length > 0) {
122
148
  // Children of `input` are reached by going through `ownerName` (if any).
@@ -130,6 +156,8 @@ async function collectSteps(input, ownerName, viaChain, ctx) {
130
156
  });
131
157
  continue;
132
158
  }
159
+ if (ignorePresets.includes(entry))
160
+ continue;
133
161
  const redirect = followRemovedPresets(entry);
134
162
  if (redirect.kind === "cycle") {
135
163
  ctx.presetsUnresolved.push({
@@ -180,14 +208,16 @@ async function collectSteps(input, ownerName, viaChain, ctx) {
180
208
  const { value, missingArgs, unknownTemplates } = applyArgs(body, parsed.args);
181
209
  recordTemplateWarnings(entry, parsed.args.length, missingArgs, unknownTemplates, ctx.warnings);
182
210
  ctx.stack.push(parsed.key);
183
- await collectSteps(value, entry, childVia, ctx);
211
+ await collectSteps(value, entry, childVia, ignorePresets, ctx);
184
212
  ctx.stack.pop();
185
213
  ctx.presetsResolved.push(entry);
186
214
  }
187
215
  }
188
216
  // Own keys (siblings of `extends`) merge last for this node. Skip empty
189
217
  // own-key sets so a single-preset config stays a single step (no worker).
190
- const { extends: _drop, ...ownKeys } = input;
218
+ // Renovate's resolveConfigPresets deletes `ignorePresets` at every level,
219
+ // the root included.
220
+ const { extends: _drop, ignorePresets: _ignored, ...ownKeys } = input;
191
221
  if (Object.keys(ownKeys).length > 0) {
192
222
  ctx.steps.push({
193
223
  label: ownerName ?? OWN_SOURCE,
@@ -279,9 +309,16 @@ export async function loadPresetBody(parsed, ctx) {
279
309
  });
280
310
  return null;
281
311
  }
312
+ if (ctx.authError) {
313
+ unresolvedList.push({ preset: parsed.original, reason: `Not fetched: ${ctx.authError}` });
314
+ return null;
315
+ }
316
+ // Renovate routes only `local>` through the configured endpoint; the other
317
+ // sources hard-code their public API.
282
318
  const result = await fetchExternalPreset(effective, {
283
319
  timeoutMs: ctx.timeoutMs,
284
- endpoint: ctx.endpoint,
320
+ endpoint: parsed.source === "local" ? ctx.endpoint : undefined,
321
+ fetchImpl: ctx.fetchImpl,
285
322
  cache: ctx.cache,
286
323
  });
287
324
  if (!result.ok) {
@@ -328,6 +365,12 @@ const BUILTIN_NAMESPACES = new Set([
328
365
  ]);
329
366
  /** True when Renovate would treat `entry` as a `local>` preset (prefixed or bare `owner/repo`). */
330
367
  export function isLocalPreset(entry) {
368
+ // Renovate tests an unscoped `npm>` entry for `/` before stripping the prefix,
369
+ // so `npm>foo/bar` is local there unless it names a built-in namespace.
370
+ if (entry.startsWith("npm>") && !entry.startsWith("npm>@")) {
371
+ const rest = entry.slice("npm>".length);
372
+ return rest.includes("/") && !BUILTIN_NAMESPACES.has(rest.split(":", 1)[0]);
373
+ }
331
374
  return parsePreset(entry).source === "local";
332
375
  }
333
376
  export function parsePreset(raw) {
@@ -1,8 +1,8 @@
1
1
  import { Worker } from "node:worker_threads";
2
2
  /**
3
3
  * Shared driver for the worker-isolated Renovate carve-outs (`migrationWorker`,
4
- * `mergeWorker`, `packageRulesWorker`, `scheduleWorker` — ADR-0001 / 0004 /
5
- * 0006 / 0007). Spawns
4
+ * `mergeWorker`, `packageRulesWorker`, `scheduleWorker`, `presetFlattenWorker`
5
+ * — ADR-0001 / 0004 / 0006 / 0007 / 0009). Spawns
6
6
  * `entry` with `workerData`, resolves with the single `{ ok: true, ... }`
7
7
  * message the worker posts, and always terminates the worker afterwards. The
8
8
  * main process never imports `renovate`; only the `*WorkerImpl` entries do.
@@ -55,8 +55,7 @@ export async function runRenovateWorker(entry, workerData, { timeoutMs, label })
55
55
  return reply;
56
56
  }
57
57
  finally {
58
- if (timer)
59
- clearTimeout(timer);
58
+ clearTimeout(timer);
60
59
  await worker.terminate().catch(() => undefined);
61
60
  }
62
61
  }
@@ -0,0 +1,143 @@
1
+ import { constants, promises as fs } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import path from "node:path";
4
+ import { serializeConfig } from "./configWriter.js";
5
+ import { REPO_COPY_MAX_BYTES, REPO_COPY_MAX_FILES } from "./inputLimits.js";
6
+ import { listRepoFiles } from "./pathSelection.js";
7
+ const COPY_CONCURRENCY = 32;
8
+ const isInside = (p, root) => p === root || p.startsWith(root + path.sep);
9
+ /** Run `fn` over `items`, at most COPY_CONCURRENCY at a time. */
10
+ async function eachLimited(items, fn) {
11
+ for (let i = 0; i < items.length; i += COPY_CONCURRENCY) {
12
+ // allSettled: on a failure the whole batch has stopped before the caller
13
+ // removes the copy, so nothing is still writing into it.
14
+ const results = await Promise.allSettled(items.slice(i, i + COPY_CONCURRENCY).map(fn));
15
+ const failed = results.find((r) => r.status === "rejected");
16
+ if (failed)
17
+ throw failed.reason;
18
+ }
19
+ }
20
+ /**
21
+ * Copy `repoPath` into a fresh `mkdtemp` directory (mode 0700, realpath, so
22
+ * Renovate's own view of its cwd matches). Never copies `.git`; skips paths
23
+ * that no longer exist, directories (gitlinks), files whose directory
24
+ * resolves outside the repo, and relative symlinks pointing outside it (with
25
+ * a warning). Absolute symlinks into the repo are rewritten as relative links
26
+ * to the same place inside the copy; other symlinks are recreated verbatim. Refuses before copying
27
+ * when the listing or the lstat sum is over the caps, and removes a partial
28
+ * copy when copying fails.
29
+ */
30
+ export async function copyRepo(repoPath, limits = {}) {
31
+ const maxFiles = limits.maxFiles ?? REPO_COPY_MAX_FILES;
32
+ const maxBytes = limits.maxBytes ?? REPO_COPY_MAX_BYTES;
33
+ const realRepo = await fs.realpath(repoPath);
34
+ const givenRepo = path.resolve(repoPath);
35
+ const { files, source } = await listRepoFiles(realRepo);
36
+ const listed = files.filter((rel) => !rel.split("/").includes(".git"));
37
+ if (listed.length > maxFiles) {
38
+ return {
39
+ ok: false,
40
+ reason: `The repository has ${listed.length} files to copy, over the limit of ${maxFiles} files for dry_run's temporary copy.`,
41
+ };
42
+ }
43
+ const warnings = [];
44
+ const parents = new Map();
45
+ const realParent = (dir) => {
46
+ if (!parents.has(dir))
47
+ parents.set(dir, fs.realpath(dir).catch(() => null));
48
+ return parents.get(dir);
49
+ };
50
+ const entries = [];
51
+ await eachLimited(listed, async (rel) => {
52
+ const abs = path.join(realRepo, rel);
53
+ const parent = await realParent(path.dirname(abs));
54
+ if (parent === null)
55
+ return;
56
+ if (!isInside(parent, realRepo)) {
57
+ warnings.push(`Skipped \`${rel}\` in the temporary copy: its directory resolves outside the repository.`);
58
+ return;
59
+ }
60
+ const st = await fs.lstat(abs).catch(() => null);
61
+ if (st?.isSymbolicLink()) {
62
+ let target = await fs.readlink(abs);
63
+ if (!path.isAbsolute(target) && !isInside(path.resolve(parent, target), realRepo)) {
64
+ warnings.push(`Skipped \`${rel}\` in the temporary copy: it is a relative symlink pointing outside the repository.`);
65
+ return;
66
+ }
67
+ // An absolute link into the repo would lead the run back into repoPath:
68
+ // point it at the same place inside the copy, relative to the link.
69
+ const root = [realRepo, givenRepo].find((r) => path.isAbsolute(target) && isInside(target, r));
70
+ if (root)
71
+ target = path.relative(path.dirname(path.join(root, rel)), target) || ".";
72
+ entries.push({ rel, kind: "symlink", target });
73
+ }
74
+ else if (st?.isFile()) {
75
+ entries.push({ rel, kind: "file", size: st.size });
76
+ }
77
+ });
78
+ const bytes = entries.reduce((n, e) => n + (e.kind === "file" ? e.size : 0), 0);
79
+ if (bytes > maxBytes) {
80
+ return {
81
+ ok: false,
82
+ reason: `The repository has ${bytes} bytes to copy, over the limit of ${maxBytes} bytes for dry_run's temporary copy.`,
83
+ };
84
+ }
85
+ const dir = await fs.realpath(await fs.mkdtemp(path.join(tmpdir(), "renovate-mcp-dryrun-")));
86
+ try {
87
+ await eachLimited(entries, async (entry) => {
88
+ const dest = path.join(dir, entry.rel);
89
+ await fs.mkdir(path.dirname(dest), { recursive: true });
90
+ if (entry.kind === "symlink")
91
+ await fs.symlink(entry.target, dest);
92
+ else
93
+ await fs.copyFile(path.join(realRepo, entry.rel), dest, constants.COPYFILE_FICLONE);
94
+ });
95
+ }
96
+ catch (err) {
97
+ await removeRepoCopy(dir);
98
+ return { ok: false, reason: `Could not copy the repository for dry_run's temporary copy: ${err.message}` };
99
+ }
100
+ // ponytail: only the root `.npmrc` is checked. Renovate reads other
101
+ // untracked files from disk too (docs/tools.md lists the limit); warn per
102
+ // file when one of them turns out to matter.
103
+ if (source === "git" && !listed.includes(".npmrc") && (await fs.stat(path.join(realRepo, ".npmrc")).catch(() => null))) {
104
+ warnings.push("`.npmrc` is not tracked by git, so it is not in the temporary copy this run uses: npm lookups run without its registries and credentials and may report no updates. Pass the credentials as `hostRules` or set them on the MCP server instead.");
105
+ }
106
+ warnings.sort();
107
+ return { ok: true, dir, source, warnings };
108
+ }
109
+ /**
110
+ * Write the flattened config at `relPath` inside the copy `dir`. The parent
111
+ * directory must resolve inside the copy (a symlinked directory could point
112
+ * anywhere). The existing entry is unlinked first, so a symlinked config is
113
+ * replaced, never written through: other configs get fresh JSON bytes, and
114
+ * `package.json` gets its `renovate` key round-tripped with the siblings kept.
115
+ */
116
+ export async function writeFlattenedConfig(dir, relPath, config) {
117
+ const target = path.join(dir, relPath);
118
+ const parent = await fs.realpath(path.dirname(target)).catch(() => null);
119
+ if (parent === null || !isInside(parent, await fs.realpath(dir))) {
120
+ return {
121
+ ok: false,
122
+ reason: `Refusing to write the flattened config: the directory of \`${relPath}\` is missing or resolves outside the temporary copy.`,
123
+ };
124
+ }
125
+ const fail = (why) => ({ ok: false, reason: `Could not write the flattened config into \`${relPath}\`: ${why}.` });
126
+ try {
127
+ const existing = path.basename(relPath) === "package.json" ? await fs.readFile(target, "utf8") : undefined;
128
+ const serialized = serializeConfig({ targetPath: target, nextConfig: config, existing });
129
+ if ("refuse" in serialized)
130
+ return fail(serialized.reason);
131
+ await fs.rm(target, { force: true });
132
+ await fs.writeFile(target, serialized.bytes, { flag: "wx" });
133
+ }
134
+ catch (err) {
135
+ // The caller's catch would report it as a missing Renovate binary.
136
+ return fail(err.message);
137
+ }
138
+ return { ok: true };
139
+ }
140
+ /** Remove a copy made by `copyRepo`; a missing directory is fine. */
141
+ export async function removeRepoCopy(dir) {
142
+ await fs.rm(dir, { recursive: true, force: true });
143
+ }