Class: BenderBot

Inherits:
Object
  • Object
show all
Includes:
Bot::Plugin, Helpers
Defined in:
lib/bender/bot.rb

Constant Summary collapse

JARO =
FuzzyStringMatch::JaroWinkler.create :native

Constants included from Helpers

Helpers::DEFAULT_CONFIG, Helpers::QUOTES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#friendly_date, #incident_link, #incident_url, #normalize_date, #normalize_value, #one_day, #recent_incident?, #refresh_incidents, #select_incident, #short_severity

Class Method Details

.set_commands(without) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bender/bot.rb', line 39

def self.set_commands without
  commands = {
    help: {
      desc: 'Display this help text',
    },
    list: {
      cmd: '',
      desc: 'List open incidents'
    },
    show: {
      cmd: '',
      fmt: 'INCIDENT_NUMBER',
      desc: 'Display incident details'
    },
    resolve: {
      fmt: 'INCIDENT_NUMBER',
      desc: 'Resolve an incident',
    },
    close: {
      fmt: 'INCIDENT_NUMBER',
      desc: 'Close an incident',
    },
    open: {
      fmt: "#{SEVERITY_FIELD.upcase}=#{SEVERITIES.keys.sort_by(&:to_i).join(',')} SUMMARY_TEXT",
      desc: 'Open a new incident',
    },
    summary: {
      desc: 'Summarize incidents from past 24 hours (open or closed)'
    },
    comment: {
      fmt: 'INCIDENT_NUMBER COMMENT_TEXT',
      desc: 'Add a comment to an incident'
    }
  }
  without.each { |wo| commands.delete wo.to_sym }
  BenderBot.const_set :COMMANDS, commands
end

Instance Method Details

#handle(room, sender, message) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/bender/bot.rb', line 117

