Skip to content

Commit

Permalink
Merge pull request #15 from samvera-labs/replacing-branch-ref
Browse files Browse the repository at this point in the history
Favoring default_branch instead of "master"
  • Loading branch information
jeremyf authored Jun 15, 2020
2 parents e2c28ab + a55127a commit 4333ecf
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 4333ecf

Please sign in to comment.