Skip to content

Commit

Permalink
fix: better approach to normalizing file path
Browse files Browse the repository at this point in the history
feedback: #735 (comment)
  • Loading branch information
kanadgupta committed Feb 1, 2023
1 parent 05d5f0c commit 3093bca
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
42 changes: 42 additions & 0 deletions __tests__/cmds/openapi/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,48 @@ describe('rdme openapi', () => {
exampleMock.done();
return mock.done();
});

it('should contain request header with correct URL with working directory', async () => {
const registryUUID = getRandomRegistryId();
const mock = getAPIMock()
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version: '1.0.0' })
.post('/api/v1/api-registry')
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const getMock = getAPIMockWithVersionHeader(version)
.get('/api/v1/api-specification')
.basicAuth({ user: key })
.reply(200, []);

const postMock = getAPIMock({
'x-rdme-ci': 'GitHub Actions (test)',
'x-readme-source': 'cli-gh',
'x-readme-source-url':
'https://github.com/octocat/Hello-World/blob/ffac537e6cbbf934b08745a378932722df287a53/__tests__/__fixtures__/relative-ref-oas/petstore.json',
'x-readme-version': version,
})
.post('/api/v1/api-specification', { registryUUID })
.basicAuth({ user: key })
.reply(201, { _id: 1 }, { location: exampleRefLocation });

const spec = 'petstore.json';

await expect(
openapi.run({
spec,
key,
version,
workingDirectory: './__tests__/__fixtures__/relative-ref-oas',
})
).resolves.toBe(successfulUpload(spec));

getMock.done();
postMock.done();
mock.done();
return after();
});
});
});

Expand Down
24 changes: 16 additions & 8 deletions src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import nodeFetch, { Headers } from 'node-fetch';
import pkg from '../../package.json';

import APIError from './apiError';
import { git } from './createGHA';
import isCI, { ciName, isGHA } from './isCI';
import { debug, warn } from './logger';

Expand Down Expand Up @@ -99,11 +100,18 @@ function getUserAgent() {
}

/**
* Resolves relative path references for local paths,
* Creates a relative path for the file from the root of the repo,
* otherwise returns the path
*/
function normalizeFilePath(opts: FilePathDetails) {
if (opts.fileType === 'path') return path.relative('', opts.filePath);
async function normalizeFilePath(opts: FilePathDetails) {
if (opts.fileType === 'path') {
const repoRoot = await git.revparse(['--show-toplevel']).catch(e => {

This comment has been minimized.

Copy link
@erunion

erunion Feb 1, 2023

Member

:nice-rgb:

$ git rev-parse --show-toplevel
/Users/erunion/code/readme/readme
debug(`[fetch] error grabbing git root: ${e.message}`);
return '';
});

return path.relative(repoRoot, opts.filePath);
}
return opts.filePath;
}

Expand All @@ -122,7 +130,7 @@ function sanitizeHeaders(headers: Headers) {
* @param filePath local path for the file that's being sent. We use this to construct
* a full URL that points to the file in version control systems.
*/
export default function fetch(
export default async function fetch(
url: string,
options: RequestInit = { headers: new Headers() },
fileOpts: FilePathDetails = { filePath: '', fileType: false }
Expand All @@ -134,8 +142,6 @@ export default function fetch(
headers = new Headers(options.headers);
}

const filePath = normalizeFilePath(fileOpts);

headers.set('User-Agent', getUserAgent());

if (isGHA()) {
Expand All @@ -146,6 +152,8 @@ export default function fetch(
headers.set('x-github-run-number', process.env.GITHUB_RUN_NUMBER);
headers.set('x-github-sha', process.env.GITHUB_SHA);

const filePath = await normalizeFilePath(fileOpts);

if (filePath) {
/**
* Constructs a full URL to the file using GitHub Actions runner variables
Expand All @@ -165,8 +173,8 @@ export default function fetch(

headers.set('x-readme-source', source);

if (filePath && fileOpts.fileType === 'url') {
headers.set('x-readme-source-url', filePath);
if (fileOpts.filePath && fileOpts.fileType === 'url') {
headers.set('x-readme-source-url', fileOpts.filePath);
}

const fullUrl = `${getProxy()}${url}`;
Expand Down

0 comments on commit 3093bca

Please sign in to comment.