-
Notifications
You must be signed in to change notification settings - Fork 3
/
concierge_handler.rb
112 lines (87 loc) · 2.84 KB
/
concierge_handler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require_relative 'lib/slack_api'
require_relative 'lib/reservation'
require_relative 'lib/concierge'
class ConciergeHandler
def self.handle(event:, context:)
ConciergeHandler.new(event).action
end
def initialize(payload)
@payload = payload
@slack_api = SlackAPI.new(slack_token: ENV['BOT_TOKEN'])
end
attr_reader :payload, :slack_api
def action
requesting_user = slack_api.user_details(user_id)
p "Received mention from #{requesting_user.name}: #{incoming_message}"
case incoming_message
when /@\w+ list$/
reply(nicer_env_list)
when /@\w+ supported-envs/
reply(supported_envs)
when /@\w+ help$/
reply(help_me_obi_wan)
else
reservation = Reservation.from_message(message: incoming_message, user: requesting_user)
if reservation.valid_env?
Concierge.new.reserve(reservation: reservation)
reply(reservation.human_readable)
else
reply(supported_envs)
end
end
end
private
def user_id
payload['event']['user']
end
def incoming_message
Slack::Messages::Formatting.unescape(payload['event']['text'])
end
def incoming_channel
Slack::Messages::Formatting.unescape(payload['event']['channel'])
end
def reply(message)
slack_api.talk_back(user_id: user_id, channel: incoming_channel, message: message)
end
def nicer_env_list
reservations = Concierge.new.reservations
return 'All environments are free for use' if reservations.empty?
the_list="\n"
reservations.reduce('') do |acc, e|
end_time = e.end_time.in_time_zone(e.timezone).strftime('%a %d, %R')
the_list += <<~TEXT
#{e.user_name} has `#{e.environment}` until #{end_time}. Reason: #{e.comment}
TEXT
end
the_list
end
def help_me_obi_wan
<<~TEXT
Reservebot - Reserve staging environment stacks on a specific stack and time limited basis.
Basic Syntax is:
@reservebot <env name> <start time> <end time> <comment>
Example usages are:
// Reserve for 1 hour starting now, with a comment
`@reservebot #{env_list.sample} now 1h Just Testing`
// Reserve starting at 1pm with no set end (i.e use '-')
`@reservebot #{env_list.sample} 13:00 - Reserving for good reasons`
// Reserve starting at 1pm with a specific end time (and no comment)
`@reservebot #{env_list.sample} 13:00 16:45`
// Release an environment again
`@reservebot #{env_list.sample} free`
// See what's currently held
`@reservebot list`
// List environments supported by CI
`@reservebot supported-envs`
TEXT
end
def env_list
ENV['SUPPORTED_ENVS'].split(',')
end
def supported_envs
message = <<~TEXT
Environments that CI will recognise:
TEXT
message + env_list.map { |env| "* #{env}\n" }.join
end
end