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

Init flow demo #6218

Merged
merged 39 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
4147110
testing
svnsairam Aug 4, 2023
1c025f1
testing
svnsairam Aug 4, 2023
4b3e805
Init demo flow
svnsairam Aug 4, 2023
2fac766
Create Stack request body changed
svnsairam Sep 1, 2023
cefe927
Merge remote-tracking branch 'origin/master' into svnsairam-fsk-001
svnsairam Sep 12, 2023
90af159
Merge branch 'svnsairam-fsk-001' of https://github.com/firebase/fireb…
svnsairam Sep 12, 2023
a540248
Merge remote-tracking branch 'origin/master' into svnsairam-fsk-001
svnsairam Sep 12, 2023
18bc06c
Merge remote-tracking branch 'origin/master' into svnsairam-fsk-001
svnsairam Sep 13, 2023
1609091
Updated CRUD API's for stacks
svnsairam Sep 14, 2023
7c8b3e3
Updated changes
svnsairam Sep 14, 2023
64337b1
updated comments
svnsairam Sep 15, 2023
a73bfd6
Update CRUD options
svnsairam Sep 19, 2023
cc028c7
Merge remote-tracking branch 'origin/master' into svnsairam-fsk-001
svnsairam Sep 19, 2023
a956766
Merge branch 'master' into svnsairam-fsk-001
svnsairam Oct 10, 2023
9e58710
resolved comments
svnsairam Oct 10, 2023
4e43d68
resolved comments
svnsairam Oct 10, 2023
285d148
resolved comments
svnsairam Oct 10, 2023
76e0c6d
Merge remote-tracking branch 'origin/master' into svnsairam-fsk-001
svnsairam Oct 10, 2023
af5db62
Changing stack to backend in APIs
svnsairam Oct 10, 2023
f3bdc35
Added changes to modify api
svnsairam Oct 10, 2023
c8c3ce9
minor change to region
svnsairam Oct 10, 2023
9781527
Added error messages
svnsairam Oct 10, 2023
0243f7f
Merge branch 'master' into svnsairam-fsk-001
svnsairam Oct 11, 2023
efe259b
Merge branch 'master' into svnsairam-fsk-001
svnsairam Oct 11, 2023
d44fb6d
catch block added
svnsairam Oct 12, 2023
5d48639
Merge branch 'svnsairam-fsk-001' of https://github.com/firebase/fireb…
svnsairam Oct 12, 2023
b30ee48
Added few changes
svnsairam Oct 16, 2023
333d8f8
Merge branch 'master' into svnsairam-fsk-001
svnsairam Oct 17, 2023
5d008dd
Merge branch 'master' into svnsairam-fsk-001
svnsairam Oct 20, 2023
56d8a12
Merge branch 'master' into svnsairam-fsk-001
svnsairam Oct 23, 2023
7b80e73
Returned stack
svnsairam Oct 25, 2023
fcf5708
Return stack
svnsairam Oct 25, 2023
d992188
Added return statements
svnsairam Oct 25, 2023
8f942aa
Removed comments
svnsairam Oct 25, 2023
8db5e15
changed comments
svnsairam Oct 25, 2023
fdeda7c
Added minor change
svnsairam Oct 27, 2023
b5e7cf9
Merge branch 'master' into svnsairam-fsk-001
svnsairam Oct 27, 2023
71d534c
Format code
svnsairam Oct 27, 2023
681a695
Format code
svnsairam Oct 27, 2023
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
Prev Previous commit
Next Next commit
Init demo flow
  • Loading branch information
svnsairam committed Aug 4, 2023
commit 4b3e8056870f0772a83dad601e91965b5d8c2e9b
14 changes: 14 additions & 0 deletions src/commands/frameworks-stacks-create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Command } from "../command";
import { Options } from "../options";
import { needProjectId } from "../projectUtils";
import requireInteractive from "../requireInteractive";
import { doSetup } from "../init/features/frameworks";

export const command = new Command("stacks:create")
.description("create stacks for a project")
.before(requireInteractive)
.action(async (options: Options) => {
const projectId = needProjectId(options);
await doSetup({}, projectId);
svnsairam marked this conversation as resolved.
Show resolved Hide resolved
console.log(projectId);
});
17 changes: 17 additions & 0 deletions src/commands/frameworks-stacks-delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Command } from "../command";
import { Options } from "../options";
import { needProjectId } from "../projectUtils";
import requireInteractive from "../requireInteractive";
import * as gcp from "../gcp/frameworks";

