Class: Hookworm::Judger

Inherits:
Object
  • Object
show all
Defined in:
lib/hookworm/rogue_commit_handler.rb

Defined Under Namespace

Classes: EmailRenderContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg, env = ENV) ⇒ Judger

Returns a new instance of Judger.



79
80
81
82
# File 'lib/hookworm/rogue_commit_handler.rb', line 79

def initialize(cfg, env = ENV)
  @cfg = cfg
  @env = env
end

Instance Attribute Details

#cfgObject (readonly)

Returns the value of attribute cfg.



77
78
79
# File 'lib/hookworm/rogue_commit_handler.rb', line 77

def cfg
  @cfg
end

#envObject (readonly)

Returns the value of attribute env.



77
78
79
# File 'lib/hookworm/rogue_commit_handler.rb', line 77

def env
  @env
end

Instance Method Details

#emailerObject



183
184
185
186
187
188
# File 'lib/hookworm/rogue_commit_handler.rb', line 183

def emailer
  @emailer ||= Hookworm::Emailer.new(
    env['HOOKWORM_EMAIL_URI'] || cfg[:worm_flags][:email_uri],
    env['HOOKWORM_EMAIL_HELO'] || 'localhost'
  )
end

#fromaddrObject



169
170
171
# File 'lib/hookworm/rogue_commit_handler.rb', line 169

def fromaddr
  @fromaddr ||= cfg[:worm_flags][:email_from_addr]
end

#judge_payload(payload) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/hookworm/rogue_commit_handler.rb', line 84

def judge_payload(payload)
  return unless watched_branch?(payload)
  hcid = payload[:head_commit][:id]
  return if pr_merge?(payload, hcid)
  return unless watched_path?(payload, hcid)
  safe_send_rogue_commit_email!(payload)
end

#logObject



197
198
199
# File 'lib/hookworm/rogue_commit_handler.rb', line 197

def log
  @log ||= Logger.new($stderr)
end

#log_rogue_commit(payload) ⇒ Object



190
191
192
193
194
195
# File 'lib/hookworm/rogue_commit_handler.rb', line 190

def log_rogue_commit(payload)
  log.warn do
    "WARNING rogue commit! #{payload}, " <<
    "head commit: #{payload[:head_commit]}"
  end
end

#new_judge_payload(payload) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/hookworm/rogue_commit_handler.rb', line 92

def new_judge_payload(payload)
  return unless watched_branch?(payload)
  hcid = payload[:head_commit][:id]
  return if pr_merge?(payload, hcid)
  return unless watched_path?(payload, hcid)
  safe_send_rogue_commit_email!(payload)
end

#pr_merge?(payload, hcid) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
# File 'lib/hookworm/rogue_commit_handler.rb', line 110

def pr_merge?(payload, hcid)
  if payload[:is_pr_merge]
    log.info { "#{hcid} is a pull request merge, yay!" }
    return true
  end

  log.debug { "#{hcid} is not a pull request merge!" }
  false
end

#recipientsObject



165
166
167
# File 'lib/hookworm/rogue_commit_handler.rb', line 165

def recipients
  @recipients ||= (cfg[:worm_flags][:email_recipients] || '').commasplit
end

#render_email(payload) ⇒ Object



153
154
155
# File 'lib/hookworm/rogue_commit_handler.rb', line 153

def render_email(payload)
  EmailRenderContext.new(self, payload, rogue_commit_email_tmpl).render
end

#rogue_commit_email_tmplObject



157
158
159
# File 'lib/hookworm/rogue_commit_handler.rb', line 157

def rogue_commit_email_tmpl
  ERB.new(rogue_commit_email_tmpl_string)
end

#rogue_commit_email_tmpl_stringObject



161
162
163
# File 'lib/hookworm/rogue_commit_handler.rb', line 161

def rogue_commit_email_tmpl_string
  File.read(File.expand_path('../rogue-commit-email-tmpl.erb', __FILE__))
end

#safe_send_rogue_commit_email!(payload) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/hookworm/rogue_commit_handler.rb', line 130

def safe_send_rogue_commit_email!(payload)
  send_rogue_commit_email!(payload)
  log.debug { "Sent rogue commit email to #{recipients}" }
rescue => e
  log.error { "#{e.class.name} #{e.message}" }
  log.debug { e.backtrace.join("\n") }
  raise e
end

#send_rogue_commit_email!(payload) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/hookworm/rogue_commit_handler.rb', line 139

def send_rogue_commit_email!(payload)
  log_rogue_commit(payload)

  if recipients.empty?
    log.warn { 'No email recipients specified, so no emailing!' }
    return
  end

  email_body = render_email(payload)
  log.debug { "Email message:\n#{email_body}" }

  emailer.send(fromaddr, recipients, email_body)
end

#watched_branch?(payload) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
# File 'lib/hookworm/rogue_commit_handler.rb', line 100

def watched_branch?(payload)
  unless payload[:is_watched_branch]
    log.debug { "#{payload[:ref]} is not a watched branch, yay!" }
    return false
  end

  log.debug { "#{payload[:ref]} is a watched branch!" }
  true
end

#watched_branchesObject



173
174
175
176
177
# File 'lib/hookworm/rogue_commit_handler.rb', line 173

def watched_branches
  @watched_branches ||= (
    cfg[:worm_flags][:watched_branches] || ''
  ).commasplit
end

#watched_path?(payload, hcid) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
# File 'lib/hookworm/rogue_commit_handler.rb', line 120

def watched_path?(payload, hcid)
  unless payload[:has_watched_path]
    log.debug { "#{hcid} does not contain watched paths, yay!" }
    return false
  end

  log.debug { "#{hcid} contains watched paths!" }
  true
end

#watched_pathsObject



179
180
181
# File 'lib/hookworm/rogue_commit_handler.rb', line 179

def watched_paths
  @watched_paths ||= (cfg[:worm_flags][:watched_paths] || '').commasplit
end