Module: OpsAsk::Helpers

Included in:
App
Defined in:
lib/opsask/helpers.rb

Instance Method Summary collapse

Instance Method Details

#asks_in_current_sprint ⇒ Object



112
113
114
# File 'lib/opsask/helpers.rb', line 112

def asks_in_current_sprint
  asks_in_sprint current_sprint_num
end

#asks_in_sprint(num) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/opsask/helpers.rb', line 116

def asks_in_sprint num
  return [] unless logged_in?
  issues = []
  query = normalized_jql("labels in (#{sprint_label_prefix}#{num})", project: nil)
  @jira_client.Issue.jql(query, max_results: 500).each do |i|
    issues << i.attrs
  end
  return issues
end

#classes_for(jira) ⇒ Object



272
273
274
# File 'lib/opsask/helpers.rb', line 272

def classes_for jira
  raw_classes_for(jira).join(' ')
end

#components ⇒ Object



385
386
387
# File 'lib/opsask/helpers.rb', line 385

def components
  return @project.components.map(&:name).select { |c| c =~ /^Ops/ }
end

#create_jira(duedate, component, summary, description, assign_to_me, epic, ops_only) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/opsask/helpers.rb', line 339

def create_jira duedate, component, summary, description, assign_to_me, epic, ops_only
  epic       = 'INF-3091' if epic.nil? # OpsAsk default epic
  assignee   = assign_to_me ? @me : settings.config[:assignee]
  components = []
  components = [ { name: component } ] unless component
  labels     = [ 'OpsAsk', current_sprint_name ].compact
  labels    << 'OpsOnly' if ops_only
  labels    << settings.config[:require_label] if settings.config[:require_label]
  data       = {
    fields: {
      project: { key: settings.config[:project_key] },
      issuetype: { name: settings.config[:issue_type] },
      versions: [ { name: settings.config[:version] } ],
      duedate: duedate,
      summary: summary,
      description: description,
      components: components,
      assignee: { name: assignee },
      reporter: { name: @me },
      labels: labels,
      customfield_10002: 1, # Story Points = 1
      # customfield_10350: epic,
      customfield_10040: { id: '-1' } # Release Priority = None
    }
  }

  url = "#{settings.config[:jira_url]}/rest/api/latest/issue"
  curl_request = Curl::Easy.http_post(url, data.to_json) do |curl|
    curl.headers['Accept'] = 'application/json'
    curl.headers['Content-Type'] = 'application/json'
    curl.http_auth_types = :basic
    curl.username = settings.config[:jira_user]
    curl.password = settings.config[:jira_pass]
    curl.verbose  = true
  end

  raw_response = curl_request.body_str
  begin
    response = JSON::parse raw_response
  rescue
    $stderr.puts "Failed to parse response from JIRA: #{raw_response}"
    return nil
  end
  return response
end

#current_sprint(keys = [ 'sprintsData', 'sprints', 0 ]) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/opsask/helpers.rb', line 208

def current_sprint keys=[ 'sprintsData', 'sprints', 0 ]
  url = "#{settings.config[:jira_url]}/rest/greenhopper/1.0/xboard/work/allData.json?rapidViewId=#{settings.config[:agile_board]}"
  curl_request = Curl::Easy.http_get(url) do |curl|
    curl.headers['Accept'] = 'application/json'
    curl.headers['Content-Type'] = 'application/json'
    curl.http_auth_types = :basic
    curl.username = settings.config[:jira_user]
    curl.password = settings.config[:jira_pass]
    curl.verbose  = true
  end

  raw_response = curl_request.body_str
  begin
    data = JSON::parse(raw_response)
    keys.each { |k| data = data[k] }
    return data unless data.nil?
  rescue
    $stderr.puts "Failed to parse response from JIRA: #{raw_response}"
  end
  get_sprint(nil, sprints.last['id'])
end

#current_sprint_ends(sprint = current_sprint) ⇒ Object



189
190
191
192
193
194
# File 'lib/opsask/helpers.rb', line 189

