From 82a0cae84d73380a5f32a398a30dd8b68131b18f Mon Sep 17 00:00:00 2001 From: Midigo Frank <39288959+midigofrank@users.noreply.github.com> Date: Mon, 2 Nov 2026 18:29:37 +0300 Subject: [PATCH 1/3] Keep non-ASCII letters and punctuation in ids derived from names (#1605) * Keep non-ASCII letters and punctuation in ids slugify replaced every non-word character with a hyphen, so names in other scripts lost their letters and their ids didn't match Lightning's. It now only replaces characters that aren't safe in file names. Names that differ only by case or by space vs hyphen still give the same id. A pull now rejects them, as Lightning's export does, so the resources.yaml writer no longer adds -2 suffixes to channel keys. * Track channel ids in the checkout file The new slug rules change some resources.yaml keys, and merging only matched channels by key, so a channel whose key changed lost its id and was treated as removed. The checkout file now records channel_uuids by key, as it already does for workflows. A merge matches channels by id first, then by key, then by name. * add changeset for channels change * comments and tests --------- Co-authored-by: Joe Clark --- .changeset/track-channel-ids.md | 6 + .changeset/unique-slug-ids.md | 6 + packages/project/src/Project.ts | 4 +- packages/project/src/merge/merge-project.ts | 35 +++-- packages/project/src/parse/from-app-state.ts | 31 ++++ packages/project/src/parse/from-fs.ts | 9 +- packages/project/src/util/config.ts | 13 +- packages/project/src/util/resources.ts | 10 +- packages/project/src/util/slugify.ts | 12 +- .../project/test/merge/merge-project.test.ts | 47 ++++++ .../project/test/parse/from-app-state.test.ts | 68 +++++++++ packages/project/test/parse/from-fs.test.ts | 16 ++ packages/project/test/util/config.test.ts | 19 +++ packages/project/test/util/slugify.test.ts | 141 ++++++++++++++++++ 14 files changed, 395 insertions(+), 22 deletions(-) create mode 100644 .changeset/track-channel-ids.md create mode 100644 .changeset/unique-slug-ids.md create mode 100644 packages/project/test/util/slugify.test.ts diff --git a/.changeset/track-channel-ids.md b/.changeset/track-channel-ids.md new file mode 100644 index 000000000..4b2893530 --- /dev/null +++ b/.changeset/track-channel-ids.md @@ -0,0 +1,6 @@ +--- +'@openfn/project': patch +'@openfn/cli': patch +--- + +Channels are now tracked by their Lightning id, so changing a channel's key in resources.yaml no longer deletes and recreates it on deploy. diff --git a/.changeset/unique-slug-ids.md b/.changeset/unique-slug-ids.md new file mode 100644 index 000000000..826370a1a --- /dev/null +++ b/.changeset/unique-slug-ids.md @@ -0,0 +1,6 @@ +--- +'@openfn/project': minor +'@openfn/cli': patch +--- + +Workflow, step and channel ids now keep non-ASCII letters and punctuation from their names, matching Lightning. Names with these characters get new ids, so their folders and resources.yaml keys change on the next pull. diff --git a/packages/project/src/Project.ts b/packages/project/src/Project.ts index e6040f5be..44dcee47c 100644 --- a/packages/project/src/Project.ts +++ b/packages/project/src/Project.ts @@ -42,7 +42,7 @@ type CLIMeta = { version?: number; alias?: string | null; forked_from?: Record; - // uuid of each workflow, by local id. Tracked but not used + // uuid of each workflow, by local id workflow_uuids?: Record; }; @@ -54,7 +54,7 @@ export class Project { /** Human readable project name. This corresponds to the label in Lightning */ name?: string; - /** Project id. Must be url safe. May be derived from the name. NOT a UUID */ + /** Project id. Must be file safe. May be derived from the name. NOT a UUID */ id: string; description?: string; diff --git a/packages/project/src/merge/merge-project.ts b/packages/project/src/merge/merge-project.ts index 8e1642990..7d9736510 100644 --- a/packages/project/src/merge/merge-project.ts +++ b/packages/project/src/merge/merge-project.ts @@ -201,10 +201,10 @@ export function merge( baseMerge(target, source, ['channels'], assigns as any) ); - // target channels with an id that the source dropped have been removed - const kept = new Set(source.channels?.map(channelKey)); + // target channels whose id didn't match any source channel have been removed + const kept = new Set(merged.channels?.map((c) => c.id)); merged.removedChannels = source.channels - ? (target.channels ?? []).filter((c) => c.id && !kept.has(channelKey(c))) + ? (target.channels ?? []).filter((c) => c.id && !kept.has(c.id)) : []; return merged; } @@ -225,8 +225,9 @@ export function mergeCollections( })); } -// Source channels win, but keep the target's id on a key match. Matching on the -// key rather than the name means renaming a channel keeps its id +// Source channels win, but keep the target's id on a match. Like workflows, +// a channel is matched by id (from the checkout file), then key, then name. +// Matching on the id or key means renaming a channel keeps its id // If the source has no channels at all (no resources.yaml), keep the target's export function mergeChannels( source: ChannelState[] | undefined, @@ -239,11 +240,25 @@ export function mergeChannels( if (!source) { return target; } - const targetByKey = new Map((target ?? []).map((c) => [channelKey(c), c])); - return source.map((c) => ({ - ...c, - id: targetByKey.get(channelKey(c))?.id, - })); + const available = [...(target ?? [])]; + const claim = (match?: ChannelState) => { + if (match) available.splice(available.indexOf(match), 1); + return match; + }; + const matches = new Map(); + const passes = [ + (s: ChannelState, t: ChannelState) => !!s.id && s.id === t.id, + (s: ChannelState, t: ChannelState) => channelKey(s) === channelKey(t), + (s: ChannelState, t: ChannelState) => s.name === t.name, + ]; + for (const pass of passes) { + for (const s of source) { + if (!matches.get(s)) { + matches.set(s, claim(available.find((t) => pass(s, t)))); + } + } + } + return source.map((c) => ({ ...c, id: matches.get(c)?.id })); } export const replaceCredentials = ( diff --git a/packages/project/src/parse/from-app-state.ts b/packages/project/src/parse/from-app-state.ts index 9b4c8e2b8..36aba73d5 100644 --- a/packages/project/src/parse/from-app-state.ts +++ b/packages/project/src/parse/from-app-state.ts @@ -109,9 +109,35 @@ export default ( return workflow; }) as unknown as l.WorkflowState[]; + assertUniqueIds( + Object.values(stateJson.workflows).filter((w) => !w.delete), + 'workflow names' + ); + + assertUniqueIds(channels ?? [], 'channel names'); + return new Project(proj as l.ProjectState, config); }; +// Ids are derived from names, so names that differ only by case or by +// space vs hyphen (eg "Foo Bar" and "foo-bar") share an id and would overwrite +// each other. Lightning's own export rejects these pairs too +const assertUniqueIds = (items: Array<{ name: string }>, what: string) => { + const byId: Record = {}; + for (const { name } of items) { + (byId[slugify(name)] ??= []).push(name); + } + const dupes = Object.entries(byId).filter(([, names]) => names.length > 1); + if (dupes.length) { + const list = dupes + .map(([id, names]) => `${names.map((n) => `"${n}"`).join(', ')} -> ${id}`) + .join('\n '); + throw new Error( + `These ${what} differ only by case or spaces vs hyphens, so they resolve to the same id:\n ${list}\nRename one of them in Lightning so each has a unique id.` + ); + } +}; + // TODO maybe this is a util and moved out of this file export const mapEdge = (edge: Provisioner.Edge) => { const e: any = { @@ -156,6 +182,11 @@ export const mapWorkflow = ( mapped.id = slugify(workflow.name); } + assertUniqueIds( + Object.values(jobs).filter((j) => !j.delete), + `step names in workflow "${workflow.name}"` + ); + // TODO what do we do if the condition is disabled? // I don't think that's the same as edge condition false? Object.values(triggers).forEach((trigger: Provisioner.Trigger) => { diff --git a/packages/project/src/parse/from-fs.ts b/packages/project/src/parse/from-fs.ts index 6cd8c7ecc..28cdbcb9e 100644 --- a/packages/project/src/parse/from-fs.ts +++ b/packages/project/src/parse/from-fs.ts @@ -48,6 +48,9 @@ export const parseProject = async (options: FromFsConfig) => { const workflowUuids = isPlainObject(checkout.workflow_uuids) ? (checkout.workflow_uuids as Record) : undefined; + const channelUuids = isPlainObject(checkout.channel_uuids) + ? (checkout.channel_uuids as Record) + : {}; const proj: any = { id: options.name ? slugify(options.name) : checkout.id, @@ -57,6 +60,7 @@ export const parseProject = async (options: FromFsConfig) => { 'alias', 'forked_from', 'workflow_uuids', + 'channel_uuids', 'collections', ]), @@ -83,7 +87,10 @@ export const parseProject = async (options: FromFsConfig) => { }); const parsed = resources ? yamlToJson(resources) : undefined; if (parsed?.channels) { - proj.channels = fromResourceChannels(parsed.channels); + // If the channel is tracked, we know its id + proj.channels = fromResourceChannels(parsed.channels).map((c) => + channelUuids[c.key!] ? { ...c, id: channelUuids[c.key!] } : c + ); } if (parsed && 'collections' in parsed) { // an empty key means no collections (not the same as no key!) diff --git a/packages/project/src/util/config.ts b/packages/project/src/util/config.ts index d2a9f1c97..e7b33815b 100644 --- a/packages/project/src/util/config.ts +++ b/packages/project/src/util/config.ts @@ -4,6 +4,7 @@ import path from 'node:path'; import { pickBy, isNil } from 'lodash-es'; import { yamlToJson, jsonToYaml } from './yaml'; import Project from '../Project'; +import { channelKey } from './resources'; // Recursively sort object keys so that config files serialize stably const sortKeys = (value: any): any => { @@ -72,7 +73,6 @@ export const extractCheckout = ( } // Track the uuid of each workflow, by local id - // This is recorded for future use and isn't read by anything yet const workflowUuids = source.workflows.reduce((obj: any, wf) => { if (wf.openfn?.uuid) { obj[wf.id] = wf.openfn.uuid; @@ -83,6 +83,17 @@ export const extractCheckout = ( project.workflow_uuids = workflowUuids; } + // And the id of each channel, by its key in resources.yaml + const channelUuids = (source.channels ?? []).reduce((obj: any, c) => { + if (c.id) { + obj[channelKey(c)] = c.id; + } + return obj; + }, {}); + if (Object.keys(channelUuids).length) { + project.channel_uuids = channelUuids; + } + return { path: getCheckoutPath(branch), content: jsonToYaml(sortKeys(project)), diff --git a/packages/project/src/util/resources.ts b/packages/project/src/util/resources.ts index 6f21905a3..6655cefbf 100644 --- a/packages/project/src/util/resources.ts +++ b/packages/project/src/util/resources.ts @@ -17,7 +17,7 @@ type ResourceChannels = Record< } >; -// The id a channel is matched by on merge. Remote channels have no local key, +// A channel's local id in resources.yaml. Remote channels have no local key, // so fall back to the name, slugified the same way as when it's written out export const channelKey = (c: l.ChannelState) => c.key ?? slugify(c.name); @@ -37,12 +37,8 @@ export const toResourceChannels = ( : c.destination_credential_id ?? undefined; // keep a local key if there is one, so renames survive a round trip - const base = channelKey(c); - let id = base; - for (let i = 2; id in result; i++) { - id = `${base}-${i}`; - } - result[id] = { + // (keys are unique: clashing names are rejected on pull) + result[channelKey(c)] = { name: c.name, destination_url: c.destination_url, enabled: c.enabled, diff --git a/packages/project/src/util/slugify.ts b/packages/project/src/util/slugify.ts index 4c28a71c8..317c295f5 100644 --- a/packages/project/src/util/slugify.ts +++ b/packages/project/src/util/slugify.ts @@ -1,5 +1,15 @@ +// Note: this does not cover Windows reserved names (CON, NUL) or trailing dots export default function slugify(text: string) { return ( - text?.replace(/\W/g, ' ').trim().replace(/\s+/g, '-').toLowerCase() ?? '' + text + ?.normalize('NFC') + .trim() + .toLowerCase() + .replace(/\s/g, '-') + // Handle characters Windows forbids in file names (\ : * ? " < > |) + // and the path separator (/) + .replace(/[/\\:*?"<>|]/g, '-') + // a leading dot would create a hidden file or folder on posix + .replace(/^\./, '-') ?? '' ); } diff --git a/packages/project/test/merge/merge-project.test.ts b/packages/project/test/merge/merge-project.test.ts index 766494f6a..c56959728 100644 --- a/packages/project/test/merge/merge-project.test.ts +++ b/packages/project/test/merge/merge-project.test.ts @@ -282,6 +282,53 @@ test('replace mode: target channels missing from source are flagged as removed', t.deepEqual(result.removedChannels, [removed]); }); +test('replace mode: a channel is matched by its tracked id before its key', (t) => { + const wf = assignUUIDs({ + steps: [ + { id: 'x', name: 'X', adaptor: 'common', expression: 'fn(s => s)' }, + ], + }); + const channel = { destination_url: 'https://example.com', enabled: true }; + + // the key no longer matches the remote name (eg the slug rules changed), + // but the id from the checkout file does + const target = createProject(wf, 'a', { + channels: [{ ...channel, id: 'chan-1', name: 'My Channel (v2)' }], + }); + const source = createProject(wf, 'b', { + channels: [ + { ...channel, id: 'chan-1', key: 'my-channel-v2', name: 'Renamed' }, + ], + }); + + const result = merge(source, target, { mode: REPLACE_MERGE }); + + t.is(result.channels![0].id, 'chan-1'); + t.deepEqual(result.removedChannels, []); +}); + +test('replace mode: an untracked channel whose key changed is matched by name', (t) => { + const wf = assignUUIDs({ + steps: [ + { id: 'x', name: 'X', adaptor: 'common', expression: 'fn(s => s)' }, + ], + }); + const channel = { destination_url: 'https://example.com', enabled: true }; + + const target = createProject(wf, 'a', { + channels: [{ ...channel, id: 'chan-1', name: 'My Channel (v2)' }], + }); + // key written by an older CLI, which stripped punctuation + const source = createProject(wf, 'b', { + channels: [{ ...channel, key: 'my-channel-v2', name: 'My Channel (v2)' }], + }); + + const result = merge(source, target, { mode: REPLACE_MERGE }); + + t.is(result.channels![0].id, 'chan-1'); + t.deepEqual(result.removedChannels, []); +}); + test('replace mode: renaming a channel keeps its id', (t) => { const wf = assignUUIDs({ steps: [ diff --git a/packages/project/test/parse/from-app-state.test.ts b/packages/project/test/parse/from-app-state.test.ts index 9876af3ff..14a2fe6cd 100644 --- a/packages/project/test/parse/from-app-state.test.ts +++ b/packages/project/test/parse/from-app-state.test.ts @@ -107,6 +107,74 @@ test('should load a workflow flagged delete: true and flag it removed', (t) => { t.true(project.workflows[0].isRemoved()); }); +test('should throw if two workflow names resolve to the same id', (t) => { + const s: any = cloneDeep(state); + s.workflows['cron-workflow'].name = 'webhook-workflow'; + + t.throws(() => fromAppState(s, meta), { + message: /"Webhook Workflow", "webhook-workflow" -> webhook-workflow/, + }); +}); + +test('should not throw if a colliding workflow is flagged delete', (t) => { + const s: any = cloneDeep(state); + s.workflows['cron-workflow'].name = 'webhook-workflow'; + s.workflows['cron-workflow'].delete = true; + + t.notThrows(() => fromAppState(s, meta)); +}); + +test('should throw if two step names resolve to the same id', (t) => { + const s: any = cloneDeep(state); + const wf = s.workflows['webhook-workflow']; + wf.jobs.other = { + ...wf.jobs['transform-data'], + id: 'x', + name: 'transform data', + }; + + t.throws(() => fromAppState(s, meta), { + message: + /in workflow "Webhook Workflow"[\s\S]*"Transform data", "transform data" -> transform-data/, + }); +}); + +test('should keep non-ascii and punctuation in step and workflow ids', (t) => { + const s: any = cloneDeep(state); + const wf = s.workflows['webhook-workflow']; + wf.name = 'Flujo de atención (v2.0)'; + wf.jobs['transform-data'].name = '患者確認'; + wf.jobs.other = { ...wf.jobs['transform-data'], id: 'x', name: 'فحص المريض' }; + + const [workflow] = fromAppState(s, meta).workflows; + t.is(workflow.id, 'flujo-de-atención-(v2.0)'); + t.deepEqual( + workflow.steps.map((s) => s.id), + ['webhook', '患者確認', 'فحص-المريض'] + ); + t.deepEqual(Object.keys(workflow.steps[0].next!), ['患者確認']); +}); + +test('should replace file-unsafe characters in ids', (t) => { + const s: any = cloneDeep(state); + s.workflows['webhook-workflow'].name = '..Q3/Q4 "Data:Sync"'; + + const [workflow] = fromAppState(s, meta).workflows; + t.is(workflow.id, '-.q3-q4--data-sync-'); +}); + +test('should throw if two channel names resolve to the same id', (t) => { + const s: any = cloneDeep(state); + s.channels = [ + { id: 'c1', name: 'My Channel', destination_url: 'https://a.com' }, + { id: 'c2', name: 'my-channel', destination_url: 'https://b.com' }, + ]; + + t.throws(() => fromAppState(s, meta), { + message: /"My Channel", "my-channel" -> my-channel/, + }); +}); + test('should load a job flagged delete: true and flag it removed', (t) => { const stateWithDeletedJob: any = cloneDeep(state); stateWithDeletedJob.workflows['webhook-workflow'].jobs[ diff --git a/packages/project/test/parse/from-fs.test.ts b/packages/project/test/parse/from-fs.test.ts index 3d16c47f2..7572dd296 100644 --- a/packages/project/test/parse/from-fs.test.ts +++ b/packages/project/test/parse/from-fs.test.ts @@ -565,3 +565,19 @@ test.serial('should prefer the file name to an id in the file', async (t) => { t.is(project.workflows.length, 1); t.is(project.workflows[0].id, 'my-workflow'); }); + +test.serial('should set the id on a tracked channel', async (t) => { + checkoutWithWorkflow({ channel_uuids: { 'my-channel': 'chan-1' } }); + mockFile('/ws/resources.yaml', { + channels: { + 'my-channel': { name: 'My Channel', destination_url: 'https://a.com' }, + other: { name: 'Other', destination_url: 'https://b.com' }, + }, + }); + + const project = await parseProject({ root: '/ws' }); + + t.is(project.channels![0].id, 'chan-1'); + t.falsy(project.channels![1].id); + t.falsy(project.openfn!.channel_uuids); +}); diff --git a/packages/project/test/util/config.test.ts b/packages/project/test/util/config.test.ts index 63434d79e..b8fb37286 100644 --- a/packages/project/test/util/config.test.ts +++ b/packages/project/test/util/config.test.ts @@ -369,3 +369,22 @@ test("checkout file excludes workflow_uuids if there aren't any", (t) => { const result = extractCheckout(proj); t.false(result.content.includes('workflow_uuids')); }); + +test('generate checkout file with channel_uuids, keyed by resources key', (t) => { + const proj = new Project({ + id: 'my-project', + channels: [ + // remote channel: keyed by its slugified name + { id: 'chan-1', name: 'My Channel (v2)' } as any, + // local channel: keeps its key + { id: 'chan-2', key: 'local', name: 'Other' } as any, + // not yet deployed + { key: 'new', name: 'New' } as any, + ], + }); + const result = extractCheckout(proj); + t.deepEqual(yamlToJson(result.content).channel_uuids, { + 'my-channel-(v2)': 'chan-1', + local: 'chan-2', + }); +}); diff --git a/packages/project/test/util/slugify.test.ts b/packages/project/test/util/slugify.test.ts new file mode 100644 index 000000000..26e779827 --- /dev/null +++ b/packages/project/test/util/slugify.test.ts @@ -0,0 +1,141 @@ +import test from 'ava'; +import slugify from '../../src/util/slugify'; + +test('lowercase the name', (t) => { + t.is(slugify('ABC'), 'abc'); +}); + +test('replace a space with a hyphen', (t) => { + t.is(slugify('a b c'), 'a-b-c'); +}); + +test('trim leading and trailing whitespace', (t) => { + t.is(slugify(' my workflow '), 'my-workflow'); +}); + +test('keep existing hyphens and underscores', (t) => { + t.is(slugify('my-work_flow'), 'my-work_flow'); +}); + +test('keep punctuation', (t) => { + t.is( + slugify('Event-based workflow 1 (rename)'), + 'event-based-workflow-1-(rename)' + ); +}); + +test('keep accented letters', (t) => { + t.is(slugify('Flujo de atención'), 'flujo-de-atención'); +}); + +test('keep letters in other scripts', (t) => { + t.is(slugify('患者確認'), '患者確認'); +}); + +test('replace characters that are unsafe in file names', (t) => { + t.is(slugify('a/b\\c:d*e?f"gi|j'), 'a-b-c-d-e-f-g-h-i-j'); +}); + +test('replace a leading dot', (t) => { + t.is(slugify('.hidden'), '-hidden'); +}); + +test('keep a dot that is not leading', (t) => { + t.is(slugify('v2.0'), 'v2.0'); +}); + +test('normalize to NFC', (t) => { + // "e" followed by a combining acute accent + const decomposed = 'café'; + t.is(slugify(decomposed), 'café'); +}); + +test('return an empty string for an undefined name', (t) => { + t.is(slugify(undefined as any), ''); +}); + +test('replace each file-unsafe character on its own', (t) => { + for (const char of ['/', '\\', ':', '*', '?', '"', '<', '>', '|']) { + t.is(slugify(`a${char}b`), 'a-b', `failed for ${char}`); + } +}); + +test('replace tabs and newlines with a hyphen', (t) => { + t.is(slugify('a\tb\nc'), 'a-b-c'); +}); + +test('replace a non-breaking space with a hyphen', (t) => { + t.is(slugify('a b'), 'a-b'); +}); + +// Each whitespace character becomes its own hyphen (runs are not collapsed) +test('replace each space in a run of spaces', (t) => { + t.is(slugify('a b'), 'a--b'); +}); + +test('only replace the first of several leading dots', (t) => { + t.is(slugify('..hidden'), '-.hidden'); +}); + +test('replace a name that is just a dot', (t) => { + t.is(slugify('.'), '-'); +}); + +test('return an empty string for an empty name', (t) => { + t.is(slugify(''), ''); +}); + +test('return an empty string for a name that is only whitespace', (t) => { + t.is(slugify(' '), ''); +}); + +test('lowercase letters in other scripts', (t) => { + t.is(slugify('ÜBER'), 'über'); + t.is(slugify('ПРИВЕТ МИР'), 'привет-мир'); + t.is(slugify('ΑΘΗΝΑ'), 'αθηνα'); +}); + +test('keep emoji', (t) => { + t.is(slugify('Deploy 🚀'), 'deploy-🚀'); +}); + +test('keep an already composed name unchanged', (t) => { + t.is(slugify('café'), 'café'); +}); + +test('is idempotent', (t) => { + for (const name of [ + 'My Workflow', + 'Flujo de atención (v2.0)', + '患者確認', + '.hidden', + 'a/b:c', + ]) { + const once = slugify(name); + t.is(slugify(once), once, `failed for ${name}`); + } +}); + +test('names that differ only by case resolve to the same id', (t) => { + t.is(slugify('Foo Bar'), slugify('foo bar')); +}); + +test('names that differ only by space vs hyphen resolve to the same id', (t) => { + t.is(slugify('Foo Bar'), slugify('foo-bar')); +}); + +test('names that differ only by a replaced character resolve to the same id', (t) => { + t.is(slugify('Q3/Q4'), slugify('Q3:Q4')); +}); + +test('names that differ only by a leading dot resolve to the same id', (t) => { + t.is(slugify('.foo'), slugify('-foo')); +}); + +test('names with different punctuation resolve to different ids', (t) => { + t.not(slugify('Workflow 1 (rename)'), slugify('Workflow 1 rename')); +}); + +// Known limits: these are not handled yet +test.todo('replace Windows reserved names (CON, NUL)'); +test.todo('replace a trailing dot'); From 222473d3e1db7b863e3e1a23b3c0f75f8cc64669 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Tue, 3 Nov 2026 18:01:53 +0000 Subject: [PATCH 2/3] Fix pull alias (#1610) * add failing tests * fix * changeset --- .changeset/violet-moons-knock.md | 5 +++ .../cli/test/sync/commands.test.ts | 36 +++++++++++++++ packages/cli/src/projects/fetch.ts | 10 ++++- packages/cli/test/projects/fetch.test.ts | 45 ++++++++++++++++++- 4 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 .changeset/violet-moons-knock.md diff --git a/.changeset/violet-moons-knock.md b/.changeset/violet-moons-knock.md new file mode 100644 index 000000000..b4e0dc193 --- /dev/null +++ b/.changeset/violet-moons-knock.md @@ -0,0 +1,5 @@ +--- +'@openfn/cli': patch +--- + +When fetching a sandbox, only use the id as an alias if there isn't already an existing alias diff --git a/integration-tests/cli/test/sync/commands.test.ts b/integration-tests/cli/test/sync/commands.test.ts index a3415ad5f..47cc9c78f 100644 --- a/integration-tests/cli/test/sync/commands.test.ts +++ b/integration-tests/cli/test/sync/commands.test.ts @@ -2,6 +2,8 @@ // asserting on what lands on disk (.projects state, checkout.yaml, workflows folder) import test from 'ava'; +import fs from 'node:fs'; +import path from 'node:path'; import run from '../../src/run'; import { setup } from './helpers'; @@ -256,3 +258,37 @@ test('checkout by alias', async (t) => { }); test.todo('merge by alias'); + +test('pull a sandbox by UUID, reusing the local alias if there is one', async (t) => { + const { workspace, read } = initWorkspace(t); + const project = gen(); + + // make the remote project a sandbox + lightning.state.projects[project.openfn.uuid].parent_id = + '6a8a6c6e-5c1d-4b0e-8d52-0d4c4f2f9a11'; + + await run( + `openfn project pull \ + --workspace ${workspace} \ + --endpoint ${endpoint} \ + --api-key abc \ + --alias staging \ + ${project.openfn.uuid}` + ); + t.truthy(read('.projects/staging@localhost.yaml')); + + // pull again with no alias. The existing alias should be re-used + // rather than defaulting to the sandbox id + await run( + `openfn project pull \ + --workspace ${workspace} \ + --endpoint ${endpoint} \ + --api-key abc \ + ${project.openfn.uuid}` + ); + + t.deepEqual(fs.readdirSync(path.resolve(workspace, '.projects')), [ + 'staging@localhost.yaml', + ]); + t.regex(read('.openfn/checkout.yaml'), new RegExp(project.openfn.uuid)); +}); diff --git a/packages/cli/src/projects/fetch.ts b/packages/cli/src/projects/fetch.ts index 76878bab2..b051b2111 100644 --- a/packages/cli/src/projects/fetch.ts +++ b/packages/cli/src/projects/fetch.ts @@ -136,7 +136,15 @@ export const fetchV2 = async (options: FetchOptions, logger: Logger) => { const remoteProject = await fetchRemoteProject(workspace, options, logger); // if there's no alias, and this is a sandbox, set the alias - if (!options.alias && remoteProject.sandbox?.parentId) { + if ( + // If the user doesn't set an alias... + !options.alias && + // And we're pulling a sandbox... + remoteProject.sandbox?.parentId && + // And the local project doesn't already have an alias set... + !remoteProject.cli?.alias + ) { + // ...Use the sandbox id as the alias options.alias = remoteProject.id; remoteProject.cli.alias = options.alias; logger.debug('Defaulting alias to sandbox id', options.alias); diff --git a/packages/cli/test/projects/fetch.test.ts b/packages/cli/test/projects/fetch.test.ts index b65f7b2de..9645ffe28 100644 --- a/packages/cli/test/projects/fetch.test.ts +++ b/packages/cli/test/projects/fetch.test.ts @@ -1,4 +1,4 @@ -import { readFile, writeFile } from 'node:fs/promises'; +import { readdir, readFile, writeFile } from 'node:fs/promises'; import test from 'ava'; import mock from 'mock-fs'; import { MockAgent, setGlobalDispatcher } from 'undici'; @@ -646,3 +646,46 @@ test.serial('fetch by alias when local copies share the UUID', async (t) => { const main = await readFile('/ws/.projects/jam@app.openfn.org.yaml', 'utf-8'); t.is(main, yaml_v1); }); + +test.serial( + 'fetch a sandbox by UUID, reusing the local alias if there is one', + async (t) => { + const SANDBOX_UUID = 'b2f1c8a4-5d3e-4c1a-9b7e-0a1d2c3e4f50'; + const sandbox = { + ...myProject_v1, + id: SANDBOX_UUID, + name: 'My Sandbox', + parent_id: PROJECT_UUID, + }; + + mockAgent + .get(ENDPOINT) + .intercept({ path: `/api/provision/${SANDBOX_UUID}?`, method: 'GET' }) + .reply(200, { data: sandbox }) + .times(2); + + const options = { + project: SANDBOX_UUID, + endpoint: ENDPOINT, + apiKey: 'test-api-key', + workspace: '/ws', + }; + + // First fetch with an explicit alias + await fetchHandler({ ...options, alias: 'staging' } as any, logger); + t.truthy(await readFile(getYamlPath('staging'), 'utf-8')); + + // Ensure the sandbox is not used + await t.throwsAsync(() => readFile(getYamlPath('my-sandbox'), 'utf-8')); + + // Now fetch again by UUID, with no alias. This should update the + // existing local project rather than defaulting the alias to the sandbox id + await t.notThrowsAsync(() => fetchHandler(options as any, logger)); + + // Ensure the sandbox is not used + await t.throwsAsync(() => readFile(getYamlPath('my-sandbox'), 'utf-8')); + + const files = await readdir('/ws/.projects'); + t.deepEqual(files, ['staging@app.openfn.org.yaml']); + } +); From e58c01a931c4e00b9f83e99c1be39546ee55f778 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Tue, 3 Nov 2026 18:03:32 +0000 Subject: [PATCH 3/3] versions --- .changeset/track-channel-ids.md | 6 ------ .changeset/unique-slug-ids.md | 6 ------ .changeset/violet-moons-knock.md | 5 ----- integration-tests/cli/CHANGELOG.md | 9 +++++++++ integration-tests/cli/package.json | 2 +- packages/cli/CHANGELOG.md | 11 +++++++++++ packages/cli/package.json | 2 +- packages/lightning-mock/CHANGELOG.md | 8 ++++++++ packages/lightning-mock/package.json | 2 +- packages/project/CHANGELOG.md | 10 ++++++++++ packages/project/package.json | 2 +- 11 files changed, 42 insertions(+), 21 deletions(-) delete mode 100644 .changeset/track-channel-ids.md delete mode 100644 .changeset/unique-slug-ids.md delete mode 100644 .changeset/violet-moons-knock.md diff --git a/.changeset/track-channel-ids.md b/.changeset/track-channel-ids.md deleted file mode 100644 index 4b2893530..000000000 --- a/.changeset/track-channel-ids.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -'@openfn/project': patch -'@openfn/cli': patch ---- - -Channels are now tracked by their Lightning id, so changing a channel's key in resources.yaml no longer deletes and recreates it on deploy. diff --git a/.changeset/unique-slug-ids.md b/.changeset/unique-slug-ids.md deleted file mode 100644 index 826370a1a..000000000 --- a/.changeset/unique-slug-ids.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -'@openfn/project': minor -'@openfn/cli': patch ---- - -Workflow, step and channel ids now keep non-ASCII letters and punctuation from their names, matching Lightning. Names with these characters get new ids, so their folders and resources.yaml keys change on the next pull. diff --git a/.changeset/violet-moons-knock.md b/.changeset/violet-moons-knock.md deleted file mode 100644 index b4e0dc193..000000000 --- a/.changeset/violet-moons-knock.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@openfn/cli': patch ---- - -When fetching a sandbox, only use the id as an alias if there isn't already an existing alias diff --git a/integration-tests/cli/CHANGELOG.md b/integration-tests/cli/CHANGELOG.md index 999c451c3..b2f58f475 100644 --- a/integration-tests/cli/CHANGELOG.md +++ b/integration-tests/cli/CHANGELOG.md @@ -1,5 +1,14 @@ # @openfn/integration-tests-cli +## 1.0.40 + +### Patch Changes + +- Updated dependencies [82a0cae] +- Updated dependencies [82a0cae] + - @openfn/project@0.24.0 + - @openfn/lightning-mock@2.5.10 + ## 1.0.39 ### Patch Changes diff --git a/integration-tests/cli/package.json b/integration-tests/cli/package.json index f5ecc408c..d5ad1c73b 100644 --- a/integration-tests/cli/package.json +++ b/integration-tests/cli/package.json @@ -1,7 +1,7 @@ { "name": "@openfn/integration-tests-cli", "private": true, - "version": "1.0.39", + "version": "1.0.40", "description": "CLI integration tests", "author": "Open Function Group ", "license": "ISC", diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index 65eec90ba..6e8bae6d0 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -1,5 +1,16 @@ # @openfn/cli +## 1.45.2 + +### Patch Changes + +- 82a0cae: Channels are now tracked by their Lightning id, so changing a channel's key in resources.yaml no longer deletes and recreates it on deploy. +- 82a0cae: Workflow, step and channel ids now keep non-ASCII letters and punctuation from their names, matching Lightning. Names with these characters get new ids, so their folders and resources.yaml keys change on the next pull. +- 222473d: When fetching a sandbox, only use the id as an alias if there isn't already an existing alias +- Updated dependencies [82a0cae] +- Updated dependencies [82a0cae] + - @openfn/project@0.24.0 + ## 1.45.1 ### Patch Changes diff --git a/packages/cli/package.json b/packages/cli/package.json index 0e8f76a3c..98821118d 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/cli", - "version": "1.45.1", + "version": "1.45.2", "description": "CLI devtools for the OpenFn toolchain", "repository": { "url": "https://github.com/OpenFn/kit" diff --git a/packages/lightning-mock/CHANGELOG.md b/packages/lightning-mock/CHANGELOG.md index 3e82279e6..1c0acd5a6 100644 --- a/packages/lightning-mock/CHANGELOG.md +++ b/packages/lightning-mock/CHANGELOG.md @@ -1,5 +1,13 @@ # @openfn/lightning-mock +## 2.5.10 + +### Patch Changes + +- Updated dependencies [82a0cae] +- Updated dependencies [82a0cae] + - @openfn/project@0.24.0 + ## 2.5.9 ### Patch Changes diff --git a/packages/lightning-mock/package.json b/packages/lightning-mock/package.json index 2579de12e..02ff354d2 100644 --- a/packages/lightning-mock/package.json +++ b/packages/lightning-mock/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/lightning-mock", - "version": "2.5.9", + "version": "2.5.10", "private": true, "description": "A mock Lightning server", "main": "dist/index.js", diff --git a/packages/project/CHANGELOG.md b/packages/project/CHANGELOG.md index e65750ac9..cc7b27f66 100644 --- a/packages/project/CHANGELOG.md +++ b/packages/project/CHANGELOG.md @@ -1,5 +1,15 @@ # @openfn/project +## 0.24.0 + +### Minor Changes + +- 82a0cae: Workflow, step and channel ids now keep non-ASCII letters and punctuation from their names, matching Lightning. Names with these characters get new ids, so their folders and resources.yaml keys change on the next pull. + +### Patch Changes + +- 82a0cae: Channels are now tracked by their Lightning id, so changing a channel's key in resources.yaml no longer deletes and recreates it on deploy. + ## 0.23.2 ### Patch Changes diff --git a/packages/project/package.json b/packages/project/package.json index 49483fd7c..55cf47110 100644 --- a/packages/project/package.json +++ b/packages/project/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/project", - "version": "0.23.2", + "version": "0.24.0", "description": "Read, serialize, replicate and sync OpenFn projects", "author": "Open Function Group ", "license": "ISC",