Skip to content

Commit cea6782

Browse files
author
Thomas Osowski
committed
Switch to Gem rest-client
1 parent f2ecee1 commit cea6782

File tree

3 files changed

+42
-44
lines changed

3 files changed

+42
-44
lines changed

hooks/ruby/dismiss-review-server/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ source "http://rubygems.org"
22

33
gem "json", "~> 1.8"
44
gem 'sinatra', '~> 1.3.5'
5+
gem 'rest-client'

hooks/ruby/dismiss-review-server/Gemfile.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
GEM
22
remote: http://rubygems.org/
33
specs:
4+
domain_name (0.5.20161129)
5+
unf (>= 0.0.5, < 1.0.0)
6+
http-cookie (1.0.3)
7+
domain_name (~> 0.5)
48
json (1.8.6)
9+
mime-types (3.1)
10+
mime-types-data (~> 3.2015)
11+
mime-types-data (3.2016.0521)
12+
netrc (0.11.0)
513
rack (1.6.5)
614
rack-protection (1.5.3)
715
rack
16+
rest-client (2.0.1)
17+
http-cookie (>= 1.0.2, < 2.0)
18+
mime-types (>= 1.16, < 4.0)
19+
netrc (~> 0.8)
820
sinatra (1.3.6)
921
rack (~> 1.4)
1022
rack-protection (~> 1.3)
1123
tilt (~> 1.3, >= 1.3.3)
1224
tilt (1.4.1)
25+
unf (0.1.4)
26+
unf_ext
27+
unf_ext (0.0.7.2)
1328

1429
PLATFORMS
1530
ruby
1631

1732
DEPENDENCIES
1833
json (~> 1.8)
34+
rest-client
1935
sinatra (~> 1.3.5)
2036

2137
BUNDLED WITH

hooks/ruby/dismiss-review-server/server.rb

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'sinatra'
22
require 'json'
3-
require 'uri'
4-
require 'net/http'
3+
require 'rest-client'
54

65
$github_api_token = ENV['GITHUB_API_TOKEN']
76
$github_secret_token = ENV['SECRET_TOKEN']
@@ -20,8 +19,8 @@
2019
parsed = JSON.parse(request.body.read)
2120

2221
# Get branch information
23-
branch_head = parsed['ref']
24-
branch_name = branch_head.chomp("refs/heads")
22+
branch_name = parsed['ref']
23+
branch_name.slice!("refs/heads/")
2524

2625
# Get Repository owner
2726
repo_owner = parsed["repository"]["owner"]["name"]
@@ -30,11 +29,10 @@
3029
# e.g. https://api.github.com/repos/baxterthehacker/public-repo/pulls{/number}
3130
pulls_url = parsed['repository']['pulls_url']
3231

33-
# Pull off the {/number}" and search for all Pull Requests
32+
# Pull off the "{/number}" and search for all Pull Requests
3433
# that include the branch
3534
pulls_url_filtered = pulls_url.split('{').first + "?head=#{repo_owner}:#{branch_name}"
36-
url = URI(pulls_url_filtered)
37-
pulls = get(url)
35+
pulls = get(pulls_url_filtered)
3836

3937
# parse pull requests
4038
if pulls.empty?
@@ -44,16 +42,15 @@
4442

4543
# Get all Reviews for a Pull Request via API
4644
review_url_orig = pull_request["url"] + "/reviews"
47-
review_url = URI(review_url_orig)
48-
reviews = get(review_url)
45+
reviews = get(review_url_orig)
4946

5047
reviews.each do |review|
5148

5249
# Dismiss all Reviews in 'APPROVED' state via API
5350
if review["state"] == "APPROVED"
5451
puts "INFO: found an approved Review"
5552
review_id = review["id"]
56-
dismiss_url = URI(review_url_orig + "/#{review_id}/dismissals")
53+
dismiss_url = review_url_orig + "/#{review_id}/dismissals"
5754
put(dismiss_url)
5855
end
5956
end.empty? and begin
@@ -70,43 +67,27 @@
7067
end
7168

7269
def put(url)
73-
http = Net::HTTP.new(url.host, url.port)
74-
http.use_ssl = true
75-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
76-
77-
request = Net::HTTP::Put.new(url)
78-
request["authorization"] = "token #{$github_api_token}"
79-
request["accept"] = 'application/vnd.github.black-cat-preview+json'
80-
request["content"] = '0'
81-
request["content-type"] = 'application/json'
82-
request["cache-control"] = 'no-cache'
83-
request.body = "{\n\t\"message\":\"Auto-dismissing\"\n}"
84-
85-
response = http.request(request)
86-
if response.message != "OK"
87-
[]
88-
else
89-
JSON.parse(response.read_body)
90-
end
70+
jdata = JSON.generate({ message: "Auto-dismissing"})
71+
headers = {
72+
params:
73+
{
74+
access_token: $github_api_token
75+
},
76+
accept: "application/vnd.github.black-cat-preview+json"
77+
}
78+
response = RestClient.put(url, jdata, headers)
79+
JSON.parse(response.body)
9180
end
9281

9382
def get(url)
94-
http = Net::HTTP.new(url.host, url.port)
95-
http.use_ssl = true
96-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
97-
98-
request = Net::HTTP::Get.new(url)
99-
request["authorization"] = "token #{$github_api_token}"
100-
# Use `application/vnd.github.v3+json` when Reviews is out of preview period
101-
request["accept"] = 'application/vnd.github.black-cat-preview+json'
102-
request["cache-control"] = 'no-cache'
103-
104-
response = http.request(request)
105-
if response.message != "OK"
106-
[]
107-
else
108-
JSON.parse(response.read_body)
109-
end
83+
headers = {
84+
params: {
85+
access_token: $github_api_token
86+
},
87+
accept: "application/vnd.github.black-cat-preview+json"
88+
}
89+
response = RestClient.get(url, headers)
90+
JSON.parse(response.body)
11091
end
11192

11293
def verify_signature(payload_body)

0 commit comments

Comments
 (0)