-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
200 lines (168 loc) · 5.07 KB
/
Copy pathapp.rb
File metadata and controls
200 lines (168 loc) · 5.07 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# frozen_string_literal: true
require "sinatra"
require "json"
require "faraday"
require "dotenv/load"
require "openssl"
require "rack/utils"
require "stringio"
set :bind, "0.0.0.0"
set :port, ENV.fetch("PORT", 4567)
before do
puts "HTTP_HOST => #{request.env['HTTP_HOST']}"
puts "SERVER_NAME => #{request.env['SERVER_NAME']}"
end
# -----------------------------
# Configuration
# -----------------------------
GITHUB_REPO_PREFIX="teetime-co-jp"
ALLOWED_TEAM_IDS = %w[T0D62LCBV].freeze
ALLOWED_REPOS = %w[firstee golfee greentee teetime-api].freeze
TRIGGERED_BRANCH = {
"firstee" => "master",
"golfee" => "main",
"greentee" => "main",
"teetime-api" => "main"
}
ALLOWED_STAGES = %w[staging staging_one staging_two staging_three].freeze
REQUEST_TTL_SECONDS = 300
# -----------------------------
# Helpers
# -----------------------------
helpers do
def json(body, status: 200)
content_type :json
halt status, body.to_json
end
def verify_slack_request!(team_id)
halt 403 unless ALLOWED_TEAM_IDS.include?(team_id)
timestamp = request.env["HTTP_X_SLACK_REQUEST_TIMESTAMP"]
signature = request.env["HTTP_X_SLACK_SIGNATURE"]
raw_body = request.env["rack.input.raw"]
halt 401, "Missing Slack headers" unless timestamp && signature
halt 401, "Missing raw body" unless raw_body
if (Time.now.to_i - timestamp.to_i).abs > REQUEST_TTL_SECONDS
halt 401, "Stale Slack request"
end
basestring = "v0:#{timestamp}:#{raw_body}"
digest = OpenSSL::HMAC.hexdigest(
"SHA256",
ENV.fetch("SLACK_SIGNING_SECRET"),
basestring
)
computed_signature = "v0=#{digest}"
unless Rack::Utils.secure_compare(computed_signature, signature)
halt 401, "Invalid Slack signature"
end
end
def github_client
@github_client ||= Faraday.new(
url: "https://api.github.com",
headers: {
"Authorization" => "Bearer #{ENV.fetch("GITHUB_TOKEN")}",
"Accept" => "application/vnd.github+json",
"X-GitHub-Api-Version" => "2022-11-28",
"Content-Type" => "application/json"
}
)
end
def trigger_github_action(repo:, stage:, branch:)
request_body = {
ref: "#{ENV.fetch("GITHUB_TRIGGER_BRANCH", TRIGGERED_BRANCH[repo])}",
inputs: {
stage: stage,
branch: branch
}
}.to_json
url = "/repos/#{GITHUB_REPO_PREFIX}/#{repo}/actions/workflows/#{ENV.fetch("GITHUB_WORKFLOW")}/dispatches"
p "Dispatching to #{url}"
p "request body: #{request_body}"
response = github_client.post do |req|
req.url url
req.body = request_body
end
return if response.success?
raise "GitHub dispatch failed (#{response.status}): #{response.body}"
end
end
# -----------------------------
# Routes
# -----------------------------
get "/" do
status 200
"OK v2"
end
post "/slack/deploy" do
verify_slack_request!(params[:team_id])
text = params[:text].to_s.strip
repo, stage, branch = text.split(/\s+/, 3)
p "Repo: #{repo}, Stage: #{stage}, Branch: #{branch}"
unless repo && stage && branch
return json(
{
response_type: "ephemeral",
text: "Usage: /deploy <repo> <stage> <branch>\nExample: /deploy golfee staging_two main"
}
)
end
unless ALLOWED_REPOS.include?(repo)
return json(
{
response_type: "ephemeral",
text: "Invalid stage. Allowed stages: #{ALLOWED_REPOS.join(', ')}"
},
# status: 403
)
end
unless ALLOWED_STAGES.include?(stage)
return json(
{
response_type: "ephemeral",
text: "Invalid stage. Allowed stages: #{ALLOWED_STAGES.join(', ')}"
},
# status: 403
)
end
unless branch.match?(/\A[\w\-\/\.]+\z/)
return json(
{
response_type: "ephemeral",
text: "Invalid branch name."
},
# status: 400
)
end
begin
trigger_github_action(repo: repo, stage: stage, branch: branch)
rescue => e
error_msg = e.message
p error_msg
formatted_error = if error_msg.include?("401")
"❌ *Authentication Failed*\n\nThe GitHub token appears to be invalid or expired.\nPlease check your `GITHUB_<repo>_TOKEN` configuration."
elsif error_msg.include?("404")
"❌ *Not Found*\n\nCouldn't find the repository or workflow.\nPlease verify:\n• Repo: `#{ENV['GITHUB_REPO']}`\n• Workflow: `#{ENV['GITHUB_WORKFLOW']}`"
elsif error_msg.include?("403")
"❌ *Permission Denied*\n\nThe GitHub token doesn't have permission to trigger workflows.\nMake sure the token has `workflow` scope."
else
"❌ *Deployment Failed*\n\n```#{error_msg}```"
end
return json(
{
response_type: "ephemeral",
text: formatted_error
}
)
end
json(
{
response_type: "in_channel",
text: <<~MSG.strip
🚀 Deployment started
• Repo: `#{repo}`
• Stage: `#{stage}`
• Branch: `#{branch}`
• Triggered by: <@#{params[:user_id]}>
MSG
}
)
end