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

feat: add support for github blob URLs #484

Merged
merged 2 commits into from
Jul 12, 2022
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
23 changes: 19 additions & 4 deletions packages/api/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ export default class Fetcher {

constructor(uri: string | OASDocument) {
if (typeof uri === 'string') {
// Resolve OpenAPI definition shorthand accessors from within the ReadMe API Registry.
this.uri = Fetcher.isAPIRegistryUUID(uri)
? uri.replace(Fetcher.registryUUIDRegex, 'https://dash.readme.com/api/v1/api-registry/$4')
: uri;
if (Fetcher.isAPIRegistryUUID(uri)) {
// Resolve OpenAPI definition shorthand accessors from within the ReadMe API Registry.
this.uri = uri.replace(Fetcher.registryUUIDRegex, 'https://dash.readme.com/api/v1/api-registry/$4');
} else if (Fetcher.isGitHubBlobURL(uri)) {
/**
* People may try to use a public repository URL to the source viewer on GitHub not knowing
* that this page actually serves HTML. In this case we want to rewrite these to the "raw"
* version of this page that'll allow us to access the API definition.
*
* @example https://github.com/readmeio/oas-examples/blob/main/3.1/json/petstore.json
*/
this.uri = uri.replace(/\/\/github.com/, '//raw.github.com').replace(/\/blob\//, '/');
} else {
this.uri = uri;
}
} else {
this.uri = uri;
}
Expand All @@ -31,6 +42,10 @@ export default class Fetcher {
return Fetcher.registryUUIDRegex.test(uri);
}

static isGitHubBlobURL(uri: string) {
return /\/\/github.com\/[-_a-zA-Z0-9]+\/[-_a-zA-Z0-9]+\/blob\/(.*).(yaml|json|yml)/.test(uri);
}

static getProjectPrefixFromRegistryUUID(uri: string) {
const matches = uri.match(Fetcher.registryUUIDRegex);
if (!matches) {
Expand Down
31 changes: 31 additions & 0 deletions packages/api/test/fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ describe('fetcher', function () {
});
});

describe('#isGitHubBlobURL', function () {
it('should detect GitHub blob URLs', function () {
expect(Fetcher.isGitHubBlobURL('https://github.com/readmeio/oas-examples/blob/main/3.1/json/petstore.json')).to.be
.true;
});

it('should detect a schemeless GitHub blob URL as one', function () {
expect(Fetcher.isGitHubBlobURL('//github.com/readmeio/oas-examples/blob/main/3.1/json/petstore.json')).to.be.true;
});

it("shouldn't detect raw GitHub URLs as a blob URL", function () {
expect(
Fetcher.isGitHubBlobURL('https://raw.github.com/readmeio/oas-examples/main/3.1/json/petstore.json')
).to.be.false;
});
});

describe('#getProjectPrefixFromRegistryUUID', function () {
it('should retrieve the project prefix from the shorthand `@petstore/v1.0#uuid` syntax', function () {
expect(Fetcher.getProjectPrefixFromRegistryUUID('@petstore/v1.0#n6kvf10vakpemvplx')).to.equal('petstore');
Expand Down Expand Up @@ -78,6 +95,20 @@ describe('fetcher', function () {
});
});

describe('GitHub URLs', function () {
it('should resolve a GitHub blob URL to its accessible raw counterpart', function () {
expect(new Fetcher('https://github.com/readmeio/oas-examples/blob/main/3.1/json/petstore.json').uri).to.equal(
'https://raw.github.com/readmeio/oas-examples/main/3.1/json/petstore.json'
);
});

it('should leave an already raw GitHub URL alone', function () {
expect(
new Fetcher('https://raw.github.com/readmeio/oas-examples/main/3.1/json/petstore.json').uri
).to.equal('https://raw.github.com/readmeio/oas-examples/main/3.1/json/petstore.json');
});
});

describe('ReadMe registry UUID', function () {
it('should resolve the shorthand `@petstore/v1.0#uuid` syntax to the ReadMe API', function () {
expect(new Fetcher('@petstore/v1.0#n6kvf10vakpemvplx').uri).to.equal(
Expand Down