From 15e5d6a76398eb414b8d41e533eb1aa1f392c710 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 7 Dec 2020 13:26:02 +0100 Subject: [PATCH] feat: use better logging for the publish step and the new helper with isomorphic-git --- .../src/scaffolder/stages/prepare/github.ts | 4 ++-- .../src/scaffolder/stages/publish/github.ts | 6 +++--- .../src/scaffolder/stages/publish/helpers.ts | 19 +++++++++++++++---- .../src/scaffolder/stages/publish/types.ts | 2 ++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts index f202c609cc..8e5581d63d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts @@ -69,9 +69,9 @@ export class GithubPreparer implements PreparerBase { depth: 1, onProgress: event => { const total = event.total - ? `${event.loaded / event.total}%` + ? `${Math.round((event.loaded / event.total) * 100)}%` : event.loaded; - opts.logger.info({ status: event.phase, total }); + opts.logger.info(`status={${event.phase},total={${total}}}`); }, headers: { 'user-agent': 'git/@isomorphic-git', diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts index 612e676207..e262c59274 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts @@ -46,13 +46,13 @@ export class GithubPublisher implements PublisherBase { async publish({ values, directory, + logger, }: PublisherOptions): Promise { const remoteUrl = await this.createRemote(values); - await pushToRemoteCred(directory, remoteUrl, { + await pushToRemoteCred(directory, remoteUrl, logger, { username: this.token, - password: 'x-auth-basic', - token: this.token, + password: 'x-oauth-basic', }); const catalogInfoUrl = remoteUrl.replace( diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts index 8e76649989..39b1513d2e 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts @@ -18,7 +18,7 @@ import git from 'isomorphic-git'; import globby from 'globby'; import fs from 'fs'; import http from 'isomorphic-git/http/node'; - +import { Logger } from 'winston'; /* username password GitHub | token 'x-oauth-basic' @@ -30,8 +30,10 @@ From : https://isomorphic-git.org/docs/en/onAuth export async function pushToRemoteCred( dir: string, remote: string, - auth?: { username: string; password: string; token: string }, + logger: Logger, + auth: { username: string; password: string }, ): Promise { + logger.info('Initializing Git Repo', dir); await git.init({ fs, dir, @@ -41,10 +43,13 @@ export async function pushToRemoteCred( cwd: dir, gitignore: true, }); + + logger.info('Adding files to repository', dir); for (const filepath of paths) { await git.add({ fs, dir, filepath }); } + logger.info('Creating commit', dir); await git.commit({ fs, dir, @@ -60,7 +65,7 @@ export async function pushToRemoteCred( url: remote, }); - console.warn({ username: auth.token, password: 'x-oauth-basic' }); + logger.info('Pushing code to remote', remote); await git.push({ fs, dir, @@ -68,7 +73,13 @@ export async function pushToRemoteCred( headers: { 'user-agent': 'git/@isomorphic-git', }, + onProgress: event => { + const total = event.total + ? `${Math.round((event.loaded / event.total) * 100)}%` + : event.loaded; + logger.info(`status={${event.phase},total={${total}}}`); + }, remote: 'origin', - onAuth: () => ({ username: auth.token, password: 'x-oauth-basic' }), + onAuth: () => auth, }); } diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/types.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/types.ts index f3bc59e19d..bc6b63cc57 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/types.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/types.ts @@ -17,6 +17,7 @@ import { TemplateEntityV1alpha1 } from '@backstage/catalog-model'; import { RequiredTemplateValues } from '../templater'; import { JsonValue } from '@backstage/config'; import { RemoteProtocol } from '../types'; +import { Logger } from 'winston'; /** * Publisher is in charge of taking a folder created by @@ -34,6 +35,7 @@ export type PublisherBase = { export type PublisherOptions = { values: RequiredTemplateValues & Record; + logger: Logger; directory: string; };