diff --git a/plugins/scaffolder-node/src/actions/fetch.test.ts b/plugins/scaffolder-node/src/actions/fetch.test.ts index 55b949fca7..42f75d02b4 100644 --- a/plugins/scaffolder-node/src/actions/fetch.test.ts +++ b/plugins/scaffolder-node/src/actions/fetch.test.ts @@ -124,6 +124,20 @@ describe('fetchContents helper', () => { expect(fs.ensureDir).toHaveBeenCalledWith('foo'); expect(dirFunction).toHaveBeenCalledWith({ targetDir: 'foo' }); }); + + it('should pass through the token provided through to the URL reader', async () => { + await fetchContents({ + ...options, + outputPath: 'mydir/foo', + fetchUrl: 'https://github.com/backstage/foo', + token: 'mockToken', + }); + + expect(readTree).toHaveBeenCalledWith( + 'https://github.com/backstage/foo', + expect.objectContaining({ token: 'mockToken' }), + ); + }); }); describe('fetch file', () => { @@ -211,5 +225,19 @@ describe('fetchContents helper', () => { expect(fs.ensureDir).toHaveBeenCalledWith('mydir'); expect(fs.outputFile).toHaveBeenCalledWith('mydir/foo', 'test'); }); + + it('should pass through the token provided through to the URL reader', async () => { + await fetchFile({ + ...options, + outputPath: 'mydir/foo', + fetchUrl: 'https://github.com/backstage/foo', + token: 'mockToken', + }); + + expect(readUrl).toHaveBeenCalledWith( + 'https://github.com/backstage/foo', + expect.objectContaining({ token: 'mockToken' }), + ); + }); }); }); diff --git a/plugins/scaffolder-node/src/actions/fetch.ts b/plugins/scaffolder-node/src/actions/fetch.ts index 66bbc0a842..2ddc2b1ae6 100644 --- a/plugins/scaffolder-node/src/actions/fetch.ts +++ b/plugins/scaffolder-node/src/actions/fetch.ts @@ -32,8 +32,16 @@ export async function fetchContents(options: { baseUrl?: string; fetchUrl?: string; outputPath: string; + token?: string; }) { - const { reader, integrations, baseUrl, fetchUrl = '.', outputPath } = options; + const { + reader, + integrations, + baseUrl, + fetchUrl = '.', + outputPath, + token, + } = options; const fetchUrlIsAbsolute = isFetchUrlAbsolute(fetchUrl); @@ -45,7 +53,7 @@ export async function fetchContents(options: { } else { const readUrl = getReadUrl(fetchUrl, baseUrl, integrations); - const res = await reader.readTree(readUrl); + const res = await reader.readTree(readUrl, { token }); await fs.ensureDir(outputPath); await res.dir({ targetDir: outputPath }); } @@ -63,8 +71,16 @@ export async function fetchFile(options: { baseUrl?: string; fetchUrl?: string; outputPath: string; + token?: string; }) { - const { reader, integrations, baseUrl, fetchUrl = '.', outputPath } = options; + const { + reader, + integrations, + baseUrl, + fetchUrl = '.', + outputPath, + token, + } = options; const fetchUrlIsAbsolute = isFetchUrlAbsolute(fetchUrl); @@ -76,7 +92,7 @@ export async function fetchFile(options: { } else { const readUrl = getReadUrl(fetchUrl, baseUrl, integrations); - const res = await reader.readUrl(readUrl); + const res = await reader.readUrl(readUrl, { token }); await fs.ensureDir(path.dirname(outputPath)); const buffer = await res.buffer(); await fs.outputFile(outputPath, buffer.toString());