From 632be18bbc384d3c7704fff015ed53831753aa10 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Mon, 2 May 2022 16:30:23 +0200 Subject: [PATCH 1/4] cli/create-github-app: prompt for read or write permissions during setup Signed-off-by: Johan Haals --- .changeset/three-tips-hunt.md | 5 +++++ .../GithubCreateAppServer.ts | 21 +++++++++++++++---- .../src/commands/create-github-app/index.ts | 18 +++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 .changeset/three-tips-hunt.md diff --git a/.changeset/three-tips-hunt.md b/.changeset/three-tips-hunt.md new file mode 100644 index 0000000000..3782fc315b --- /dev/null +++ b/.changeset/three-tips-hunt.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Updated `create-github-app` command to prompt for read or write permissions to simplify setup. diff --git a/packages/cli/src/commands/create-github-app/GithubCreateAppServer.ts b/packages/cli/src/commands/create-github-app/GithubCreateAppServer.ts index 58a3e97a9a..0fe321541e 100644 --- a/packages/cli/src/commands/create-github-app/GithubCreateAppServer.ts +++ b/packages/cli/src/commands/create-github-app/GithubCreateAppServer.ts @@ -60,14 +60,20 @@ export class GithubCreateAppServer { private baseUrl?: string; private webhookUrl?: string; - static async run({ org }: { org: string }): Promise { - const encodedOrg = encodeURIComponent(org); + static async run(options: { + org: string; + readWrite: boolean; + }): Promise { + const encodedOrg = encodeURIComponent(options.org); const actionUrl = `https://github.com/organizations/${encodedOrg}/settings/apps/new`; - const server = new GithubCreateAppServer(actionUrl); + const server = new GithubCreateAppServer(actionUrl, options.readWrite); return server.start(); } - constructor(private readonly actionUrl: string) { + private constructor( + private readonly actionUrl: string, + private readonly readWrite: boolean, + ) { const webhookId = crypto .randomBytes(15) .toString('base64') @@ -122,6 +128,13 @@ export class GithubCreateAppServer { url: this.webhookUrl, active: false, }, + ...(this.readWrite && { + default_permissions: { + contents: 'write', + actions: 'write', + metadata: 'read', + }, + }), }; const manifestJson = JSON.stringify(manifest).replace(/\"/g, '"'); diff --git a/packages/cli/src/commands/create-github-app/index.ts b/packages/cli/src/commands/create-github-app/index.ts index 2e5dd01072..e84e010c60 100644 --- a/packages/cli/src/commands/create-github-app/index.ts +++ b/packages/cli/src/commands/create-github-app/index.ts @@ -16,6 +16,7 @@ import fs from 'fs-extra'; import chalk from 'chalk'; +import inquirer, { Answers } from 'inquirer'; import { stringify as stringifyYaml } from 'yaml'; import inquirer, { Question } from 'inquirer'; import { paths } from '../../lib/paths'; @@ -26,8 +27,23 @@ import openBrowser from 'react-dev-utils/openBrowser'; // 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) => { + const answers: Answers = await inquirer.prompt([ + { + type: 'list', + name: 'appType', + message: chalk.blue('What will the app be used for [required]'), + choices: [ + 'Read and Write(needed by the scaffolding to create new projects)', + 'Read only', + ], + }, + ]); + const readWrite = answers.appType !== 'Read only'; await verifyGithubOrg(org); - const { slug, name, ...config } = await GithubCreateAppServer.run({ org }); + const { slug, name, ...config } = await GithubCreateAppServer.run({ + org, + readWrite, + }); const fileName = `github-app-${slug}-credentials.yaml`; const content = `# Name: ${name}\n${stringifyYaml(config)}`; From 706508ff79dc1ddff1b6592b7ded174ae8dc021a Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Tue, 3 May 2022 10:02:48 +0200 Subject: [PATCH 2/4] improve wording Signed-off-by: Johan Haals Co-authored-by: Patrik Oldsberg --- 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 e84e010c60..8882b597c6 100644 --- a/packages/cli/src/commands/create-github-app/index.ts +++ b/packages/cli/src/commands/create-github-app/index.ts @@ -33,7 +33,7 @@ export default async (org: string) => { name: 'appType', message: chalk.blue('What will the app be used for [required]'), choices: [ - 'Read and Write(needed by the scaffolding to create new projects)', + 'Read and Write (needed by Software Templates)', 'Read only', ], }, From 3a3796b9f1ce628468e15f96c47dec7bd5e0a2ae Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Tue, 3 May 2022 10:25:59 +0200 Subject: [PATCH 3/4] chore: format Signed-off-by: Johan Haals --- packages/cli/src/commands/create-github-app/index.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/cli/src/commands/create-github-app/index.ts b/packages/cli/src/commands/create-github-app/index.ts index 8882b597c6..f81ad4a81e 100644 --- a/packages/cli/src/commands/create-github-app/index.ts +++ b/packages/cli/src/commands/create-github-app/index.ts @@ -32,10 +32,7 @@ export default async (org: string) => { type: 'list', name: 'appType', message: chalk.blue('What will the app be used for [required]'), - choices: [ - 'Read and Write (needed by Software Templates)', - 'Read only', - ], + choices: ['Read and Write (needed by Software Templates)', 'Read only'], }, ]); const readWrite = answers.appType !== 'Read only'; From b21fb17bddc03f8aad7b0afe569be7a547c089a0 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Tue, 3 May 2022 10:46:38 +0200 Subject: [PATCH 4/4] chore: rebase Signed-off-by: Johan Haals --- packages/cli/src/commands/create-github-app/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/cli/src/commands/create-github-app/index.ts b/packages/cli/src/commands/create-github-app/index.ts index f81ad4a81e..175530402e 100644 --- a/packages/cli/src/commands/create-github-app/index.ts +++ b/packages/cli/src/commands/create-github-app/index.ts @@ -16,9 +16,8 @@ import fs from 'fs-extra'; import chalk from 'chalk'; -import inquirer, { Answers } from 'inquirer'; import { stringify as stringifyYaml } from 'yaml'; -import inquirer, { Question } from 'inquirer'; +import inquirer, { Question, Answers } from 'inquirer'; import { paths } from '../../lib/paths'; import { GithubCreateAppServer } from './GithubCreateAppServer'; import fetch from 'node-fetch';