From 1e7f526bd3e0a231c56f43d0329e37c4283d89e3 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 22 Dec 2020 13:09:27 +0100 Subject: [PATCH] feat: update all publishers to use isomorphic-gint --- .../src/scaffolder/stages/publish/azure.ts | 23 +++---- .../src/scaffolder/stages/publish/github.ts | 46 +++---------- .../src/scaffolder/stages/publish/gitlab.ts | 23 +++---- .../src/scaffolder/stages/publish/helpers.ts | 64 +++++++------------ 4 files changed, 49 insertions(+), 107 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts index 16956cc8fb..ffe6845f01 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts @@ -16,10 +16,10 @@ import { PublisherBase, PublisherOptions, PublisherResult } from './types'; import { GitApi } from 'azure-devops-node-api/GitApi'; -import { Git } from '@backstage/backend-common'; import { GitRepositoryCreateOptions } from 'azure-devops-node-api/interfaces/GitInterfaces'; import { JsonValue } from '@backstage/config'; import { RequiredTemplateValues } from '../templater'; +import { initRepoAndPush } from './helpers'; export class AzurePublisher implements PublisherBase { private readonly client: GitApi; @@ -38,23 +38,16 @@ export class AzurePublisher implements PublisherBase { const remoteUrl = await this.createRemote(values); const catalogInfoUrl = `${remoteUrl}?path=%2Fcatalog-info.yaml`; - const git = Git.fromAuth({ - username: 'notempty', - password: this.token, + await initRepoAndPush({ + dir: directory, + remoteUrl, + auth: { + username: 'notempty', + password: this.token, + }, logger, }); - await git.addRemote({ - dir: directory, - url: remoteUrl, - remoteName: 'origin', - }); - - await git.push({ - dir: directory, - remoteName: 'origin', - }); - return { remoteUrl, catalogInfoUrl }; } diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts index 7abcee193c..39a588882f 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts @@ -16,10 +16,9 @@ import { PublisherBase, PublisherOptions, PublisherResult } from './types'; import { Octokit } from '@octokit/rest'; -import { Git } from '@backstage/backend-common'; +import { initRepoAndPush } from './helpers'; import { JsonValue } from '@backstage/config'; import { RequiredTemplateValues } from '../templater'; -import globby from 'globby'; export type RepoVisibilityOptions = 'private' | 'internal' | 'public'; @@ -50,44 +49,17 @@ export class GithubPublisher implements PublisherBase { logger, }: PublisherOptions): Promise { const remoteUrl = await this.createRemote(values); - const git = Git.fromAuth({ - username: this.token, - password: 'x-oauth-basic', + + await initRepoAndPush({ + dir: directory, + remoteUrl, + auth: { + username: this.token, + password: 'x-oauth-basic', + }, logger, }); - await git.init({ - dir: directory, - }); - - const paths = await globby(['./**', './**/.*'], { - cwd: directory, - gitignore: true, - dot: true, - }); - - for (const filepath of paths) { - await git.add({ dir: directory, filepath }); - } - - await git.commit({ - dir: directory, - message: 'Initial commit', - author: { name: 'Scaffolder', email: 'scaffolder@backstage.io' }, - committer: { name: 'Scaffolder', email: 'scaffolder@backstage.io' }, - }); - - await git.addRemote({ - dir: directory, - url: remoteUrl, - remoteName: 'origin', - }); - - await git.push({ - dir: directory, - remoteName: 'origin', - }); - const catalogInfoUrl = remoteUrl.replace( /\.git$/, '/blob/master/catalog-info.yaml', diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts index 32b203bf51..156a4a249c 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts @@ -16,8 +16,8 @@ import { PublisherBase, PublisherOptions, PublisherResult } from './types'; import { Gitlab } from '@gitbeaker/core'; -import { Git } from '@backstage/backend-common'; import { JsonValue } from '@backstage/config'; +import { initRepoAndPush } from './helpers'; import { RequiredTemplateValues } from '../templater'; export class GitlabPublisher implements PublisherBase { @@ -36,23 +36,16 @@ export class GitlabPublisher implements PublisherBase { }: PublisherOptions): Promise { const remoteUrl = await this.createRemote(values); - const git = Git.fromAuth({ - password: this.token, - username: 'oauth2', + await initRepoAndPush({ + dir: directory, + remoteUrl, + auth: { + username: 'oauth2', + password: this.token, + }, logger, }); - await git.addRemote({ - dir: directory, - url: remoteUrl, - remoteName: 'origin', - }); - - await git.push({ - dir: directory, - remoteName: 'origin', - }); - return { remoteUrl }; } diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts index 1dcf3fdee1..bf19c2f56d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts @@ -14,44 +14,42 @@ * limitations under the License. */ -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' -GitHub App | token 'x-access-token' -BitBucket | 'x-token-auth' token -GitLab | 'oauth2' token -From : https://isomorphic-git.org/docs/en/onAuth -*/ -export async function push( - dir: string, - remote: string, - logger: Logger, - auth: { username: string; password: string }, -): Promise { - logger.info('Initializing Git Repo', dir); +import { Git } from '@backstage/backend-common'; + +export async function initRepoAndPush({ + dir, + remoteUrl, + auth, + logger, +}: { + dir: string; + remoteUrl: string; + auth: { username: string; password: string }; + logger: Logger; +}): Promise { + const git = Git.fromAuth({ + username: auth.username, + password: auth.password, + logger, + }); + await git.init({ - fs, dir, }); const paths = await globby(['./**', './**/.*'], { cwd: dir, gitignore: true, + dot: true, }); - logger.info('Adding files to repository', dir); for (const filepath of paths) { - await git.add({ fs, dir, filepath }); + await git.add({ dir, filepath }); } - logger.info('Creating commit', dir); await git.commit({ - fs, dir, message: 'Initial commit', author: { name: 'Scaffolder', email: 'scaffolder@backstage.io' }, @@ -59,27 +57,13 @@ export async function push( }); await git.addRemote({ - fs, dir, - remote: 'origin', - url: remote, + url: remoteUrl, + remoteName: 'origin', }); - logger.info('Pushing code to remote', remote); await git.push({ - fs, dir, - http, - 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: () => auth, + remoteName: 'origin', }); }