Skip to content

Commit 30e3760

Browse files
committed
Auto merge of #3252 - Turbo87:published-by, r=locks
models/version: Add `published_by` relationship For the more recent releases the crates.io backend now records who published the release and returns that information on the API. It would be useful to eventually show this data either in the versions list or on the version details page. This PR adds a corresponding `published_by` relationship to the `version` model to make this data generally available in the frontend.
2 parents 5431ded + 8d4c7fc commit 30e3760

File tree

7 files changed

+75
-4
lines changed

7 files changed

+75
-4
lines changed

app/models/version.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default class Version extends Model {
1919

2020
@belongsTo('crate', { async: false }) crate;
2121

22+
@belongsTo('user', { async: false }) published_by;
2223
@hasMany('users', { async: true }) authors;
2324
@hasMany('dependency', { async: true }) dependencies;
2425
@hasMany('version-download', { async: true }) version_downloads;

app/serializers/version.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { EmbeddedRecordsMixin } from '@ember-data/serializer/rest';
2+
3+
import ApplicationSerializer from './application';
4+
5+
export default ApplicationSerializer.extend(EmbeddedRecordsMixin, {
6+
attrs: {
7+
published_by: { embedded: 'always' },
8+
},
9+
});

mirage/models/version.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import { belongsTo, Model } from 'ember-cli-mirage';
22

33
export default Model.extend({
44
crate: belongsTo(),
5+
publishedBy: belongsTo('user'),
56
});

mirage/serializers/version.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable ember/avoid-leaking-state-in-ember-objects */
2+
13
import BaseSerializer from './application';
24

35
export default BaseSerializer.extend({
@@ -15,6 +17,8 @@ export default BaseSerializer.extend({
1517
'crate_size',
1618
],
1719

20+
include: ['publishedBy'],
21+
1822
links(version) {
1923
return {
2024
authors: `/api/v1/crates/${version.crateId}/${version.num}/authors`,
@@ -28,18 +32,29 @@ export default BaseSerializer.extend({
2832

2933
if (Array.isArray(hash)) {
3034
for (let resource of hash) {
31-
this._adjust(resource);
35+
this._adjust(resource, addToIncludes);
3236
}
3337
} else {
34-
this._adjust(hash);
38+
this._adjust(hash, addToIncludes);
3539
}
3640

41+
addToIncludes = addToIncludes.filter(it => it.modelName !== 'user');
42+
3743
return [hash, addToIncludes];
3844
},
3945

40-
_adjust(hash) {
46+
_adjust(hash, includes) {
4147
hash.dl_path = `/api/v1/crates/${hash.crate_id}/${hash.num}/download`;
4248
hash.crate = hash.crate_id;
49+
50+
if (hash.published_by_id) {
51+
let user = includes.find(it => it.modelName === 'user' && it.id === hash.published_by_id);
52+
hash.published_by = this.getHashForIncludedResource(user)[0].users[0];
53+
} else {
54+
hash.published_by = null;
55+
}
56+
4357
delete hash.crate_id;
58+
delete hash.published_by_id;
4459
},
4560
});

tests/mirage/crates-test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ module('Mirage | Crates', function (hooks) {
270270
version_downloads: '/api/v1/crates/rand/1.0.0-beta.1/downloads',
271271
},
272272
num: '1.0.0-beta.1',
273+
published_by: null,
273274
updated_at: '2017-02-24T12:34:56Z',
274275
yanked: false,
275276
},
@@ -303,6 +304,7 @@ module('Mirage | Crates', function (hooks) {
303304
version_downloads: '/api/v1/crates/rand/1.0.0/downloads',
304305
},
305306
num: '1.0.0',
307+
published_by: null,
306308
updated_at: '2017-02-24T12:34:56Z',
307309
yanked: false,
308310
},
@@ -320,6 +322,7 @@ module('Mirage | Crates', function (hooks) {
320322
version_downloads: '/api/v1/crates/rand/1.1.0/downloads',
321323
},
322324
num: '1.1.0',
325+
published_by: null,
323326
updated_at: '2017-02-24T12:34:56Z',
324327
yanked: false,
325328
},
@@ -337,6 +340,7 @@ module('Mirage | Crates', function (hooks) {
337340
version_downloads: '/api/v1/crates/rand/1.2.0/downloads',
338341
},
339342
num: '1.2.0',
343+
published_by: null,
340344
updated_at: '2017-02-24T12:34:56Z',
341345
yanked: false,
342346
},
@@ -540,9 +544,10 @@ module('Mirage | Crates', function (hooks) {
540544
});
541545

542546
test('returns all versions belonging to the specified crate', async function (assert) {
547+
let user = this.server.create('user');
543548
this.server.create('crate', { name: 'rand' });
544549
this.server.create('version', { crateId: 'rand', num: '1.0.0' });
545-
this.server.create('version', { crateId: 'rand', num: '1.1.0' });
550+
this.server.create('version', { crateId: 'rand', num: '1.1.0', publishedBy: user });
546551
this.server.create('version', { crateId: 'rand', num: '1.2.0' });
547552

548553
let response = await fetch('/api/v1/crates/rand/versions');
@@ -565,6 +570,7 @@ module('Mirage | Crates', function (hooks) {
565570
version_downloads: '/api/v1/crates/rand/1.0.0/downloads',
566571
},
567572
num: '1.0.0',
573+
published_by: null,
568574
updated_at: '2017-02-24T12:34:56Z',
569575
yanked: false,
570576
},
@@ -582,6 +588,13 @@ module('Mirage | Crates', function (hooks) {
582588
version_downloads: '/api/v1/crates/rand/1.1.0/downloads',
583589
},
584590
num: '1.1.0',
591+
published_by: {
592+
id: 1,
593+
avatar: 'https://avatars1.githubusercontent.com/u/14631425?v=4',
594+
login: 'user-1',
595+
name: 'User 1',
596+
url: 'https://github.com/user-1',
597+
},
585598
updated_at: '2017-02-24T12:34:56Z',
586599
yanked: false,
587600
},
@@ -599,6 +612,7 @@ module('Mirage | Crates', function (hooks) {
599612
version_downloads: '/api/v1/crates/rand/1.2.0/downloads',
600613
},
601614
num: '1.2.0',
615+
published_by: null,
602616
updated_at: '2017-02-24T12:34:56Z',
603617
yanked: false,
604618
},
@@ -995,6 +1009,7 @@ module('Mirage | Crates', function (hooks) {
9951009
version_downloads: '/api/v1/crates/bar/1.0.0/downloads',
9961010
},
9971011
num: '1.0.0',
1012+
published_by: null,
9981013
updated_at: '2017-02-24T12:34:56Z',
9991014
yanked: false,
10001015
},
@@ -1012,6 +1027,7 @@ module('Mirage | Crates', function (hooks) {
10121027
version_downloads: '/api/v1/crates/baz/1.0.1/downloads',
10131028
},
10141029
num: '1.0.1',
1030+
published_by: null,
10151031
updated_at: '2017-02-24T12:34:56Z',
10161032
yanked: false,
10171033
},

tests/mirage/me-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ module('Mirage | /me', function (hooks) {
241241
version_downloads: '/api/v1/crates/foo/1.2.3/downloads',
242242
},
243243
num: '1.2.3',
244+
published_by: null,
244245
updated_at: '2017-02-24T12:34:56Z',
245246
yanked: false,
246247
},

tests/models/version-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { setupTest } from 'ember-qunit';
2+
import { module, test } from 'qunit';
3+
4+
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
5+
6+
module('Model | Version', function (hooks) {
7+
setupTest(hooks);
8+
setupMirage(hooks);
9+
10+
hooks.beforeEach(function () {
11+
this.store = this.owner.lookup('service:store');
12+
});
13+
14+
test('`published_by` relationship is assigned correctly', async function (assert) {
15+
let user = this.server.create('user', { name: 'JD' });
16+
17+
let crate = this.server.create('crate');
18+
this.server.create('version', { crate, publishedBy: user });
19+
20+
let crateRecord = await this.store.findRecord('crate', crate.id);
21+
assert.ok(crateRecord);
22+
let versions = (await crateRecord.versions).toArray();
23+
assert.equal(versions.length, 1);
24+
let version = versions[0];
25+
assert.ok(version.published_by);
26+
assert.equal(version.published_by.name, 'JD');
27+
});
28+
});

0 commit comments

Comments
 (0)