def handle room, sender, message
  refresh_room room

  @room      = room
  @sender    = sender
  @message   = message

  severities = Hash.new { |h,k| h[k] = [] }


  case message.strip

  when /^\/#{config[:jira_user]}$/
    reply_html QUOTES.sample(1).first, :red

  when /^\/whoami$/
    u = user_where name: sender
    if u
      m = '<b>%{nick}</b>: %{name} (<a href="mailto:%{email}">%{email}</a>)' % u
      reply_html m, :purple
    else
      reply_html "Couldn't find the associated user in JIRA", :red
    end

  when /^\/lookup\s+(.+)$/
    u = user_where(name: $1) || user_where(nick: $1)
    if u
      m = '<b>%{nick}</b>: %{name} (<a href="mailto:%{email}">%{email}</a>)' % u
      reply_html m, :purple
    else
      reply_html "Couldn't find the associated user in JIRA", :red
    end

  # /prefix help - This help text
  when /^(\?#{prefix}|\/#{prefix}\s+help)$/
    reply_with_help

  # /prefix - List open incidents
  when /^\/#{prefix}$/
    refresh_incidents

    is = store['incidents'].reverse.map do |i|
      status = normalize_value i['fields']['status']
      unless status =~ /resolved|closed/i
        '%s (%s - %s) [%s]: %s' % [
          incident_link(i),
          short_severity(sev_for(i)),
          normalize_value(i['fields']['status']),
          friendly_date(i['fields']['created']),
          i['fields']['summary']
        ]
      end
    end.compact.join('<br />')

    if is.empty?
      reply_html 'Good news everyone! No open incidents at the moment', :green
    else
      reply_html is
    end

  # /prefix summary - Summarize recent incidents
  when /^\/#{prefix}\s+summary$/
    refresh_incidents

    statuses = Hash.new { |h,k| h[k] = 0 }

    store['incidents'].reverse.each do |i|
      if recent_incident? i
        status = normalize_value(i['fields']['status'])

        repr = '%s (%s) [%s]: %s' % [
          incident_link(i),
          status,
          friendly_date(i['fields']['created']),
          i['fields']['summary']
        ]

        severities[sev_for(i)] << repr
        statuses[status] += 1
      end
    end

    summary = []
    summary << 'By Status:'
    statuses.each do |status, size|
      summary << '%s: %d incident(s)' % [ status, size ]
    end
    summary << ''
    summary << "By #{SEVERITY_FIELD.capitalize}:"
    severities.keys.sort.each do |severity|
      summary << '%s: %d incident(s)' % [
        short_severity(severity),
        severities[severity].size
      ]
    end

    if severities.empty?
      reply_html 'Good news everyone! No open incidents at the moment', :green

    else
      is = severities.keys.sort.map do |sev|
        "%s:<br />%s" % [ sev, severities[sev].join("<br />") ]
      end.join("<br /><br />")

      reply_html(summary.join("<br />") + "<br /><br />" + is)
    end


  # /prefix NUM - Show incident details
  when /^\/#{prefix}\s+(\d+)$/
    incident = select_incident $1

    if incident.nil?
      reply_html 'Sorry, no such incident!', :red
    else
      fields = SHOW_FIELDS.keys - %w[ summary ]

      i = fields.map do |f|
        val = incident['fields'][f]
        if val
          key = SHOW_FIELDS[f]
          val = normalize_value val
          '%s: %s' % [ key, val ]
        end
      end.compact

      reply_html "%s - %s<br />%s" % [
        incident_link(incident),
        incident['fields']['summary'],
        i.join("<br />")
      ]
    end

  # /prefix resolve NUM - Resolve an incident
  when /^\/#{prefix}\s+resolve\s+(\d+)$/
    unless allowed?
      reply_html "You're not allowed to do that!", :red
    else
      incident = select_incident $1
      if incident
        reply_html(*resolve_incident(incident))
      else
        reply_html 'Sorry, no such incident!', :red
      end
    end

  # /prefix close NUM - Close an incident
  when /^\/#{prefix}\s+close\s+(\d+)$/
    unless allowed?
      reply_html "You're not allowed to do that!", :red
    else
      incident = select_incident $1
      if incident
        reply_html(*close_incident(incident))
      else
        reply_html 'Sorry, no such incident!', :red
      end
    end

  # /prefix open SEVERITY SUMMARY - File a new incident
  when /^\/#{prefix}\s+open\s+(severity|sev|s|p)?(\d+)\s+(.*)/im
    unless allowed?
      reply_html "You're not allowed to do that!", :red
    else
      user = user_where name: sender
      data = {
        fields: {
          project: { key: config[:jira_project] },
          issuetype: { name: config[:jira_type] },
          reporter: { name: user[:nick] },
          summary: $3,
          SEVERITY_FIELD => {
            id: SEVERITIES[$2.to_i]
          }
        }
      }

      reply_html(*file_incident(data))
    end


  # /prefix comment [INCIDENT_NUMBER] [COMMENT_TEXT]
  when /^\/#{prefix}\s+comment\s+(\d+)\s+(.*)/im
    incident = select_incident $1
    comment  = $2
    user     = user_where name: sender

    if !user
      reply_html "Couldn't find the associated user in JIRA", :red
    elsif incident
      reply_html(*comment_on_incident(incident, comment, user))
    else
      reply_html 'Sorry, no such incident!', :red
    end


  when /^\/#{prefix}/i
    reply_html 'Invalid usage', :red
    reply_with_help
  end


  return true
end

#refresh_room(jid) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/bender/bot.rb', line 105

def refresh_room jid
  begin
    @this_room = @@rooms.select { |r| r.xmpp_jid == jid }.first
    @@hipchat[@this_room.name].get_room # test
  rescue
    @@hipchat  = HipChat::Client.new(@@config[:hipchat_token])
    @@rooms    = @@hipchat.rooms
    @this_room = @@rooms.select { |r| r.xmpp_jid == jid }.first
  end
end

#reply_html(message, color = :yellow) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bender/bot.rb', line 78

def reply_html message, color=:yellow
  tries ||= 3
  @@hipchat[@this_room.name].send(nick, message, color: color)
rescue
  tries -= 1
  if tries > 0
    refresh_room @this_room.xmpp_jid
    log.warn reason: 'could not reply_html', remediation: 'retrying'
    retry
  end
  log.error reason: 'could not reply_html'
end

#reply_with_helpObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bender/bot.rb', line 92

def reply_with_help
  help = COMMANDS.each.map do |cmd, opts|
    opts[:cmd] ||= cmd
    opts[:cmd]   = opts[:cmd].to_s.insert(0, ' ') if opts[:cmd]
    opts[:fmt] ||= ''
    opts[:fmt]   = opts[:fmt].to_s.insert(0, ' ') if opts[:fmt]
    "<code>/#{prefix}%{cmd}%{fmt}</code> - %{desc}" % opts
  end.join('<br />')

  reply_html help
end