/
server.rb
99 lines (84 loc) · 2.7 KB
/
server.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
require 'sinatra'
require 'json'
require 'rest-client'
$github_api_token = ENV['GITHUB_API_TOKEN']
$github_secret_token = ENV['SECRET_TOKEN']
post '/payload' do
# Only validate secret token if set
if !$github_secret_token.nil?
payload_body = request.body.read
verify_signature(payload_body)
end
github_event = request.env['HTTP_X_GITHUB_EVENT']
if github_event == "push"
request.body.rewind
parsed = JSON.parse(request.body.read)
# Get branch information
branch_name = parsed['ref']
removed_slice = branch_name.slice!("refs/heads/")
if removed_slice.nil?
return "Not a branch. Nothing to do."
end
# Get Repository owner
repo_owner = parsed["repository"]["owner"]["name"]
# Create URL to look up Pull Requests for this branch
# e.g. https://api.github.com/repos/baxterthehacker/public-repo/pulls{/number}
pulls_url = parsed['repository']['pulls_url']
# Pull off the "{/number}" and search for all Pull Requests
# that include the branch
pulls_url_filtered = pulls_url.split('{').first + "?head=#{repo_owner}:#{branch_name}"
pulls = get(pulls_url_filtered)
# parse pull requests
if pulls.empty?
puts "empty"
else
pulls.each do |pull_request|
# Get all Reviews for a Pull Request via API
review_url_orig = pull_request["url"] + "/reviews"
reviews = get(review_url_orig)
reviews.each do |review|
# Dismiss all Reviews in 'APPROVED' state via API
if review["state"] == "APPROVED"
puts "INFO: found an approved Review"
review_id = review["id"]
dismiss_url = review_url_orig + "/#{review_id}/dismissals"
put(dismiss_url)
end
end.empty? and begin
puts "no reviews"
end
end
end
elsif github_event == "ping"
puts github_event
else
puts github_event
end
"message received"
end
def put(url)
jdata = JSON.generate({ message: "Auto-dismissing"})
headers = {
params:
{
access_token: $github_api_token
},
accept: "application/vnd.github.black-cat-preview+json"
}
response = RestClient.put(url, jdata, headers)
JSON.parse(response.body)
end
def get(url)
headers = {
params: {
access_token: $github_api_token
},
accept: "application/vnd.github.black-cat-preview+json"
}
response = RestClient.get(url, headers)
JSON.parse(response.body)
end
def verify_signature(payload_body)
signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), ENV['SECRET_TOKEN'], payload_body)
return halt 500, "Signatures didn't match!" unless Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE'])
end