chore: support overriding the token

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-01-25 20:14:40 +01:00
parent 78c100bc60
commit e9987a467d
2 changed files with 87 additions and 6 deletions
@@ -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',
@@ -92,13 +92,31 @@ export class GithubUrlReader implements UrlReader {
return response.buffer();
}
private getCredentials = async (
url: string,
options?: { token?: string },
): Promise<GithubCredentials> => {
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<ReadUrlResponse> {
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,