Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release Aug 13 #269

Merged
merged 3 commits into from
Aug 13, 2024
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 .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Neon CLI",
"postCreateCommand": "npm install -g bun && bun install && bun run build",
"waitFor": "postCreateCommand"
}
47 changes: 47 additions & 0 deletions mocks/main/projects/GET.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,53 @@ export default function (req, res) {
expect(req.query).toMatchObject({
limit: '100',
});

if (req.query.org_id) {
expect(['org-2', 'org-3']).toContain(req.query.org_id);

if (req.query.org_id === 'org-2') {
return res.json({
projects: [
{
id: 4,
name: 'Project_4',
created_at: '2019-01-01T00:00:00.000Z',
updated_at: '2019-01-01T00:00:00.000Z',
org_id: 'org-2',
},
{
id: 5,
name: 'Project_5',
created_at: '2019-01-01T00:00:00.000Z',
updated_at: '2019-01-01T00:00:00.000Z',
org_id: 'org-2',
},
{
id: 6,
name: 'Project_6',
created_at: '2019-01-01T00:00:00.000Z',
updated_at: '2019-01-01T00:00:00.000Z',
org_id: 'org-2',
},
],
});
}

if (req.query.org_id === 'org-3') {
return res.json({
projects: [
{
id: 7,
name: 'Project_7',
created_at: '2019-01-01T00:00:00.000Z',
updated_at: '2019-01-01T00:00:00.000Z',
org_id: 'org-3',
},
],
});
}
}

res.json({
projects: [
{
Expand Down
22 changes: 22 additions & 0 deletions mocks/main/projects/POST.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ export default function (req, res) {
},
});
}

if (req.body.project.org_id) {
expect(req.body.project.org_id).toBe('org-2');

return res.json({
project: {
id: 'new-project-789012',
name: 'test_project',
created_at: '2022-01-01T00:00:00.000Z',
org_id: 'org-2',
},
connection_uris: [
{ connection_uri: 'postgres://localhost:5432/test_project' },
],
branch: {
id: 'br-test-branch-789012',
name: 'test_branch',
created_at: '2022-01-01T00:00:00.000Z',
},
});
}

res.send({
project: {
id: 'new-project-123456',
Expand Down
30 changes: 30 additions & 0 deletions src/commands/__snapshots__/projects.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ connection_uris:
"
`;

exports[`projects > create with org id 1`] = `
"project:
id: new-project-789012
name: test_project
created_at: 2022-01-01T00:00:00.000Z
org_id: org-2
connection_uris:
- connection_uri: postgres://localhost:5432/test_project
"
`;

exports[`projects > delete 1`] = `
"id: test
name: test
Expand Down Expand Up @@ -112,6 +123,25 @@ shared_with_me:
"
`;

exports[`projects > list with org id 1`] = `
"- id: 4
name: Project_4
created_at: 2019-01-01T00:00:00.000Z
updated_at: 2019-01-01T00:00:00.000Z
org_id: org-2
- id: 5
name: Project_5
created_at: 2019-01-01T00:00:00.000Z
updated_at: 2019-01-01T00:00:00.000Z
org_id: org-2
- id: 6
name: Project_6
created_at: 2019-01-01T00:00:00.000Z
updated_at: 2019-01-01T00:00:00.000Z
org_id: org-2
"
`;

exports[`projects > update ip allow 1`] = `
"id: test
name: test_project
Expand Down
68 changes: 62 additions & 6 deletions src/commands/__snapshots__/set_context.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`set_context > should set the context > get project id overrides context set project 1`] = `
exports[`set_context > can set the context to project and organization at the same time > set-context 1`] = `""`;

exports[`set_context > can set the context to project and organization at the same time > set-context 2`] = `
"{
"projectId": "test_project",
"orgId": "org-2"
}"
`;

exports[`set_context > should set the context to organization > create projects selecting organization from the context 1`] = `
"project:
id: new-project-789012
name: test_project
created_at: 2022-01-01T00:00:00.000Z
org_id: org-2
connection_uris:
- connection_uri: postgres://localhost:5432/test_project
"
`;

exports[`set_context > should set the context to organization > list projects selecting organization from the context 1`] = `
"- id: 4
name: Project_4
created_at: 2019-01-01T00:00:00.000Z
updated_at: 2019-01-01T00:00:00.000Z
org_id: org-2
- id: 5
name: Project_5
created_at: 2019-01-01T00:00:00.000Z
updated_at: 2019-01-01T00:00:00.000Z
org_id: org-2
- id: 6
name: Project_6
created_at: 2019-01-01T00:00:00.000Z
updated_at: 2019-01-01T00:00:00.000Z
org_id: org-2
"
`;

exports[`set_context > should set the context to organization > list projects with explicit org id overrides context 1`] = `
"- id: 7
name: Project_7
created_at: 2019-01-01T00:00:00.000Z
updated_at: 2019-01-01T00:00:00.000Z
org_id: org-3
"
`;

exports[`set_context > should set the context to organization > set-context 1`] = `""`;

exports[`set_context > should set the context to organization > set-context 2`] = `
"{
"orgId": "org-2"
}"
`;

exports[`set_context > should set the context to project > get project id overrides context set project 1`] = `
"id: project-id-123
name: project name 123
created_at: 2019-01-01T00:00:00Z
Expand All @@ -12,7 +68,7 @@ settings:
"
`;

exports[`set_context > should set the context > list branches selecting project from the context 1`] = `
exports[`set_context > should set the context to project > list branches selecting project from the context 1`] = `
"- id: br-main-branch-123456
name: main
default: true
Expand Down Expand Up @@ -41,7 +97,7 @@ exports[`set_context > should set the context > list branches selecting project
"
`;

exports[`set_context > should set the context > set the branchId and projectId is from context 1`] = `
exports[`set_context > should set the context to project > set the branchId and projectId is from context 1`] = `
"- name: db1
owner_name: user1
- name: db2
Expand All @@ -51,11 +107,11 @@ exports[`set_context > should set the context > set the branchId and projectId i
"
`;

exports[`set_context > should set the context > set the branchId and projectId is from context 2`] = `""`;
exports[`set_context > should set the context to project > set the branchId and projectId is from context 2`] = `""`;

exports[`set_context > should set the context > set-context 1`] = `""`;
exports[`set_context > should set the context to project > set-context 1`] = `""`;

exports[`set_context > should set the context > set-context 2`] = `
exports[`set_context > should set the context to project > set-context 2`] = `
"{
"projectId": "test"
}"
Expand Down
15 changes: 15 additions & 0 deletions src/commands/projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,25 @@ describe('projects', () => {
await testCliCommand(['projects', 'list']);
});

test('list with org id', async ({ testCliCommand }) => {
await testCliCommand(['projects', 'list', '--org-id', 'org-2']);
});

test('create', async ({ testCliCommand }) => {
await testCliCommand(['projects', 'create', '--name', 'test_project']);
});

test('create with org id', async ({ testCliCommand }) => {
await testCliCommand([
'projects',
'create',
'--name',
'test_project',
'--org-id',
'org-2',
]);
});

test('create with database and role', async ({ testCliCommand }) => {
await testCliCommand([
'projects',
Expand Down
41 changes: 30 additions & 11 deletions src/commands/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ export const builder = (argv: yargs.Argv) => {
.command(
'list',
'List projects',
(yargs) => yargs,
(yargs) =>
yargs.options({
'org-id': {
describe: 'List projects of a given organization',
type: 'string',
},
}),
async (args) => {
await list(args as any);
},
Expand All @@ -60,6 +66,10 @@ export const builder = (argv: yargs.Argv) => {
describe: `The region ID. Possible values: ${REGIONS.join(', ')}`,
type: 'string',
},
'org-id': {
describe: "The project's organization ID",
type: 'string',
},
psql: {
type: 'boolean',
describe: 'Connect to a new project via psql',
Expand Down Expand Up @@ -146,7 +156,7 @@ export const handler = (args: yargs.Argv) => {
return args;
};

const list = async (props: CommonProps) => {
const list = async (props: CommonProps & { orgId?: string }) => {
const getList = async (
fn:
| typeof props.apiClient.listProjects
Expand All @@ -158,6 +168,7 @@ const list = async (props: CommonProps) => {
while (!end) {
const { data } = await fn({
limit: PROJECTS_LIST_LIMIT,
org_id: props.orgId,
cursor,
});
result.push(...data.projects);
Expand All @@ -175,21 +186,25 @@ const list = async (props: CommonProps) => {
return result;
};

const [ownedProjects, sharedProjects] = await Promise.all([
getList(props.apiClient.listProjects),
getList(props.apiClient.listSharedProjects),
]);
const ownedProjects = getList(props.apiClient.listProjects);
const sharedProjects = props.orgId
? undefined
: getList(props.apiClient.listSharedProjects);

const out = writer(props);

out.write(ownedProjects, {
out.write(await ownedProjects, {
fields: PROJECT_FIELDS,
title: 'Projects',
});
out.write(sharedProjects, {
fields: PROJECT_FIELDS,
title: 'Shared with me',
});

if (sharedProjects) {
out.write(await sharedProjects, {
fields: PROJECT_FIELDS,
title: 'Shared with me',
});
}

out.end();
};

Expand All @@ -198,6 +213,7 @@ const create = async (
name?: string;
regionId?: string;
cu?: string;
orgId?: string;
database?: string;
role?: string;
psql: boolean;
Expand All @@ -212,6 +228,9 @@ const create = async (
if (props.regionId) {
project.region_id = props.regionId;
}
if (props.orgId) {
project.org_id = props.orgId;
}
project.branch = {};
if (props.database) {
project.branch.database_name = props.database;
Expand Down
Loading