@aspruyt/xfg 8.5.0 → 8.5.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.
@@ -23,12 +23,16 @@ export interface LabelChange {
23
23
  * An explicit empty string "" means "set to empty."
24
24
  * GitHub API returns null for labels without descriptions — treat null and
25
25
  * undefined as equivalent when comparing (neither triggers an update).
26
+ * A `new_name` entry whose source is missing is treated as an already-applied
27
+ * rename: the target is compared (or created) in its place and never deleted.
28
+ * A rename target that is itself a `new_name` entry (a chain) is rejected, as
29
+ * is a rename onto a label that already exists.
26
30
  *
27
31
  * @param current - Current labels from GitHub API
28
32
  * @param desired - Desired labels from config (name -> label)
29
33
  * @param deleteOrphaned - If true, delete current labels not in desired config
30
34
  * @param noDelete - If true, skip delete operations
31
35
  * @returns Array of changes to apply
32
- * @throws ValidationError if rename collisions are detected
36
+ * @throws ValidationError if rename collisions or chains are detected
33
37
  */
34
38
  export declare function diffLabels(current: GitHubLabel[], desired: Record<string, Label>, deleteOrphaned: boolean, noDelete: boolean): LabelChange[];
@@ -9,22 +9,24 @@ import { ValidationError } from "../../shared/errors.js";
9
9
  * An explicit empty string "" means "set to empty."
10
10
  * GitHub API returns null for labels without descriptions — treat null and
11
11
  * undefined as equivalent when comparing (neither triggers an update).
12
+ * A `new_name` entry whose source is missing is treated as an already-applied
13
+ * rename: the target is compared (or created) in its place and never deleted.
14
+ * A rename target that is itself a `new_name` entry (a chain) is rejected, as
15
+ * is a rename onto a label that already exists.
12
16
  *
13
17
  * @param current - Current labels from GitHub API
14
18
  * @param desired - Desired labels from config (name -> label)
15
19
  * @param deleteOrphaned - If true, delete current labels not in desired config
16
20
  * @param noDelete - If true, skip delete operations
17
21
  * @returns Array of changes to apply
18
- * @throws ValidationError if rename collisions are detected
22
+ * @throws ValidationError if rename collisions or chains are detected
19
23
  */
20
24
  export function diffLabels(current, desired, deleteOrphaned, noDelete) {
21
25
  const changes = [];
22
- // Build case-insensitive lookup of current labels
23
26
  const currentByName = new Map();
24
27
  for (const label of current) {
25
28
  currentByName.set(label.name.toLowerCase(), label);
26
29
  }
27
- // Collect rename targets for collision detection
28
30
  const renameTargets = new Map(); // lowercase target -> source name
29
31
  for (const [name, label] of Object.entries(desired)) {
30
32
  if (label.new_name) {
@@ -35,54 +37,66 @@ export function diffLabels(current, desired, deleteOrphaned, noDelete) {
35
37
  renameTargets.set(targetLower, name);
36
38
  }
37
39
  }
38
- // Determine which labels will be deleted (for collision checking)
39
- const desiredLower = new Set(Object.keys(desired).map((n) => n.toLowerCase()));
40
- const deletedNames = new Set();
41
- if (deleteOrphaned && !noDelete) {
42
- for (const nameLower of currentByName.keys()) {
43
- if (!desiredLower.has(nameLower)) {
44
- deletedNames.add(nameLower);
45
- }
40
+ for (const [name, label] of Object.entries(desired)) {
41
+ if (!label.new_name)
42
+ continue;
43
+ const targetLower = label.new_name.toLowerCase();
44
+ if (targetLower === name.toLowerCase())
45
+ continue;
46
+ const chained = Object.entries(desired).find(([n, l]) => n.toLowerCase() === targetLower && l.new_name);
47
+ if (chained) {
48
+ throw new ValidationError(`Rename chain: '${name}' renames to '${label.new_name}', which is itself renamed to '${chained[1].new_name}'. Chained renames are not supported; run them separately`);
46
49
  }
47
50
  }
48
- // Check rename targets for collisions with existing labels.
49
- // Note: Chain renames (A->B and B->C) are allowed when the target label is
50
- // itself being renamed away. This is safe because the apply ordering
51
- // (deletes -> updates -> creates) ensures both renames execute in the same batch.
52
- // This deviates from the original design plan which called for flagging chains
53
- // as errors, but the permissive behavior is correct and more user-friendly.
51
+ // A rename whose source is gone was already applied: its target is the managed label
52
+ const renamedTargets = new Set();
53
+ for (const [name, label] of Object.entries(desired)) {
54
+ if (label.new_name && !currentByName.has(name.toLowerCase())) {
55
+ renamedTargets.add(label.new_name.toLowerCase());
56
+ }
57
+ }
58
+ const desiredLower = new Set([
59
+ ...Object.keys(desired).map((n) => n.toLowerCase()),
60
+ ...renamedTargets,
61
+ ]);
54
62
  for (const [name, label] of Object.entries(desired)) {
55
63
  if (!label.new_name)
56
64
  continue;
57
65
  const targetLower = label.new_name.toLowerCase();
58
66
  const nameLower = name.toLowerCase();
59
- // Check if target collides with an existing label that is NOT:
60
- // 1. The source label itself
61
- // 2. Being deleted in this diff
62
- // 3. Being renamed away in this diff
63
- if (currentByName.has(targetLower) &&
64
- targetLower !== nameLower &&
65
- !deletedNames.has(targetLower)) {
66
- const collidingDesired = Object.entries(desired).find(([n]) => n.toLowerCase() === targetLower);
67
- if (!collidingDesired || !collidingDesired[1].new_name) {
68
- throw new ValidationError(`Rename collision: '${name}' would rename to '${label.new_name}', but that label already exists`);
69
- }
67
+ if (!currentByName.has(nameLower))
68
+ continue;
69
+ // Never resolve a collision by deleting the target: deleting a label strips it from every PR
70
+ if (currentByName.has(targetLower) && targetLower !== nameLower) {
71
+ throw new ValidationError(`Rename collision: '${name}' would rename to '${label.new_name}', but that label already exists`);
70
72
  }
71
73
  }
72
- // Check each desired label
73
74
  for (const [name, desiredLabel] of Object.entries(desired)) {
74
75
  const nameLower = name.toLowerCase();
75
- const currentLabel = currentByName.get(nameLower);
76
+ let currentLabel = currentByName.get(nameLower);
77
+ let labelName = name;
78
+ let labelConfig = desiredLabel;
79
+ if (!currentLabel && desiredLabel.new_name) {
80
+ const targetLower = desiredLabel.new_name.toLowerCase();
81
+ // The target's own entry, if any, already covers this label
82
+ if (Object.keys(desired).some((n) => n.toLowerCase() === targetLower)) {
83
+ continue;
84
+ }
85
+ const { new_name: targetName, ...config } = desiredLabel;
86
+ labelName = targetName;
87
+ labelConfig = config;
88
+ currentLabel = currentByName.get(targetLower);
89
+ }
76
90
  if (!currentLabel) {
77
91
  changes.push({
78
92
  action: "create",
79
- name,
80
- desired: desiredLabel,
93
+ name: labelName,
94
+ desired: labelConfig,
81
95
  });
82
96
  }
83
97
  else {
84
98
  const propChanges = [];
85
- const desiredColor = normalizeColor(desiredLabel.color);
99
+ const desiredColor = normalizeColor(labelConfig.color);
86
100
  const currentColor = currentLabel.color.toLowerCase();
87
101
  if (desiredColor !== currentColor) {
88
102
  propChanges.push({
@@ -91,46 +105,44 @@ export function diffLabels(current, desired, deleteOrphaned, noDelete) {
91
105
  newValue: desiredColor,
92
106
  });
93
107
  }
94
- // Description: undefined = don't compare, explicit value = compare
95
- if (desiredLabel.description !== undefined) {
108
+ if (labelConfig.description !== undefined) {
96
109
  const currentDesc = currentLabel.description ?? "";
97
- if (desiredLabel.description !== currentDesc) {
110
+ if (labelConfig.description !== currentDesc) {
98
111
  propChanges.push({
99
112
  property: "description",
100
113
  oldValue: currentLabel.description ?? undefined,
101
- newValue: desiredLabel.description,
114
+ newValue: labelConfig.description,
102
115
  });
103
116
  }
104
117
  }
105
- // new_name always triggers an update
106
- if (desiredLabel.new_name) {
118
+ if (labelConfig.new_name &&
119
+ labelConfig.new_name !== currentLabel.name) {
107
120
  propChanges.push({
108
121
  property: "new_name",
109
- oldValue: name,
110
- newValue: desiredLabel.new_name,
122
+ oldValue: currentLabel.name,
123
+ newValue: labelConfig.new_name,
111
124
  });
112
125
  }
113
126
  if (propChanges.length > 0) {
114
127
  changes.push({
115
128
  action: "update",
116
- name,
117
- newName: desiredLabel.new_name,
129
+ name: currentLabel.name,
130
+ newName: labelConfig.new_name,
118
131
  current: currentLabel,
119
- desired: desiredLabel,
132
+ desired: labelConfig,
120
133
  propertyChanges: propChanges,
121
134
  });
122
135
  }
123
136
  else {
124
137
  changes.push({
125
138
  action: "unchanged",
126
- name,
139
+ name: labelName,
127
140
  current: currentLabel,
128
- desired: desiredLabel,
141
+ desired: labelConfig,
129
142
  });
130
143
  }
131
144
  }
132
145
  }
133
- // Desired-state orphan detection: delete ALL current not in desired
134
146
  if (deleteOrphaned && !noDelete) {
135
147
  for (const [nameLower, currentLabel] of currentByName) {
136
148
  if (!desiredLower.has(nameLower)) {
@@ -142,7 +154,6 @@ export function diffLabels(current, desired, deleteOrphaned, noDelete) {
142
154
  }
143
155
  }
144
156
  }
145
- // Sort: delete first, then update, then create, then unchanged
146
157
  const actionOrder = {
147
158
  delete: 0,
148
159
  update: 1,
@@ -97,6 +97,15 @@ export class SyncWorkflow {
97
97
  };
98
98
  }
99
99
  if (isDirectMode) {
100
+ if (runCtx.dryRun) {
101
+ return {
102
+ success: true,
103
+ repoName,
104
+ message: `[DRY RUN] Would push directly to ${session.baseBranch}`,
105
+ diffStats: workResult.diffStats,
106
+ fileChanges: workResult.fileChangeDetails,
107
+ };
108
+ }
100
109
  this.log.info(`Changes pushed directly to ${session.baseBranch}`);
101
110
  return {
102
111
  success: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aspruyt/xfg",
3
- "version": "8.5.0",
3
+ "version": "8.5.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",