From a55127ad926889d43e7e1a31139f91bf07884a9d Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Sun, 14 Jun 2020 18:33:08 -0400 Subject: [PATCH] Favoring default_branch instead of "master" First, "master" is a lazy assumption. Github supports a default branch, which at the time of writing assumes "master". However, the default of "master" may not be the case. Instead, this change now checks with the repository: "What is your default branch?" From that default branch Huborg creates pull requests. The default_branch is an attribute on the repository object ([as per documentation][1]). Note, I discovered this when I ran Samvera's maintenance script to push up our recent changes to Samvera's Contributing document. See [samvera/maintenance#44][2]. ```console Octokit::NotFound: GET https://api.github.com/repos/samvera-labs/sufia.io/git/refs/heads/master: 404 - Not Found // See: https://developer.github.com/v3/git/refs/#get-a-reference ```` [1]:https://developer.github.com/v3/repos/#list-organization-repositories [2]:https://github.com/samvera/maintenance/pull/44 --- lib/huborg.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/huborg.rb b/lib/huborg.rb index 7451045..bf7044f 100644 --- a/lib/huborg.rb +++ b/lib/huborg.rb @@ -1,3 +1,4 @@ +# coding: utf-8 require "huborg/version" require 'octokit' require 'git' @@ -357,7 +358,7 @@ def each_github_repository(&block) end # @note Due to an implementation detail in octokit.rb, refs sometimes - # need to be "heads/master" and "refs/heads/master" as detailed + # need to be "heads/" and "refs/heads/" as detailed # below # @param repo [#full_name, #archived] Likely the result of Octokit::Client#org # @param template [String] name of the template to push out to all @@ -371,11 +372,11 @@ def each_github_repository(&block) # would likely want to overwrite. def push_template_to!(repo:, template:, filename:, overwrite: false) return if repo.archived - # Note: Sometimes I'm using "heads/master" and other times I'm using - # "refs/heads/master". There appears to be an inconsistency in + # Note: Sometimes I'm using "heads/" and other times I'm using + # "refs/heads/". There appears to be an inconsistency in # the implementation of octokit. - master = client.ref(repo.full_name, "heads/master") - copy_on_master = begin + default_branch = client.ref(repo.full_name, "heads/#{repo.default_branch}") + filename_ref_on_default_branch = begin # I have seen both a return value of nil or seen raised an Octokit::NotFound # exception (one for a file at root, the other for a file in a non-existent # directory) @@ -386,20 +387,20 @@ def push_template_to!(repo:, template:, filename:, overwrite: false) commit_message = "Adding/updating #{filename}\n\nThis was uploaded via automation." logger.info("Creating pull request for #{filename} on #{repo.full_name}") target_branch_name = "refs/heads/autoupdate-#{Time.now.utc.to_s.gsub(/\D+/,'')}" - if copy_on_master + if filename_ref_on_default_branch return unless overwrite - client.create_reference(repo.full_name, target_branch_name, master.object.sha) + client.create_reference(repo.full_name, target_branch_name, default_branch.object.sha) client.update_contents( repo.full_name, filename, commit_message, - copy_on_master.sha, + filename_ref_on_default_branch.sha, file: File.new(template, "r"), branch: target_branch_name ) - client.create_pull_request(repo.full_name, "refs/heads/master", target_branch_name, commit_message) + client.create_pull_request(repo.full_name, "refs/heads/#{repo.default_branch}", target_branch_name, commit_message) else - client.create_reference(repo.full_name, target_branch_name, master.object.sha) + client.create_reference(repo.full_name, target_branch_name, default_branch.object.sha) client.create_contents( repo.full_name, filename, @@ -407,7 +408,7 @@ def push_template_to!(repo:, template:, filename:, overwrite: false) file: File.new(template, "r"), branch: target_branch_name ) - client.create_pull_request(repo.full_name, "refs/heads/master", target_branch_name, commit_message) + client.create_pull_request(repo.full_name, "refs/heads/#{repo.default_branch}", target_branch_name, commit_message) end end @@ -434,9 +435,9 @@ def clone_and_rebase_one!(repo:, directory:, skip_dirty: true, force: false, sha git.branch.stashes.save("Stashing via #{self.class}#clone_and_rebase!") end end - git.branch("master").checkout - logger.info("Pulling down master branch from origin for #{repo_path}") - git.pull("origin", "master") + git.branch(repo.default_branch).checkout + logger.info("Pulling down #{repo.default_branch} branch from origin for #{repo_path}") + git.pull("origin", repo.default_branch) else parent_directory = File.dirname(repo_path) logger.info("Creating #{parent_directory}")