Skip to content

Updated the backends:create flow for M2. #6518

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

Merged
merged 10 commits into from
Nov 17, 2023
60 changes: 26 additions & 34 deletions src/init/features/frameworks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import * as clc from "colorette";
import * as utils from "../../../utils";
import { logger } from "../../../logger";
import { promptOnce } from "../../../prompt";
import {
DEFAULT_REGION,
ALLOWED_REGIONS,
DEFAULT_DEPLOY_METHOD,
ALLOWED_DEPLOY_METHODS,
} from "./constants";
import { DEFAULT_REGION, ALLOWED_REGIONS } from "./constants";
import * as repo from "./repo";
import { Backend, BackendOutputOnlyFields } from "../../../gcp/frameworks";
import { Repository } from "../../../gcp/cloudbuild";
Expand All @@ -30,14 +25,14 @@ const frameworksPollerOptions: Omit<poller.OperationPollerOptions, "operationRes
export async function doSetup(setup: any, projectId: string): Promise<void> {
setup.frameworks = {};

utils.logBullet("First we need a few details to create your service.");
utils.logBullet("First we need a few details to create your backend.");

await promptOnce(
{
name: "serviceName",
type: "input",
default: "acme-inc-web",
message: "Create a name for your service [1-30 characters]",
message: "Create a name for your backend [1-30 characters]",
},
setup.frameworks
);
Expand All @@ -57,22 +52,17 @@ export async function doSetup(setup: any, projectId: string): Promise<void> {

utils.logSuccess(`Region set to ${setup.frameworks.region}.`);

logger.info(clc.bold(`\n${clc.white("===")} Deploy Setup`));

await promptOnce(
{
name: "deployMethod",
type: "list",
default: DEFAULT_DEPLOY_METHOD,
message: "How do you want to deploy",
choices: ALLOWED_DEPLOY_METHODS,
},
setup.frameworks
);

const backend: Backend | undefined = await getOrCreateBackend(projectId, setup);
if (backend) {
utils.logSuccess(`Successfully created a backend: ${backend.name}`);
logger.info();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the purpose of these blank logger.info() lines? just to add blank space?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the print statements are cluttered if these are not present.
Found usage of these in other places of codebase.

utils.logSuccess(`Successfully created backend:\n ${backend.name}`);
logger.info();
utils.logSuccess(`Your site is being deployed at:\n https://${backend.uri}`);
logger.info();
utils.logSuccess(
`View the rollout status by running:\n firebase backends:get --backend=${backend.name}`
);
logger.info();
}
}

Expand All @@ -94,22 +84,24 @@ export async function getOrCreateBackend(
setup: any
): Promise<Backend | undefined> {
const location: string = setup.frameworks.region;
const deployMethod: string = setup.frameworks.deployMethod;
try {
return await getExistingBackend(projectId, setup, location);
} catch (err: unknown) {
if ((err as FirebaseError).status === 404) {
logger.info("Creating new backend.");
if (deployMethod === "github") {
const cloudBuildConnRepo = await repo.linkGitHubRepository(projectId, location);
const backendDetails = toBackend(cloudBuildConnRepo);
return await createBackend(
projectId,
location,
backendDetails,
setup.frameworks.serviceName
);
}
const cloudBuildConnRepo = await repo.linkGitHubRepository(projectId, location);
logger.info();
await promptOnce(
{
name: "branchName",
type: "input",
default: "main",
message: "Which branch do you want to deploy?",
},
setup.frameworks
);
const backendDetails = toBackend(cloudBuildConnRepo);
logger.info(clc.bold(`\n${clc.white("===")} Creating your backend`));
return await createBackend(projectId, location, backendDetails, setup.frameworks.serviceName);
} else {
throw new FirebaseError(
`Failed to get or create a backend using the given initialization details: ${err}`
Expand Down
11 changes: 7 additions & 4 deletions src/init/features/frameworks/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { logger } from "../../../logger";
import * as poller from "../../../operation-poller";
import * as utils from "../../../utils";
import { promptOnce } from "../../../prompt";
import * as clc from "colorette";

const gcbPollerOptions: Omit<poller.OperationPollerOptions, "operationResourceName"> = {
apiOrigin: cloudbuildOrigin,
Expand Down Expand Up @@ -51,6 +52,7 @@ export async function linkGitHubRepository(
projectId: string,
location: string
): Promise<gcb.Repository> {
logger.info(clc.bold(`\n${clc.white("===")} Connect a github repository`));
const connectionId = generateConnectionId(location);
await getOrCreateConnection(projectId, location, connectionId);

Expand All @@ -66,7 +68,8 @@ export async function linkGitHubRepository(
}

const repo = await getOrCreateRepository(projectId, location, connectionId, remoteUri);
logger.info(`Successfully linked GitHub repository at remote URI ${remoteUri}.`);
logger.info();
utils.logSuccess(`Successfully linked GitHub repository at remote URI:\n ${remoteUri}`);
return repo;
}

Expand All @@ -93,7 +96,7 @@ async function promptRepositoryURI(

return await promptOnce({
type: "list",
message: "Which of the following repositories would you like to link?",
message: "Which of the following repositories would you like to deploy?",
choices,
});
}
Expand All @@ -104,13 +107,13 @@ async function promptConnectionAuth(
location: string,
connectionId: string
): Promise<gcb.Connection> {
logger.info(conn.installationState.message);
logger.info("First, log in to GitHub, install and authorize Cloud Build app:");
logger.info(conn.installationState.actionUri);
await utils.openInBrowser(conn.installationState.actionUri);
await promptOnce({
type: "input",
message:
"Press any key once you have authorized the app (Cloud Build) to access your GitHub repo.",
"Press Enter once you have authorized the app (Cloud Build) to access your GitHub repo.",
});
return await gcb.getConnection(projectId, location, connectionId);
}
Expand Down
1 change: 1 addition & 0 deletions src/test/init/frameworks/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe("operationsConverter", () => {
serviceName: backendId,
existingBackend: true,
deployMethod: "github",
branchName: "main",
},
};
const cloudBuildConnRepo = {
Expand Down