Skip to content

Commit

Permalink
Favoring default_branch instead of "master"
Browse files Browse the repository at this point in the history
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]:samvera/maintenance#44
  • Loading branch information
jeremyf committed Jun 14, 2020
1 parent e2c28ab commit a55127a
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions lib/huborg.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding: utf-8
require "huborg/version"
require 'octokit'
require 'git'
Expand Down Expand Up @@ -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/<branch>" and "refs/heads/<branch>" 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
Expand 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/<default>" and other times I'm using
# "refs/heads/<default>". 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)
Expand All @@ -386,28 +387,28 @@ 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,
commit_message,
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

Expand All @@ -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}")
Expand Down

0 comments on commit a55127a

Please sign in to comment.