Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(http/github): add utility function to fetch raw files #30155

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions lib/util/http/github.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'node:buffer';
import { codeBlock } from 'common-tags';
import { DateTime } from 'luxon';
import * as httpMock from '../../../test/http-mock';
Expand Down Expand Up @@ -786,4 +787,166 @@ describe('util/http/github', () => {
).rejects.toThrow(EXTERNAL_HOST_ERROR);
});
});

describe('getRawFile()', () => {
it('add header and return', async () => {
httpMock
.scope(githubApiHost)
.get('/foo/bar/contents/lore/ipsum.txt')
.matchHeader(
'accept',
'application/vnd.github.raw+json, application/vnd.github.v3+json',
)
.reply(200, 'foo');
await expect(
githubApi.getRawTextFile(
`${githubApiHost}/foo/bar/contents/lore/ipsum.txt`,
secustor marked this conversation as resolved.
Show resolved Hide resolved
),
).resolves.toMatchObject({
body: 'foo',
});
});

it('support relative path', async () => {
httpMock
.scope(githubApiHost)
.get('/foo/bar/contents/lore/ipsum.txt')
.matchHeader(
'accept',
'application/vnd.github.raw+json, application/vnd.github.v3+json',
)
.reply(200, 'foo');
await expect(
githubApi.getRawTextFile(
`${githubApiHost}/foo/bar/contents/foo/../lore/ipsum.txt`,
secustor marked this conversation as resolved.
Show resolved Hide resolved
),
).resolves.toMatchObject({
body: 'foo',
});
});

it('support default to api.github.com if no baseURL has been supplied', async () => {
httpMock
.scope(githubApiHost)
.get('/foo/bar/contents/lore/ipsum.txt')
.matchHeader(
'accept',
'application/vnd.github.raw+json, application/vnd.github.v3+json',
)
.reply(200, 'foo');
await expect(
githubApi.getRawTextFile(`foo/bar/contents/lore/ipsum.txt`),
).resolves.toMatchObject({
body: 'foo',
});
});

it('support custom host if a baseURL has been supplied', async () => {
const customApiHost = 'https://my.comapny.com/api/v3/';
httpMock
.scope(customApiHost)
.get('/foo/bar/contents/lore/ipsum.txt')
.matchHeader(
'accept',
'application/vnd.github.raw+json, application/vnd.github.v3+json',
)
.reply(200, 'foo');
await expect(
githubApi.getRawTextFile(`foo/bar/contents/lore/ipsum.txt`, {
baseUrl: customApiHost,
}),
).resolves.toMatchObject({
body: 'foo',
});
});

it('support default to api.github.com if no baseURL, but repository has been supplied', async () => {
httpMock
.scope(githubApiHost)
.get('/foo/bar/contents/lore/ipsum.txt')
.matchHeader(
'accept',
'application/vnd.github.raw+json, application/vnd.github.v3+json',
)
.reply(200, 'foo');
await expect(
githubApi.getRawTextFile(`lore/ipsum.txt`, {
repository: 'foo/bar',
}),
).resolves.toMatchObject({
body: 'foo',
});
});

it('support custom host if a baseURL and repository has been supplied', async () => {
const customApiHost = 'https://my.comapny.com/api/v3/';
httpMock
.scope(customApiHost)
.get('/foo/bar/contents/lore/ipsum.txt')
.matchHeader(
'accept',
'application/vnd.github.raw+json, application/vnd.github.v3+json',
)
.reply(200, 'foo');
await expect(
githubApi.getRawTextFile(`lore/ipsum.txt`, {
baseUrl: customApiHost,
repository: 'foo/bar',
}),
).resolves.toMatchObject({
body: 'foo',
});
});

it('support default to api.github.com if content path is used', async () => {
httpMock
.scope(githubApiHost)
.get('/foo/bar/contents/lore/ipsum.txt')
.matchHeader(
'accept',
'application/vnd.github.raw+json, application/vnd.github.v3+json',
)
.reply(200, 'foo');
await expect(
githubApi.getRawTextFile(`foo/bar/contents/lore/ipsum.txt`),
).resolves.toMatchObject({
body: 'foo',
});
});

it('support custom host if content path is used', async () => {
const customApiHost = 'https://my.comapny.com/api/v3/';
httpMock
.scope(customApiHost)
.get('/foo/bar/contents/lore/ipsum.txt')
.matchHeader(
'accept',
'application/vnd.github.raw+json, application/vnd.github.v3+json',
)
.reply(200, 'test');
await expect(
githubApi.getRawTextFile(`foo/bar/contents/lore/ipsum.txt`, {
baseUrl: customApiHost,
}),
).resolves.toMatchObject({
body: 'test',
});
});

it('throw error if a ', async () => {
httpMock
.scope(githubApiHost)
.get('/foo/bar/contents/lore/ipsum.bin')
.matchHeader(
'accept',
'application/vnd.github.raw+json, application/vnd.github.v3+json',
)
.reply(200, Buffer.from('foo', 'binary'));
await expect(
githubApi.getRawTextFile(`foo/bar/contents/lore/ipsum.bin`, {
responseType: 'buffer',
}),
).rejects.toThrow();
});
});
});
37 changes: 37 additions & 0 deletions lib/util/http/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,41 @@ export class GithubHttp extends Http<GithubHttpOptions> {

return result;
}

/**
* Get the raw text file from a URL.
* Only use this method to fetch text files.
*
* @param url Full API URL, contents path or path inside the repository to the file
* @param options
*
* @example url = 'https://api.github.com/repos/renovatebot/renovate/contents/package.json'
* @example url = 'renovatebot/renovate/contents/package.json'
* @example url = 'package.json' & options.repository = 'renovatebot/renovate'
*/
public async getRawTextFile(
url: string,
options: InternalHttpOptions & GithubHttpOptions = {},
): Promise<HttpResponse> {
const newOptions: InternalHttpOptions & GithubHttpOptions = {
...options,
headers: {
accept: 'application/vnd.github.raw+json',
},
};

let newURL = url;
const httpRegex = regEx(/^https?:\/\//);
if (options.repository && !httpRegex.test(options.repository)) {
newURL = joinUrlParts(options.repository, 'contents', url);
}
viceice marked this conversation as resolved.
Show resolved Hide resolved

const result = await this.get(newURL, newOptions);
if (!is.string(result.body)) {
throw new Error(
`Expected raw text file but received ${typeof result.body}`,
);
}
return result;
}
}