Class: FlashFlow::Merge::Acceptance

Inherits:
Base
  • Object
show all
Defined in:
lib/flash_flow/merge/acceptance.rb

Instance Method Summary collapse

Methods inherited from Base

#check_repo, #check_version, #git_merge, #logger, #merge_branches

Constructor Details

#initialize(opts = {}) ⇒ Acceptance

Returns a new instance of Acceptance.



7
8
9
10
11
12
13
14
15
16
# File 'lib/flash_flow/merge/acceptance.rb', line 7

def initialize(opts={})
  super(opts)

  @data = Data::Base.new(Config.configuration.branches, Config.configuration.branch_info_file, @git, logger: logger)

  @do_not_merge = opts[:do_not_merge]
  @force = opts[:force]
  @rerere_forget = opts[:rerere_forget]
  @stories = [opts[:stories]].flatten.compact
end

Instance Method Details

#commit_branch_infoObject



57
58
59
60
61
62
# File 'lib/flash_flow/merge/acceptance.rb', line 57

def commit_branch_info
  @stories.each do |story_id|
    @data.add_story(@git.merge_remote, @git.working_branch, story_id)
  end
  @data.save!
end

#commit_messageObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/flash_flow/merge/acceptance.rb', line 138

def commit_message
  message ="Flash Flow run from branch: \#{@local_git.working_branch}\n\nMerged branches:\n\#{@data.successes.empty? ? 'None' : @data.successes.sort_by(&:merge_order).map(&:ref).join(\"\\n\")}\n\nFailed branches:\n\#{@data.failures.empty? ? 'None' : @data.failures.map(&:ref).join(\"\\n\")}\n\nRemoved branches:\n\#{@data.removals.empty? ? 'None' : @data.removals.map(&:ref).join(\"\\n\")}\n  EOS\n  message.gsub(/'/, '')\nend\n"

#commit_rerereObject



64
65
66
67
68
69
# File 'lib/flash_flow/merge/acceptance.rb', line 64

def commit_rerere
  current_branches = @data.to_a.select { |branch| !@git.master_branch_contains?(branch.sha) && (Time.now - branch.updated_at < two_weeks) }
  current_rereres = current_branches.map { |branch| branch.resolutions.to_h.values }.flatten

  @git.commit_rerere(current_rereres)
end

#format_errorsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/flash_flow/merge/acceptance.rb', line 119

def format_errors
  errors = []
  branch_not_merged = nil
  @data.failures.each do |branch|
    if branch.ref == @local_git.working_branch
      branch_not_merged = "ERROR: Your branch did not merge to #{@local_git.merge_branch}. Run 'flash_flow --resolve', fix the merge conflict(s) and then re-run this script\n"
    else
      errors << "WARNING: Unable to merge branch #{branch.remote}/#{branch.ref} to #{@local_git.merge_branch} due to conflicts."
    end
  end
  errors << branch_not_merged if branch_not_merged

  if errors.empty?
    "Success!"
  else
    errors.join("\n")
  end
end

#is_working_branch(branch) ⇒ Object



96
97
98
# File 'lib/flash_flow/merge/acceptance.rb', line 96

def is_working_branch(branch)
  branch.ref == @git.working_branch
end

#open_pull_requestObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/flash_flow/merge/acceptance.rb', line 100

def open_pull_request
  return false if [@local_git.master_branch, @local_git.merge_branch].include?(@local_git.working_branch)

  # TODO - This should use the actual remote for the branch we're on
  @local_git.push(@local_git.working_branch, @force)
  raise OutOfSyncWithRemote.new("Your branch is out of sync with the remote. If you want to force push, run 'flash_flow -f'") unless @local_git.last_success?

  # TODO - This should use the actual remote for the branch we're on
  if @do_not_merge
    @data.remove_from_merge(@local_git.merge_remote, @local_git.working_branch)
  else
    @data.add_to_merge(@local_git.merge_remote, @local_git.working_branch)
  end
end


115
116
117
# File 'lib/flash_flow/merge/acceptance.rb', line 115

def print_errors
  puts format_errors
end

#process_result(branch, merger) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/flash_flow/merge/acceptance.rb', line 75

def process_result(branch, merger)
  case merger.result
    when :deleted
      @data.mark_deleted(branch)
      @notifier.deleted_branch(branch) unless is_working_branch(branch)

    when :success
      branch.sha = merger.sha
      @data.mark_success(branch)
      @data.set_resolutions(branch, merger.resolutions)

    when :conflict
      if is_working_branch(branch)
        @data.mark_failure(branch, merger.conflict_sha)
      else
        @data.mark_failure(branch, nil)
        @notifier.merge_conflict(branch)
      end
  end
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/flash_flow/merge/acceptance.rb', line 18

def run
  check_version
  check_repo
  puts "Building #{@local_git.merge_branch}... Log can be found in #{FlashFlow::Config.configuration.log_file}"
  logger.info "\n\n### Beginning #{@local_git.merge_branch} merge ###\n\n"

  begin
    open_pull_request

    @lock.with_lock do
      @git.fetch(@git.merge_remote)
      @git.in_original_merge_branch do
        @git.initialize_rerere
      end

      @git.reset_temp_merge_branch
      @git.in_temp_merge_branch do
        merge_branches(@data.mergeable) do |branch, merger|
          process_result(branch, merger)
        end
        commit_branch_info
        commit_rerere
      end

      @git.copy_temp_to_branch(@git.merge_branch, commit_message)
      @git.delete_temp_merge_branch
      @git.push(@git.merge_branch, true)
    end

    print_errors
    logger.info "### Finished #{@local_git.merge_branch} merge ###"
  rescue Lock::Error, OutOfSyncWithRemote => e
    puts 'Failure!'
    puts e.message
  ensure
    @local_git.run("checkout #{@local_git.working_branch}")
  end
end

#two_weeksObject



71
72
73
# File 'lib/flash_flow/merge/acceptance.rb', line 71

def two_weeks
  60 * 60 * 24 * 14
end