feat: update all publishers to use isomorphic-gint

This commit is contained in:
blam
2020-12-22 13:09:27 +01:00
parent 3727e3b367
commit 1e7f526bd3
4 changed files with 49 additions and 107 deletions
@@ -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 };
}
@@ -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<PublisherResult> {
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',
@@ -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<PublisherResult> {
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 };
}
@@ -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<void> {
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<void> {
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',
});
}