export const command = new Command("stacks:delete")
taeold marked this conversation as resolved.
Show resolved Hide resolved
.option("-l, --location <location>", "Stack backend location", "us-central1")
.option("-stack, --stackId <stackId>", "Stack backend location", "")
.description("list stacks for a project")
.before(requireInteractive)
svnsairam marked this conversation as resolved.
Show resolved Hide resolved
.action(async (options: Options) => {
const projectId = needProjectId(options);
const location = options.location as string;
const stackId = options.stackId as string;
svnsairam marked this conversation as resolved.
Show resolved Hide resolved
await gcp.deleteStack(projectId, location, stackId);
svnsairam marked this conversation as resolved.
Show resolved Hide resolved
});
20 changes: 20 additions & 0 deletions src/commands/frameworks-stacks-get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Command } from "../command";
import { Options } from "../options";
import { needProjectId } from "../projectUtils";
import requireInteractive from "../requireInteractive";
import * as gcp from "../gcp/frameworks";
import { Stack } from "../gcp/frameworks";

export const command = new Command("stacks:get")
.option("-l, --location <location>", "Stack backend location", "us-central1")
.option("--stack, --stackId <stackId>", "Id for the stack", "")
.description("list stacks for a project")
.before(requireInteractive)
svnsairam marked this conversation as resolved.
Show resolved Hide resolved
.action(async (options: Options) => {
const projectId = needProjectId(options);
const location = options.location as string;
const stackId = options.stackId as string;
svnsairam marked this conversation as resolved.
Show resolved Hide resolved
const stack: Stack = await gcp.getStack(projectId, location, stackId);

console.log(stack);
});
17 changes: 17 additions & 0 deletions src/commands/frameworks-stacks-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Command } from "../command";
import { Options } from "../options";
import { needProjectId } from "../projectUtils";
import requireInteractive from "../requireInteractive";
import * as gcp from "../gcp/frameworks";
import { ListStacksResponse } from "../gcp/frameworks";

export const command = new Command("stacks:list")
.option("-l, --location <location>", "Stack backend location", "us-central1")
.description("list stacks for a project")
.before(requireInteractive)
svnsairam marked this conversation as resolved.
Show resolved Hide resolved
.action(async (options: Options) => {
const projectId = needProjectId(options);
const location = options.location as string;
const resp: ListStacksResponse = await gcp.listStack(projectId, location);
console.log(resp);
});
8 changes: 8 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
/**
* Loads all commands for our parser.
*/
export function load(client: any): any {

Check warning on line 6 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unexpected any. Specify a different type

Check warning on line 6 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unexpected any. Specify a different type
function loadCommand(name: string) {

Check warning on line 7 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Missing return type on function
const t0 = process.hrtime.bigint();
const { command: cmd } = require(`./${name}`);

Check warning on line 9 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe assignment of an `any` value

Check warning on line 9 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Require statement not part of import statement
cmd.register(client);

Check warning on line 10 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .register on an `any` value

Check warning on line 10 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe call of an `any` typed value
const t1 = process.hrtime.bigint();
const diffMS = (t1 - t0) / BigInt(1e6);
if (diffMS > 75) {
Expand All @@ -15,7 +15,7 @@
// console.error(`Loading ${name} took ${diffMS}ms`);
}

return cmd.runner();

Check warning on line 18 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe return of an `any` typed value

Check warning on line 18 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .runner on an `any` value

Check warning on line 18 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe call of an `any` typed value
}

const t0 = process.hrtime.bigint();
Expand Down Expand Up @@ -151,6 +151,14 @@
client.internaltesting.functions = {};
client.internaltesting.functions.discover = loadCommand("internaltesting-functions-discover");
}
if (experiments.isEnabled("frameworks")) {
svnsairam marked this conversation as resolved.
Show resolved Hide resolved
client.frameworks = {};
client.frameworks.stacks = {};
client.frameworks.stacks.list = loadCommand("frameworks-stacks-list");
client.frameworks.stacks.create = loadCommand("frameworks-stacks-create");
client.frameworks.stacks.create = loadCommand("frameworks-stacks-get");
client.frameworks.stacks.create = loadCommand("frameworks-stacks-delete");
}
client.login = loadCommand("login");
client.login.add = loadCommand("login-add");
client.login.ci = loadCommand("login-ci");
Expand Down
31 changes: 30 additions & 1 deletion src/gcp/frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface Stack {
uri: string;
}

export type StackOutputOnlyFields = "createTime" | "updateTime" | "uri" | "codebase";
export type StackOutputOnlyFields = "createTime" | "updateTime" | "uri";

