chore: reworking how we deal with exporting things from the main package
This commit is contained in:
@@ -24,3 +24,4 @@ export * from './reading';
|
||||
export * from './service';
|
||||
export * from './paths';
|
||||
export * from './hot';
|
||||
export * from './scm';
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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';
|
||||
@@ -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<string> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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<PublisherResult> {
|
||||
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(
|
||||
|
||||
@@ -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<void> {
|
||||
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,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user