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

fix(fetch): stricter source URL type-checking #818

Merged
merged 2 commits into from
Jun 5, 2023
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
24 changes: 24 additions & 0 deletions __tests__/lib/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ describe('#fetch()', () => {
mock.done();
});

it('should omit source URL header if URL is invalid', async () => {
const key = 'API_KEY';
delete process.env.GITHUB_SERVER_URL;

const mock = getAPIMock()
.get('/api/v1')
.basicAuth({ user: key })
.reply(200, function () {
return this.req.headers;
});

const headers = await readmeAPIFetch(
'/api/v1',
{
method: 'get',
headers: cleanHeaders(key),
},
{ filePath: './📈 Dashboard & Metrics/openapi.json', fileType: 'path' }
).then(handleRes);

expect(headers['x-readme-source-url']).toBeUndefined();
mock.done();
});

it('should include source URL header with relative path', async () => {
const key = 'API_KEY';

Expand Down
12 changes: 7 additions & 5 deletions src/lib/readmeAPIFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ export default async function readmeAPIFetch(
* @see {@link https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables}
* @example https://github.com/readmeio/rdme/blob/cb4129d5c7b51ff3b50f933a9c7d0c3d0d33d62c/documentation/rdme.md
*/
headers.set(
'x-readme-source-url',
encodeURI(
try {
const sourceUrl = new URL(
`${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/blob/${process.env.GITHUB_SHA}/${filePath}`
)
);
).href;
headers.set('x-readme-source-url', sourceUrl);
} catch (e) {
debug(`error constructing github source url: ${e.message}`);
}
}
}

Expand Down