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

Migrate from docs.rs' builds API to status API #6900

Merged
merged 1 commit into from
Jul 31, 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
8 changes: 4 additions & 4 deletions app/models/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ export default class Version extends Model {
}
});

loadDocsBuildsTask = task(async () => {
return await ajax(`https://docs.rs/crate/${this.crateName}/${this.num}/builds.json`);
loadDocsStatusTask = task(async () => {
return await ajax(`https://docs.rs/crate/${this.crateName}/=${this.num}/status.json`);
Copy link
Member

@Turbo87 Turbo87 Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bildschirmfoto 2023-07-31 um 09 57 48

it looks like the URL with = just redirects to the same URL without the =. are you sure that this is necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright, I just had a quick look at the linked PR. looks like =1.2.3 will return 404 if the version does not exist and redirect to 1.2.3 if the version exists. then 1.2.3 will look for 1.2.3 and if that does not exist will try ^1.2.3, but since we know from the first call that it exists it won't interpret it as semver.

I'm not a huge fan of this API decision, but I guess we have to live with it now :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only redirects if that exact version is known about, without the = it does a semver match and will redirect to a different version if one is available. As an example https://docs.rs/crate/stylish-core/0.1.0/status.json redirects to https://docs.rs/crate/stylish-core/0.1.1/status.json (because of reasons around circular dependencies 0.1.0 was never added to the docs.rs database).

This is existing behavior for all the endpoints, including /builds.json, but that did not get CORS headers applied when it returned a redirect making it still get treated as an error, e.g. on https://crates.io/crates/stylish-core/0.1.0

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://docs.rs/crate/stylish-core/0.1.0/builds.json. (Reason: CORS request did not succeed). Status code: (null).

Since that is also fixed on this endpoint, it would link to the 0.1.1 docs without the =.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I understand that this is just legacy behavior we have to live with now. it's fine, was just a bit surprising to see that we need the extra ping-pong for exact version queries. maybe it would make sense for the JSON endpoints to return the content directly for =1.2.3 queries instead of redirecting?

});

get hasDocsRsLink() {
let docsBuilds = this.loadDocsBuildsTask.lastSuccessful?.value;
return docsBuilds?.[0]?.build_status === true;
let docsStatus = this.loadDocsStatusTask.lastSuccessful?.value;
return docsStatus?.doc_status === true;
}

get docsRsLink() {
Expand Down
2 changes: 1 addition & 1 deletion app/routes/crate/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default class VersionRoute extends Route {

let { crate, version } = model;
if (!crate.documentation || crate.documentation.startsWith('https://docs.rs/')) {
version.loadDocsBuildsTask.perform().catch(error => {
version.loadDocsStatusTask.perform().catch(error => {
// report unexpected errors to Sentry and ignore `ajax()` errors
if (!didCancel(error) && !(error instanceof AjaxError)) {
this.sentry.captureException(error);
Expand Down
4 changes: 2 additions & 2 deletions mirage/route-handlers/docs-rs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function register(server) {
server.get('https://docs.rs/crate/:crate/:version/builds.json', function () {
return [];
server.get('https://docs.rs/crate/:crate/:version/status.json', function () {
return {};
});
}
48 changes: 13 additions & 35 deletions tests/routes/crate/version/docs-link-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module('Route | crate.version | docs link', function (hooks) {
let crate = this.server.create('crate', { name: 'foo' });
this.server.create('version', { crate, num: '1.0.0' });

this.server.get('https://docs.rs/crate/:crate/:version/builds.json', []);
this.server.get('https://docs.rs/crate/:crate/:version/status.json', 'not found', 404);

await visit('/crates/foo');
assert.dom('[data-test-docs-link] a').doesNotExist();
Expand All @@ -28,16 +28,10 @@ module('Route | crate.version | docs link', function (hooks) {
let crate = this.server.create('crate', { name: 'foo' });
this.server.create('version', { crate, num: '1.0.0' });

this.server.get('https://docs.rs/crate/:crate/:version/builds.json', [
{
id: 42,
rustc_version: 'rustc 1.50.0-nightly (1c389ffef 2020-11-24)',
docsrs_version: 'docsrs 0.6.0 (31c864e 2020-11-22)',
build_status: true,
build_time: '2020-12-06T09:04:36.610302Z',
output: null,
},
]);
this.server.get('https://docs.rs/crate/:crate/:version/status.json', {
doc_status: true,
version: '1.0.0',
});

await visit('/crates/foo');
assert.dom('[data-test-docs-link] a').hasAttribute('href', 'https://docs.rs/foo/1.0.0');
Expand All @@ -47,7 +41,7 @@ module('Route | crate.version | docs link', function (hooks) {
let crate = this.server.create('crate', { name: 'foo', documentation: 'https://docs.rs/foo/0.6.2' });
this.server.create('version', { crate, num: '1.0.0' });

this.server.get('https://docs.rs/crate/:crate/:version/builds.json', []);
this.server.get('https://docs.rs/crate/:crate/:version/status.json', 'not found', 404);

await visit('/crates/foo');
assert.dom('[data-test-docs-link] a').hasAttribute('href', 'https://docs.rs/foo/0.6.2');
Expand All @@ -57,16 +51,10 @@ module('Route | crate.version | docs link', function (hooks) {
let crate = this.server.create('crate', { name: 'foo', documentation: 'https://docs.rs/foo/0.6.2' });
this.server.create('version', { crate, num: '1.0.0' });

this.server.get('https://docs.rs/crate/:crate/:version/builds.json', [
{
id: 42,
rustc_version: 'rustc 1.50.0-nightly (1c389ffef 2020-11-24)',
docsrs_version: 'docsrs 0.6.0 (31c864e 2020-11-22)',
build_status: true,
build_time: '2020-12-06T09:04:36.610302Z',
output: null,
},
]);
this.server.get('https://docs.rs/crate/:crate/:version/status.json', {
doc_status: true,
version: '1.0.0',
});

await visit('/crates/foo');
assert.dom('[data-test-docs-link] a').hasAttribute('href', 'https://docs.rs/foo/1.0.0');
Expand All @@ -76,27 +64,17 @@ module('Route | crate.version | docs link', function (hooks) {
let crate = this.server.create('crate', { name: 'foo', documentation: 'https://docs.rs/foo/0.6.2' });
this.server.create('version', { crate, num: '1.0.0' });

this.server.get('https://docs.rs/crate/:crate/:version/builds.json', {}, 500);
this.server.get('https://docs.rs/crate/:crate/:version/status.json', 'error', 500);

await visit('/crates/foo');
assert.dom('[data-test-docs-link] a').hasAttribute('href', 'https://docs.rs/foo/0.6.2');
});

test('null builds in docs.rs responses are ignored', async function (assert) {
test('empty docs.rs responses are ignored', async function (assert) {
let crate = this.server.create('crate', { name: 'foo', documentation: 'https://docs.rs/foo/0.6.2' });
this.server.create('version', { crate, num: '0.6.2' });

this.server.get('https://docs.rs/crate/:crate/:version/builds.json', [null]);

await visit('/crates/foo');
assert.dom('[data-test-docs-link] a').hasAttribute('href', 'https://docs.rs/foo/0.6.2');
});

test('empty arrays in docs.rs responses are ignored', async function (assert) {
let crate = this.server.create('crate', { name: 'foo', documentation: 'https://docs.rs/foo/0.6.2' });
this.server.create('version', { crate, num: '0.6.2' });

this.server.get('https://docs.rs/crate/:crate/:version/builds.json', []);
this.server.get('https://docs.rs/crate/:crate/:version/status.json', {});

await visit('/crates/foo');
assert.dom('[data-test-docs-link] a').hasAttribute('href', 'https://docs.rs/foo/0.6.2');
Expand Down