Fix handling default_branch from pusher
Pusher payloads don't have all of the information that is available in API V3, so we need to do some normalizing.
This commit is contained in:
parent
9b4d5c5b4e
commit
e1a334678d
|
@ -1,7 +1,7 @@
|
||||||
import Ember from 'ember';
|
import Ember from 'ember';
|
||||||
import V3Serializer from 'travis/serializers/v3';
|
import V2FallbackSerializer from 'travis/serializers/v2_fallback';
|
||||||
|
|
||||||
export default V3Serializer.extend({
|
export default V2FallbackSerializer.extend({
|
||||||
extractAttributes(klass, payload) {
|
extractAttributes(klass, payload) {
|
||||||
payload.id = payload['@href'];
|
payload.id = payload['@href'];
|
||||||
return this._super(...arguments);
|
return this._super(...arguments);
|
||||||
|
|
|
@ -63,17 +63,33 @@ var Serializer = V2FallbackSerializer.extend({
|
||||||
var data, href, id, repoId, result;
|
var data, href, id, repoId, result;
|
||||||
|
|
||||||
// TODO: remove this after switching to V3 entirely
|
// TODO: remove this after switching to V3 entirely
|
||||||
if(!resourceHash['@type']) {
|
if(!resourceHash['@type'] && resourceHash.commit && resourceHash.commit.branch_is_default) {
|
||||||
let build = resourceHash.build,
|
let build = resourceHash.build,
|
||||||
commit = resourceHash.commit;
|
commit = resourceHash.commit;
|
||||||
let branch = {
|
let branch = {
|
||||||
name: commit.branch,
|
name: commit.branch,
|
||||||
default_branch: build.is_on_default_branch,
|
default_branch: commit.branch_is_default,
|
||||||
"@href": `/repo/${build.repository_id}/branch/${commit.branch}`
|
"@href": `/repo/${build.repository_id}/branch/${commit.branch}`
|
||||||
};
|
};
|
||||||
resourceHash.build.branch = branch;
|
resourceHash.build.branch = branch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fix pusher payload, it doesn't include a branch record:
|
||||||
|
if(!resourceHash['@type'] && resourceHash.build &&
|
||||||
|
resourceHash.repository && resourceHash.repository.default_branch) {
|
||||||
|
let branchName = resourceHash.build.branch,
|
||||||
|
repository = resourceHash.repository,
|
||||||
|
defaultBranchName = repository.default_branch.name;
|
||||||
|
|
||||||
|
resourceHash.build.branch = {
|
||||||
|
name: branchName,
|
||||||
|
default_branch: branchName === defaultBranchName,
|
||||||
|
'@href': `/repo/${repository.id}/branch/${branchName}`
|
||||||
|
};
|
||||||
|
|
||||||
|
repository.default_branch['@href'] = `/repo/${repository.id}/branch/${defaultBranchName}`;
|
||||||
|
}
|
||||||
|
|
||||||
result = this._super(modelClass, resourceHash);
|
result = this._super(modelClass, resourceHash);
|
||||||
|
|
||||||
data = result.data;
|
data = result.data;
|
||||||
|
|
|
@ -14,10 +14,11 @@ export default V3Serializer.extend({
|
||||||
// V2 API payload
|
// V2 API payload
|
||||||
let relationship = null;
|
let relationship = null;
|
||||||
let relationshipKey = this.keyForV2Relationship(key, relationshipMeta.kind, 'deserialize');
|
let relationshipKey = this.keyForV2Relationship(key, relationshipMeta.kind, 'deserialize');
|
||||||
|
let alternativeRelationshipKey = key.underscore();
|
||||||
|
|
||||||
if (resourceHash.hasOwnProperty(key) || resourceHash.hasOwnProperty(relationshipKey)) {
|
if (resourceHash.hasOwnProperty(alternativeRelationshipKey) || resourceHash.hasOwnProperty(relationshipKey)) {
|
||||||
let data = null;
|
let data = null;
|
||||||
let relationshipHash = resourceHash[key] || resourceHash[relationshipKey];
|
let relationshipHash = resourceHash[alternativeRelationshipKey] || resourceHash[relationshipKey];
|
||||||
if (relationshipMeta.kind === 'belongsTo') {
|
if (relationshipMeta.kind === 'belongsTo') {
|
||||||
data = this.extractRelationship(relationshipMeta.type, relationshipHash);
|
data = this.extractRelationship(relationshipMeta.type, relationshipHash);
|
||||||
} else if (relationshipMeta.kind === 'hasMany') {
|
} else if (relationshipMeta.kind === 'hasMany') {
|
||||||
|
@ -62,7 +63,7 @@ export default V3Serializer.extend({
|
||||||
let process = function(data) {
|
let process = function(data) {
|
||||||
if(Object.keys(data).sort()+'' !== 'id,type' || (data['@href'] && data.type == 'branch')) {
|
if(Object.keys(data).sort()+'' !== 'id,type' || (data['@href'] && data.type == 'branch')) {
|
||||||
// no need to add records if they have only id and type
|
// no need to add records if they have only id and type
|
||||||
let type = key.singularize();
|
let type = key === 'defaultBranch' ? 'branch' : key.singularize();
|
||||||
let serializer = store.serializerFor(type);
|
let serializer = store.serializerFor(type);
|
||||||
let modelClass = store.modelFor(type);
|
let modelClass = store.modelFor(type);
|
||||||
let normalized = serializer.normalize(modelClass, data);
|
let normalized = serializer.normalize(modelClass, data);
|
||||||
|
|
|
@ -70,6 +70,10 @@ Store = DS.Store.extend
|
||||||
# things I want to update here:
|
# things I want to update here:
|
||||||
if type == 'build' && (json.repository || json.repo)
|
if type == 'build' && (json.repository || json.repo)
|
||||||
data = json.repository || json.repo
|
data = json.repository || json.repo
|
||||||
|
|
||||||
|
if data.default_branch
|
||||||
|
data.default_branch.default_branch = true
|
||||||
|
|
||||||
@push(this.normalize('repo', data))
|
@push(this.normalize('repo', data))
|
||||||
|
|
||||||
`export default Store`
|
`export default Store`
|
||||||
|
|
Loading…
Reference in New Issue
Block a user