Class: BenderBot

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

Constant Summary collapse

JARO =
FuzzyStringMatch::JaroWinkler.create :native
CLOSED_TRANSITIONS =
%w[ 61 71 ]
CLOSED_STATE =
/close/i
SEVERITIES =
{
  1 => '10480',
  2 => '10481',
  3 => '10482',
  4 => '10483',
  5 => '10484'
}
SHOW_FIELDS =
{
  'summary' => 'Summary',
  'description' => 'Description',
  'customfield_11250' => 'Severity',
  'customfield_11251' => 'Impact Started',
  'customfield_11252' => 'Impact Ended',
  'customfield_11253' => 'Reported By',
  'customfield_11254' => 'Services Affected',
  'customfield_11255' => 'Cause',
  'status' => 'Status',
  'created' => 'Created',
  'updated' => 'Updated'
}
QUOTES =
[
  'Bite my shiny metal ass!',
  'This is the worst kind of discrimination there is: the kind against me!',
  'I guess if you want children beaten, you have to do it yourself.',
  "Hahahahaha! Oh wait you're serious. Let me laugh even harder.",
  "You know what cheers me up? Other people's misfortune.",
  'Anything less than immortality is a complete waste of time.',
  "Blackmail is such an ugly word. I prefer extortion. The 'x' makes it sound cool.",
  'Have you tried turning off the TV, sitting down with your children, and hitting them?',
  "You're a pimple on society’s ass and you'll never amount to anything!",
  'Shut up baby, I know it!',
  "I'm so embarrassed. I wish everyone else was dead!",
  "Afterlife? If I thought I had to live another life, I'd kill myself right now!",
  "I'm back baby!",
  "LET'S GO ALREADYYYYYY!",
  "I don't have emotions and sometimes that makes me very sad",
  "Life is hilariously cruel"
]
COMMANDS =
{
  help: {
    desc: 'Display this help text',
  },
  list: {
    cmd: '',
    desc: 'List open incidents'
  },
  show: {
    cmd: '',
    fmt: 'INCIDENT_NUMBER',
    desc: 'Display incident details'
  },
  close: {
    fmt: 'INCIDENT_NUMBER',
    desc: 'Display incident details',
  },
  open: {
    fmt: "SEVERITY=#{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'
  }
}

Instance Method Summary collapse

Instance Method Details

#handle(room, sender, message) ⇒ Object



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
# File 'lib/bender/bot.rb', line 128

def handle room, sender, message
  @room_name = @@rooms.select { |r| r.xmpp_jid == room }.first.name
  @room      = room
  @sender    = sender
  @message   = message

  severity_field = SHOW_FIELDS.key 'Severity'
  severities = Hash.new { |h,k| h[k] = [] }

  if message =~ /\/inc\s+(\w+)?\s*/
    unless COMMANDS.include? $1.to_sym
      reply_html 'Invalid usage', :red
      reply_with_help
      return
    end
  end

  case message

  when /^\s*\/bender\s*$/
    reply_html QUOTES.sample(1).first, :red

  when /^\s*\/whoami\s*$/
    u = user_where name: sender
    m = '<b>%{nick}</b>: %{name} (<a href="mailto:%{email}">%{email}</a>)' % u
    reply_html m, :purple

  when /^\s*\/lookup\s+(.+)\s*$/
    u = user_where(name: $1) || user_where(nick: $1)
    m = '<b>%{nick}</b>: %{name} (<a href="mailto:%{email}">%{email}</a>)' % u
    reply_html m, :purple

  # /inc help - This help text
  when /^\s*(\?inc|\/inc\s+help)\s*$/
    reply_with_help

  # /inc - List open incidents
  when /^\s*\/inc\s*$/
    refresh_incidents

    is = store['incidents'].reverse.map do |i|
      status = normalize_value i['fields']['status']
      unless status =~ /done|complete|closed/i
        '%s (%s - %s) [%s]: %s' % [
          incident_link(i),
          short_severity(i['fields'][severity_field]['value']),
          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

  # /inc summary - Summarize recent incidents
  when /^\s*\/inc\s+summary\s*$/
    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']
        ]

        sev  = i['fields'][severity_field]['value']
        severities[sev] << 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:'
    severities.keys.sort.each do |severity|
      summary << '%s: %d incident(s)' % [
        short_severity(severity),
        severities[severity].size
      ]
    end

    if severities.empty?
      reply_html 'No recent incidents! Woohoo!', :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


  # /inc NUM - Show incident details
  when /^\s*\/inc\s+(\d+)\s*$/
    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

  # /inc close NUM - Close an incident
  when /^\s*\/inc\s+close\s+(\d+)\s*$/
    incident = select_incident $1
    if incident
      reply_html *close_incident(incident)
    else
      reply_html 'Sorry, no such incident!', :red
    end

  # /inc open SEVERITY SUMMARY - File a new incident
  when /^\s*\/inc\s+open\s+(severity|sev|s|p)?(\d+)\s+(.*?)\s*$/i
    user = user_where name: sender
    data = {
      fields: {
        project: { key: options.jira_project },
        issuetype: { name: options.jira_type },
        reporter: { name: user[:nick] },
        summary: $3,
        SHOW_FIELDS.key('Severity') => {
          id: SEVERITIES[$2.to_i]
        }
      }
    }

    reply_html *file_incident(data)


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

    if incident
      reply_html *comment_on_incident(incident, comment, user)
    else
      reply_html 'Sorry, no such incident!', :red
    end
  end


  return true
end

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



111
112
113
# File 'lib/bender/bot.rb', line 111

def reply_html message, color=:yellow
  @@hipchat[@room_name].send(nick, message, color: color)
end

#reply_with_helpObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bender/bot.rb', line 115

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>/inc%{cmd}%{fmt}</code> - %{desc}' % opts
  end.join('<br />')

  reply_html help
end