Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 18 additions & 47 deletions Library/Homebrew/cmd/gist-logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ def gistify_logs(f)
url = create_gist(files)

if ARGV.include?("--new-issue") || ARGV.switch?("n")
auth = :AUTH_TOKEN

unless HOMEBREW_GITHUB_API_TOKEN
puts "You can create a personal access token: https://github.com/settings/tokens"
puts "and then set HOMEBREW_GITHUB_API_TOKEN as authentication method."
puts

auth = :AUTH_BASIC
end

url = new_issue(f.tap, "#{f.name} failed to build on #{MACOS_FULL_VERSION}", url, auth)
url = new_issue(f.tap, "#{f.name} failed to build on #{MACOS_FULL_VERSION}", url)
end

puts url if url
Expand All @@ -49,15 +39,6 @@ def noecho_gets
result
end

def login(request)
print "GitHub User: "
user = $stdin.gets.chomp
print "Password: "
password = noecho_gets.chomp
puts
request.basic_auth(user, password)
end

def load_logs(dir)
logs = {}
dir.children.sort.each do |file|
Expand All @@ -69,11 +50,11 @@ def load_logs(dir)
end

def create_gist(files)
post("/gists", "public" => true, "files" => files)["html_url"]
post("/gists", "public" => true, "files" => files)
end

def new_issue(repo, title, body, auth)
post("/repos/#{repo}/issues", { "title" => title, "body" => body }, auth)["html_url"]
def new_issue(repo, title, body)
post("/repos/#{repo}/issues", { "title" => title, "body" => body })
end

def http
Expand All @@ -90,53 +71,43 @@ def http
end
end

def make_request(path, data, auth)
def make_request(path, data)
headers = {
"User-Agent" => HOMEBREW_USER_AGENT_CURL,
"Accept" => "application/vnd.github.v3+json",
"Content-Type" => "application/json"
"User-Agent" => HOMEBREW_USER_AGENT_CURL,
"Accept" => "application/vnd.github.v3+json",
"Content-Type" => "application/json",
"Authorization" => "token #{HOMEBREW_GITHUB_API_TOKEN}"
}

if auth == :AUTH_TOKEN || (auth.nil? && HOMEBREW_GITHUB_API_TOKEN)
headers["Authorization"] = "token #{HOMEBREW_GITHUB_API_TOKEN}"
end

request = Net::HTTP::Post.new(path, headers)

login(request) if auth == :AUTH_BASIC

request.body = Utils::JSON.dump(data)
request
end

def post(path, data, auth = nil)
request = make_request(path, data, auth)
def post(path, data)
request = make_request(path, data)

case response = http.request(request)
when Net::HTTPCreated
Utils::JSON.load get_body(response)
/"html_url": "([^\"]+)"/.match(response.body)[1]
else
raise "HTTP #{response.code} #{response.message} (expected 201)"
end
end

def get_body(response)
if !response.body.respond_to?(:force_encoding)
response.body
elsif response["Content-Type"].downcase == "application/json; charset=utf-8"
response.body.dup.force_encoding(Encoding::UTF_8)
else
response.body.encode(Encoding::UTF_8, :undef => :replace)
end
end

def gist_logs
if ARGV.resolved_formulae.length != 1
puts "usage: brew gist-logs [--new-issue|-n] <formula>"
Homebrew.failed = true
return
end

unless HOMEBREW_GITHUB_API_TOKEN
puts "Create a personal Github access token at: https://github.com/settings/tokens"
puts "and then set the environment variable HOMEBREW_GITHUB_API_TOKEN to its value."
return
end

gistify_logs(ARGV.resolved_formulae[0])
end
end