backstage-common: implement readUrl for GitHub UrlReader

Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-06-30 17:08:32 +02:00
parent 7b1dd36f78
commit 80177048d2
@@ -35,6 +35,8 @@ import {
SearchResponse,
SearchResponseFile,
UrlReader,
ReadUrlOptions,
ReadUrlResponse,
} from './types';
export type GhRepoResponse = RestEndpointMethodTypes['repos']['get']['response']['data'];
@@ -77,6 +79,14 @@ export class GithubUrlReader implements UrlReader {
}
async read(url: string): Promise<Buffer> {
const response = await this.readUrl(url);
return response.buffer();
}
async readUrl(
url: string,
options?: ReadUrlOptions,
): Promise<ReadUrlResponse> {
const ghUrl = getGitHubFileFetchUrl(url, this.integration.config);
const { headers } = await this.deps.credentialsProvider.getCredentials({
url,
@@ -86,6 +96,7 @@ export class GithubUrlReader implements UrlReader {
response = await fetch(ghUrl.toString(), {
headers: {
...headers,
...(options?.etag && { 'If-None-Match': options.etag }),
Accept: 'application/vnd.github.v3.raw',
},
});
@@ -93,8 +104,12 @@ export class GithubUrlReader implements UrlReader {
throw new Error(`Unable to read ${url}, ${e}`);
}
if (response.status === 304) {
throw new NotModifiedError();
}
if (response.ok) {
return Buffer.from(await response.text());
return { buffer: async () => Buffer.from(await response.text()) };
}
const message = `${url} could not be read as ${ghUrl}, ${response.status} ${response.statusText}`;