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..175530402e 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, { Question } from 'inquirer'; +import inquirer, { Question, Answers } from 'inquirer'; import { paths } from '../../lib/paths'; import { GithubCreateAppServer } from './GithubCreateAppServer'; import fetch from 'node-fetch'; @@ -26,8 +26,20 @@ 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 Software Templates)', '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)}`;