diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts index ff814c20ba..f202c609cc 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts @@ -61,7 +61,7 @@ export class GithubPreparer implements PreparerBase { try { await git.clone({ - fs: require('fs'), + fs, http, url: repositoryCheckoutUrl, dir: checkoutLocation, diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts index d5542e8800..0306bb72e9 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 } from './types'; import { Octokit } from '@octokit/rest'; -import { pushToRemoteUserPass } from './helpers'; +import { pushToRemoteCred } from './helpers'; import { JsonValue } from '@backstage/config'; import { RequiredTemplateValues } from '../templater'; @@ -51,12 +51,10 @@ export class GithubPublisher implements PublisherBase { directory: string; }): Promise<{ remoteUrl: string }> { const remoteUrl = await this.createRemote(values); - await pushToRemoteUserPass( - directory, - remoteUrl, - this.token, - 'x-oauth-basic', - ); + await pushToRemoteCred(directory, remoteUrl, { + username: this.token, + password: 'x-auth-basic', + }); 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 51f5a4c854..61e76a31be 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts @@ -13,43 +13,47 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Repository, Remote, Signature, Cred } from 'nodegit'; + +import git from 'isomorphic-git'; +import globby from 'globby'; +import fs from 'fs'; +import http from 'isomorphic-git/http/node'; export async function pushToRemoteCred( - directory: string, + dir: string, remote: string, - credentialsProvider: () => Cred, + auth?: { username: string; password: string }, ): Promise { - const repo = await Repository.init(directory, 0); - const index = await repo.refreshIndex(); - await index.addAll(); - await index.write(); - const oid = await index.writeTree(); - await repo.createCommit( - 'HEAD', - Signature.now('Scaffolder', 'scaffolder@backstage.io'), - Signature.now('Scaffolder', 'scaffolder@backstage.io'), - 'initial commit', - oid, - [], - ); + await git.init({ + fs, + dir, + }); - const remoteRepo = await Remote.create(repo, 'origin', remote); + const paths = await globby(['./**', './**/.*'], { gitignore: true }); + for (const filepath of paths) { + await git.add({ fs, dir, filepath }); + } - await remoteRepo.push(['refs/heads/master:refs/heads/master'], { - callbacks: { - credentials: credentialsProvider, - }, + 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, + }); + + await git.push({ + fs, + dir, + http, + remote: 'origin', + onAuth: () => auth, }); } - -export async function pushToRemoteUserPass( - directory: string, - remote: string, - username: string, - password: string, -): Promise { - return pushToRemoteCred(directory, remote, () => - Cred.userpassPlaintextNew(username, password), - ); -}