Skip to content

Commit 7f34172

Browse files
committed
models/version: Add purl property
1 parent 3425d43 commit 7f34172

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

app/models/version.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { alias } from 'macro-decorators';
99
import semverParse from 'semver/functions/parse';
1010

1111
import ajax from '../utils/ajax';
12+
import { addRegistryUrl } from '../utils/purl';
1213

1314
const EIGHT_DAYS = 8 * 24 * 60 * 60 * 1000;
1415

@@ -52,6 +53,15 @@ export default class Version extends Model {
5253
return this.belongsTo('crate').id();
5354
}
5455

56+
/**
57+
* Returns the Package URL (PURL) for this version.
58+
* @type {string}
59+
*/
60+
get purl() {
61+
let basePurl = `pkg:cargo/${this.crateName}@${this.num}`;
62+
return addRegistryUrl(basePurl);
63+
}
64+
5565
get editionMsrv() {
5666
if (this.edition === '2018') {
5767
return '1.31.0';

tests/models/version-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { module, test } from 'qunit';
22

33
import { calculateReleaseTracks } from '@crates-io/msw/utils/release-tracks';
4+
import window from 'ember-window-mock';
5+
import { setupWindowMock } from 'ember-window-mock/test-support';
46

57
import { setupTest } from 'crates-io/tests/helpers';
68
import setupMsw from 'crates-io/tests/helpers/setup-msw';
79

810
module('Model | Version', function (hooks) {
911
setupTest(hooks);
1012
setupMsw(hooks);
13+
setupWindowMock(hooks);
1114

1215
hooks.beforeEach(function () {
1316
this.store = this.owner.lookup('service:store');
@@ -345,4 +348,36 @@ module('Model | Version', function (hooks) {
345348
assert.ok(version.published_by);
346349
assert.strictEqual(version.published_by.name, 'JD');
347350
});
351+
352+
module('purl', function () {
353+
test('generates PURL for crates.io version', async function (assert) {
354+
let { db, store } = this;
355+
356+
window.location = 'https://crates.io';
357+
358+
let crate = db.crate.create({ name: 'serde' });
359+
db.version.create({ crate, num: '1.0.136' });
360+
361+
let crateRecord = await store.findRecord('crate', crate.name);
362+
let versions = (await crateRecord.versions).slice();
363+
let version = versions[0];
364+
365+
assert.strictEqual(version.purl, 'pkg:cargo/serde@1.0.136');
366+
});
367+
368+
test('generates PURL with registry URL for non-crates.io hosts', async function (assert) {
369+
let { db, store } = this;
370+
371+
window.location = 'https://staging.crates.io';
372+
373+
let crate = db.crate.create({ name: 'test-crate' });
374+
db.version.create({ crate, num: '2.5.0' });
375+
376+
let crateRecord = await store.findRecord('crate', crate.name);
377+
let versions = (await crateRecord.versions).slice();
378+
let version = versions[0];
379+
380+
assert.strictEqual(version.purl, 'pkg:cargo/test-crate@2.5.0?repository_url=https%3A%2F%2Fstaging.crates.io%2F');
381+
});
382+
});
348383
});

0 commit comments

Comments
 (0)