feat: use better logging for the publish step and the new helper with isomorphic-git

This commit is contained in:
blam
2020-12-07 13:26:02 +01:00
parent 3df4cdd1b5
commit 15e5d6a763
4 changed files with 22 additions and 9 deletions
@@ -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',
@@ -46,13 +46,13 @@ export class GithubPublisher implements PublisherBase {
async publish({
values,
directory,
logger,
}: PublisherOptions): Promise<PublisherResult> {
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(
@@ -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<void> {
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,
});
}
@@ -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<string, JsonValue>;
logger: Logger;
directory: string;
};