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'
}
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



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

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


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

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

#incident_url(incident) ⇒ Object



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

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

#normalize_date(val) ⇒ Object



93
94
95
# File 'lib/bender/helpers.rb', line 93

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

#normalize_value(val) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bender/helpers.rb', line 79

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



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

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

#recent_incident?(i) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#refresh_incidents(bot = self) ⇒ Object



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/bender/helpers.rb', line 54

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
  issues = JSON.parse(resp.body)['issues']

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

#select_incident(num, refresh = true) ⇒ Object



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

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

#short_severity(s) ⇒ Object



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

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