Skip to content

Instantly share code, notes, and snippets.

@atmos
Created February 26, 2013 21:54
Show Gist options
  • Save atmos/5042609 to your computer and use it in GitHub Desktop.
Save atmos/5042609 to your computer and use it in GitHub Desktop.
Simple integration branching with octokit
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'octokit'
class IntegrationBranch
def self.user
ENV['GITHUB_USER'] || ENV['USER']
end
def self.token
ENV['GITHUB_API_TOKEN'] || fail("Please set a GITHUB_API_TOKEN")
end
def self.api
@api ||= Octokit::Client.new(:oauth_token => token)
end
def api
self.class.api
end
attr_reader :name, :branch, :left_branch, :right_branch
def initialize(name, left_branch, right_branch)
@name = name
@branch = "#{left_branch}+#{right_branch}"
@left_branch = left_branch
@right_branch = right_branch
end
def left_head
@left_head ||= find_ref(left_branch)
end
def right_head
@right_head ||= find_ref(right_branch)
end
def find_ref(ref)
refs = api.ref(name, "heads/#{ref}")
if refs.kind_of?(Array)
match = refs.find { |r| r.ref == ref }
raise Octokit::NotFound.new unless match
else
match = refs
end
match.object
rescue Octokit::NotFound => e
raise ArgumentError.new("#{name} doesn't have a '#{ref}' ref.")
end
def branch_head
@branch_head ||= find_ref(branch)
rescue ArgumentError
puts "Creating #{name} - refs/heads/#{branch}@#{left_head['sha']}"
@branch_head = api.create_ref(name, "heads/#{branch}", left_head.sha)
end
def verify_refs
left_head
right_head
branch_head
end
def create_integration_branch
verify_refs
commit_message = "Integration branch created for #{left_branch} and #{right_branch}"
response = api.merge(name, branch, right_head.sha, :commit_message => commit_message)
response.respond_to?(:commit)
rescue Octokit::NotFound => e
puts e.message
false
end
end
if ARGV.size > 2
ib = IntegrationBranch.new(ARGV[0], ARGV[1], ARGV[2])
puts ib.left_head.inspect
puts ib.right_head.inspect
puts ib.create_integration_branch
else
$stderr.puts "Incorrect usage for topic branch creation\n"
$stderr.puts "#{$0} github/github branch1 branch2"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment