travis-web/assets/scripts/app/models/repo.coffee
Piotr Sarnacki 985e218981 Fix odd behavior on running jobs list and on queued jobs list
In order to not load too many repositories when not needed I was using
construct of repoData on Job and Worker models. repoData was a simple
object with id and slug attributes, which was used to generate url for a
repo. That way I didn't have to instantiate Repo object for generating
urls. The problem is that our API does not return repositorySlug along
with Job record, so the value of repositorySlug was overwritten and in
consequence repoData was becoming empty.

I could change the API, but I feel that the whole repoData concept is
flawed. A bit better solution is to load incomplete repository data into
the store (just an id and a slug) and then instantiate repo record - as
long as it will not need to provide other fields than an id and a slug,
we will not have to do an ajax request.
2013-05-15 17:23:53 +02:00

130 lines
3.4 KiB
CoffeeScript

require 'travis/expandable_record_array'
require 'travis/model'
@Travis.Repo = Travis.Model.extend
slug: DS.attr('string')
description: DS.attr('string')
lastBuildId: DS.attr('number')
lastBuildNumber: DS.attr('string')
lastBuildState: DS.attr('string')
lastBuildStartedAt: DS.attr('string')
lastBuildFinishedAt: DS.attr('string')
_lastBuildDuration: DS.attr('number')
lastBuild: DS.belongsTo('Travis.Build')
lastBuildHash: (->
{
id: @get('lastBuildId')
number: @get('lastBuildNumber')
repo: this
}
).property('lastBuildId', 'lastBuildNumber')
allBuilds: (->
Travis.Build.find()
).property()
builds: (->
id = @get('id')
builds = Travis.Build.byRepoId id, event_type: 'push'
# TODO: move to controller
array = Travis.ExpandableRecordArray.create
type: Travis.Build
content: Ember.A([])
store: @get('store')
array.load(builds)
id = @get('id')
array.observe(@get('allBuilds'), (build) -> build.get('isLoaded') && build.get('eventType') && build.get('repo.id') == id && !build.get('isPullRequest') )
array
).property()
pullRequests: (->
id = @get('id')
builds = Travis.Build.byRepoId id, event_type: 'pull_request'
array = Travis.ExpandableRecordArray.create
type: Travis.Build
content: Ember.A([])
store: @get('store')
array.load(builds)
id = @get('id')
array.observe(@get('allBuilds'), (build) -> build.get('isLoaded') && build.get('eventType') && build.get('repo.id') == id && build.get('isPullRequest') )
array
).property()
branches: (->
Travis.Build.branches repoId: @get('id')
).property()
events: (->
Travis.Event.byRepoId @get('id')
).property()
owner: (->
(@get('slug') || '').split('/')[0]
).property('slug')
name: (->
(@get('slug') || '').split('/')[1]
).property('slug')
lastBuildDuration: (->
duration = @get('_lastBuildDuration')
duration = Travis.Helpers.durationFrom(@get('lastBuildStartedAt'), @get('lastBuildFinishedAt')) unless duration
duration
).property('_lastBuildDuration', 'lastBuildStartedAt', 'lastBuildFinishedAt')
sortOrder: (->
# cuz sortAscending seems buggy when set to false
if lastBuildFinishedAt = @get('lastBuildFinishedAt')
- new Date(lastBuildFinishedAt).getTime()
else
- new Date('9999').getTime() - parseInt(@get('lastBuildId'))
).property('lastBuildFinishedAt', 'lastBuildId')
stats: (->
if @get('slug')
@get('_stats') || $.get("https://api.github.com/repos/#{@get('slug')}", (data) =>
@set('_stats', data)
@notifyPropertyChange 'stats'
) && {}
).property('slug')
updateTimes: ->
@notifyPropertyChange 'lastBuildDuration'
regenerateKey: (options) ->
Travis.ajax.ajax '/repos/' + @get('id') + '/key', 'post', options
@Travis.Repo.reopenClass
recent: ->
@find()
ownedBy: (login) ->
@find(owner_name: login, orderBy: 'name')
accessibleBy: (login) ->
@find(member: login, orderBy: 'name')
search: (query) ->
@find(search: query, orderBy: 'name')
withLastBuild: ->
@filter( (repo) -> !repo.get('incomplete') && repo.get('lastBuildId') )
bySlug: (slug) ->
repo = $.select(@find().toArray(), (repo) -> repo.get('slug') == slug)
if repo.length > 0 then repo else @find(slug: slug)
# buildURL: (slug) ->
# if slug then slug else 'repos'