Guest User

Untitled

a guest
Mar 4th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. module Integrity
  2. module Notifiers
  3. def self.notify(&block)
  4. block.call(Notificator.new)
  5. end
  6.  
  7. def self.register(name, notifier)
  8. notifiers[name.to_s] = notifier
  9. end
  10.  
  11. def self.[](name)
  12. notifiers[name.to_s]
  13. end
  14.  
  15. def self.notifiers
  16. @notifiers ||= {}
  17. end
  18.  
  19. class Notificator
  20. @@notifications = Hash.new {|h,k| h[k] = [] }
  21.  
  22. def every_build(*notifiers)
  23. failed_build(*nofifiers)
  24. successful_build(*notifiers)
  25. end
  26.  
  27. def failed_build(*notifiers)
  28. notify_via(notifiers, :failed_build)
  29. end
  30.  
  31. def successful_build(*notifiers)
  32. notify_via(notifiers, :successful_build)
  33. end
  34.  
  35. def first_successful_build(*notifiers)
  36. notify_via(notifiers, :first_successful_build)
  37. end
  38.  
  39. private
  40.  
  41. def notify_via(notifiers, event)
  42. notifiers.reject! {|n| @@notifications[event].include?(n) }
  43. return if notifiers.empty?
  44.  
  45. Beacon.watch(event) do |project, build|
  46. notifiers.each do |notifier|
  47. config = project.notifier_config(Notifiers[notifier])
  48. Notifiers[notifier].new(project, build, config).notify!
  49. end
  50. end
  51. @@notifications[event] += notifiers
  52. end
  53. end
  54.  
  55. class Base
  56. def self.default_config
  57. @default_config ||= {}
  58. end
  59.  
  60. def self.default_config=(config)
  61. @default_config = config
  62. end
  63.  
  64. def self.user_config
  65. #...handle creating the form from the specified info
  66. end
  67.  
  68. def self.notify_with(notifier)
  69. @notifier = notifier
  70. end
  71.  
  72. def self.notifier
  73. @notifier
  74. end
  75.  
  76. attr_reader :project, :build, :config
  77.  
  78. def initialize(project, build, config={})
  79. @project, @build = project, build
  80. @config = self.class.default_config.merge(config)
  81. end
  82.  
  83. def notify!
  84. self.class.notifier.new(config).say(message)
  85. end
  86.  
  87. # and other methods to give a nice api inside messages
  88. # (like full_message, short_message)
  89. end
  90.  
  91. class Email < Base
  92. notify_with ShoutEverything::Email
  93.  
  94. user_config do |c|
  95. c.textbox :to, :from, :host, :port, :domain
  96. c.textbox :user, :password
  97. c.select :auth_type, [["none", ""], "login", "cram_md5", "plain"]
  98. end
  99.  
  100. def message
  101. { :subject => "[Integrity] #{project.name} #{project.human_readable_status}"
  102. :body => detailed_output }
  103. end
  104. end
  105. register :email, Email
  106.  
  107. class Irc < Base
  108. notify_with ShoutEverything::Irc
  109.  
  110. user_config do |c|
  111. c.textbox :uri, :hint => "irc://user@server:6667/#channel"
  112. end
  113.  
  114. def message
  115. { :body => "#{project.name}: #{short_message}" }
  116. end
  117. end
  118. register :irc, Irc
  119.  
  120. class Campfire < Base
  121. notify_with ShoutEverything::Campfire
  122.  
  123. user_cofig do |c|
  124. c.textbox :account, :user, :password, :room
  125. c.checkbox :ssl, :default => false
  126. end
  127.  
  128. def message
  129. { :body => "[Integrity] #{project.name}: #{short_message}" }
  130. end
  131. end
  132. register :campfire, Campfire
  133. end
  134. end
  135.  
  136. module Integrity
  137. class Project
  138. def start_building(commit_id, info)
  139. commit_by_id(commit_id).tap do |commit|
  140. commit.update_info(info)
  141. Beacon.fire(:start_building, self, commit)
  142. end
  143. end
  144.  
  145. def finish_building(commit_id, status, output)
  146. commit_by_id(commit_id).tap do |commit|
  147. if commit.previous.failed? && build.successful?
  148. Beacon.fire(:first_successful_build, self, build)
  149. end
  150.  
  151. event = build.successful? ? :successful_build : :failed_build
  152. Beacon.fire(event, self, build)
  153. end
  154. end
  155.  
  156. def commit_by_id(commit_id)
  157. # find or create by identifier
  158. end
  159. end
  160.  
  161. class Commit
  162. def update_info(info)
  163. needs_updating = info.detect {|k,v| send(k) != v }
  164. update_attributes(info) if needs_updating
  165. end
  166.  
  167. # Find the commit immediately before this given the default sort
  168. # for commits in a project
  169. def previous
  170. end
  171.  
  172. def failed?
  173. builds.last.failed?
  174. end
  175.  
  176. def successful?
  177. builds.last.successful?
  178. end
  179. end
  180. end
  181.  
  182. module Integrity
  183. Notifiers.notify do |on|
  184. on.every_build :campfire
  185. on.failed_build :email
  186. on.first_successful_build :email
  187. end
  188.  
  189. Notifiers::Email.default_config = {
  190. :to => "all@company.com",
  191. :from => "integrity@company.com",
  192. :user => "integrity@company.com",
  193. :password => "secret",
  194. :host => "smtp.company.com",
  195. :port => "25"
  196. }
  197.  
  198. Notifiers::Campfire.default_config = {
  199. :account => "company",
  200. :user => "foo",
  201. :password => "secret",
  202. :room => "lobby"
  203. }
  204. end
Add Comment
Please, Sign In to add comment