From 71fe64308d92e6afb31df5f756bbc84bb0d35216 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 28 Apr 2022 11:27:41 +0200 Subject: [PATCH 1/4] chore: starting to add validation for a test organization Signed-off-by: blam --- .../src/commands/create-github-app/index.ts | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/commands/create-github-app/index.ts b/packages/cli/src/commands/create-github-app/index.ts index 32fa1e9a6d..ed1c8a9b8a 100644 --- a/packages/cli/src/commands/create-github-app/index.ts +++ b/packages/cli/src/commands/create-github-app/index.ts @@ -17,9 +17,11 @@ import fs from 'fs-extra'; import chalk from 'chalk'; import { stringify as stringifyYaml } from 'yaml'; +import inquirer, { Answers, Question } from 'inquirer'; import { paths } from '../../lib/paths'; import { GithubCreateAppServer } from './GithubCreateAppServer'; import fetch from 'node-fetch'; +import octokit from 'octokit'; // This is an experimental command that at this point does not support GitHub Enterprise // due to lacking support for creating apps from manifests. @@ -67,8 +69,41 @@ async function verifyGithubOrg(org: string): Promise { } if (response?.status === 404) { - throw new Error( - `GitHub organization '${org}' does not exist. Please verify the name and try again.`, - ); + const questions: Question[] = [ + { + type: 'confirm', + name: 'shouldCreateOrg', + message: `GitHub organization ${chalk.cyan( + org, + )} does not exist. Would you like to create a demo organization instead?`, + }, + { + type: 'input', + message: 'What would you like your demo organization to be called?', + name: 'orgName', + when: (answers: Answers) => answers.shouldCreateOrg, + validate: async (orgName: string) => { + const { status } = await fetch( + `https://api.github.com/orgs/${encodeURIComponent(orgName)}`, + ); + return status === 404 || 'GitHub organization already exists'; + }, + }, + { + type: 'token', + name: 'token', + message: 'GitHub Token', + when: () => !process.env.GITHUB_TOKEN, + }, + ]; + + const answers = await inquirer.prompt(questions); + + if (!answers.shouldCreateOrg) { + console.log( + chalk.yellow('GitHub organization must exist to create GitHub app'), + ); + process.exit(1); + } } } From 346fad1cca94f7941c68f0628150ba59bb60ff7f Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 2 May 2022 15:04:09 +0200 Subject: [PATCH 2/4] chore: help creating a new organization Signed-off-by: blam --- .../src/commands/create-github-app/index.ts | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/packages/cli/src/commands/create-github-app/index.ts b/packages/cli/src/commands/create-github-app/index.ts index ed1c8a9b8a..8e213eaf20 100644 --- a/packages/cli/src/commands/create-github-app/index.ts +++ b/packages/cli/src/commands/create-github-app/index.ts @@ -21,8 +21,7 @@ import inquirer, { Answers, Question } from 'inquirer'; import { paths } from '../../lib/paths'; import { GithubCreateAppServer } from './GithubCreateAppServer'; import fetch from 'node-fetch'; -import octokit from 'octokit'; - +import openBrowser from 'react-dev-utils/openBrowser'; // This is an experimental command that at this point does not support GitHub Enterprise // due to lacking support for creating apps from manifests. // https://docs.github.com/en/free-pro-team@latest/developers/apps/creating-a-github-app-from-a-manifest @@ -75,25 +74,7 @@ async function verifyGithubOrg(org: string): Promise { name: 'shouldCreateOrg', message: `GitHub organization ${chalk.cyan( org, - )} does not exist. Would you like to create a demo organization instead?`, - }, - { - type: 'input', - message: 'What would you like your demo organization to be called?', - name: 'orgName', - when: (answers: Answers) => answers.shouldCreateOrg, - validate: async (orgName: string) => { - const { status } = await fetch( - `https://api.github.com/orgs/${encodeURIComponent(orgName)}`, - ); - return status === 404 || 'GitHub organization already exists'; - }, - }, - { - type: 'token', - name: 'token', - message: 'GitHub Token', - when: () => !process.env.GITHUB_TOKEN, + )} does not exist. Would you like to create a new Organization instead?`, }, ]; @@ -105,5 +86,15 @@ async function verifyGithubOrg(org: string): Promise { ); process.exit(1); } + + openBrowser('https://github.com/account/organizations/new'); + + console.log( + chalk.yellow( + 'Please re-run this command when you have created your new organization', + ), + ); + + process.exit(0); } } From 2737777e02f3b5e4ffd3469003253a1834d91c35 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 2 May 2022 15:04:42 +0200 Subject: [PATCH 3/4] chore: added changeset Signed-off-by: blam --- .changeset/short-chicken-act.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/short-chicken-act.md diff --git a/.changeset/short-chicken-act.md b/.changeset/short-chicken-act.md new file mode 100644 index 0000000000..ed8e70edac --- /dev/null +++ b/.changeset/short-chicken-act.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Added the ability to help a user get started with a new organization From 614c775f91486a2335b8f9494dbddba06e504d28 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 2 May 2022 15:08:08 +0200 Subject: [PATCH 4/4] chore: remove unused import Signed-off-by: blam --- packages/cli/src/commands/create-github-app/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/create-github-app/index.ts b/packages/cli/src/commands/create-github-app/index.ts index 8e213eaf20..2e5dd01072 100644 --- a/packages/cli/src/commands/create-github-app/index.ts +++ b/packages/cli/src/commands/create-github-app/index.ts @@ -17,7 +17,7 @@ import fs from 'fs-extra'; import chalk from 'chalk'; import { stringify as stringifyYaml } from 'yaml'; -import inquirer, { Answers, Question } from 'inquirer'; +import inquirer, { Question } from 'inquirer'; import { paths } from '../../lib/paths'; import { GithubCreateAppServer } from './GithubCreateAppServer'; import fetch from 'node-fetch';