Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-moons-knock.md
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions integration-tests/cli/test/sync/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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));
});
10 changes: 9 additions & 1 deletion packages/cli/src/projects/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
45 changes: 44 additions & 1 deletion packages/cli/test/projects/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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']);
}
);
Loading