Merge pull request #2799 from taras/scaffolder-add-github-auth-to-preparer
fix: Add authentication to GitHub preparer
This commit is contained in:
@@ -90,7 +90,6 @@ describe('GitHubPreparer', () => {
|
||||
{},
|
||||
);
|
||||
});
|
||||
|
||||
it('return the temp directory with the path to the folder if it is specified', async () => {
|
||||
const preparer = new GithubPreparer();
|
||||
mockEntity.spec.path = './template/test/1/2/3';
|
||||
@@ -100,4 +99,20 @@ describe('GitHubPreparer', () => {
|
||||
/\/template\/test\/1\/2\/3$/,
|
||||
);
|
||||
});
|
||||
it('calls the clone command with the token when provided', async () => {
|
||||
const preparer = new GithubPreparer({ token: 'abc' });
|
||||
await preparer.prepare(mockEntity);
|
||||
expect(mocks.Clone.clone).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'https://github.com/benjdlambert/backstage-graphql-template',
|
||||
expect.any(String),
|
||||
{
|
||||
fetchOpts: {
|
||||
callbacks: {
|
||||
credentials: expect.any(Function),
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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(
|
||||
@@ -45,9 +52,19 @@ export class GithubPreparer implements PreparerBase {
|
||||
template.spec.path ?? '.',
|
||||
);
|
||||
|
||||
await Clone.clone(repositoryCheckoutUrl, tempDir, {
|
||||
// TODO(blam): Maybe need some auth here?
|
||||
});
|
||||
const cloneOptions = token
|
||||
? {
|
||||
fetchOpts: {
|
||||
callbacks: {
|
||||
credentials() {
|
||||
return Cred.userpassPlaintextNew(token, 'x-oauth-basic');
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
: {};
|
||||
|
||||
await Clone.clone(repositoryCheckoutUrl, tempDir, cloneOptions);
|
||||
|
||||
return path.resolve(tempDir, templateDirectory);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user