-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.rb
75 lines (59 loc) · 1.68 KB
/
app.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
ENV["RACK_ENV"] ||= "development"
Bundler.require(:default, ENV["RACK_ENV"])
require "sinatra/custom_logger"
class App < Sinatra::Base
helpers Sinatra::CustomLogger
use Sentry::Rack::CaptureExceptions
configure do
debug_logging = ENV["DEBUG_LOGGING"] == "true"
logger = Logger.new($stdout)
logger.level = debug_logging ? Logger::DEBUG : Logger::INFO
set :logger, logger
Sentry.init do |config|
config.enabled_environments = %w[production development]
config.release = File.read(File.join(__dir__, "VERSION")).strip
end
end
get "/" do
"It works"
end
post "/webhook" do
payload = JSON.parse(request.body.read)
logger.debug { "payload=#{payload}, params=#{params}" }
message = <<~MSG
Build finished by @#{payload["push_data"]["pusher"]} :beer:
#{payload["repository"]["repo_name"]}:#{payload["push_data"]["tag"]}
#{payload["repository"]["repo_url"]}
MSG
logger.debug { "message=#{message}" }
channel =
if params[:channel].blank?
ENV["SLACK_CHANNEL"]
else
"##{params[:channel]}"
end
App.post_slack(
webhook_url: ENV["SLACK_WEBHOOK_URL"],
username: ENV["SLACK_USERNAME"],
channel:,
message:,
)
""
end
def self.post_slack(webhook_url:, channel:, username:, message:)
notifier = Slack::Notifier.new(webhook_url)
options = {
icon_emoji: ":whale:",
attachments: [
{
fallback: message,
text: message,
color: "good",
},
],
}
options[:username] = username if username
options[:channel] = channel if channel
notifier.ping(nil, options)
end
end