Class: Octopolo::PullRequestMerger

Inherits:
Object
  • Object
show all
Includes:
CLIWrapper, ConfigWrapper, GitWrapper
Defined in:
lib/octopolo/pull_request_merger.rb

Instance Attribute Summary collapse

Attributes included from GitWrapper

#git

Attributes included from CLIWrapper

#cli

Attributes included from ConfigWrapper

#config

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(branch_type, pull_request_id, options = {}) ⇒ PullRequestMerger

Public: Initialize a new instance of DatedBranchCreator

branch_type - Name of the type of branch (e.g., staging or deployable) pull_request_id - The pull request id to use for the merge options - hash of options

- post_comment - whether or not to post a comment on the pull-request


20
21
22
23
24
# File 'lib/octopolo/pull_request_merger.rb', line 20

def initialize(branch_type, pull_request_id, options={})
  self.branch_type = branch_type
  self.pull_request_id = pull_request_id
  self.options = options
end

Instance Attribute Details

#branch_typeObject

Returns the value of attribute branch_type.



12
13
14
# File 'lib/octopolo/pull_request_merger.rb', line 12

def branch_type
  @branch_type
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/octopolo/pull_request_merger.rb', line 12

def options
  @options
end

#pull_requestObject

Public: Find the pull request

Returns a GitHub::PullRequest



93
94
95
# File 'lib/octopolo/pull_request_merger.rb', line 93

def pull_request
  @pull_request
end

#pull_request_idObject

Returns the value of attribute pull_request_id.



12
13
14
# File 'lib/octopolo/pull_request_merger.rb', line 12

def pull_request_id
  @pull_request_id
end

Class Method Details

.perform(branch_type, pull_request_id, options = {}) ⇒ Object

Public: Create a new branch of the given type for today’s date

branch_type - Name of the type of branch (e.g., staging or deployable) post_comment - Whether or not to comment on the pull-request

Returns a DatedBranchCreator



32
33
34
35
36
# File 'lib/octopolo/pull_request_merger.rb', line 32

def self.perform(branch_type, pull_request_id, options={})
  new(branch_type, pull_request_id, options).tap do |creator|
    creator.perform
  end
end

Instance Method Details

#branch_to_merge_intoObject

Public: Find the branch

Returns a String



100
101
102
# File 'lib/octopolo/pull_request_merger.rb', line 100

def branch_to_merge_into
  @branch_to_merge_into ||= git.latest_branch_for(branch_type)
end

#check_out_branchObject

Public: Check out the branch



62
63
64
65
66
67
# File 'lib/octopolo/pull_request_merger.rb', line 62

def check_out_branch
  git.check_out branch_to_merge_into
rescue Git::NoBranchOfType
  cli.say "No #{branch_type} branch available. Creating one now."
  git.check_out DatedBranchCreator.perform(branch_type).branch_name
end

#comment_about_mergeObject

Public: Comment that the pull request was merged into the branch



75
76
77
# File 'lib/octopolo/pull_request_merger.rb', line 75

def comment_about_merge
  pull_request.write_comment comment_body
end

#comment_bodyObject

Public: The content of the comment to post when merged

Returns a String



82
83
84
85
86
87
88
# File 'lib/octopolo/pull_request_merger.rb', line 82

def comment_body
  body = "Merged into #{branch_to_merge_into}."
  if options[:user_notifications]
    body << " /cc #{options[:user_notifications].map {|name| "@#{name}"}.join(' ')}"
  end
  body
end

#merge_pull_requestObject

Public: Merge the pull request’s branch into the checked-out branch



70
71
72
# File 'lib/octopolo/pull_request_merger.rb', line 70

def merge_pull_request
  git.merge pull_request.branch
end

#performObject

Public: Create the branch and handle related processing



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/octopolo/pull_request_merger.rb', line 39

def perform
  git.if_clean do
    check_out_branch
    merge_pull_request
    comment_about_merge
  end
rescue => e
  case e
  when GitHub::PullRequest::NotFound
    cli.say "Unable to find pull request #{pull_request_id}. Please retry with a valid ID."
  when Git::MergeFailed
    cli.say "Merge failed. Please identify the source of this merge conflict resolve this conflict in your pull request's branch. NOTE: Merge conflicts resolved in the #{branch_type} branch are NOT used when deploying."
  when Git::CheckoutFailed
    cli.say "Checkout of #{branch_to_merge_into} failed. Please contact Infrastructure to determine the cause."
  when GitHub::PullRequest::CommentFailed
    cli.say "Unable to write comment. Please navigate to #{pull_request.url} and add the comment, '#{comment_body}'"
  else
    cli.say "An unknown error occurred: #{e.inspect}"
  end
  raise
end