cli: Move file logic to each type of github auth to simplify logic
Signed-off-by: Marcus Eide <eide@spotify.com>
This commit is contained in:
committed by
Philipp Hugenroth
parent
ba46a19232
commit
e3e0b14ec4
@@ -19,7 +19,7 @@ import * as fs from 'fs-extra';
|
||||
import yaml from 'yaml';
|
||||
import { findPaths } from '@backstage/cli-common';
|
||||
|
||||
export type GithubAuthConfig = {
|
||||
type GithubAuthConfig = {
|
||||
auth: {
|
||||
providers: {
|
||||
github: {
|
||||
@@ -36,7 +36,7 @@ export type GithubAuthConfig = {
|
||||
/* eslint-disable-next-line no-restricted-syntax */
|
||||
const { targetRoot } = findPaths(__dirname);
|
||||
const APP_CONFIG_FILE = path.join(targetRoot, 'app-config.local.yaml');
|
||||
const ENV_CONFIG_FILE = path.join(targetRoot, '.env.development');
|
||||
const ENV_CONFIG_FILE = path.join(targetRoot, '.env.local');
|
||||
|
||||
const readConfigFile = async (file: string) => {
|
||||
return yaml.parse(await fs.readFile(file, 'utf8'));
|
||||
@@ -56,10 +56,10 @@ export const updateConfigFile = async (config: GithubAuthConfig) => {
|
||||
);
|
||||
};
|
||||
|
||||
export const updateEnvFile = async (config: GithubAuthConfig) => {
|
||||
export const updateEnvFile = async (clientId: string, clientSecret: string) => {
|
||||
const content = `
|
||||
AUTH_GITHUB_CLIENT_ID=${config.auth.providers.github.development.clientId}
|
||||
AUTH_GITHUB_CLIENT_SECRET=${config.auth.providers.github.development.clientId}`;
|
||||
AUTH_GITHUB_CLIENT_ID=${clientId}
|
||||
AUTH_GITHUB_CLIENT_SECRET=${clientSecret}`;
|
||||
|
||||
if (fs.existsSync(ENV_CONFIG_FILE)) {
|
||||
await fs.appendFile(ENV_CONFIG_FILE, content, 'utf8');
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
import chalk from 'chalk';
|
||||
import inquirer from 'inquirer';
|
||||
import { adminCli } from '../../../create-github-app';
|
||||
import { updateConfigFile, updateEnvFile } from '../file';
|
||||
|
||||
// TODO(tudi2d): Wrapper for admin CLI around `create-github-app` - potentially to be removed
|
||||
export const app = async () => {
|
||||
export const app = async (useEnvForSecrets: boolean) => {
|
||||
// TODO(tudi2d): Make the GitHub Org optional
|
||||
const input = await inquirer.prompt<{ org: string }>([
|
||||
{
|
||||
@@ -27,5 +28,14 @@ export const app = async () => {
|
||||
message: chalk.blue('Enter a GitHub Org [required]'),
|
||||
},
|
||||
]);
|
||||
return await adminCli(input.org);
|
||||
|
||||
const { auth } = await adminCli(input.org);
|
||||
|
||||
await updateConfigFile({ auth });
|
||||
if (useEnvForSecrets) {
|
||||
await updateEnvFile(
|
||||
auth.providers.github.development.clientId,
|
||||
auth.providers.github.development.clientSecret,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
|
||||
import inquirer from 'inquirer';
|
||||
import { GithubAuthConfig, updateConfigFile, updateEnvFile } from '../file';
|
||||
import { app } from './app';
|
||||
import { oauth } from './oauth';
|
||||
|
||||
@@ -42,20 +41,12 @@ export const github = async () => {
|
||||
|
||||
switch (type) {
|
||||
case 'GitHub OAuth': {
|
||||
const config: GithubAuthConfig = await oauth(useEnvForSecrets);
|
||||
await updateConfigFile(config);
|
||||
if (useEnvForSecrets) {
|
||||
await updateEnvFile(config);
|
||||
}
|
||||
await oauth(useEnvForSecrets);
|
||||
break;
|
||||
}
|
||||
case 'GitHub App': {
|
||||
const { auth }: GithubAuthConfig = await app();
|
||||
// TODO(tudi2d): Also change integrations
|
||||
await updateConfigFile({ auth });
|
||||
if (useEnvForSecrets) {
|
||||
await updateEnvFile({ auth });
|
||||
}
|
||||
await app(useEnvForSecrets);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -18,6 +18,7 @@ import { OAuthApp } from '@octokit/oauth-app';
|
||||
import chalk from 'chalk';
|
||||
import inquirer from 'inquirer';
|
||||
import { Task } from '../../../../lib/tasks';
|
||||
import { updateConfigFile, updateEnvFile } from '../file';
|
||||
|
||||
const validateCredentials = async (clientId: string, clientSecret: string) => {
|
||||
try {
|
||||
@@ -44,7 +45,7 @@ const validateCredentials = async (clientId: string, clientSecret: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const oauth = async (useEnvForSecrets?: boolean) => {
|
||||
export const oauth = async (useEnvForSecrets: boolean) => {
|
||||
Task.log(`
|
||||
To add GitHub authentication, you must create an OAuth App from the GitHub developer settings: ${chalk.blue(
|
||||
'https://github.com/settings/developers',
|
||||
@@ -94,23 +95,26 @@ export const oauth = async (useEnvForSecrets?: boolean) => {
|
||||
|
||||
await validateCredentials(answers.clientId, answers.clientSecret);
|
||||
|
||||
return {
|
||||
auth: {
|
||||
providers: {
|
||||
github: {
|
||||
development: {
|
||||
clientId: useEnvForSecrets
|
||||
? '${AUTH_GITHUB_CLIENT_ID}'
|
||||
: answers.clientId,
|
||||
clientSecret: useEnvForSecrets
|
||||
? '${AUTH_GITHUB_CLIENT_SECRET}'
|
||||
: answers.clientSecret,
|
||||
...(answers.hasGithubEnterprise && {
|
||||
enterpriseInstanceUrl: answers.enterpriseInstanceUrl,
|
||||
}),
|
||||
},
|
||||
const auth = {
|
||||
providers: {
|
||||
github: {
|
||||
development: {
|
||||
clientId: useEnvForSecrets
|
||||
? '${AUTH_GITHUB_CLIENT_ID}'
|
||||
: answers.clientId,
|
||||
clientSecret: useEnvForSecrets
|
||||
? '${AUTH_GITHUB_CLIENT_SECRET}'
|
||||
: answers.clientSecret,
|
||||
...(answers.hasGithubEnterprise && {
|
||||
enterpriseInstanceUrl: answers.enterpriseInstanceUrl,
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
await updateConfigFile({ auth });
|
||||
if (useEnvForSecrets) {
|
||||
await updateEnvFile(answers.clientId, answers.clientSecret);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user