Skip to content
This repository has been archived by the owner on May 10, 2018. It is now read-only.

Commit

Permalink
refactor towards more skinny event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
svenfuchs committed Oct 31, 2012
1 parent dc242d7 commit 6444c6e
Show file tree
Hide file tree
Showing 105 changed files with 1,880 additions and 2,560 deletions.
1 change: 1 addition & 0 deletions lib/travis/api/v0.rb
Expand Up @@ -2,6 +2,7 @@ module Travis
module Api
# V0 is an internal api that we can change at any time
module V0
autoload :Event, 'travis/api/v0/event'
autoload :Notification, 'travis/api/v0/notification'
autoload :Worker, 'travis/api/v0/worker'
end
Expand Down
12 changes: 12 additions & 0 deletions lib/travis/api/v0/event.rb
@@ -0,0 +1,12 @@
module Travis
module Api
module V0
module Event
autoload :Build, 'travis/api/v0/event/build'
autoload :Test, 'travis/api/v0/event/test'
end
end
end
end


@@ -1,6 +1,6 @@
module Travis
module Api
module V2
module V0
module Event
class Build
include Formats
Expand All @@ -12,8 +12,8 @@ def initialize(build, options = {})
@repository = build.repository
@request = build.request
@commit = build.commit
@broadcasts = Broadcast.by_repo(build.repository)
@options = options
# @broadcasts = Broadcast.by_repo(build.repository_id)
# @options = options
end

def data(extra = {})
Expand All @@ -22,8 +22,7 @@ def data(extra = {})
'request' => request_data,
'commit' => commit_data,
'build' => build_data,
'jobs' => build.matrix.map { |job| job_data(job) },
'broadcasts' => broadcasts.map { |broadcast| broadcast_data(broadcast) }
'jobs' => build.matrix.map { |job| job_data(job) }
}
end

Expand All @@ -36,7 +35,7 @@ def build_data
'commit_id' => build.commit_id,
'number' => build.number,
'pull_request' => build.pull_request?,
'config' => build.obfuscated_config.stringify_keys,
'config' => build.config,
'state' => build.state.to_s,
'result' => build.result,
'previous_result' => build.previous_result,
Expand All @@ -50,22 +49,33 @@ def build_data
def repository_data
{
'id' => repository.id,
'key' => repository.key.public_key,
'slug' => repository.slug,
'description' => repository.description,
'last_build_id' => repository.last_build_id,
'last_build_number' => repository.last_build_number,
'last_build_result' => repository.last_build_result,
'last_build_duration' => repository.last_build_duration,
'last_build_language' => repository.last_build_language,
'last_build_started_at' => format_date(repository.last_build_started_at),
'last_build_finished_at' => format_date(repository.last_build_finished_at),
'owner_email' => repository.owner_email,
'admin_token' => repository_admin_token
# 'description' => repository.description,
# 'last_build_id' => repository.last_build_id,
# 'last_build_number' => repository.last_build_number,
# 'last_build_result' => repository.last_build_result,
# 'last_build_duration' => repository.last_build_duration,
# 'last_build_language' => repository.last_build_language,
# 'last_build_started_at' => format_date(repository.last_build_started_at),
# 'last_build_finished_at' => format_date(repository.last_build_finished_at),
}
end

def repository_admin_token
repository.admin.try(:github_oauth_token)
rescue Travis::AdminMissing => error
Travis.logger.error error.message
nil
end

def request_data
{
'head_commit' => (request.head_commit || '')[0..7],
'base_commit' => (request.base_commit || '')[0..7]
'token' => request.token,
'head_commit' => (request.head_commit || '')
# 'base_commit' => (request.base_commit || '')
}
end

Expand All @@ -75,7 +85,7 @@ def commit_data
'sha' => commit.commit,
'branch' => commit.branch,
'message' => commit.message,
'committed_at' => format_date(commit.committed_at),
'committed_at' => commit.committed_at,
'author_name' => commit.author_name,
'author_email' => commit.author_email,
'committer_name' => commit.committer_name,
Expand All @@ -87,30 +97,31 @@ def commit_data
def job_data(job)
{
'id' => job.id,
'repository_id' => job.repository_id,
'build_id' => job.source_id,
'commit_id' => job.commit_id,
'log_id' => job.log.id,
'state' => job.state.to_s,
'number' => job.number,
'config' => job.obfuscated_config.stringify_keys,
'result' => job.result,
'started_at' => format_date(job.started_at),
'finished_at' => format_date(job.finished_at),
'queue' => job.queue,
'allow_failure' => job.allow_failure,
'tags' => job.tags
# 'repository_id' => job.repository_id,
# 'build_id' => job.source_id,
# 'commit_id' => job.commit_id,
# 'log_id' => job.log.id,
# 'state' => job.state.to_s,
# 'config' => job.obfuscated_config.stringify_keys,
# 'started_at' => format_date(job.started_at),
# 'finished_at' => format_date(job.finished_at),
# 'queue' => job.queue,
# 'allow_failure' => job.allow_failure,
}
end

def broadcast_data(broadcast)
{
'message' => broadcast.message
}
end
# def broadcast_data(broadcast)
# {
# 'message' => broadcast.message
# }
# end
end
end
end
end
end


38 changes: 38 additions & 0 deletions lib/travis/api/v0/event/test.rb
@@ -0,0 +1,38 @@
module Travis
module Api
module V0
module Event
class Test
include Formats

attr_reader :job

def initialize(job, options = {})
@job = job
# @options = options
end

def data(extra = {})
{
'job' => job_data,
}
end

private

def job_data
{
'queue' => job.queue,
'created_at' => job.created_at,
'started_at' => job.started_at,
'finished_at' => job.finished_at,
}
end
end
end
end
end
end



