From 8ab2a8226b2dcea6a082be2aa9dd06b2fd9917df Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Wed, 20 Apr 2022 14:00:45 +0200 Subject: [PATCH] cli: Throw error if non org is provided to create-github-app Signed-off-by: Johan Haals --- .changeset/swift-bugs-share.md | 5 +++++ packages/cli/package.json | 1 + .../cli/src/commands/create-github-app/index.ts | 13 +++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 .changeset/swift-bugs-share.md diff --git a/.changeset/swift-bugs-share.md b/.changeset/swift-bugs-share.md new file mode 100644 index 0000000000..c018bdfacf --- /dev/null +++ b/.changeset/swift-bugs-share.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Updated the `create-github-app` command to throw an error if the organization argument is a user or a non existing organization. diff --git a/packages/cli/package.json b/packages/cli/package.json index abe0e1f713..628e7ed93e 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -93,6 +93,7 @@ "lodash": "^4.17.21", "mini-css-extract-plugin": "^2.4.2", "minimatch": "5.0.1", + "node-fetch": "^2.6.7", "node-libs-browser": "^2.2.1", "npm-packlist": "^5.0.0", "ora": "^5.3.0", diff --git a/packages/cli/src/commands/create-github-app/index.ts b/packages/cli/src/commands/create-github-app/index.ts index f2d6ba0a91..617ae12f36 100644 --- a/packages/cli/src/commands/create-github-app/index.ts +++ b/packages/cli/src/commands/create-github-app/index.ts @@ -19,11 +19,13 @@ import chalk from 'chalk'; import { stringify as stringifyYaml } from 'yaml'; import { paths } from '../../lib/paths'; import { GithubCreateAppServer } from './GithubCreateAppServer'; +import fetch from 'node-fetch'; // 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 export default async (org: string) => { + await verifyGithubOrg(org); const { slug, name, ...config } = await GithubCreateAppServer.run({ org }); const fileName = `github-app-${slug}-credentials.yaml`; @@ -47,3 +49,14 @@ integrations: - $include: ${fileName}`), ); }; + +async function verifyGithubOrg(org: string): Promise { + const response = await fetch( + `https://api.github.com/orgs/${encodeURIComponent(org)}`, + ); + if (!response.ok) { + throw new Error( + `GitHub organization '${org}' does not exist. Please verify the name and try again.`, + ); + } +}