Module: FerrisBueller::Replies

Includes:
Constants
Included in:
Web
Defined in:
lib/ferris-bueller/replies.rb

Constant Summary

Constants included from Constants

Constants::CLOSED_STATE, Constants::CLOSED_TRANSITIONS, Constants::HELP_TEXT, Constants::RESOLVED_STATE, Constants::RESOLVED_TRANSITIONS, Constants::RETRY_DELAY, Constants::SEVERITIES, Constants::SEVERITY_FIELD, Constants::SHOW_FIELDS

Instance Method Summary collapse

Instance Method Details

#reply_close(inc_num, params) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ferris-bueller/replies.rb', line 118

def reply_close inc_num, params
  return { response_type: 'in_channel' }, lambda do
    return { response_type: 'in_channel', text: "You're not allowed to do that" }, params[:response_url] unless allowed? params

    incident = select_incident inc_num
    return { response_type: 'in_channel', text: 'Could not list incidents' }, params[:response_url] unless incident

    resolution = close_incident incident
    return { response_type: 'in_channel', text: 'Could not close incident' }, params[:response_url] unless resolution

    return {
      response_type: 'in_channel',
      text: 'Closed incident',
      attachments: [
        attach_incident(incident)
      ]
    }, params[:response_url]
  end
end

#reply_comment(inc_num, message, params) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ferris-bueller/replies.rb', line 166

def reply_comment inc_num, message, params
  incident = select_incident inc_num
  return { response_type: 'in_channel', text: 'Could not list incidents' } unless incident

  comment = construct_comment message, params
  annotation = comment_on_incident incident, comment
  return { response_type: 'in_channel', text: 'Could not comment on incident' } unless annotation

  {
    response_type: 'in_channel',
    text: 'Commented on incident',
    attachments: [
      attach_incident(incident)
    ]
  }
end

#reply_dunno(params) ⇒ Object



39
40
41
# File 'lib/ferris-bueller/replies.rb', line 39

def reply_dunno params
  { text: "Invalid usage. Try the `help` command" }
end

#reply_help(params) ⇒ Object



34
35
36
# File 'lib/ferris-bueller/replies.rb', line 34

def reply_help params
  { text: HELP_TEXT }
end

#reply_list(params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ferris-bueller/replies.rb', line 44

def reply_list params
  incidents = open_incidents
  return { text: 'Could not list incidents' } if incidents.nil?
  return { text: 'No open incidents at the moment' } if incidents.empty?

  attachments = incidents.map do |i|
    attach_incident(i)
  end
  {
    text: 'Found %d open incidents' % attachments.size,
    attachments: attachments
  }
end

#reply_open(sev_num, summary, params) ⇒ Object



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
# File 'lib/ferris-bueller/replies.rb', line 139

def reply_open sev_num, summary, params
  return { response_type: 'in_channel' }, lambda do
    return { response_type: 'in_channel', text: "You're not allowed to do that" }, params[:response_url] unless allowed? params

    new_incident = construct_incident sev_num, summary, params
    incident = open_incident new_incident, summary
    return { response_type: 'in_channel', text: 'Could not open incident' }, params[:response_url] unless incident

    incident = new_incident.merge incident
    log.debug \
      event: 'created incident',
      incident: incident
    return {
      response_type: 'in_channel',
      text: 'Opened incident',
      attachments: [
        {
           title: incident[:key],
           title_link: File.join(settings.jira_url, 'browse', incident[:key]),
           text: incident[:fields][:summary]
        }
      ]
    }, params[:response_url]
  end
end

#reply_resolve(inc_num, params) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ferris-bueller/replies.rb', line 96

def reply_resolve inc_num, params
  return { response_type: 'in_channel' }, lambda do
    return { response_type: 'in_channel', text: "You're not allowed to do that" }, params[:response_url]  unless allowed? params

    incident = select_incident inc_num
    return { response_type: 'in_channel', text: 'Could not list incidents' }, params[:response_url]  unless incident

    resolution = resolve_incident incident
    return { response_type: 'in_channel', text: 'Could not resolve incident' }, params[:response_url]  if resolution.nil?
    return { response_type: 'in_channel', text: 'Already resolved' } if resolution == false

    return {
      response_type: 'in_channel',
      text: 'Resolved incident',
      attachments: [
        attach_incident(incident)
      ]
    }, params[:response_url]
  end
end

#reply_show(inc_num, params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ferris-bueller/replies.rb', line 83

def reply_show inc_num, params
  incident = select_incident inc_num
  return { text: 'Could not list incidents' } unless incident

  {
    text: 'Incident info',
    attachments: [
      attach_incident(incident, true)
    ]
  }
end

#reply_summary(params) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ferris-bueller/replies.rb', line 59

def reply_summary params
  incidents = recent_incidents
  return { response_type: 'in_channel', text: 'Could not list incidents' } if incidents.nil?
  return { response_type: 'in_channel', text: 'No recent incidents' } if incidents.empty?

  incidents = incidents.sort { |i| i[:id].to_i }.reverse

  partitioned_incidents = []
  severities = incidents.map { |i| i[:fields][:customfield_11250][:value] }.sort.uniq
  severities.each do |severity|
    partitioned_incidents += incidents.select { |i| i[:fields][:customfield_11250][:value] == severity }
  end

  attachments = partitioned_incidents.map do |i|
    attach_incident i
  end
  {
    response_type: 'in_channel',
    text: 'Found %d recent incidents' % attachments.size,
    attachments: attachments
  }
end

#reply_whoami(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ferris-bueller/replies.rb', line 9

def reply_whoami params
  u = user_lookup(params)
  if u
    { text: "You're <@#{params[:user_id]}>",
      attachments: [
        {
          title: 'Slack User',
          text: "```#{JSON.pretty_generate(u[:slack])}```",
          mrkdwn_in: %w[ text pretext ]
        },
        {
          title: 'Jira User',
          text: "```#{JSON.pretty_generate(u[:jira])}```",
          mrkdwn_in: %w[ text pretext ]
        }
      ]
    }
  else
    {
      text: "You're <@#{params[:user_id]}>, but I can't say much more than that"
    }
  end
end