def current_sprint_ends sprint=current_sprint
  if ends = sprint['endDate']
    ends = DateTime.strptime(ends, '%d/%b/%y %l:%M %p')
    ends.strftime('%Y/%m/%d')
  end
end

#current_sprint_id(sprint = current_sprint) ⇒ Object



204
205
206
# File 'lib/opsask/helpers.rb', line 204

def current_sprint_id sprint=current_sprint
  sprint.nil? ? nil : sprint['id']
end

#current_sprint_name(sprint = current_sprint) ⇒ Object



196
197
198
# File 'lib/opsask/helpers.rb', line 196

def current_sprint_name sprint=current_sprint
  sprint.nil? ? nil : sprint['name'].gsub(/\s+/, '')
end

#current_sprint_num(sprint = current_sprint) ⇒ Object



200
201
202
# File 'lib/opsask/helpers.rb', line 200

def current_sprint_num sprint=current_sprint
  sprint.nil? ? nil : sprint['name'].gsub(/\D+/, '')
end

#current_sprint_starts(sprint = current_sprint) ⇒ Object



182
183
184
185
186
187
# File 'lib/opsask/helpers.rb', line 182

def current_sprint_starts sprint=current_sprint
  if starts = sprint['startDate']
    starts = DateTime.strptime(starts, '%d/%b/%y %l:%M %p')
    starts.strftime('%Y/%m/%d')
  end
end

#date_for_new_jiras(really = false) ⇒ Object



301
302
303
304
305
306
307
308
309
# File 'lib/opsask/helpers.rb', line 301

def date_for_new_jiras really=false
  if now.hour < settings.config[:cutoff_hour] || its_the_weekend?
    return today    if room_for_new_jiras_for? today, really
    return tomorrow if room_for_new_jiras_for? tomorrow, really
  else
    return tomorrow if room_for_new_jiras_for? tomorrow, really
  end
  return nil
end

#epic(key) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/opsask/helpers.rb', line 446

def epic key
  url = "#{settings.config[:jira_url]}/rest/api/latest/issue/#{key}"
  curl_request = Curl::Easy.http_get(url) do |curl|
    curl.headers['Accept'] = 'application/json'
    curl.headers['Content-Type'] = 'application/json'
    curl.http_auth_types = :basic
    curl.username = settings.config[:jira_user]
    curl.password = settings.config[:jira_pass]
    curl.verbose  = true
  end

  raw_response = curl_request.body_str
  begin
    response = JSON::parse raw_response
  rescue
    $stderr.puts "Failed to parse response from JIRA: #{raw_response}"
    return nil
  end
  return {
    'key' => response['key'],
    'name' => response['fields']['customfield_10351'] || response['fields']['summary']
  }
end

#epics ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/opsask/helpers.rb', line 414

def epics
  data = {
    jql: normalized_jql("type = Epic"),
    startAt: 0,
    maxResults: 1000
  }

  url = "#{settings.config[:jira_url]}/rest/api/latest/search"
  curl_request = Curl::Easy.http_post(url, data.to_json) do |curl|
    curl.headers['Accept'] = 'application/json'
    curl.headers['Content-Type'] = 'application/json'
    curl.http_auth_types = :basic
    curl.username = settings.config[:jira_user]
    curl.password = settings.config[:jira_pass]
    curl.verbose  = true
  end

  raw_response = curl_request.body_str
  begin
    response = JSON::parse raw_response
  rescue
    $stderr.puts "Failed to parse response from JIRA: #{raw_response}"
    return nil
  end
  return response['issues'].map do |epic|
    {
      'key' => epic['key'],
      'name' => epic['fields']['customfield_10351'] || epic['fields']['summary']
    }
  end
end

#get_sprint(num, id = nil) ⇒ Object



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
# File 'lib/opsask/helpers.rb', line 155

