@aspruyt/xfg 6.3.0 → 6.4.1

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/PR.md CHANGED
@@ -8,6 +8,6 @@ Automated sync of configuration files to ${xfg:repo.fullName}.
8
8
 
9
9
  ${xfg:pr.fileChanges}
10
10
 
11
- ---
11
+ ______________________________________________________________________
12
12
 
13
13
  _This PR was automatically generated by [xfg](https://github.com/anthony-spruyt/xfg)_
package/README.md CHANGED
@@ -1,13 +1,8 @@
1
1
  # xfg
2
2
 
3
- [![CI](https://github.com/anthony-spruyt/xfg/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/anthony-spruyt/xfg/actions/workflows/ci.yaml)
4
- [![codecov](https://codecov.io/gh/anthony-spruyt/xfg/graph/badge.svg)](https://codecov.io/gh/anthony-spruyt/xfg)
5
- [![Socket Badge](https://badge.socket.dev/npm/package/@aspruyt/xfg)](https://socket.dev/npm/package/@aspruyt/xfg)
6
- [![npm version](https://img.shields.io/npm/v/@aspruyt/xfg.svg)](https://www.npmjs.com/package/@aspruyt/xfg)
7
- [![npm downloads](https://img.shields.io/npm/dw/@aspruyt/xfg.svg)](https://www.npmjs.com/package/@aspruyt/xfg)
8
- [![GitHub Marketplace](https://img.shields.io/badge/Marketplace-xfg-blue?logo=github)](https://github.com/marketplace/actions/xfg-repo-as-code)
9
- [![docs](https://img.shields.io/badge/docs-GitHub%20Pages-blue)](https://anthony-spruyt.github.io/xfg/)
10
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
3
+ [![CI](https://github.com/anthony-spruyt/xfg/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/anthony-spruyt/xfg/actions/workflows/ci.yaml) [![codecov](https://codecov.io/gh/anthony-spruyt/xfg/graph/badge.svg)](https://codecov.io/gh/anthony-spruyt/xfg) [![Socket Badge](https://badge.socket.dev/npm/package/@aspruyt/xfg)](https://socket.dev/npm/package/@aspruyt/xfg)
4
+ [![npm version](https://img.shields.io/npm/v/@aspruyt/xfg.svg)](https://www.npmjs.com/package/@aspruyt/xfg) [![npm downloads](https://img.shields.io/npm/dw/@aspruyt/xfg.svg)](https://www.npmjs.com/package/@aspruyt/xfg) [![GitHub Marketplace](https://img.shields.io/badge/Marketplace-xfg-blue?logo=github)](https://github.com/marketplace/actions/xfg-repo-as-code)
5
+ [![docs](https://img.shields.io/badge/docs-GitHub%20Pages-blue)](https://anthony-spruyt.github.io/xfg/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
11
6
 
12
7
  Manage files, settings, and repositories across GitHub, Azure DevOps, and GitLab — declaratively, from a single YAML config.
13
8
 
@@ -1,6 +1,2 @@
1
+ export { validateBranchName } from "../shared/branch-validation.js";
1
2
  export declare function sanitizeBranchName(fileName: string): string;
2
- /**
3
- * Validates a user-provided branch name against git's naming rules.
4
- * @throws ValidationError if the branch name is invalid
5
- */
6
- export declare function validateBranchName(branchName: string): void;
@@ -1,4 +1,4 @@
1
- import { ValidationError } from "../shared/errors.js";
1
+ export { validateBranchName } from "../shared/branch-validation.js";
2
2
  export function sanitizeBranchName(fileName) {
3
3
  return fileName
4
4
  .toLowerCase()
@@ -7,24 +7,3 @@ export function sanitizeBranchName(fileName) {
7
7
  .replace(/-+/g, "-") // Collapse multiple dashes
8
8
  .replace(/^-|-$/g, ""); // Remove leading/trailing dashes
9
9
  }
10
- /**
11
- * Validates a user-provided branch name against git's naming rules.
12
- * @throws ValidationError if the branch name is invalid
13
- */
14
- export function validateBranchName(branchName) {
15
- if (!branchName || branchName.trim() === "") {
16
- throw new ValidationError("Branch name cannot be empty");
17
- }
18
- if (branchName.startsWith(".") || branchName.startsWith("-")) {
19
- throw new ValidationError('Branch name cannot start with "." or "-"');
20
- }
21
- // Git disallows: space, ~, ^, :, ?, *, [, \, and consecutive dots (..)
22
- if (/[\s~^:?*[\\]/.test(branchName) || branchName.includes("..")) {
23
- throw new ValidationError("Branch name contains invalid characters");
24
- }
25
- if (branchName.endsWith("/") ||
26
- branchName.endsWith(".lock") ||
27
- branchName.endsWith(".")) {
28
- throw new ValidationError("Branch name has invalid ending");
29
- }
30
- }
@@ -56,8 +56,9 @@ async function runFileSyncPhase(repo, ctx) {
56
56
  const repoNumber = repo.index + 1;
57
57
  try {
58
58
  ctx.logger.progress(repoNumber, repo.repoName, "Processing...");
59
+ const branchName = repo.repoConfig.prOptions?.branch ?? ctx.branchName;
59
60
  const result = await ctx.processor.process(repo.repoConfig, repo.repoInfo, {
60
- branchName: ctx.branchName,
61
+ branchName,
61
62
  workDir: repo.workDir,
62
63
  configId: ctx.config.id,
63
64
  dryRun: ctx.options.dryRun,
@@ -95,12 +96,16 @@ async function runFileSyncPhase(repo, ctx) {
95
96
  export async function runSingleRepo(repoConfig, index, ctx) {
96
97
  const { config, options, logger } = ctx;
97
98
  const repoNumber = index + 1;
98
- const effectivePrOptions = options.merge || options.mergeStrategy || options.deleteBranch
99
+ const effectivePrOptions = options.merge ||
100
+ options.mergeStrategy ||
101
+ options.deleteBranch ||
102
+ options.branch
99
103
  ? {
100
104
  ...repoConfig.prOptions,
101
105
  merge: options.merge ?? repoConfig.prOptions?.merge,
102
106
  mergeStrategy: options.mergeStrategy ?? repoConfig.prOptions?.mergeStrategy,
103
107
  deleteBranch: options.deleteBranch ?? repoConfig.prOptions?.deleteBranch,
108
+ branch: options.branch ?? repoConfig.prOptions?.branch,
104
109
  }
105
110
  : repoConfig.prOptions;
106
111
  const effectiveRepoConfig = { ...repoConfig, prOptions: effectivePrOptions };
@@ -1,5 +1,5 @@
1
1
  import { readFileSync, statSync, readdirSync } from "node:fs";
2
- import { dirname, join, extname } from "node:path";
2
+ import { dirname, join, extname, relative } from "node:path";
3
3
  import { parse } from "yaml";
4
4
  import { validateRawConfig } from "./validator.js";
5
5
  import { normalizeConfig as normalizeConfigInternal } from "./normalizer.js";
@@ -51,49 +51,78 @@ function loadRawConfigFromFile(filePath) {
51
51
  validateRawConfig(rawConfig);
52
52
  return rawConfig;
53
53
  }
54
- function loadRawConfigFromDirectory(dirPath) {
54
+ const MAX_CONFIG_DEPTH = 10;
55
+ function collectYamlFiles(rootDir, currentDir, depth) {
56
+ if (depth > MAX_CONFIG_DEPTH) {
57
+ /* c8 ignore next -- rootDir === currentDir impossible at depth > MAX_CONFIG_DEPTH */
58
+ const rel = relative(rootDir, currentDir) || ".";
59
+ throw new ValidationError(`Config directory nesting exceeds maximum depth of ${MAX_CONFIG_DEPTH} at ${rel}`);
60
+ }
55
61
  let entries;
56
62
  try {
57
- entries = readdirSync(dirPath, { withFileTypes: true });
63
+ entries = readdirSync(currentDir, { withFileTypes: true });
58
64
  }
59
65
  catch (error) {
60
- throw new ValidationError(`Failed to read config directory ${dirPath}: ${toErrorMessage(error)}`, { cause: error });
66
+ const displayPath = relative(rootDir, currentDir) || currentDir;
67
+ throw new ValidationError(`Failed to read config directory ${displayPath}: ${toErrorMessage(error)}`, { cause: error });
61
68
  }
62
- const yamlFiles = entries
63
- .filter((entry) => entry.isFile() &&
64
- [".yaml", ".yml"].includes(extname(entry.name).toLowerCase()))
65
- .map((entry) => entry.name)
66
- .sort();
69
+ const files = [];
70
+ const subdirs = [];
71
+ for (const entry of entries) {
72
+ if (entry.name.startsWith(".")) {
73
+ continue;
74
+ }
75
+ const ext = extname(entry.name).toLowerCase();
76
+ const isYaml = ext === ".yaml" || ext === ".yml";
77
+ if ((entry.isFile() || entry.isSymbolicLink()) && isYaml) {
78
+ files.push({
79
+ relativePath: relative(rootDir, join(currentDir, entry.name)),
80
+ absolutePath: join(currentDir, entry.name),
81
+ });
82
+ }
83
+ else if (entry.isDirectory()) {
84
+ subdirs.push(entry.name);
85
+ }
86
+ }
87
+ files.sort((a, b) => a.relativePath.localeCompare(b.relativePath));
88
+ subdirs.sort((a, b) => a.localeCompare(b));
89
+ const result = [...files];
90
+ for (const subdir of subdirs) {
91
+ result.push(...collectYamlFiles(rootDir, join(currentDir, subdir), depth + 1));
92
+ }
93
+ return result;
94
+ }
95
+ function loadRawConfigFromDirectory(dirPath) {
96
+ const yamlFiles = collectYamlFiles(dirPath, dirPath, 0);
67
97
  if (yamlFiles.length === 0) {
68
98
  throw new ValidationError(`No .yaml or .yml files found in directory: ${dirPath}`);
69
99
  }
70
- const fragments = yamlFiles.map((fileName) => {
71
- const filePath = join(dirPath, fileName);
100
+ const fragments = yamlFiles.map(({ relativePath, absolutePath }) => {
72
101
  let content;
73
102
  try {
74
- content = readFileSync(filePath, "utf-8");
103
+ content = readFileSync(absolutePath, "utf-8");
75
104
  }
76
105
  catch (error) {
77
- throw new ValidationError(`Failed to read config file ${filePath}: ${toErrorMessage(error)}`, { cause: error });
106
+ throw new ValidationError(`Failed to read config file ${relativePath}: ${toErrorMessage(error)}`, { cause: error });
78
107
  }
79
- const configDir = dirname(filePath);
108
+ const configDir = dirname(absolutePath);
80
109
  let config;
81
110
  try {
82
111
  config = parse(content);
83
112
  }
84
113
  catch (error) {
85
114
  const message = toErrorMessage(error);
86
- throw new ValidationError(`Failed to parse YAML config at ${filePath}: ${message}`, { cause: error });
115
+ throw new ValidationError(`Failed to parse YAML config at ${relativePath}: ${message}`, { cause: error });
87
116
  }
88
117
  if (!config || typeof config !== "object") {
89
- throw new ValidationError(`Config file ${fileName} is empty or invalid — expected a YAML mapping`);
118
+ throw new ValidationError(`Config file ${relativePath} is empty or invalid — expected a YAML mapping`);
90
119
  }
91
120
  // Safe cast: resolveFileReferencesInConfig only accesses optional fields
92
121
  // (files, groups, etc.), so fragments missing id/repos work correctly.
93
122
  config = resolveFileReferencesInConfig(config, {
94
123
  configDir,
95
124
  });
96
- return { fileName, config };
125
+ return { fileName: relativePath, config };
97
126
  });
98
127
  const merged = mergeConfigFragments(fragments);
99
128
  validateRawConfig(merged);
@@ -7,6 +7,7 @@ export interface PRMergeOptions {
7
7
  deleteBranch?: boolean;
8
8
  bypassReason?: string;
9
9
  labels?: string[];
10
+ branch?: string;
10
11
  }
11
12
  export type RulesetTarget = "branch" | "tag";
12
13
  export type RulesetEnforcement = "active" | "disabled" | "evaluate";
@@ -1,6 +1,7 @@
1
1
  import { validateFileName } from "./validators/file-validator.js";
2
2
  import { isPlainObject } from "../shared/type-guards.js";
3
3
  import { ValidationError } from "../shared/errors.js";
4
+ import { validateBranchName } from "../shared/branch-validation.js";
4
5
  import { validateFileConfigFields, validateSettings, } from "./validators/shared.js";
5
6
  import { validateGroups, validateConditionalGroups, } from "./validators/group-validator.js";
6
7
  import { validateRepoEntry } from "./validators/repo-entry-validator.js";
@@ -68,6 +69,9 @@ function validateGithubHosts(config) {
68
69
  }
69
70
  }
70
71
  function validatePrOptions(config) {
72
+ if (config.prOptions?.branch !== undefined) {
73
+ validateBranchName(config.prOptions.branch);
74
+ }
71
75
  if (config.prOptions?.labels === undefined)
72
76
  return;
73
77
  if (!Array.isArray(config.prOptions.labels)) {
@@ -1,6 +1,7 @@
1
1
  import { resolveExtendsChain } from "../extends-resolver.js";
2
2
  import { isPlainObject } from "../../shared/type-guards.js";
3
3
  import { ValidationError } from "../../shared/errors.js";
4
+ import { validateBranchName } from "../../shared/branch-validation.js";
4
5
  import { validateFileConfigFields, validateSettings, buildRootSettingsContext, } from "./shared.js";
5
6
  function validateGroupExtends(groupName, extends_, groupNames) {
6
7
  if (typeof extends_ === "string") {
@@ -102,6 +103,9 @@ export function validateGroups(config) {
102
103
  if (group.settings !== undefined) {
103
104
  validateSettings(group.settings, `groups.${groupName}`, rootCtx);
104
105
  }
106
+ if (group.prOptions?.branch !== undefined) {
107
+ validateBranchName(group.prOptions.branch);
108
+ }
105
109
  }
106
110
  validateNoCircularExtends(config.groups);
107
111
  }
@@ -163,5 +167,8 @@ export function validateConditionalGroups(config) {
163
167
  if (entry.settings !== undefined) {
164
168
  validateSettings(entry.settings, ctx, rootCtx);
165
169
  }
170
+ if (entry.prOptions?.branch !== undefined) {
171
+ validateBranchName(entry.prOptions.branch);
172
+ }
166
173
  }
167
174
  }
@@ -3,6 +3,7 @@ import { validateFileName } from "./file-validator.js";
3
3
  import { isPlainObject } from "../../shared/type-guards.js";
4
4
  import { escapeRegExp } from "../../shared/regex-utils.js";
5
5
  import { ValidationError } from "../../shared/errors.js";
6
+ import { validateBranchName } from "../../shared/branch-validation.js";
6
7
  import { validateFileConfigFields, validateSettings, buildRootSettingsContext, enrichSettingsContext, } from "./shared.js";
7
8
  function isValidGitUrl(url) {
8
9
  return /^git@[^:]+:.+$/.test(url) || /^https?:\/\/[^/]+\/.+$/.test(url);
@@ -156,10 +157,16 @@ function validateRepoSettingsEntry(config, repo, repoLabel) {
156
157
  }
157
158
  validateSettings(repo.settings, `Repo ${repoLabel}`, rootCtx);
158
159
  }
160
+ function validateRepoPrOptions(repo) {
161
+ if (repo.prOptions?.branch !== undefined) {
162
+ validateBranchName(repo.prOptions.branch);
163
+ }
164
+ }
159
165
  export function validateRepoEntry(config, repo, index) {
160
166
  const repoLabel = validateRepoGitField(repo, index);
161
167
  validateRepoOrigins(config, repo, repoLabel);
162
168
  validateRepoGroups(config, repo, index);
163
169
  validateRepoFiles(config, repo, index, repoLabel);
164
170
  validateRepoSettingsEntry(config, repo, repoLabel);
171
+ validateRepoPrOptions(repo);
165
172
  }
@@ -0,0 +1,2 @@
1
+ /** Validates a user-provided branch name against git's naming rules. @throws ValidationError if the branch name is invalid */
2
+ export declare function validateBranchName(branchName: string): void;
@@ -0,0 +1,19 @@
1
+ import { ValidationError } from "./errors.js";
2
+ /** Validates a user-provided branch name against git's naming rules. @throws ValidationError if the branch name is invalid */
3
+ export function validateBranchName(branchName) {
4
+ if (!branchName || branchName.trim() === "") {
5
+ throw new ValidationError("Branch name cannot be empty");
6
+ }
7
+ if (branchName.startsWith(".") || branchName.startsWith("-")) {
8
+ throw new ValidationError('Branch name cannot start with "." or "-"');
9
+ }
10
+ // Git disallows: space, ~, ^, :, ?, *, [, \, and consecutive dots (..)
11
+ if (/[\s~^:?*[\\]/.test(branchName) || branchName.includes("..")) {
12
+ throw new ValidationError("Branch name contains invalid characters");
13
+ }
14
+ if (branchName.endsWith("/") ||
15
+ branchName.endsWith(".lock") ||
16
+ branchName.endsWith(".")) {
17
+ throw new ValidationError("Branch name has invalid ending");
18
+ }
19
+ }
@@ -41,12 +41,33 @@ export class FileWriter {
41
41
  if (file.createOnly) {
42
42
  const existsOnBase = await gitOps.fileExistsOnBranch(file.fileName, baseBranch);
43
43
  if (existsOnBase) {
44
- log.info(`Skipping ${file.fileName} (createOnly: exists on ${baseBranch})`);
45
- fileChanges.set(file.fileName, {
46
- fileName: file.fileName,
47
- content: null,
48
- action: "skip",
49
- });
44
+ const desiredMode = shouldBeExecutable(file)
45
+ ? "100755"
46
+ : "100644";
47
+ const currentMode = await gitOps.getFileMode(file.fileName);
48
+ modeCache.set(file.fileName, currentMode);
49
+ const modeDiffers = currentMode !== null && currentMode !== desiredMode;
50
+ if (modeDiffers) {
51
+ fileChanges.set(file.fileName, {
52
+ fileName: file.fileName,
53
+ content: null,
54
+ action: "update",
55
+ mode: desiredMode,
56
+ modeOnly: true,
57
+ });
58
+ incrementDiffStats(diffStats, "MODIFIED");
59
+ if (dryRun) {
60
+ log.info(`Would change mode: ${file.fileName} ${currentMode} -> ${desiredMode}`);
61
+ }
62
+ }
63
+ else {
64
+ log.info(`Skipping ${file.fileName} (createOnly: exists on ${baseBranch})`);
65
+ fileChanges.set(file.fileName, {
66
+ fileName: file.fileName,
67
+ content: null,
68
+ action: "skip",
69
+ });
70
+ }
50
71
  return;
51
72
  }
52
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aspruyt/xfg",
3
- "version": "6.3.0",
3
+ "version": "6.4.1",
4
4
  "description": "Manage files, settings, and repositories across GitHub, Azure DevOps, and GitLab — declaratively, from a single YAML config",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",