Various cleanup and fixes.

Rename some variables and use existing regexes and a single variable.
This commit is contained in:
Mike McQuaid 2018-05-21 15:36:26 +01:00
parent 26e77dd75c
commit e2968c7889
2 changed files with 30 additions and 23 deletions

View File

@ -137,16 +137,18 @@ module Homebrew
end end
end end
create_issue_failed = false created_pr_comment = false
if new_formula && !new_formula_problem_lines.size.zero? && !GitHub.create_issue_no_op? if new_formula && !new_formula_problem_lines.empty?
begin begin
GitHub.create_issue_comment(new_formula_problem_lines.join("\n")) if GitHub.create_issue_comment(new_formula_problem_lines.join("\n"))
created_pr_comment = true
end
rescue *GitHub.api_errors rescue *GitHub.api_errors
create_issue_failed = true nil
end end
end end
if GitHub.create_issue_no_op? || create_issue_failed unless created_pr_comment
problem_count += new_formula_problem_lines.size problem_count += new_formula_problem_lines.size
puts new_formula_problem_lines.map { |s| " #{s}" } puts new_formula_problem_lines.map { |s| " #{s}" }
end end

View File

@ -10,6 +10,8 @@ module GitHub
CREATE_ISSUE_FORK_OR_PR_SCOPES = ["public_repo"].freeze CREATE_ISSUE_FORK_OR_PR_SCOPES = ["public_repo"].freeze
ALL_SCOPES = (CREATE_GIST_SCOPES + CREATE_ISSUE_FORK_OR_PR_SCOPES).freeze ALL_SCOPES = (CREATE_GIST_SCOPES + CREATE_ISSUE_FORK_OR_PR_SCOPES).freeze
ALL_SCOPES_URL = Formatter.url("https://github.com/settings/tokens/new?scopes=#{ALL_SCOPES.join(",")}&description=Homebrew").freeze ALL_SCOPES_URL = Formatter.url("https://github.com/settings/tokens/new?scopes=#{ALL_SCOPES.join(",")}&description=Homebrew").freeze
PR_ENV_KEY = "HOMEBREW_NEW_FORMULA_PULL_REQUEST_URL".freeze
PR_ENV = ENV[PR_ENV_KEY]
Error = Class.new(RuntimeError) Error = Class.new(RuntimeError)
HTTPNotFoundError = Class.new(Error) HTTPNotFoundError = Class.new(Error)
@ -254,14 +256,14 @@ module GitHub
end end
def create_fork(repo) def create_fork(repo)
url = "https://api.github.com/repos/#{repo}/forks" url = "#{API_URL}/repos/#{repo}/forks"
data = {} data = {}
scopes = CREATE_ISSUE_FORK_OR_PR_SCOPES scopes = CREATE_ISSUE_FORK_OR_PR_SCOPES
open_api(url, data: data, scopes: scopes) open_api(url, data: data, scopes: scopes)
end end
def create_pull_request(repo, title, head, base, body) def create_pull_request(repo, title, head, base, body)
url = "https://api.github.com/repos/#{repo}/pulls" url = "#{API_URL}/repos/#{repo}/pulls"
data = { title: title, head: head, base: base, body: body } data = { title: title, head: head, base: base, body: body }
scopes = CREATE_ISSUE_FORK_OR_PR_SCOPES scopes = CREATE_ISSUE_FORK_OR_PR_SCOPES
open_api(url, data: data, scopes: scopes) open_api(url, data: data, scopes: scopes)
@ -292,27 +294,30 @@ module GitHub
open_api(uri) { |json| json.fetch("items", []) } open_api(uri) { |json| json.fetch("items", []) }
end end
def create_issue_no_op? def create_issue_comment(body)
!ENV.key?("HOMEBREW_GH_REPO") || !ENV.key?("HOMEBREW_NEW_FORMULA_PULL_REQUEST_URL") return false unless PR_ENV
_, user, repo, pr = *PR_ENV.match(HOMEBREW_PULL_OR_COMMIT_URL_REGEX)
if !user || !repo || !pr
opoo <<-EOS.undent
#{PR_ENV_KEY} set but regex matched:
user: #{user.inspect}, repo: #{repo.inspect}, pr: #{pr.inspect}
EOS
return false
end end
def create_issue_comment(body) url = "#{API_URL}/repos/#{user}/#{repo}/issues/#{pr}/comments"
repo = ENV["HOMEBREW_GH_REPO"]
pull_request = extract_pull_request_number
url = "https://api.github.com/repos/Homebrew/#{repo}/issues/#{pull_request}/comments"
data = { "body" => body } data = { "body" => body }
return if issue_comment_exists?(repo, pr_number, body) if issue_comment_exists?(user, repo, pr, body)
scopes = CREATE_ISSUE_SCOPES ohai "Skipping: identical comment exists on #{PR_ENV}."
return true
end
scopes = CREATE_ISSUE_FORK_OR_PR_SCOPES
open_api(url, data: data, scopes: scopes) open_api(url, data: data, scopes: scopes)
end end
def extract_pull_request_number def issue_comment_exists?(user, repo, pr, body)
pattern = /#(\d+)$/ url = "#{API_URL}/repos/#{user}/#{repo}/issues/#{pr}/comments"
ENV["HOMEBREW_NEW_FORMULA_PULL_REQUEST_URL"].match(pattern)[1]
end
def issue_comment_exists?(repo, pull_reqest, body)
url = "https://api.github.com/repos/Homebrew/#{repo}/issues/#{pull_reqest}/comments"
comments = open_api(url) comments = open_api(url)
return unless comments return unless comments
comments.any? { |comment| comment["body"].eql?(body) } comments.any? { |comment| comment["body"].eql?(body) }