fix(GithubUrlReader): use token from options to fetch repoDetails

Signed-off-by: Adam Letizia <LetiziaAdam@JohnDeere.com>
This commit is contained in:
Adam Letizia
2025-05-01 10:46:01 -05:00
parent ee613ecd90
commit 0e7a640f98
3 changed files with 58 additions and 24 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-defaults': patch
---
The `GithubUrlReader` will now use the token from `options` when fetching repo details
@@ -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<GhCombinedCommitStatusResponse>;
// Tarballs
beforeEach(() => {
worker.use(
@@ -773,21 +802,6 @@ describe('GithubUrlReader', () => {
},
} as Partial<GhCombinedCommitStatusResponse>;
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<GhCombinedCommitStatusResponse>;
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) => {
@@ -154,7 +154,7 @@ export class GithubUrlReader implements UrlReaderService {
url: string,
options?: UrlReaderServiceReadTreeOptions,
): Promise<UrlReaderServiceReadTreeResponse> {
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(