From 47439657a1c3a2a8805f00b9aecbb45fd0bf476f Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 19 Oct 2015 16:28:57 +0200 Subject: [PATCH] Use lastBuild from defaultBranch on repository One thing that is not standard here is a serializer for branch, which uses @href as id. At this point branches don't have ids and ember-data needs one, so using @href is the easiest way. --- app/adapters/repo.js | 4 +-- app/components/repos-list-item.coffee | 4 +++ app/controllers/repo.coffee | 6 ++--- app/models/branch.coffee | 21 +-------------- app/models/repo.coffee | 28 +++++++++----------- app/routes/repo/index.coffee | 2 +- app/serializers/branch.js | 12 +++++++++ app/serializers/repo.coffee | 3 --- app/serializers/v3.js | 2 +- app/services/store.coffee | 1 + app/templates/components/repos-list-item.hbs | 22 +++++++-------- app/templates/repo.hbs | 2 +- 12 files changed, 49 insertions(+), 58 deletions(-) create mode 100644 app/serializers/branch.js diff --git a/app/adapters/repo.js b/app/adapters/repo.js index 560506ee..eccf65f6 100644 --- a/app/adapters/repo.js +++ b/app/adapters/repo.js @@ -14,9 +14,9 @@ export default V3Adapter.extend({ } if(hash.data.include) { - hash.data.include += ',repository.last_build,build.commit'; + hash.data.include += ',repository.default_branch,branch.last_build,build.commit'; } else { - hash.data.include = 'repository.last_build,build.commit'; + hash.data.include = 'repository.default_branch,branch.last_build,build.commit'; } return this._super(url, type, hash); diff --git a/app/components/repos-list-item.coffee b/app/components/repos-list-item.coffee index e6c5da71..8b6c804e 100644 --- a/app/components/repos-list-item.coffee +++ b/app/components/repos-list-item.coffee @@ -15,6 +15,10 @@ ReposListItemComponent = Ember.Component.extend Polling, @get('repo') == @get('selectedRepo') ).property('selectedRepo') + color: (-> + colorForState(@get('repo.defaultBranch.lastBuild.state')) + ).property('repo.defaultBranch.lastBuild.state') + scrollTop: (-> if (window.scrollY > 0) $('html, body').animate({scrollTop: 0}, 200) diff --git a/app/controllers/repo.coffee b/app/controllers/repo.coffee index 16e9904b..8987b66d 100644 --- a/app/controllers/repo.coffee +++ b/app/controllers/repo.coffee @@ -77,15 +77,15 @@ Controller = Ember.Controller.extend Ember.run.scheduleOnce('actions', this, @_lastBuildDidChange); _lastBuildDidChange: -> - build = @get('repo.lastBuild') + build = @get('repo.defaultBranch.lastBuild') @set('build', build) stopObservingLastBuild: -> - @removeObserver('repo.lastBuild', this, 'lastBuildDidChange') + @removeObserver('repo.defaultBranch.lastBuild', this, 'lastBuildDidChange') observeLastBuild: -> @lastBuildDidChange() - @addObserver('repo.lastBuild', this, 'lastBuildDidChange') + @addObserver('repo.defaultBranch.lastBuild', this, 'lastBuildDidChange') connectTab: (tab) -> # TODO: such implementation seems weird now, because we render diff --git a/app/models/branch.coffee b/app/models/branch.coffee index 72aaea3d..3ed2411d 100644 --- a/app/models/branch.coffee +++ b/app/models/branch.coffee @@ -2,25 +2,6 @@ `import Model from 'travis/models/model'` Branch = Model.extend - repositoryId: DS.attr('number') - commitId: DS.attr('number') - state: DS.attr() - number: DS.attr('number') - branch: DS.attr() - message: DS.attr() - result: DS.attr('number') - duration: DS.attr('number') - startedAt: DS.attr() - finishedAt: DS.attr() - - commit: DS.belongsTo('commit') - - repo: (-> - @store.find('repo', @get('repositoryId')) if @get('repositoryId') - ).property('repositoryId') - - updateTimes: -> - @notifyPropertyChange 'started_at' - @notifyPropertyChange 'finished_at' + lastBuild: DS.belongsTo('build') `export default Branch` diff --git a/app/models/repo.coffee b/app/models/repo.coffee index bace43f2..31f586d7 100644 --- a/app/models/repo.coffee +++ b/app/models/repo.coffee @@ -13,10 +13,12 @@ Repo = Model.extend private: DS.attr('boolean') githubLanguage: DS.attr() active: DS.attr() - lastBuild: DS.belongsTo('build') + + #lastBuild: DS.belongsTo('build') + defaultBranch: DS.belongsTo('branch') withLastBuild: -> - @filter( (repo) -> repo.get('lastBuild') ) + @filter( (repo) -> repo.get('defaultBranch.lastBuild') ) sshKey: (-> @store.find('ssh_key', @get('id')) @@ -83,27 +85,21 @@ Repo = Model.extend (@get('slug') || '').split('/')[1] ).property('slug') - lastBuildDuration: (-> - duration = @get('_lastBuildDuration') - duration = durationFromHelper(@get('lastBuildStartedAt'), @get('lastBuildFinishedAt')) unless duration - duration - ).property('_lastBuildDuration', 'lastBuildStartedAt', 'lastBuildFinishedAt') - sortOrderForLandingPage: (-> - state = @get('lastBuildState') + state = @get('defaultBranch.lastBuild.state') if state != 'passed' && state != 'failed' 0 else - parseInt(@get('lastBuildId')) - ).property('lastBuildId', 'lastBuildState') + parseInt(@get('defaultBranch.lastBuild.id')) + ).property('defaultBranch.lastBuild.id', 'defaultBranch.lastBuild.state') sortOrder: (-> # cuz sortAscending seems buggy when set to false - if lastBuildFinishedAt = @get('lastBuildFinishedAt') + if lastBuildFinishedAt = @get('defaultBranch.lastBuild.finishedAt') - new Date(lastBuildFinishedAt).getTime() else - - new Date('9999').getTime() - parseInt(@get('lastBuildId')) - ).property('lastBuildFinishedAt', 'lastBuildId') + - new Date('9999').getTime() - parseInt(@get('defaultBranch.lastBuild.id')) + ).property('defaultBranch.lastBuild.state', 'defaultBranch.lastBuild.finishedAt') stats: (-> if @get('slug') @@ -114,7 +110,7 @@ Repo = Model.extend ).property('slug') updateTimes: -> - @notifyPropertyChange 'lastBuildDuration' + @notifyPropertyChange 'defaultBranch.lastBuild.duration' regenerateKey: (options) -> @get('ajax').ajax '/repos/' + @get('id') + '/key', 'post', options @@ -153,7 +149,7 @@ Repo.reopenClass withLastBuild: (store) -> repos = store.filter('repo', {}, (build) -> - build.get('lastBuild') + build.get('defaultBranch.lastBuild') ) repos.then () -> diff --git a/app/routes/repo/index.coffee b/app/routes/repo/index.coffee index 6c32b7d3..e607a1fe 100644 --- a/app/routes/repo/index.coffee +++ b/app/routes/repo/index.coffee @@ -6,7 +6,7 @@ Route = TravisRoute.extend @controllerFor('repo').activate('current') renderTemplate: -> - if @modelFor('repo').get('lastBuild') + if @modelFor('repo').get('defaultBranch.lastBuild') @render 'build' else @render 'builds/not_found' diff --git a/app/serializers/branch.js b/app/serializers/branch.js new file mode 100644 index 00000000..fa44d17c --- /dev/null +++ b/app/serializers/branch.js @@ -0,0 +1,12 @@ +import Ember from 'ember'; +import V3Serializer from 'travis/serializers/v3'; + +export default V3Serializer.extend({ + extractAttributes(klass, payload) { + payload.id = payload['@href']; + return this._super(...arguments); + }, + extractId(modelClass, resourceHash) { + return resourceHash.id || resourceHash['@href']; + } +}); diff --git a/app/serializers/repo.coffee b/app/serializers/repo.coffee index 5bba95f8..8517b44e 100644 --- a/app/serializers/repo.coffee +++ b/app/serializers/repo.coffee @@ -3,8 +3,5 @@ Serializer = V2FallbackSerializer.extend isNewSerializerAPI: true - attrs: { - _lastBuildDuration: { key: 'last_build_duration' } - } `export default Serializer` diff --git a/app/serializers/v3.js b/app/serializers/v3.js index 05b1a55f..043c4ad9 100644 --- a/app/serializers/v3.js +++ b/app/serializers/v3.js @@ -52,7 +52,7 @@ export default DS.JSONSerializer.extend({ if(type = payload['@type']) { items = payload[type]; } else { - items = payload[primaryModelClass.modelName + 's']; + items = payload[primaryModelClass.modelName.underscore() + 's']; } documentHash.data = items.map((item) => { diff --git a/app/services/store.coffee b/app/services/store.coffee index 70d26755..60931a80 100644 --- a/app/services/store.coffee +++ b/app/services/store.coffee @@ -20,6 +20,7 @@ Store = DS.Store.extend canHandleEvent: (event, data) -> [name, type] = event.split(':') auth = @get('auth') + if event != 'job:log' && auth.get('signedIn') && !config.pro && !config.enterprise # if recent repos hasn't been opened yet, we can safely diff --git a/app/templates/components/repos-list-item.hbs b/app/templates/components/repos-list-item.hbs index 77e8dc00..8df5deec 100644 --- a/app/templates/components/repos-list-item.hbs +++ b/app/templates/components/repos-list-item.hbs @@ -1,19 +1,19 @@ -
-