export interface Build {
name: string;
Expand Down Expand Up @@ -81,6 +81,12 @@ export interface Operation {
// end oneof result
}

export interface ListStacksResponse {
stacks: Stack[];
nextPageToken: string;
unreachable: string[];
}

/**
* Creates a new Stack in a given project and location.
*/
Expand Down Expand Up @@ -113,6 +119,29 @@ export async function getStack(
return res.body;
}

/**
* List a all stacks present in a project and region.
*/
export async function listStack(projectId: string, location: string): Promise<ListStacksResponse> {
const name = `projects/${projectId}/locations/${location}/stacks/`;
const res = await client.get<ListStacksResponse>(name);

return res.body;
}

/**
* Creates a new Stack in a given project and location.
svnsairam marked this conversation as resolved.
Show resolved Hide resolved
*/
export async function deleteStack(
projectId: string,
location: string,
stackId: string
): Promise<Operation> {
const name = `projects/${projectId}/locations/${location}/stacks/${stackId}`;
const res = await client.delete<Operation>(name);

return res.body;
}
/**
* Creates a new Build in a given project and location.
*/
Expand Down
5 changes: 4 additions & 1 deletion src/init/features/frameworks/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const DEFAULT_REGION = "us-central1";
export const ALLOWED_REGIONS = [{ name: "us-central1 (Iowa)", value: "us-central1" }];
export const ALLOWED_REGIONS = [
{ name: "us-central1", value: "us-central1" },
{ name: "us-west1", value: "us-west1" },
];
export const DEFAULT_DEPLOY_METHOD = "github";
export const ALLOWED_DEPLOY_METHODS = [{ name: "Deploy using github", value: "github" }];
18 changes: 10 additions & 8 deletions src/init/features/frameworks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ const frameworksPollerOptions: Omit<poller.OperationPollerOptions, "operationRes
/**
* Setup new frameworks project.
*/
export async function doSetup(setup: any): Promise<void> {
const projectId: string = setup?.rcfile?.projects?.default;
export async function doSetup(setup: any, projectId: string): Promise<void> {
setup.frameworks = {};

utils.logBullet("First we need a few details to create your service.");
Expand Down Expand Up @@ -71,7 +70,10 @@ export async function doSetup(setup: any): Promise<void> {
setup.frameworks
);

await getOrCreateStack(projectId, setup);
const stack: Stack | undefined = await getOrCreateStack(projectId, setup);
taeold marked this conversation as resolved.
Show resolved Hide resolved
if (stack) {
utils.logSuccess(`Successfully created a stack: ${stack.name}`);
}
}

function toStack(
Expand All @@ -80,6 +82,10 @@ function toStack(
): Omit<Stack, StackOutputOnlyFields> {
return {
name: stackId,
codebase: {
repository: `cloudbuild.googleapis.com/v2/${cloudBuildConnRepo.name}`,
rootDirectory: ".",
},
labels: {},
};
}
Expand All @@ -96,11 +102,7 @@ export async function getOrCreateStack(projectId: string, setup: any): Promise<S
if ((err as FirebaseError).status === 404) {
logger.info("Creating new stack.");
if (deployMethod === "github") {
const cloudBuildConnRepo = await repo.linkGitHubRepository(
projectId,
location,
setup.frameworks.serviceName
);
const cloudBuildConnRepo = await repo.linkGitHubRepository(projectId, location);
const stackDetails = toStack(cloudBuildConnRepo, setup.frameworks.serviceName);
return await createStack(projectId, location, stackDetails);
}
Expand Down
15 changes: 9 additions & 6 deletions src/init/features/frameworks/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ function extractRepoSlugFromURI(remoteUri: string): string | undefined {
* The current implementation is subject to change in the event that
* the 1:1 Connection-to-Resource relationship no longer holds.
*/
function generateRepositoryId(): string | undefined {
return `composer-repo`;
function generateRepositoryId(remoteUri: string): string | undefined {
return extractRepoSlugFromURI(remoteUri)?.replaceAll("/", "-");
}

function generateConnectionId(location: string): string {
return `frameworks-${location}`;
}

/**
* Prompts the user to link their stack to a GitHub repository.
*/
export async function linkGitHubRepository(
projectId: string,
location: string,
stackId: string
location: string
): Promise<gcb.Repository> {
const connectionId = stackId;
const connectionId = generateConnectionId(location);
await getOrCreateConnection(projectId, location, connectionId);

let remoteUri = await promptRepositoryURI(projectId, location, connectionId);
Expand Down Expand Up @@ -147,7 +150,7 @@ export async function getOrCreateRepository(
connectionId: string,
remoteUri: string
): Promise<gcb.Repository> {
const repositoryId = generateRepositoryId();
const repositoryId = generateRepositoryId(remoteUri);
if (!repositoryId) {
throw new FirebaseError(`Failed to generate repositoryId for URI "${remoteUri}".`);
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/init/frameworks/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ describe("operationsConverter", () => {
const stackId = "stackId";
const stackInput = {
name: stackId,
codebase: {
repository: `cloudbuild.googleapis.com/v2/projects/projectId/locations/us-central1/connections/frameworks-us-central1/repositories/repoId`,
rootDirectory: ".",
},
labels: {},
};
const op = {
Expand Down
2 changes: 1 addition & 1 deletion src/test/init/frameworks/repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe("composer", () => {
getConnectionStub.resolves(pendingConn);
fetchLinkableRepositoriesStub.resolves({ repositories: [] });

await expect(repo.linkGitHubRepository(projectId, location, stackId)).to.be.rejected;
await expect(repo.linkGitHubRepository(projectId, location)).to.be.rejected;
});
});
});
Loading