def get_sprint num, id=nil
  unless sprint_id = id
    sprint = sprints.select { |s| s['name'] == "#{sprint_prefix} #{num}" }
    sprint_id = sprint.first['id']
  end
  url = "#{settings.config[:jira_url]}/rest/greenhopper/1.0/rapid/charts/sprintreport?rapidViewId=#{settings.config[:agile_board]}&sprintId=#{sprint_id}"
  curl_request = Curl::Easy.http_get(url) do |curl|
    curl.headers['Accept'] = 'application/json'
    curl.headers['Content-Type'] = 'application/json'
    curl.http_auth_types = :basic
    curl.username = settings.config[:jira_user]
    curl.password = settings.config[:jira_pass]
    curl.verbose  = true
  end

  raw_response = curl_request.body_str
  begin
    data = JSON::parse(raw_response)
    contents = data.delete('contents')
    data = data.delete('sprint')
    return data.merge(contents)
  rescue
    $stderr.puts "Failed to parse response from JIRA: #{raw_response}"
  end
  return {}
end

#issues_for(date) ⇒ Object



284
285
286
287
288
# File 'lib/opsask/helpers.rb', line 284

def issues_for date
  jiras_for(date).sort_by do |jira|
    sorting_key_for(jira)
  end.reverse
end

#items_in_current_sprint ⇒ Object



97
98
99
# File 'lib/opsask/helpers.rb', line 97

def items_in_current_sprint
  items_in_sprint current_sprint_num
end

#items_in_sprint(num) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/opsask/helpers.rb', line 101

def items_in_sprint num
  return [] unless logged_in?
  issues = []
  id = get_sprint(num)['id']
  query = normalized_jql("sprint = #{id}", project: nil)
  @jira_client.Issue.jql(query, max_results: 500).each do |i|
    issues << i.attrs
  end
  return issues
end

#its_the_weekend? ⇒ Boolean

Returns:

  • (Boolean)


290
291
292
# File 'lib/opsask/helpers.rb', line 290

def its_the_weekend?
  now.saturday? || now.sunday?
end

#jira_count_for(date, really = false) ⇒ Object



259
260
261
# File 'lib/opsask/helpers.rb', line 259

def jira_count_for date, really=false
  jiras_for(date, really).length
end

#jira_count_for_today(really = false) ⇒ Object



263
# File 'lib/opsask/helpers.rb', line 263

def jira_count_for_today really=false ; jira_count_for(today, really) end

#jira_count_for_tomorrow(really = false) ⇒ Object



265
# File 'lib/opsask/helpers.rb', line 265

def jira_count_for_tomorrow really=false ; jira_count_for(tomorrow, really) end

#jiras_for(date, really = false) ⇒ Object



250
251
252
253
254
255
256
257
# File 'lib/opsask/helpers.rb', line 250

def jiras_for date, really=false
  return [] unless logged_in?
  if !ops? || really
    @jira_client.Issue.jql normalized_jql("due = #{date} and type != Change and labels in (OpsAsk) and labels not in (OpsOnly)"), max_results: 100
  else
    @jira_client.Issue.jql normalized_jql("due = #{date} and labels in (OpsAsk) and type != Change"), max_results: 100
  end
end

#logged_in? ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/opsask/helpers.rb', line 3

def logged_in?
  !!session[:jira_auth]
end

#name_for_coming_week ⇒ Object



246
247
248
# File 'lib/opsask/helpers.rb', line 246

def name_for_coming_week
  todays_date.strftime 'Week of %-d %b'
end

#name_for_today(offset = 0) ⇒ Object



238
239
240
# File 'lib/opsask/helpers.rb', line 238

def name_for_today offset=0
  todays_date(offset).strftime '%A %-d %b'
end

#name_for_tomorrow ⇒ Object



242
243
244
# File 'lib/opsask/helpers.rb', line 242

def name_for_tomorrow
  name_for_today(one_day)
end

#normalized_jql(query, project: , require_label: , ignore_label: ) ⇒ Object



470
471
472
473
474
475
476
477
478
479
# File 'lib/opsask/helpers.rb', line 470

def normalized_jql query, \
  project: settings.config[:project_name], \
  require_label: settings.config[:require_label],
  ignore_label: settings.config[:ignore_label]
