Fix handling branches
This commit fixes handling of branches when using both V3 and V2. The changes include: * proper definition of relationships that reflect V3 structure, so for example build belongs to a branch * setting up inverse records for some of the relationships. without doing that Ember Data can handle relationships in a surprising way, for example if the same record is referenced in 2 places in a belongsTo relationship, Ember Data will remove one of the references without proper inverse definitions * we need to add id when extracting branch as a relationship. Ember Data expects all of the relationships to have an id * lastly, we need to mimic the structure of the V3 API in V2 payloads, so for a build payload I'm now creating a branch record
This commit is contained in:
parent
147ab06fcf
commit
7ae4d6aa7d
|
@ -2,6 +2,11 @@
|
||||||
`import Model from 'travis/models/model'`
|
`import Model from 'travis/models/model'`
|
||||||
|
|
||||||
Branch = Model.extend
|
Branch = Model.extend
|
||||||
|
name: DS.attr('string')
|
||||||
|
defaultBranch: DS.attr('boolean')
|
||||||
|
|
||||||
lastBuild: DS.belongsTo('build')
|
lastBuild: DS.belongsTo('build')
|
||||||
|
builds: DS.hasMany('builds', inverse: 'branch')
|
||||||
|
repo: DS.belongsTo('repo', inverse: 'defaultBranch')
|
||||||
|
|
||||||
`export default Branch`
|
`export default Branch`
|
||||||
|
|
|
@ -9,7 +9,6 @@ Build = Model.extend DurationCalculations,
|
||||||
|
|
||||||
state: DS.attr()
|
state: DS.attr()
|
||||||
number: DS.attr('number')
|
number: DS.attr('number')
|
||||||
branch: DS.attr('string')
|
|
||||||
message: DS.attr('string')
|
message: DS.attr('string')
|
||||||
_duration: DS.attr('number')
|
_duration: DS.attr('number')
|
||||||
_config: DS.attr('object')
|
_config: DS.attr('object')
|
||||||
|
@ -21,8 +20,9 @@ Build = Model.extend DurationCalculations,
|
||||||
eventType: DS.attr('string')
|
eventType: DS.attr('string')
|
||||||
repositoryId: DS.attr('number')
|
repositoryId: DS.attr('number')
|
||||||
|
|
||||||
|
branch: DS.belongsTo('branch', async: false, inverse: 'builds')
|
||||||
repo: DS.belongsTo('repo', async: true)
|
repo: DS.belongsTo('repo', async: true)
|
||||||
commit: DS.belongsTo('commit')
|
commit: DS.belongsTo('commit', async: false)
|
||||||
jobs: DS.hasMany('job', async: true)
|
jobs: DS.hasMany('job', async: true)
|
||||||
|
|
||||||
config: (->
|
config: (->
|
||||||
|
|
|
@ -24,6 +24,8 @@ Job = Model.extend DurationCalculations,
|
||||||
build: DS.belongsTo('build', async: true)
|
build: DS.belongsTo('build', async: true)
|
||||||
commit: DS.belongsTo('commit', async: true)
|
commit: DS.belongsTo('commit', async: true)
|
||||||
|
|
||||||
|
branch: Ember.computed.alias('build.branch')
|
||||||
|
|
||||||
annotations: DS.hasMany('annotation')
|
annotations: DS.hasMany('annotation')
|
||||||
|
|
||||||
_config: DS.attr('object')
|
_config: DS.attr('object')
|
||||||
|
|
|
@ -15,7 +15,7 @@ Repo = Model.extend
|
||||||
active: DS.attr()
|
active: DS.attr()
|
||||||
|
|
||||||
#lastBuild: DS.belongsTo('build')
|
#lastBuild: DS.belongsTo('build')
|
||||||
defaultBranch: DS.belongsTo('branch')
|
defaultBranch: DS.belongsTo('branch', async: false)
|
||||||
|
|
||||||
withLastBuild: ->
|
withLastBuild: ->
|
||||||
@filter( (repo) -> repo.get('defaultBranch.lastBuild') )
|
@filter( (repo) -> repo.get('defaultBranch.lastBuild') )
|
||||||
|
|
|
@ -54,6 +54,18 @@ var Serializer = V2FallbackSerializer.extend({
|
||||||
normalize: function(modelClass, resourceHash) {
|
normalize: function(modelClass, resourceHash) {
|
||||||
var data, href, id, repoId, result;
|
var data, href, id, repoId, result;
|
||||||
|
|
||||||
|
// TODO: remove this after switching to V3 entirely
|
||||||
|
if(!resourceHash['@type']) {
|
||||||
|
let build = resourceHash.build,
|
||||||
|
commit = resourceHash.commit;
|
||||||
|
let branch = {
|
||||||
|
name: commit.branch,
|
||||||
|
default_branch: build.is_on_default_branch,
|
||||||
|
"@href": `/repo/${build.repository_id}/branch/${commit.branch}`
|
||||||
|
};
|
||||||
|
resourceHash.build.branch = branch;
|
||||||
|
}
|
||||||
|
|
||||||
result = this._super(modelClass, resourceHash);
|
result = this._super(modelClass, resourceHash);
|
||||||
|
|
||||||
data = result.data;
|
data = result.data;
|
||||||
|
|
|
@ -60,7 +60,7 @@ export default V3Serializer.extend({
|
||||||
Object.keys(data.relationships).forEach(function (key) {
|
Object.keys(data.relationships).forEach(function (key) {
|
||||||
let relationship = data.relationships[key];
|
let relationship = data.relationships[key];
|
||||||
let process = function(data) {
|
let process = function(data) {
|
||||||
if(Object.keys(data).sort()+'' !== 'id,type') {
|
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.singularize();
|
||||||
let serializer = store.serializerFor(type);
|
let serializer = store.serializerFor(type);
|
||||||
|
|
|
@ -28,6 +28,10 @@ export default DS.JSONSerializer.extend({
|
||||||
isNewSerializerAPI: true,
|
isNewSerializerAPI: true,
|
||||||
|
|
||||||
extractRelationship(type, hash) {
|
extractRelationship(type, hash) {
|
||||||
|
if(!hash.id && hash['@href']) {
|
||||||
|
hash.id = hash['@href'];
|
||||||
|
}
|
||||||
|
|
||||||
let relationshipHash = this._super(...arguments);
|
let relationshipHash = this._super(...arguments);
|
||||||
if(relationshipHash && relationshipHash['@type']) {
|
if(relationshipHash && relationshipHash['@type']) {
|
||||||
relationshipHash.type = relationshipHash['@type'];
|
relationshipHash.type = relationshipHash['@type'];
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
<small class="commit-branch" title={{item.pullRequestTitle}}>Pull Request #{{item.pullRequestNumber}}</small>
|
<small class="commit-branch" title={{item.pullRequestTitle}}>Pull Request #{{item.pullRequestNumber}}</small>
|
||||||
{{item.pullRequestTitle}}
|
{{item.pullRequestTitle}}
|
||||||
{{else}}
|
{{else}}
|
||||||
<small class="commit-branch" title={{item.commit.branch}}>{{item.commit.branch}}</small>
|
<small class="commit-branch" title={{item.branch.name}}>{{item.branch.name}}</small>
|
||||||
{{format-message item.commit.subject repo=item.repo}}
|
{{format-message item.commit.subject repo=item.repo}}
|
||||||
{{/if}}
|
{{/if}}
|
||||||
</h2>
|
</h2>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user