cli/create-github-app: prompt for read or write permissions during setup

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2022-05-02 16:30:23 +02:00
parent b6aa189b4d
commit 632be18bbc
3 changed files with 39 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---
Updated `create-github-app` command to prompt for read or write permissions to simplify setup.
@@ -60,14 +60,20 @@ export class GithubCreateAppServer {
private baseUrl?: string;
private webhookUrl?: string;
static async run({ org }: { org: string }): Promise<GithubAppConfig> {
const encodedOrg = encodeURIComponent(org);
static async run(options: {
org: string;
readWrite: boolean;
}): Promise<GithubAppConfig> {
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, '&quot;');
@@ -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)}`;