From d0a972eb63bb727f321504e485b5d8730740453d Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 21 Dec 2020 16:05:10 +0100 Subject: [PATCH] chore: reworking how we deal with exporting things from the main package --- packages/backend-common/src/index.ts | 1 + packages/backend-common/src/scm/git.ts | 23 +++-- packages/backend-common/src/scm/index.ts | 16 ++++ .../src/scaffolder/stages/prepare/github.ts | 40 +++------ .../src/scaffolder/stages/publish/github.ts | 17 +++- .../src/scaffolder/stages/publish/helpers.ts | 85 ------------------- 6 files changed, 57 insertions(+), 125 deletions(-) create mode 100644 packages/backend-common/src/scm/index.ts delete mode 100644 plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts diff --git a/packages/backend-common/src/index.ts b/packages/backend-common/src/index.ts index e968edef69..3f04f32b34 100644 --- a/packages/backend-common/src/index.ts +++ b/packages/backend-common/src/index.ts @@ -24,3 +24,4 @@ export * from './reading'; export * from './service'; export * from './paths'; export * from './hot'; +export * from './scm'; diff --git a/packages/backend-common/src/scm/git.ts b/packages/backend-common/src/scm/git.ts index eecfd36612..25115bab26 100644 --- a/packages/backend-common/src/scm/git.ts +++ b/packages/backend-common/src/scm/git.ts @@ -18,11 +18,19 @@ import http from 'isomorphic-git/http/node'; import fs from 'fs-extra'; import { Logger } from 'winston'; +/* +provider 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 +*/ class SCM { constructor( private readonly config: { - username: string; - password: string; + username?: string; + password?: string; logger?: Logger; }, ) {} @@ -126,14 +134,15 @@ class SCM { } } +// TODO(blam): This could potentially become something like for URL +// and use the integrations config for URLReading instead. +// But for now, I don't want to do all that in this PR. export const fromAuth = ({ username, password, logger, }: { - username: string; - password: string; + username?: string; + password?: string; logger?: Logger; -}) => { - return new SCM({ username, password, logger }); -}; +}) => new SCM({ username, password, logger }); diff --git a/packages/backend-common/src/scm/index.ts b/packages/backend-common/src/scm/index.ts new file mode 100644 index 0000000000..76af535fd0 --- /dev/null +++ b/packages/backend-common/src/scm/index.ts @@ -0,0 +1,16 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export * as Git from './git'; diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts index 8e5581d63d..761259c0c5 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts @@ -18,11 +18,9 @@ import fs from 'fs-extra'; import path from 'path'; import { TemplateEntityV1alpha1 } from '@backstage/catalog-model'; import { parseLocationAnnotation } from '../helpers'; -import { InputError } from '@backstage/backend-common'; +import { InputError, Git } from '@backstage/backend-common'; import { PreparerBase, PreparerOptions } from './types'; import GitUriParser from 'git-url-parse'; -import git from 'isomorphic-git'; -import http from 'isomorphic-git/http/node'; export class GithubPreparer implements PreparerBase { token?: string; @@ -37,7 +35,6 @@ export class GithubPreparer implements PreparerBase { ): Promise { const { protocol, location } = parseLocationAnnotation(template); const workingDirectory = opts?.workingDirectory ?? os.tmpdir(); - const { token } = this; if (!['github', 'url'].includes(protocol)) { throw new InputError( @@ -58,33 +55,16 @@ export class GithubPreparer implements PreparerBase { ); const checkoutLocation = path.resolve(tempDir, templateDirectory); + const git = Git.fromAuth({ + username: this.token, + password: 'x-oauth-basic', + logger: opts.logger, + }); - try { - await git.clone({ - fs, - http, - url: repositoryCheckoutUrl, - dir: checkoutLocation, - singleBranch: true, - depth: 1, - onProgress: event => { - const total = event.total - ? `${Math.round((event.loaded / event.total) * 100)}%` - : event.loaded; - opts.logger.info(`status={${event.phase},total={${total}}}`); - }, - headers: { - 'user-agent': 'git/@isomorphic-git', - }, - onAuth: () => ({ username: token, password: 'x-oauth-basic' }), - }); - } catch (ex) { - opts.logger.error( - `Failed checking out repository: ${repositoryCheckoutUrl}`, - ); - opts.logger.error(ex.message); - throw ex; - } + await git.clone({ + url: repositoryCheckoutUrl, + dir: checkoutLocation, + }); return checkoutLocation; } diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts index e262c59274..6e99590e15 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts @@ -16,7 +16,7 @@ import { PublisherBase, PublisherOptions, PublisherResult } from './types'; import { Octokit } from '@octokit/rest'; -import { pushToRemoteCred } from './helpers'; +import { Git } from '@backstage/backend-common'; import { JsonValue } from '@backstage/config'; import { RequiredTemplateValues } from '../templater'; @@ -49,10 +49,21 @@ export class GithubPublisher implements PublisherBase { logger, }: PublisherOptions): Promise { const remoteUrl = await this.createRemote(values); - - await pushToRemoteCred(directory, remoteUrl, logger, { + const git = Git.fromAuth({ username: this.token, password: 'x-oauth-basic', + logger, + }); + + await git.addRemote({ + dir: directory, + url: remoteUrl, + remoteName: 'origin', + }); + + await git.push({ + dir: directory, + remoteName: 'origin', }); 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 deleted file mode 100644 index 1dcf3fdee1..0000000000 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * 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); - await git.init({ - fs, - dir, - }); - - const paths = await globby(['./**', './**/.*'], { - 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, - message: 'Initial commit', - author: { name: 'Scaffolder', email: 'scaffolder@backstage.io' }, - committer: { name: 'Scaffolder', email: 'scaffolder@backstage.io' }, - }); - - await git.addRemote({ - fs, - dir, - remote: 'origin', - url: remote, - }); - - 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, - }); -}