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']); + } +);