Add authentication to GitHub preparer

This commit is contained in:
Taras Mankovski
2020-10-08 07:27:49 -04:00
parent 55602200a3
commit 4df660e37f
2 changed files with 20 additions and 4 deletions
+5 -2
View File
@@ -47,13 +47,12 @@ export default async function createPlugin({
templaters.register('cra', craTemplater);
const filePreparer = new FilePreparer();
const githubPreparer = new GithubPreparer();
const gitlabPreparer = new GitlabPreparer(config);
const azurePreparer = new AzurePreparer(config);
const preparers = new Preparers();
preparers.register('file', filePreparer);
preparers.register('github', githubPreparer);
preparers.register('gitlab', gitlabPreparer);
preparers.register('gitlab/api', gitlabPreparer);
preparers.register('azure/api', azurePreparer);
@@ -75,6 +74,10 @@ export default async function createPlugin({
token: githubToken,
repoVisibility,
});
const githubPreparer = new GithubPreparer({ token: githubToken });
preparers.register('github', githubPreparer);
publishers.register('file', githubPublisher);
publishers.register('github', githubPublisher);
} catch (e) {
@@ -21,11 +21,18 @@ import { parseLocationAnnotation } from '../helpers';
import { InputError } from '@backstage/backend-common';
import { PreparerBase } from './types';
import GitUriParser from 'git-url-parse';
import { Clone } from 'nodegit';
import { Clone, Cred } from 'nodegit';
export class GithubPreparer implements PreparerBase {
token: string;
constructor(params: { token: string }) {
this.token = params.token;
}
async prepare(template: TemplateEntityV1alpha1): Promise<string> {
const { protocol, location } = parseLocationAnnotation(template);
const { token } = this;
if (protocol !== 'github') {
throw new InputError(
@@ -46,7 +53,13 @@ export class GithubPreparer implements PreparerBase {
);
await Clone.clone(repositoryCheckoutUrl, tempDir, {
// TODO(blam): Maybe need some auth here?
fetchOpts: {
callbacks: {
credentials() {
return Cred.userpassPlaintextNew(token, 'x-oauth-basic');
},
},
},
});
return path.resolve(tempDir, templateDirectory);