feat: update all preparers to use isomorphic git

This commit is contained in:
blam
2020-12-22 13:00:36 +01:00
parent ab82709595
commit 3727e3b367
6 changed files with 59 additions and 33 deletions
+1 -1
View File
@@ -114,7 +114,7 @@ class SCM {
this.config.logger?.info(
`Pushing directory to remote {dir=${dir},remoteName=${remoteName}}`,
);
git.push({
return git.push({
fs,
dir,
http,
-1
View File
@@ -53,7 +53,6 @@
"isomorphic-git": "^1.8.0",
"jsonschema": "^1.2.6",
"morgan": "^1.10.0",
"nodegit": "^0.27.0",
"uuid": "^8.2.0",
"winston": "^3.2.1",
"yaml": "^1.10.0"
@@ -18,10 +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 { Clone, Cred } from 'nodegit';
import { Config } from '@backstage/config';
export class AzurePreparer implements PreparerBase {
@@ -38,6 +37,7 @@ export class AzurePreparer implements PreparerBase {
): Promise<string> {
const { protocol, location } = parseLocationAnnotation(template);
const workingDirectory = opts?.workingDirectory ?? os.tmpdir();
const { logger } = opts;
if (!['azure/api', 'url'].includes(protocol)) {
throw new InputError(
@@ -57,19 +57,20 @@ export class AzurePreparer implements PreparerBase {
template.spec.path ?? '.',
);
const options = this.privateToken
? {
fetchOpts: {
callbacks: {
credentials: () =>
// Username can anything but the empty string according to: https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page#use-a-pat
Cred.userpassPlaintextNew('notempty', this.privateToken),
},
},
}
: {};
// Username can anything but the empty string according to:
// https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page#use-a-pat
const git = this.privateToken
? Git.fromAuth({
password: this.privateToken,
username: 'notempty',
logger,
})
: Git.fromAuth({ logger });
await Clone.clone(repositoryCheckoutUrl, tempDir, options);
await git.clone({
url: repositoryCheckoutUrl,
dir: tempDir,
});
return path.resolve(tempDir, templateDirectory);
}
@@ -35,6 +35,7 @@ export class GithubPreparer implements PreparerBase {
): Promise<string> {
const { protocol, location } = parseLocationAnnotation(template);
const workingDirectory = opts?.workingDirectory ?? os.tmpdir();
const { logger } = opts;
if (!['github', 'url'].includes(protocol)) {
throw new InputError(
@@ -56,11 +57,13 @@ 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,
});
const git = this.token
? Git.fromAuth({
username: this.token,
password: 'x-oauth-basic',
logger,
})
: Git.fromAuth({ logger });
await git.clone({
url: repositoryCheckoutUrl,
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { InputError } from '@backstage/backend-common';
import { InputError, Git } from '@backstage/backend-common';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import {
@@ -22,7 +22,6 @@ import {
} from '@backstage/integration';
import fs from 'fs-extra';
import GitUriParser from 'git-url-parse';
import { Clone, Cred } from 'nodegit';
import os from 'os';
import path from 'path';
import { parseLocationAnnotation } from '../helpers';
@@ -46,6 +45,7 @@ export class GitlabPreparer implements PreparerBase {
opts: PreparerOptions,
): Promise<string> {
const { protocol, location } = parseLocationAnnotation(template);
const { logger } = opts;
const workingDirectory = opts?.workingDirectory ?? os.tmpdir();
if (!['gitlab', 'gitlab/api', 'url'].includes(protocol)) {
@@ -67,17 +67,18 @@ export class GitlabPreparer implements PreparerBase {
);
const token = this.getToken(parsedGitLocation.resource);
const options = token
? {
fetchOpts: {
callbacks: {
credentials: () => Cred.userpassPlaintextNew('oauth2', token),
},
},
}
: {};
const git = token
? Git.fromAuth({
password: token,
username: 'oauth2',
logger,
})
: Git.fromAuth({ logger });
await Clone.clone(repositoryCheckoutUrl, tempDir, options);
await git.clone({
url: repositoryCheckoutUrl,
dir: tempDir,
});
return path.resolve(tempDir, templateDirectory);
}
@@ -19,6 +19,7 @@ import { Octokit } from '@octokit/rest';
import { Git } from '@backstage/backend-common';
import { JsonValue } from '@backstage/config';
import { RequiredTemplateValues } from '../templater';
import globby from 'globby';
export type RepoVisibilityOptions = 'private' | 'internal' | 'public';
@@ -55,6 +56,27 @@ export class GithubPublisher implements PublisherBase {
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,