# ...
  query += %Q| and project = #{project}| if project
  query += %Q| and labels = #{require_label}| if require_label
  query += %Q| and (labels != #{ignore_label} OR labels is empty)| if ignore_label
  return query
end

#now ⇒ Object



19
20
21
# File 'lib/opsask/helpers.rb', line 19

def now
  Time.now # + 3 * one_day # DEBUG
end

#one_day ⇒ Object



15
16
17
# File 'lib/opsask/helpers.rb', line 15

def one_day
  1 * 24 * 60 * 60 # Day * Hour * Minute * Second = Seconds / Day
end

#ops? ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
# File 'lib/opsask/helpers.rb', line 7

def ops?
  return false unless logged_in?
  @myself['groups']['items'].each do |i|
    return true if i['name'] == settings.config[:ops_group]
  end
  return false
end

#outlier_issues(sprint, num) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/opsask/helpers.rb', line 88

def outlier_issues sprint, num
  return [] unless logged_in?
  issues = []
  @jira_client.Issue.jql(outlier_query(sprint, num), max_results: 500).each do |i|
    issues << i.attrs
  end
  return issues
end


84
85
86
# File 'lib/opsask/helpers.rb', line 84

def outlier_link sprint, num
  "#{settings.config[:jira_url]}/issues/?jql=#{URI::escape outlier_query(sprint, num)}"
end

#outlier_query(sprint, num) ⇒ Object



78
79
80
81
82
# File 'lib/opsask/helpers.rb', line 78

def outlier_query sprint, num
  starts = current_sprint_starts sprint
  ends   = current_sprint_ends sprint
  normalized_jql("(sprint != #{sprint['id']} or sprint is empty) and (labels != \"#{sprint_label_prefix}#{num}\" OR labels is empty) and resolved >= \"#{starts}\" and resolved <= \"#{ends}\"")
end

#raw_classes_for(jira) ⇒ Object



267
268
269
270
# File 'lib/opsask/helpers.rb', line 267

def raw_classes_for jira
  classes = [ jira.fields['resolution'].nil? ? 'open' : 'closed' ]
  classes << jira.fields['assignee']['name'].downcase.gsub(/\W+/, '')
end

#room_for_new_jiras?(really = false) ⇒ Boolean

Returns:

  • (Boolean)


311
312
313
314
315
316
# File 'lib/opsask/helpers.rb', line 311

def room_for_new_jiras? really=false
  return true  if params[:force]
  return false if params[:full]
  return true  if ops? && !really
  !date_for_new_jiras(really).nil?
end

#room_for_new_jiras_for?(date, really = false) ⇒ Boolean

Returns:

  • (Boolean)


294
295
296
297
298
299
# File 'lib/opsask/helpers.rb', line 294

def room_for_new_jiras_for? date, really=false
  return true  if params[:force]
  return false if params[:full]
  return true  if ops? && !really
  jira_count_for(date, really) < settings.config[:queue_size]
end

#sorting_key_for(jira) ⇒ Object



276
277
278
279
280
281
282
# File 'lib/opsask/helpers.rb', line 276

def sorting_key_for jira
  rcs = raw_classes_for(jira)
  idx = 1
  idx = 2 if rcs.include? 'denimcores'
  idx = 0 if rcs.include? 'closed'
  return "#{idx}-#{jira.key}"
end

#sprint_label_prefix ⇒ Object



151
152
153
# File 'lib/opsask/helpers.rb', line 151

def sprint_label_prefix
  sprint_prefix.gsub(/\s+/, '')
end

#sprint_prefix ⇒ Object



147
148
149
# File 'lib/opsask/helpers.rb', line 147

def sprint_prefix
  settings.config[:sprint_prefix] ||= 'Sprint'
end

#sprints ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/opsask/helpers.rb', line 126

def sprints
  url = "#{settings.config[:jira_url]}/rest/greenhopper/1.0/sprintquery/#{settings.config[:agile_board]}"
  curl_request = Curl::Easy.http_get(url) do |curl|
    curl.headers['Accept'] = 'application/json'
    curl.headers['Content-Type'] = 'application/json'
    curl.http_auth_types = :basic
    curl.username = settings.config[:jira_user]
    curl.password = settings.config[:jira_pass]
    curl.verbose  = true
  end

  raw_response = curl_request.body_str
  begin
    data = JSON::parse(raw_response)
    return data['sprints']
  rescue
    $stderr.puts "Failed to parse response from JIRA: #{raw_response}"
  end
  return nil
end

#stats_for(issues, resolved_link, unresolved_link, unsized_link) ⇒ Object



30
31
32
33
34
35
36
37
38
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
76
# File 'lib/opsask/helpers.rb', line 30

def stats_for issues, resolved_link, unresolved_link, unsized_link
  return {} unless logged_in?
  return {} unless issues

  unsized_issues, resolved_issues, unresolved_issues = [], [], []

  issues.map! do |i|
    key = i['key']
    status = i['fields']['status']['name']
    resolution = i['fields']['resolution']['name'] rescue nil
    points = i['fields']['customfield_10002'].to_i

    issue = {
      key: key,
      status: status,
      resolution: resolution,
      points: points
    }

    if i['fields']['customfield_10002'].nil?
      unsized_issues << issue
    end

    if resolution.nil?
      unresolved_issues << issue
    else
      resolved_issues << issue
    end
  end

  {
    resolved: {
      number: resolved_issues.size,
      points: resolved_issues.map { |i| i[:points] }.reduce(0, :+),
      link: resolved_link
    },
    unresolved: {
      number: unresolved_issues.size,
      points: unresolved_issues.map { |i| i[:points] }.reduce(0, :+),
      link: unresolved_link
    },
    unsized: {
      number: unsized_issues.size,
      link: unsized_link
    }
  }
end

#straggling_issues ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/opsask/helpers.rb', line 401

def straggling_issues
  return [] unless logged_in?
  constraints = [
    "due < #{today}",
    "labels in (OpsAsk)",
    "resolution = unresolved",
    "assignee != denimcores"
  ].join(' and ')
  @jira_client.Issue.jql(normalized_jql(constraints), max_results: 100).sort_by do |jira|
    sorting_key_for(jira)
  end.reverse
end

#today(offset = 0) ⇒ Object



230
231
232
# File 'lib/opsask/helpers.rb', line 230

def today offset=0
  todays_date(offset).strftime '%Y-%m-%d'
end

#todays_date(offset = 0) ⇒ Object



23
24
25
26
27
28
# File 'lib/opsask/helpers.rb', line 23

def todays_date offset=0
  date  = now + offset
  date += one_day if date.saturday?
  date += one_day if date.sunday?
  return date
end

#tomorrow ⇒ Object



234
235
236
# File 'lib/opsask/helpers.rb', line 234

def tomorrow
  today(one_day)
end

#untracked_issues ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
# File 'lib/opsask/helpers.rb', line 389

def untracked_issues
  return [] unless logged_in?
  constraints = [
    "due < #{today}",
    "resolution = unresolved",
    "assignee = denimcores"
  ].join(' and ')
  @jira_client.Issue.jql(normalized_jql(constraints), max_results: 100).sort_by do |jira|
    sorting_key_for(jira)
  end.reverse
end

#validate_jira_params ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/opsask/helpers.rb', line 325

def validate_jira_params
  flash[:error] = []
  flash[:error] << 'Summary is required' if params['jira-summary'].empty?
  redirect '/' unless flash[:error].empty?
  return [
    params['jira-component'],
    params['jira-summary'],
    params['jira-description'],
    !!params['jira-assign_to_me'],
    params['jira-epic'],
    !!params['jira-ops_only']
  ]
end

#validate_room_for_new_jiras ⇒ Object



318
319
320
321
322
323
# File 'lib/opsask/helpers.rb', line 318

def validate_room_for_new_jiras
  duedate = date_for_new_jiras
  return duedate unless duedate.nil?
  flash[:error] = [ "Sorry, there's is no room for new JIRAs" ]
  redirect '/'
end