From e9987a467dbec75c06e077541356a7d6422b5875 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 25 Jan 2024 20:14:40 +0100 Subject: [PATCH] chore: support overriding the token Signed-off-by: blam --- .../src/reading/GithubUrlReader.test.ts | 65 +++++++++++++++++++ .../src/reading/GithubUrlReader.ts | 28 ++++++-- 2 files changed, 87 insertions(+), 6 deletions(-) diff --git a/packages/backend-common/src/reading/GithubUrlReader.test.ts b/packages/backend-common/src/reading/GithubUrlReader.test.ts index 5e6d30f519..ec775183b3 100644 --- a/packages/backend-common/src/reading/GithubUrlReader.test.ts +++ b/packages/backend-common/src/reading/GithubUrlReader.test.ts @@ -271,6 +271,33 @@ describe('GithubUrlReader', () => { expect(response.etag).toBe('foo'); expect(response.lastModifiedAt).toEqual(new Date('2021-01-01T00:00:00Z')); }); + + it('should override the token if its provided', async () => { + expect.assertions(1); + + (mockCredentialsProvider.getCredentials as jest.Mock).mockResolvedValue({ + headers: { + Authorization: 'bearer blah', + }, + }); + + worker.use( + rest.get( + 'https://ghe.github.com/api/v3/repos/backstage/mock/tree/contents/', + (req, res, ctx) => { + expect(req.headers.get('authorization')).toBe( + 'Bearer overridentoken', + ); + return res(ctx.status(200)); + }, + ), + ); + + await gheProcessor.readUrl( + 'https://github.com/backstage/mock/tree/blob/main', + { token: 'overridentoken' }, + ); + }); }); /* @@ -463,6 +490,44 @@ describe('GithubUrlReader', () => { ); }); + it('should override the token when provided', async () => { + expect.assertions(1); + + const mockHeaders = { + Authorization: 'bearer blah', + }; + + (mockCredentialsProvider.getCredentials as jest.Mock).mockResolvedValue({ + headers: mockHeaders, + }); + + worker.use( + rest.get( + 'https://ghe.github.com/api/v3/repos/backstage/mock/tarball/etag123abc', + (req, res, ctx) => { + expect(req.headers.get('authorization')).toBe( + 'Bearer overridentoken', + ); + + return res( + ctx.status(200), + ctx.set('Content-Type', 'application/x-gzip'), + ctx.set( + 'content-disposition', + 'attachment; filename=backstage-mock-etag123.tar.gz', + ), + ctx.body(repoBuffer), + ); + }, + ), + ); + + await gheProcessor.readTree( + 'https://ghe.github.com/backstage/mock/tree/main', + { token: 'overridentoken' }, + ); + }); + it('includes the subdomain in the github url', async () => { const response = await gheProcessor.readTree( 'https://ghe.github.com/backstage/mock/tree/main/docs', diff --git a/packages/backend-common/src/reading/GithubUrlReader.ts b/packages/backend-common/src/reading/GithubUrlReader.ts index 462821a20f..b476435771 100644 --- a/packages/backend-common/src/reading/GithubUrlReader.ts +++ b/packages/backend-common/src/reading/GithubUrlReader.ts @@ -92,13 +92,31 @@ export class GithubUrlReader implements UrlReader { return response.buffer(); } + private getCredentials = async ( + url: string, + options?: { token?: string }, + ): Promise => { + if (options?.token) { + return { + headers: { + Authorization: `Bearer ${options.token}`, + }, + type: 'token', + token: options.token, + }; + } + + return await this.deps.credentialsProvider.getCredentials({ + url, + }); + }; + async readUrl( url: string, options?: ReadUrlOptions, ): Promise { - const credentials = await this.deps.credentialsProvider.getCredentials({ - url, - }); + const credentials = await this.getCredentials(url, options); + const ghUrl = getGithubFileFetchUrl( url, this.integration.config, @@ -141,9 +159,7 @@ export class GithubUrlReader implements UrlReader { } const { filepath } = parseGitUrl(url); - const { headers } = await this.deps.credentialsProvider.getCredentials({ - url, - }); + const { headers } = await this.getCredentials(url, options); return this.doReadTree( repoDetails.repo.archive_url,