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!"
]

Instance Method Summary collapse

Instance Method Details

#handle(room, sender, message) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
# File 'lib/bender/bot.rb', line 85

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] = [] }


  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 - This help text
  when /^\s*\?inc\s*$/
    reply_html [
      '<code>?inc</code> - Display this help text',
      '<code>/inc</code> - List open incidents',
      '<code>/inc <i>INCIDENT_NUMBER</i></code> - Display incident details',
      '<code>/inc close <i>INCIDENT_NUMBER</i></code> - Close an incident',
      '<code>/inc open <i>SEVERITY=1,2,3,4,5</i> <i>SUMMARY_TEXT</i></code> - Open a new incident',
      '<code>/inc summary</code> - Summarize incidents from past 24 hours (open or closed)',
      '<code>/inc comment <i>INCIDENT_NUMBER</i> <i>COMMENT_TEXT</i></code> - Add a comment to an incident'
    ].join('<br />')

  # /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 '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



80
81
82
# File 'lib/bender/bot.rb', line 80

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