Module: Helpers

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

Constant Summary collapse

DEFAULT_CONFIG =
{
  hipchat_token: nil,
  hipchat_v2_token: nil,
  primary_room_id: nil,
  jid: nil,
  password: nil,
  nick: nil,
  mention: nil,
  rooms: nil,
  database: nil,
  jira_user: nil,
  jira_pass: nil,
  jira_site: nil,
  jira_project: nil,
  jira_group: nil,
  jira_type: nil,
  user_refresh: 300,
  issue_refresh: 10,
  group_refresh: 60,
  room_base_name: nil,
  order: false,
  prefix: 'inc',
  resolved_transitions: %w[ 51 ],
  resolved_state: /resolve/i.to_s,
  closed_transitions: %w[ 61 71 ],
  closed_state: /close/i.to_s,
  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: 'customfield_11250',
  without_commands: []
}
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



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

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


152
153
154
155
156
157
# File 'lib/bender/helpers.rb', line 152

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

#incident_url(incident) ⇒ Object



148
149
150
# File 'lib/bender/helpers.rb', line 148

def incident_url incident
  config[:jira_site] + '/browse/' + incident['key']
end

#normalize_date(val) ⇒ Object



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

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

#normalize_value(val) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bender/helpers.rb', line 102

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



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

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

#recent_incident?(i) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#refresh_incidents(bot = self) ⇒ Object



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

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

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

  req = Net::HTTP::Get.new uri
  req.basic_auth config[:jira_user], config[: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



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

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

#short_severity(s) ⇒ Object



143
144
145
# File 'lib/bender/helpers.rb', line 143

def short_severity s
  s && s.include?(' - ') ? s.split(' - ', 2).first : s
end