diff --git a/.changeset/warm-cases-bathe.md b/.changeset/warm-cases-bathe.md new file mode 100644 index 0000000000..21e9ceb098 --- /dev/null +++ b/.changeset/warm-cases-bathe.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-defaults': patch +--- + +The `GithubUrlReader` will now use the token from `options` when fetching repo details diff --git a/packages/backend-defaults/src/entrypoints/urlReader/lib/GithubUrlReader.test.ts b/packages/backend-defaults/src/entrypoints/urlReader/lib/GithubUrlReader.test.ts index 70b18df091..4e1c5a2d08 100644 --- a/packages/backend-defaults/src/entrypoints/urlReader/lib/GithubUrlReader.test.ts +++ b/packages/backend-defaults/src/entrypoints/urlReader/lib/GithubUrlReader.test.ts @@ -492,7 +492,7 @@ describe('GithubUrlReader', () => { }); it('should override the token when provided', async () => { - expect.assertions(1); + expect.assertions(2); const mockHeaders = { Authorization: 'bearer blah', @@ -503,6 +503,20 @@ describe('GithubUrlReader', () => { }); worker.use( + rest.get( + 'https://ghe.github.com/api/v3/repos/backstage/mock/commits/main/status', + (req, res, ctx) => { + expect(req.headers.get('authorization')).toBe( + 'Bearer overridentoken', + ); + + return res( + ctx.status(200), + ctx.set('Content-Type', 'application/json'), + ctx.json(commitStatusGheResponse), + ); + }, + ), rest.get( 'https://ghe.github.com/api/v3/repos/backstage/mock/tarball/etag123abc', (req, res, ctx) => { @@ -678,6 +692,21 @@ describe('GithubUrlReader', () => { }, ]; + const gheCommitsResponse = { + sha: 'etag123abc', + repository: { + id: 123, + full_name: 'backstage/mock', + default_branch: 'main', + branches_url: + 'https://ghe.github.com/api/v3/repos/backstage/mock/branches{/branch}', + archive_url: + 'https://ghe.github.com/api/v3/repos/backstage/mock/{archive_format}{/ref}', + trees_url: + 'https://ghe.github.com/api/v3/repos/backstage/mock/git/trees{/sha}', + }, + } as Partial; + // Tarballs beforeEach(() => { worker.use( @@ -773,21 +802,6 @@ describe('GithubUrlReader', () => { }, } as Partial; - const gheResponse = { - sha: 'etag123abc', - repository: { - id: 123, - full_name: 'backstage/mock', - default_branch: 'main', - branches_url: - 'https://ghe.github.com/api/v3/repos/backstage/mock/branches{/branch}', - archive_url: - 'https://ghe.github.com/api/v3/repos/backstage/mock/{archive_format}{/ref}', - trees_url: - 'https://ghe.github.com/api/v3/repos/backstage/mock/git/trees{/sha}', - }, - } as Partial; - worker.use( rest.get( 'https://api.github.com/repos/backstage/mock/commits/main/status', @@ -810,7 +824,7 @@ describe('GithubUrlReader', () => { return res( ctx.status(200), ctx.set('Content-Type', 'application/json'), - ctx.json(gheResponse), + ctx.json(gheCommitsResponse), ); } @@ -969,9 +983,23 @@ describe('GithubUrlReader', () => { }); it('passes through a token for the search request', async () => { - expect.assertions(1); + expect.assertions(2); worker.use( + rest.get( + 'https://ghe.github.com/api/v3/repos/backstage/mock/commits/main/status', + (req, res, ctx) => { + expect(req.headers.get('authorization')).toBe( + 'Bearer overridentoken', + ); + + return res( + ctx.status(200), + ctx.set('Content-Type', 'application/json'), + ctx.json(gheCommitsResponse), + ); + }, + ), rest.get( 'https://ghe.github.com/api/v3/repos/backstage/mock/git/trees/etag123abc', (req, res, ctx) => { diff --git a/packages/backend-defaults/src/entrypoints/urlReader/lib/GithubUrlReader.ts b/packages/backend-defaults/src/entrypoints/urlReader/lib/GithubUrlReader.ts index d78475de0b..1dbade8477 100644 --- a/packages/backend-defaults/src/entrypoints/urlReader/lib/GithubUrlReader.ts +++ b/packages/backend-defaults/src/entrypoints/urlReader/lib/GithubUrlReader.ts @@ -154,7 +154,7 @@ export class GithubUrlReader implements UrlReaderService { url: string, options?: UrlReaderServiceReadTreeOptions, ): Promise { - const repoDetails = await this.getRepoDetails(url); + const repoDetails = await this.getRepoDetails(url, options); const commitSha = repoDetails.commitSha; if (options?.etag && options.etag === commitSha) { @@ -212,7 +212,7 @@ export class GithubUrlReader implements UrlReaderService { } } - const repoDetails = await this.getRepoDetails(url); + const repoDetails = await this.getRepoDetails(url, options); const commitSha = repoDetails.commitSha; if (options?.etag && options.etag === commitSha) { @@ -320,7 +320,10 @@ export class GithubUrlReader implements UrlReaderService { })); } - private async getRepoDetails(url: string): Promise<{ + private async getRepoDetails( + url: string, + options?: { token?: string }, + ): Promise<{ commitSha: string; repo: { archive_url: string; @@ -330,9 +333,7 @@ export class GithubUrlReader implements UrlReaderService { const parsed = parseGitUrl(url); const { ref, full_name } = parsed; - const credentials = await this.deps.credentialsProvider.getCredentials({ - url, - }); + const credentials = await this.getCredentials(url, options); const { headers } = credentials; const commitStatus: GhCombinedCommitStatusResponse = await this.fetchJson(