Class: BenderBot

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

Constant Summary collapse

JARO =
FuzzyStringMatch::JaroWinkler.create :native
CLOSE_TRANSITIONS =
%w[ 21 41 ]
CLOSE_STATE =
/done/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'
}

Instance Method Summary collapse

Instance Method Details

#handle(time, sender, message) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
# File 'lib/bender/bot.rb', line 58

def handle time, sender, message
  severity_field = SHOW_FIELDS.key 'Severity'
  severities = Hash.new { |h,k| h[k] = [] }

  case message


  when /^\s*\?opts\s*$/
    reply options.inspect

  when /^\s*\?whoami\s*$/
    u = user_where name: sender
    reply '%s: %s (%s)' % [ u[:nick], u[:name], u[:email] ]

  when /^\s*\?lookup\s+(.+)\s*$/
    u = user_where(name: $1) || user_where(nick: $1)
    reply '%s: %s (%s)' % [ u[:nick], u[:name], u[:email] ]

  # ?inc - This help text
  when /^\s*\?inc\s*$/
    reply [
      '?inc - Display this help text',
      '/inc - List open incidents',
      '/inc [INCIDENT_NUMBER] - Display incident details',
      '/inc close [INCIDENT_NUMBER] - Close an incident',
      '/inc open [SEVERITY=1,2,3,4,5] [SUMMARY_TEXT] - Open a new incident',
      '/inc summary - Summarize incidents from past 24 hours (open or closed)',
      '/inc comment [INCIDENT_NUMBER] [COMMENT_TEXT] - Add a comment to an incident'
    ].join("\n")

  # /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]: %s' % [
          options.jira_project,
          i['num'],
          short_severity(i['fields'][severity_field]['value']),
          normalize_value(i['fields']['status']),
          friendly_date(i['fields']['created']),
          i['fields']['summary']
        ]
      end
    end.compact.join("\n")

    is = 'No open incidents at the moment!' if is.empty?

    reply is

  # /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]: %s' % [
          options.jira_project,
          i['num'],
          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 'No recent incidents! Woohoo!'

    else
      is = severities.keys.sort.map do |sev|
        "%s:\n%s" % [ sev, severities[sev].join("\n") ]
      end.join("\n\n")

      reply(summary.join("\n") + "\n\n" + is)
    end


  # /inc NUM - Show incident details
  when /^\s*\/inc\s+(\d+)\s*$/
    incident = select_incident $1

    if incident.nil?
      reply 'Sorry, no such incident!'
    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 "%s\n%s: %s\n%s" % [
        (options.jira_site + '/browse/' + incident['key']),
        incident['key'],
        incident['fields']['summary'],
        i.join("\n")
      ]
    end

  # /inc close NUM - Close an incident
  when /^\s*\/inc\s+close\s+(\d+)\s*$/
    incident = select_incident $1
    if incident
      reply close_incident(incident)
    else
      reply 'Sorry, no such incident!'
    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 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

    reply comment_on_incident(incident, comment, user)
  end

  return true
end