Skip to content

Instantly share code, notes, and snippets.

@foca
Created May 16, 2009 08:28
Show Gist options
  • Save foca/112623 to your computer and use it in GitHub Desktop.
Save foca/112623 to your computer and use it in GitHub Desktop.
some ideas for notifications in integrity
module Integrity
module Notifiers
def self.notify(&block)
block.call(Notificator.new)
end
def self.register(name, notifier)
notifiers[name.to_s] = notifier
end
def self.[](name)
notifiers[name.to_s]
end
def self.notifiers
@notifiers ||= {}
end
class Notificator
@@notifications = Hash.new {|h,k| h[k] = [] }
def every_build(*notifiers)
failed_build(*nofifiers)
successful_build(*notifiers)
end
def failed_build(*notifiers)
notify_via(notifiers, :failed_build)
end
def successful_build(*notifiers)
notify_via(notifiers, :successful_build)
end
def first_successful_build(*notifiers)
notify_via(notifiers, :first_successful_build)
end
private
def notify_via(notifiers, event)
notifiers.reject! {|n| @@notifications[event].include?(n) }
return if notifiers.empty?
Beacon.watch(event) do |project, build|
notifiers.each do |notifier|
config = project.notifier_config(Notifiers[notifier])
Notifiers[notifier].new(project, build, config).notify!
end
end
@@notifications[event] += notifiers
end
end
class Base
def self.default_config
@default_config ||= {}
end
def self.default_config=(config)
@default_config = config
end
def self.user_config
#...handle creating the form from the specified info
end
def self.notify_with(notifier)
@notifier = notifier
end
def self.notifier
@notifier
end
attr_reader :project, :build, :config
def initialize(project, build, config={})
@project, @build = project, build
@config = self.class.default_config.merge(config)
end
def notify!
self.class.notifier.new(config).say(message)
end
# and other methods to give a nice api inside messages
# (like full_message, short_message)
end
class Email < Base
notify_with ShoutEverything::Email
user_config do |c|
c.textbox :to, :from, :host, :port, :domain
c.textbox :user, :password
c.select :auth_type, [["none", ""], "login", "cram_md5", "plain"]
end
def message
{ :subject => "[Integrity] #{project.name} #{project.human_readable_status}"
:body => detailed_output }
end
end
register :email, Email
class Irc < Base
notify_with ShoutEverything::Irc
user_config do |c|
c.textbox :uri, :hint => "irc://user@server:6667/#channel"
end
def message
{ :body => "#{project.name}: #{short_message}" }
end
end
register :irc, Irc
class Campfire < Base
notify_with ShoutEverything::Campfire
user_cofig do |c|
c.textbox :account, :user, :password, :room
c.checkbox :ssl, :default => false
end
def message
{ :body => "[Integrity] #{project.name}: #{short_message}" }
end
end
register :campfire, Campfire
end
end
module Integrity
class Project
def start_building(commit_id, info)
commit_by_id(commit_id).tap do |commit|
commit.update_info(info)
Beacon.fire(:start_building, self, commit)
end
end
def finish_building(commit_id, status, output)
commit_by_id(commit_id).tap do |commit|
if commit.previous.failed? && build.successful?
Beacon.fire(:first_successful_build, self, build)
end
event = build.successful? ? :successful_build : :failed_build
Beacon.fire(event, self, build)
end
end
def commit_by_id(commit_id)
# find or create by identifier
end
end
class Commit
def update_info(info)
needs_updating = info.detect {|k,v| send(k) != v }
update_attributes(info) if needs_updating
end
# Find the commit immediately before this given the default sort
# for commits in a project
def previous
end
def failed?
builds.last.failed?
end
def successful?
builds.last.successful?
end
end
end
module Integrity
Notifiers.notify do |on|
on.every_build :campfire
on.failed_build :email
on.first_successful_build :email
end
Notifiers::Email.default_config = {
:to => "[email protected]",
:from => "[email protected]",
:user => "[email protected]",
:password => "secret",
:host => "smtp.company.com",
:port => "25"
}
Notifiers::Campfire.default_config = {
:account => "company",
:user => "foo",
:password => "secret",
:room => "lobby"
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment