Class: Octopolo::DatedBranchCreator

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

Constant Summary collapse

InvalidBranchType =
Class.new(StandardError)

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, should_delete_old_branches = false) ⇒ DatedBranchCreator

Public: Initialize a new instance of DatedBranchCreator

branch_type - Name of the type of branch (e.g., staging or deployable) should_delete_old_branches - Flag to delete old branches of the given type.



18
19
20
21
# File 'lib/octopolo/dated_branch_creator.rb', line 18

def initialize(branch_type, should_delete_old_branches=false)
  self.branch_type = branch_type
  self.should_delete_old_branches = should_delete_old_branches
end

Instance Attribute Details

#branch_typeObject

Returns the value of attribute branch_type.



11
12
13
# File 'lib/octopolo/dated_branch_creator.rb', line 11

def branch_type
  @branch_type
end

#should_delete_old_branchesObject

Returns the value of attribute should_delete_old_branches.



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

def should_delete_old_branches
  @should_delete_old_branches
end

Class Method Details

.perform(branch_type, should_delete_old_branches = false) ⇒ 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) should_delete_old_branches - Flag to delete old branches of the given type.

Returns a DatedBranchCreator



29
30
31
32
33
# File 'lib/octopolo/dated_branch_creator.rb', line 29

def self.perform(branch_type, should_delete_old_branches=false)
  new(branch_type, should_delete_old_branches).tap do |creator|
    creator.perform
  end
end

Instance Method Details

#branch_nameObject

Public: The name of the branch to create



52
53
54
55
56
57
58
59
# File 'lib/octopolo/dated_branch_creator.rb', line 52

def branch_name
  case branch_type
  when Git::DEPLOYABLE_PREFIX, Git::STAGING_PREFIX, Git::QAREADY_PREFIX
    "#{branch_type}.#{date_suffix}"
  else
    raise InvalidBranchType, "'#{branch_type}' is not a valid branch type"
  end
end

#create_branchObject

Public: Create the desired branch



42
43
44
# File 'lib/octopolo/dated_branch_creator.rb', line 42

def create_branch
  git.new_branch(branch_name, config.deploy_branch)
end

#date_suffixObject

Public: The date suffix to append to the branch name



47
48
49
# File 'lib/octopolo/dated_branch_creator.rb', line 47

def date_suffix
  Date.today.strftime("%Y.%m.%d")
end

#delete_old_branchesObject

Public: If necessary, and if user opts to, delete old branches of its type



62
63
64
65
66
67
68
69
70
71
# File 'lib/octopolo/dated_branch_creator.rb', line 62

def delete_old_branches
  return unless extra_branches.any?
  should_delete = should_delete_old_branches || cli.ask_boolean("Do you want to delete the old #{branch_type} branch(es)? (#{extra_branches.join(", ")})")

  if should_delete
    extra_branches.each do |extra|
      Git.delete_branch(extra)
    end
  end
end

#extra_branchesObject

Public: The list of extra branches that exist after creating the new branch

Returns an Array of Strings of the branch names



76
77
78
79
80
81
82
83
# File 'lib/octopolo/dated_branch_creator.rb', line 76

def extra_branches
  case branch_type
  when Git::DEPLOYABLE_PREFIX, Git::STAGING_PREFIX, Git::QAREADY_PREFIX
    Git.branches_for(branch_type) - [branch_name]
  else
    raise InvalidBranchType, "'#{branch_type}' is not a valid branch type"
  end
end

#performObject

Public: Create the branch and handle related processing



36
37
38
39
# File 'lib/octopolo/dated_branch_creator.rb', line 36

def perform
  create_branch
  delete_old_branches
end