Empty file.
10 changes: 0 additions & 10 deletions lib/travis/api/v2/event.rb

This file was deleted.

3 changes: 2 additions & 1 deletion lib/travis/event.rb
Expand Up @@ -19,13 +19,14 @@ module Event
autoload :Subscription, 'travis/event/subscription'
autoload :SecureConfig, 'travis/event/secure_config'

SUBSCRIBERS = %w(metrics worker)
SUBSCRIBERS = %w(metrics)

class << self
include Logging

def subscriptions
@subscriptions ||= subscribers.map do |name|
name = 'github_status' if name == 'github_commit_status' # TODO compat, remove once configs have been updated
subscription = Subscription.new(name)
subscription if subscription.subscriber
end.compact
Expand Down
117 changes: 65 additions & 52 deletions lib/travis/event/config.rb
Expand Up @@ -14,73 +14,86 @@ class Config
:failure => { :email => :always, :webhooks => :always, :campfire => :always, :hipchat => :always, :irc => :always, :flowdock => :always }
}

attr_reader :repository, :build, :config
attr_reader :payload, :build, :repository, :config

def initialize(build)
@build = build
@config = build.config
@repository = build.repository
def initialize(payload)
@payload = payload
@build = payload['build']
@config = build['config']
@repository = payload['repository']
end

private
def enabled?(key)
return !!notifications[key] if notifications.has_key?(key) # TODO this seems inconsistent. what if :email => { :disabled => true }
[:disabled, :disable].each { |key| return !notifications[key] if notifications.has_key?(key) } # TODO deprecate disabled and disable
true
end

def send_on_start_for?(type)
config = with_fallbacks(type, :on_start, DEFAULTS[:start][type])
config == true || config == :always
end
def send_on?(type, event)
send(:"send_on_#{event}_for?", type)
end

def send_on_finish_for?(type)
send_on_initial_build? || send_on_success_for?(type) || send_on_failure_for?(type)
end
def send_on_start_for?(type)
config = with_fallbacks(type, :on_start, DEFAULTS[:start][type])
config == true || config == :always
end

def send_on_finished_for?(type)
send_on_initial_build? || send_on_success_for?(type) || send_on_failure_for?(type)
end

def send_on_initial_build?
build['previous_result'].nil?
end

def send_on_initial_build?
build.previous_result.nil?
def send_on_success_for?(type)
!!if build['result'] == 1
config = with_fallbacks(type, :on_success, DEFAULTS[:success][type])
config == :always || (config == :change && build['previous_result'] != 1)
end
end

def send_on_success_for?(type)
!!if build.passed?
config = with_fallbacks(type, :on_success, DEFAULTS[:success][type])
config == :always || (config == :change && !build.previous_passed?)
end
def send_on_failure_for?(type)
!!if build['result'] == 0
config = with_fallbacks(type, :on_failure, DEFAULTS[:failure][type])
config == :always || (config == :change && build['previous_result'] == 1)
end
end

def send_on_failure_for?(type)
!!if build.failed?
config = with_fallbacks(type, :on_failure, DEFAULTS[:failure][type])
config == :always || (config == :change && build.previous_passed?)
end
# Fetches config with fallbacks. (notification type > global > default)
# Filters can be configured for each notification type.
# If no rules are configured for the given type, then fall back to the global rules, and then to the defaults.
def with_fallbacks(type, key, default)
config = if (notifications[type] && notifications[type].is_a?(Hash) && notifications[type].has_key?(key))
# Returns the type config if key is present (:notifications => :email => [:on_success])
notifications[type][key]
elsif notifications.has_key?(key)
# Returns the global config if key is present (:notifications => [:on_success])
notifications[key]
else
# Else, returns the given default
default
end

# Fetches config with fallbacks. (notification type > global > default)
# Filters can be configured for each notification type.
# If no rules are configured for the given type, then fall back to the global rules, and then to the defaults.
def with_fallbacks(type, key, default)
config = if (notifications[type] && notifications[type].is_a?(Hash) && notifications[type].has_key?(key))
# Returns the type config if key is present (:notifications => :email => [:on_success])
notifications[type][key]
elsif notifications.has_key?(key)
# Returns the global config if key is present (:notifications => [:on_success])
notifications[key]
else
# Else, returns the given default
default
end
config.respond_to?(:to_sym) ? config.to_sym : config
end

config.respond_to?(:to_sym) ? config.to_sym : config
end
# Returns (recipients, urls, channels) for (email, webhooks, irc)
# Supported data types are Hash, Array and String
def notification_values(type, key)
config = notifications[type] rescue {}
# Notification type config can be a string, an array of values,
# or a hash containing a key for these values.
normalize_array(config.is_a?(Hash) ? config[key] : config)
end

# Returns (recipients, urls, channels) for (email, webhooks, irc)
# Supported data types are Hash, Array and String
def notification_values(type, key)
config = notifications[type] rescue {}
# Notification type config can be a string, an array of values,
# or a hash containing a key for these values.
Array(config.is_a?(Hash) ? config[key] : config)
end
def notifications
Travis::Event::SecureConfig.decrypt(config.fetch(:notifications, {}), repository['key'])
end

def notifications
Travis::Event::SecureConfig.decrypt(config.fetch(:notifications, {}), repository.key)
end
def normalize_array(array)
Array(array).map { |room| room.split(',') }.flatten.map(&:strip).reject(&:blank?)
end
end
end
end
17 changes: 0 additions & 17 deletions lib/travis/event/config/campfire.rb

This file was deleted.

0 comments on commit 6444c6e

Please sign in to comment.