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] ]
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")
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
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
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
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
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)
when /^\s*\/inc\s+comment\s+(\d+)\s+(.*?)\s*$/i
incident = select_incident $1
= $2
user = user_where name: sender
reply (incident, , user)
end
return true
end
|