|
1 | 1 | require 'sinatra'
|
2 | 2 | require 'json'
|
3 |
| -require 'uri' |
4 |
| -require 'net/http' |
| 3 | +require 'rest-client' |
5 | 4 |
|
6 | 5 | $github_api_token = ENV['GITHUB_API_TOKEN']
|
7 | 6 | $github_secret_token = ENV['SECRET_TOKEN']
|
|
20 | 19 | parsed = JSON.parse(request.body.read)
|
21 | 20 |
|
22 | 21 | # 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/") |
25 | 24 |
|
26 | 25 | # Get Repository owner
|
27 | 26 | repo_owner = parsed["repository"]["owner"]["name"]
|
|
30 | 29 | # e.g. https://api.github.com/repos/baxterthehacker/public-repo/pulls{/number}
|
31 | 30 | pulls_url = parsed['repository']['pulls_url']
|
32 | 31 |
|
33 |
| - # Pull off the {/number}" and search for all Pull Requests |
| 32 | + # Pull off the "{/number}" and search for all Pull Requests |
34 | 33 | # that include the branch
|
35 | 34 | 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) |
38 | 36 |
|
39 | 37 | # parse pull requests
|
40 | 38 | if pulls.empty?
|
|
44 | 42 |
|
45 | 43 | # Get all Reviews for a Pull Request via API
|
46 | 44 | 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) |
49 | 46 |
|
50 | 47 | reviews.each do |review|
|
51 | 48 |
|
52 | 49 | # Dismiss all Reviews in 'APPROVED' state via API
|
53 | 50 | if review["state"] == "APPROVED"
|
54 | 51 | puts "INFO: found an approved Review"
|
55 | 52 | review_id = review["id"]
|
56 |
| - dismiss_url = URI(review_url_orig + "/#{review_id}/dismissals") |
| 53 | + dismiss_url = review_url_orig + "/#{review_id}/dismissals" |
57 | 54 | put(dismiss_url)
|
58 | 55 | end
|
59 | 56 | end.empty? and begin
|
|
70 | 67 | end
|
71 | 68 |
|
72 | 69 | 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) |
91 | 80 | end
|
92 | 81 |
|
93 | 82 | 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) |
110 | 91 | end
|
111 | 92 |
|
112 | 93 | def verify_signature(payload_body)
|
|
0 commit comments