Module: Helpers

Included in:
Bender::Main, BenderBot
Defined in:
lib/bender/helpers.rb

Constant Summary collapse

RESOLVED_TRANSITIONS =
%w[ 51 ]
RESOLVED_STATE =
/resolve/i
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'
}
SEVERITY_FIELD =
SHOW_FIELDS.key('Severity')
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"
]

Instance Method Summary collapse

Instance Method Details

#friendly_date(val) ⇒ Object



104
105
106
# File 'lib/bender/helpers.rb', line 104

def friendly_date val
  Time.parse(val).strftime('%Y-%m-%d %H:%M %Z')
end


135
136
137
138
139
140
# File 'lib/bender/helpers.rb', line 135

def incident_link incident
  '<a href="%s">%s</a>' % [
    incident_url(incident),
    incident['key']
  ]
end

#incident_url(incident) ⇒ Object



131
132
133
# File 'lib/bender/helpers.rb', line 131

def incident_url incident
  options.jira_site + '/browse/' + incident['key']
end

#normalize_date(val) ⇒ Object



99
100
101
# File 'lib/bender/helpers.rb', line 99

def normalize_date val
  Time.parse(val).utc.iso8601(0).sub(/Z$/, 'UTC')
end

#normalize_value(val) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bender/helpers.rb', line 85

def normalize_value val
  case val
  when Hash
    val['name'] || val['value'] || val
  when Array
    val.map { |v| v['value'] }.join(', ')
  when /^\d{4}\-\d{2}\-\d{2}/
    '%s (%s)' % [ val, normalize_date(val) ]
  else
    val
  end
end

#one_dayObject



115
116
117
# File 'lib/bender/helpers.rb', line 115

def one_day
  24 * 60 * 60 # seconds/day
end

#recent_incident?(i) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/bender/helpers.rb', line 109

def recent_incident? i
  it = Time.parse(i['fields']['created'])
  Time.now - it < one_day
end

#refresh_incidents(bot = self) ⇒ Object



55
56
57
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
# File 'lib/bender/helpers.rb', line 55

def refresh_incidents bot=self
  req_path = '/rest/api/2/search'
  req_params = QueryParams.encode \
    jql: "project = #{options.jira_project} ORDER BY created ASC, priority DESC",
    fields: SHOW_FIELDS.keys.join(','),
    startAt: 0,
    maxResults: 1_000_000

  uri = URI(options.jira_site + req_path + '?' + req_params)
  http = Net::HTTP.new uri.hostname, uri.port

  req = Net::HTTP::Get.new uri
  req.basic_auth options.jira_user, options.jira_pass
  req['Content-Type'] = 'application/json'
  req['Accept'] = 'application/json'

  resp = http.request req
  begin
    issues = JSON.parse(resp.body)['issues']
  rescue JSON::ParserError
    log.warn 'Could not refresh incidents!'
    return
  end

  bot.store['incidents'] = issues.map! do |i|
    i['num'] = i['key'].split('-', 2).last ; i
  end
end

#select_incident(num, refresh = true) ⇒ Object



120
121
122
123
# File 'lib/bender/helpers.rb', line 120

def select_incident num, refresh=true
  refresh_incidents if refresh
  store['incidents'].select { |i| i['num'] == num }.first
end

#short_severity(s) ⇒ Object



126
127
128
# File 'lib/bender/helpers.rb', line 126

def short_severity s
  s.split(' - ', 2).first
end