From 620e96b929d364ee95ce4a3bcead890308e8717a Mon Sep 17 00:00:00 2001 From: Maxim Belkin Date: Mon, 11 Nov 2019 19:12:55 +0000 Subject: [PATCH 1/7] bump-formula-pr: enable same-repo (no-fork) Pull Requests --- Library/Homebrew/dev-cmd/bump-formula-pr.rb | 47 ++++++++++++--------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index c50ab97cfe..f33d4b822c 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -38,6 +38,8 @@ module Homebrew description: "Run `brew audit --strict` before opening the PR." switch "--no-browse", description: "Print the pull request URL instead of opening in a browser." + switch "--no-fork", + description: "Don't try to fork the repository." flag "--mirror=", description: "Use the specified as a mirror URL." flag "--version=", @@ -324,7 +326,7 @@ module Homebrew changed_files += alias_rename if alias_rename.present? if args.dry_run? - ohai "try to fork repository with GitHub API" + ohai "try to fork repository with GitHub API" unless args.no_fork? ohai "git fetch --unshallow origin" if shallow ohai "git add #{alias_rename.first} #{alias_rename.last}" if alias_rename.present? ohai "git checkout --no-track -b #{branch} #{origin_branch}" @@ -335,27 +337,32 @@ module Homebrew ohai "create pull request with GitHub API" else - begin - response = GitHub.create_fork(tap_full_name) - # GitHub API responds immediately but fork takes a few seconds to be ready. - sleep 3 - - if system("git", "config", "--get-regexp", "remote\..*\.url", "git@github.com:.*") - remote_url = response.fetch("ssh_url") - else - remote_url = response.fetch("clone_url") - end - username = response.fetch("owner").fetch("login") - rescue GitHub::AuthenticationFailedError => e - raise unless e.github_message.match?(/forking is disabled/) - - # If the repository is private, forking might be disabled. - # Create branches in the repository itself instead. + if args.no_fork? remote_url = Utils.popen_read("git remote get-url --push origin").chomp username = formula.tap.user - rescue *GitHub.api_errors => e - formula.path.atomic_write(backup_file) unless args.dry_run? - odie "Unable to fork: #{e.message}!" + else + begin + response = GitHub.create_fork(formula.tap.full_name) + # GitHub API responds immediately but fork takes a few seconds to be ready. + sleep 3 + + if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*") + remote_url = response.fetch("ssh_url") + else + remote_url = response.fetch("clone_url") + end + username = response.fetch("owner").fetch("login") + rescue GitHub::AuthenticationFailedError => e + raise unless e.github_message.match?(/forking is disabled/) + + # If the repository is private, forking might be disabled. + # Create branches in the repository itself instead. + remote_url = Utils.popen_read("git remote get-url --push origin").chomp + username = formula.tap.user + rescue *GitHub.api_errors => e + formula.path.atomic_write(backup_file) unless args.dry_run? + odie "Unable to fork: #{e.message}!" + end end safe_system "git", "fetch", "--unshallow", "origin" if shallow From 407baf4bcdc20fb05ddcebddd47371ac454ff4d4 Mon Sep 17 00:00:00 2001 From: Maxim Belkin Date: Mon, 11 Nov 2019 19:49:59 +0000 Subject: [PATCH 2/7] Update docs and man pages --- docs/Manpage.md | 2 ++ manpages/brew.1 | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/docs/Manpage.md b/docs/Manpage.md index b0100f2379..94c439cff6 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -762,6 +762,8 @@ uses. Run `brew audit --strict` before opening the PR. * `--no-browse`: Print the pull request URL instead of opening in a browser. +* `--no-fork`: + Don't try to fork the repository. * `--mirror`: Use the specified *`URL`* as a mirror URL. * `--version`: diff --git a/manpages/brew.1 b/manpages/brew.1 index e4e7936970..2162b8558b 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -966,6 +966,10 @@ Run \fBbrew audit \-\-strict\fR before opening the PR\. Print the pull request URL instead of opening in a browser\. . .TP +\fB\-\-no\-fork\fR +Don\'t try to fork the repository\. +. +.TP \fB\-\-mirror\fR Use the specified \fIURL\fR as a mirror URL\. . From 845f65b9451d290214f8c04dfcb9d9d3862d258d Mon Sep 17 00:00:00 2001 From: Maxim Belkin Date: Tue, 19 Nov 2019 13:08:28 -0600 Subject: [PATCH 3/7] Factor out some code into functions --- Library/Homebrew/dev-cmd/bump-formula-pr.rb | 40 ++++++++++----------- Library/Homebrew/utils/github.rb | 9 +++++ 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index f33d4b822c..a2af323e36 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -341,28 +341,7 @@ module Homebrew remote_url = Utils.popen_read("git remote get-url --push origin").chomp username = formula.tap.user else - begin - response = GitHub.create_fork(formula.tap.full_name) - # GitHub API responds immediately but fork takes a few seconds to be ready. - sleep 3 - - if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*") - remote_url = response.fetch("ssh_url") - else - remote_url = response.fetch("clone_url") - end - username = response.fetch("owner").fetch("login") - rescue GitHub::AuthenticationFailedError => e - raise unless e.github_message.match?(/forking is disabled/) - - # If the repository is private, forking might be disabled. - # Create branches in the repository itself instead. - remote_url = Utils.popen_read("git remote get-url --push origin").chomp - username = formula.tap.user - rescue *GitHub.api_errors => e - formula.path.atomic_write(backup_file) unless args.dry_run? - odie "Unable to fork: #{e.message}!" - end + remote_url, username = forked_repo_info(formula) end safe_system "git", "fetch", "--unshallow", "origin" if shallow @@ -401,6 +380,23 @@ module Homebrew end end + def forked_repo_info(formula) + response = GitHub.create_fork(formula.tap.full_name) + rescue GitHub::AuthenticationFailedError, *GitHub.api_errors => e + formula.path.atomic_write(backup_file) unless args.dry_run? + odie "Unable to fork: #{e.message}!" + else + # GitHub API responds immediately but fork takes a few seconds to be ready. + sleep 1 until GitHub.check_fork_exists(formula.tap.full_name) + if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*") + remote_url = response.fetch("ssh_url") + else + remote_url = response.fetch("clone_url") + end + username = response.fetch("owner").fetch("login") + [remote_url, username] + end + def inreplace_pairs(path, replacement_pairs) if args.dry_run? contents = path.open("r") { |f| Formulary.ensure_utf8_encoding(f).read } diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 9a73d7b5d0..e9be0ac326 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -340,6 +340,15 @@ module GitHub open_api(url, data: data, scopes: scopes) end + def check_fork_exists(repo) + username = api_credentials[1] + reponame = repo.split("/")[1] + json = open_api(url_to("repos", username, reponame)) + return false if json["message"] == "Not Found" + + true + end + def create_pull_request(repo, title, head, base, body) url = "#{API_URL}/repos/#{repo}/pulls" data = { title: title, head: head, base: base, body: body } From a031fe911e502ec05723415b93db69f2ede117a1 Mon Sep 17 00:00:00 2001 From: Maxim Belkin Date: Tue, 19 Nov 2019 13:43:51 -0600 Subject: [PATCH 4/7] Switch from formula.tap.full_name to tap_formula_name --- Library/Homebrew/dev-cmd/bump-formula-pr.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index a2af323e36..7ac703fc5f 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -341,7 +341,7 @@ module Homebrew remote_url = Utils.popen_read("git remote get-url --push origin").chomp username = formula.tap.user else - remote_url, username = forked_repo_info(formula) + remote_url, username = forked_repo_info(tap_full_name) end safe_system "git", "fetch", "--unshallow", "origin" if shallow @@ -380,14 +380,14 @@ module Homebrew end end - def forked_repo_info(formula) - response = GitHub.create_fork(formula.tap.full_name) + def forked_repo_info(tap_full_name) + response = GitHub.create_fork(tap_full_name) rescue GitHub::AuthenticationFailedError, *GitHub.api_errors => e formula.path.atomic_write(backup_file) unless args.dry_run? odie "Unable to fork: #{e.message}!" else # GitHub API responds immediately but fork takes a few seconds to be ready. - sleep 1 until GitHub.check_fork_exists(formula.tap.full_name) + sleep 1 until GitHub.check_fork_exists(tap_full_name) if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*") remote_url = response.fetch("ssh_url") else From ba7a05a9194458604d0feaad5c15e2565b61930f Mon Sep 17 00:00:00 2001 From: Maxim Belkin Date: Wed, 20 Nov 2019 09:43:45 -0600 Subject: [PATCH 5/7] bump-formula-pr.rb: Apply suggestions from code review Co-Authored-By: Mike McQuaid --- Library/Homebrew/dev-cmd/bump-formula-pr.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index 7ac703fc5f..ed644e28d0 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -388,10 +388,10 @@ module Homebrew else # GitHub API responds immediately but fork takes a few seconds to be ready. sleep 1 until GitHub.check_fork_exists(tap_full_name) - if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*") - remote_url = response.fetch("ssh_url") + remote_url = if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*") + response.fetch("ssh_url") else - remote_url = response.fetch("clone_url") + response.fetch("clone_url") end username = response.fetch("owner").fetch("login") [remote_url, username] From 1a0361a57d076fd0cee0348a3fc7a5bea1b026fd Mon Sep 17 00:00:00 2001 From: Maxim Belkin Date: Wed, 20 Nov 2019 12:38:50 -0600 Subject: [PATCH 6/7] Don't check dry run status where it is not needed --- Library/Homebrew/dev-cmd/bump-formula-pr.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index ed644e28d0..58cb293265 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -383,7 +383,7 @@ module Homebrew def forked_repo_info(tap_full_name) response = GitHub.create_fork(tap_full_name) rescue GitHub::AuthenticationFailedError, *GitHub.api_errors => e - formula.path.atomic_write(backup_file) unless args.dry_run? + formula.path.atomic_write(backup_file) odie "Unable to fork: #{e.message}!" else # GitHub API responds immediately but fork takes a few seconds to be ready. From c7f065b8da7a0da65bc5a39a156a6a14eb28f834 Mon Sep 17 00:00:00 2001 From: Maxim Belkin Date: Thu, 21 Nov 2019 11:10:43 -0600 Subject: [PATCH 7/7] utils/github.rb: use parallel assignments rather than indices Co-Authored-By: Mike McQuaid --- Library/Homebrew/utils/github.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index e9be0ac326..2e6695f4f4 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -341,8 +341,8 @@ module GitHub end def check_fork_exists(repo) - username = api_credentials[1] - reponame = repo.split("/")[1] + _, username = api_credentials + _, reponame = repo.split("/") json = open_api(url_to("repos", username, reponame)) return false if json["message"] == "Not Found"