forked from joffotron/env-reservation-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcierge_handler.rb
120 lines (97 loc) · 2.85 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
113
114
115
116
117
118
119
120
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)
Concierge.new.reserve(reservation: reservation)
reply(reservation.human_readable)
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.
Example usages are:
// Reserve for 1 hour starting now, with a comment
`@reservebot user-experience now 1h Just Testing`
// Reserve starting at 1pm with no set end (no comment)
`@reservebot backend:lifecycle 13:00 -`
// Release an environment again
`@reservebot smartshift free`
// See what's currently held
`@reservebot list`
// List environments supported by CI
`@reservebot supported-envs`
TEXT
end
def supported_envs
<<~TEXT
Environments that CI will recognise:
* admin-web
* cba-signup
* mobile-app
* webapp
* api
* cba-api
* cognito-triggers
* pg-migrations
* internal-process
* Backend stacks:
* smartshift
* smartshift-enrollments
* demand-response
* workflow
* jump
* office-signage
* ux (user-experience)
* forecasting
* lifecycle
* notifications
* operations
* telemetry
TEXT
end
end