forked from github/homebrew-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrew-report-issue.rb
More file actions
executable file
·141 lines (123 loc) · 4.1 KB
/
brew-report-issue.rb
File metadata and controls
executable file
·141 lines (123 loc) · 4.1 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
#!/usr/bin/env ruby
# Creates and closes failure debugging issues on a project.
close = !!ARGV.delete("--close")
user_repo = ARGV.shift
message = ARGV.shift
if user_repo.to_s.empty? || message.to_s.empty?
abort "Usage: brew report-issue [--close] <user/repo> <message> [<STDIN piped body>]"
end
unless close
abort "Error: the issue/comment body should be piped over STDIN!" if STDIN.tty?
body = STDIN.read
end
@strap_url = ENV["STRAP_URL"]
@strap_url ||= "https://strap.githubapp.com"
if `git config --global credential.helper`.chomp.empty?
abort <<-EOS
Error: your Git HTTP(S) credential helper is not set! Set it by running Strap:
#{@strap_url}
EOS
end
def credential_helper(command, input)
IO.popen({"RUBYLIB" => nil, "RUBYOPT" => nil},
["git", "credential", "#{command}"], "w+") do |io|
io.puts input
io.close_write
io.read
end
end
@github_credentials = credential_helper :fill, "protocol=https\nhost=github.com"
/username=(?<github_username>.+)/ =~ @github_credentials
/password=(?<github_password>.+)/ =~ @github_credentials
if github_username.to_s.empty?
abort <<-EOS
Error: your GitHub username is not set! Set it by running Strap:
#{@strap_url}
EOS
end
@github_username = ENV["BOXEN_GITHUB_LOGIN"].to_s
@github_username = `git config github.user`.chomp if @github_username.empty?
@github_username = github_username if @github_username.empty?
@github_api_username = github_username
if github_password.to_s.empty?
abort <<-EOS
Error: your GitHub password is not set! Set it by running Strap:
#{@strap_url}
EOS
end
@github_api_password = github_password
credential_helper :approve, @github_credentials
require "net/http"
require "json"
def http_request type, url, body=nil
uri = URI url
request = if type == :post
post_request = Net::HTTP::Post.new uri
post_request.body = body
post_request
elsif type == :get
Net::HTTP::Get.new uri
end
return unless request
request.basic_auth @github_api_username, @github_api_password
Net::HTTP.start uri.hostname, uri.port, use_ssl: true do |http|
http.request request
end
end
def response_check response, action
return if response.is_a? Net::HTTPSuccess
# If there's bad credentials, erase them.
credential_helper :reject, @github_credentials if response.code == "401"
STDERR.puts "Error: failed to #{action}!"
unless response.body.empty?
failure = JSON.parse response.body
STDERR.puts "--\n#{response.code}: #{failure["message"] }"
end
if response.code == "401"
STDERR.puts <<-EOS
Error: your GitHub username/access token are not correct! Fix by running Strap:
#{@strap_url}
EOS
end
exit 1
end
def create_issue user_repo, title, body
new_issue_json = { title: title, body: body }.to_json
issues_url = "https://api.github.com/repos/#{user_repo}/issues"
response = http_request :post, issues_url, new_issue_json
response_check response, "create issue (#{issues_url})"
issue = JSON.parse response.body
puts "Created issue: #{issue["html_url"]}"
issue
end
def comment_issue issue, comment_body, options={}
comments_url = issue["comments_url"]
issue_comment_json = { body: comment_body }.to_json
response = http_request :post, comments_url, issue_comment_json
response_check response, "create comment (#{comments_url})"
puts "Commented on issue: #{issue["html_url"]}" if options[:notify]
end
def close_issue issue
issue_url = issue["url"]
close_issue_json = { state: "closed" }.to_json
response = http_request :post, issue_url, close_issue_json
response_check response, "close issue (#{issue_url})"
end
open_issues_url = \
"https://api.github.com/repos/#{user_repo}/issues?creator=#{@github_username}"
response = http_request :get, open_issues_url
response_check response, "get issues (#{open_issues_url})"
open_issues = JSON.parse response.body
if close
open_issues.each do |issue|
comment_body = "Succeeded at #{message}."
comment_issue issue, comment_body
close_issue issue
end
elsif open_issues.any?
issue = open_issues.first
comment_issue issue, body, notify: true
else
title = "#{message} failed for #{@github_username}"
create_issue user_repo, title, body
end