travis-web/app/services/store.coffee
Piotr Sarnacki e1a334678d 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.
2015-12-08 10:18:07 +01:00

80 lines
2.3 KiB
CoffeeScript

`import DS from 'ember-data'`
`import config from 'travis/config/environment'`
Store = DS.Store.extend
auth: Ember.inject.service()
defaultAdapter: 'application'
adapter: 'application'
init: ->
@_super.apply(this, arguments)
@set('pusherEventHandlerGuards', {})
addPusherEventHandlerGuard: (name, callback) ->
@get('pusherEventHandlerGuards')[name] = callback
removePusherEventHandlerGuard: (name) ->
delete @get('pusherEventHandlerGuards')[name]
canHandleEvent: (event, data) ->
[name, type] = event.split(':')
for name, callback of @get('pusherEventHandlerGuards')
unless callback(event, data)
return false
return true
receivePusherEvent: (event, data) ->
[name, type] = event.split(':')
return unless @canHandleEvent(event, data)
if name == 'job' && data.job?.commit
@push(this.normalize('commit', data.job.commit))
if name == 'build' && data.build?.commit
# TODO: commit should be a sideload record on build, not mixed with it
build = data.build
commit = {
id: build.commit_id
author_email: build.author_email
author_name: build.author_name
branch: build.branch
committed_at: build.committed_at
committer_email: build.committer_email
committer_name: build.committer_name
compare_url: build.compare_url
message: build.message
sha: build.commit
}
delete(data.build.commit)
@push(this.normalize('commit', commit))
if event == 'job:log'
data = data.job
job = @recordForId('job', data.id)
job.appendLog(number: parseInt(data.number), content: data._log, final: data.final)
else if data[name]
@loadOne(name, data)
else
throw "can't load data for #{name}" unless type
loadOne: (type, json) ->
record = @push(this.normalize(type, json))
# we get other types of records only in a few situations and
# it's not always needed to update data, so I'm specyfing which
# things I want to update here:
if type == 'build' && (json.repository || json.repo)
data = json.repository || json.repo
if data.default_branch
data.default_branch.default_branch = true
@push(this.normalize('repo', data))
`export default Store`