From a7d5ade5a16599e5797d03b9dec3bd42d9716dc9 Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Tue, 15 Sep 2020 16:31:35 -0400 Subject: [PATCH] Merge GitHub scaffolding instructions --- .../software-templates/installation.md | 40 ++++++++++++++----- .../backend/src/plugins/scaffolder.ts | 19 +++++++-- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/docs/features/software-templates/installation.md b/docs/features/software-templates/installation.md index eea78e3571..14bdc1318d 100644 --- a/docs/features/software-templates/installation.md +++ b/docs/features/software-templates/installation.md @@ -90,17 +90,19 @@ import { GithubPublisher, CreateReactAppTemplater, Templaters, + RepoVisilityOptions, } from '@backstage/plugin-scaffolder-backend'; import { Octokit } from '@octokit/rest'; import type { PluginEnvironment } from '../types'; import Docker from 'dockerode'; -export default async function createPlugin({ logger }: PluginEnvironment) { +export default async function createPlugin({ + logger, + config, +}: PluginEnvironment) { const cookiecutterTemplater = new CookieCutter(); const craTemplater = new CreateReactAppTemplater(); const templaters = new Templaters(); - - // Register default templaters templaters.register('cookiecutter', cookiecutterTemplater); templaters.register('cra', craTemplater); @@ -108,13 +110,20 @@ export default async function createPlugin({ logger }: PluginEnvironment) { const githubPreparer = new GithubPreparer(); const preparers = new Preparers(); - // Register default preparers preparers.register('file', filePreparer); preparers.register('github', githubPreparer); - // Create GitHub client with your access token from environment variables - const githubClient = new Octokit({ auth: process.env.GITHUB_ACCESS_TOKEN }); - const publisher = new GithubPublisher({ client: githubClient }); + const githubToken = config.getString('scaffolder.github.token'); + const repoVisibility = config.getString( + 'scaffolder.github.visibility', + ) as RepoVisilityOptions; + + const githubClient = new Octokit({ auth: githubToken }); + const publisher = new GithubPublisher({ + client: githubClient, + token: githubToken, + repoVisibility, + }); const dockerClient = new Docker(); return await createRouter({ @@ -177,8 +186,21 @@ docs on creating private GitHub access tokens is available Note that the need for private GitHub access tokens will be replaced with GitHub Apps integration further down the line. -The GitHub access token is passed along using the `GITHUB_ACCESS_TOKEN` -environment variable. +> **Right now it is only possible to scaffold repositories inside GitHub +> organizations, and not under personal accounts.** + +The Github access token is retrieved from environment variables by the +config. The config file needs to specify what environment variable the +token is retrieved from. Your config should have the following objects. + +```yaml +scaffolder: + github: + token: + $secret: + env: GITHUB_ACCESS_TOKEN + visibility: public # or 'internal' or 'private' +``` ### Running the Backend diff --git a/packages/create-app/templates/default-app/packages/backend/src/plugins/scaffolder.ts b/packages/create-app/templates/default-app/packages/backend/src/plugins/scaffolder.ts index ffa10cc3c6..fecef240df 100644 --- a/packages/create-app/templates/default-app/packages/backend/src/plugins/scaffolder.ts +++ b/packages/create-app/templates/default-app/packages/backend/src/plugins/scaffolder.ts @@ -7,12 +7,16 @@ import { GithubPublisher, CreateReactAppTemplater, Templaters, + RepoVisilityOptions, } from '@backstage/plugin-scaffolder-backend'; import { Octokit } from '@octokit/rest'; import type { PluginEnvironment } from '../types'; import Docker from 'dockerode'; -export default async function createPlugin({ logger }: PluginEnvironment) { +export default async function createPlugin({ + logger, + config, +}: PluginEnvironment) { const cookiecutterTemplater = new CookieCutter(); const craTemplater = new CreateReactAppTemplater(); const templaters = new Templaters(); @@ -26,8 +30,17 @@ export default async function createPlugin({ logger }: PluginEnvironment) { preparers.register('file', filePreparer); preparers.register('github', githubPreparer); - const githubClient = new Octokit({ auth: process.env.GITHUB_ACCESS_TOKEN }); - const publisher = new GithubPublisher({ client: githubClient }); + const githubToken = config.getString('scaffolder.github.token'); + const repoVisibility = config.getString( + 'scaffolder.github.visibility', + ) as RepoVisilityOptions; + + const githubClient = new Octokit({ auth: githubToken }); + const publisher = new GithubPublisher({ + client: githubClient, + token: githubToken, + repoVisibility, + }); const dockerClient = new Docker(); return await createRouter({