+
+

{{#if repo.slug}} {{#link-to "repo" repo}} - {{status-icon status=repo.lastBuildState}} + {{status-icon status=repo.defaultBranch.lastBuild.state}} {{repo.slug}} {{/link-to}} {{/if}}

{{#if repo.slug}} - {{#if repo.lastBuild.id}} -

- {{#link-to "build" repo repo.lastBuild.id}} + {{#if repo.defaultBranch.lastBuild.id}} +

+ {{#link-to "build" repo repo.defaultBranch.lastBuild.id}} - {{lastBuild.number}} + {{repo.defaultBranch.lastBuild.number}} {{/link-to}}

{{/if}} @@ -22,16 +22,16 @@

Duration: - - {{format-duration repo.lastBuild.duration}} + + {{format-duration repo.defaultBranch.lastBuild.duration}}

Finished: - - {{format-time repo.lastBuild.finishedAt}} + + {{format-time repo.defaultBranch.lastBuild.finishedAt}}

diff --git a/app/templates/repo.hbs b/app/templates/repo.hbs index dd441bb0..4c3c0c04 100644 --- a/app/templates/repo.hbs +++ b/app/templates/repo.hbs @@ -26,7 +26,7 @@ {{#if repo.active}} {{outlet}} {{else}} - {{#if repo.lastBuildId}} + {{#if repo.defaultBranch.lastBuild.id}} {{outlet}} {{else}} {{not-active user=currentUser repo=repo}}