Class: PivoFlow::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pivo_flow/base.rb

Overview

This class is responsible for setting up the project environment

  • saving Pivotal Tracker API token and project ID in git repository config

  • installing git hook

Direct Known Subclasses

Pivotal

Constant Summary collapse

GIT_DIR =

Git repository directory

'.git'
KEYS_AND_QUESTIONS =

Keys used by gem in git config, with corresponding questions which are used during project setup

{
  "pivo-flow.project-id"  => "Pivotal: what is your project's ID?",
  "pivo-flow.api-token"   => "Pivotal: what is your pivotal tracker api-token?"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Basic initialize method

Raises:

  • (PivoFlow::Errors::NoGitRepoFound)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pivo_flow/base.rb', line 20

def initialize(options={})
  @options = options
  @current_dir = Dir.pwd

  raise PivoFlow::Errors::NoGitRepoFound, "No git repository found" unless git_directory_present?

  # paths
  @git_dir = File.join(@current_dir, GIT_DIR)
  @git_hook_path = File.join(@git_dir, 'hooks', 'prepare-commit-msg')
  @pf_git_hook_name = 'pf-prepare-commit-msg'
  @pf_git_hook_path = File.join(@git_dir, 'hooks', @pf_git_hook_name)
  @pf_git_hook_local_path = File.join(File.dirname(__FILE__), '..', '..', 'bin', @pf_git_hook_name)
  @pf_git_hook_cmd = "#{@pf_git_hook_path} $1"
  @options[:repository] = Grit::Repo.new(@git_dir)

  install_git_hook if git_hook_needed?
  git_config_ok? ? parse_git_config : add_git_config
  run
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/pivo_flow/base.rb', line 9

def options
  @options
end

Instance Method Details

#git_directory_present?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/pivo_flow/base.rb', line 49

def git_directory_present?
  File.directory?(File.join(@current_dir, GIT_DIR))
end

#git_hook_needed?Boolean

Check if git hook is already installed

Returns:

  • (Boolean)


41
42
43
# File 'lib/pivo_flow/base.rb', line 41

def git_hook_needed?
  !File.executable?(@git_hook_path) || !File.read(@git_hook_path).match(/#{@pf_git_hook_name} \$1/) || !pf_git_hook_valid?
end

#install_git_hookObject

Install git hook Copy hook to .git/hooks directory and add a reference to this executable file within prepare-commit-msg hook (it may be helpful if user has his custom hooks)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pivo_flow/base.rb', line 57

def install_git_hook
  puts "Installing prepare-commit-msg hook..."
  FileUtils.mkdir_p(File.dirname(@pf_git_hook_path))
  FileUtils.cp(@pf_git_hook_local_path, @pf_git_hook_path, preserve: true)
  puts "File copied..."
  unless File.exists?(@git_hook_path) && File.read(@git_hook_path).match(@pf_git_hook_name)
    File.open(@git_hook_path, "a") { |f| f.puts(@pf_git_hook_cmd) }
    puts "Reference to pf-prepare-commit-msg added to prepare-commit-msg..."
  end
  unless File.executable?(@git_hook_path)
    FileUtils.chmod 0755, @git_hook_path unless
    puts "Chmod on #{@git_hook_path} set to 755"
  end

  puts "Success!\n"
end

#pf_git_hook_valid?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/pivo_flow/base.rb', line 45

def pf_git_hook_valid?
  File.executable?(@pf_git_hook_path) && FileUtils.compare_file(@pf_git_hook_path, @pf_git_hook_local_path)
end

#reconfigObject

Setup project by entering Pivotal api-token and Pivotal Tracker project_id



85
86
87
88
89
90
# File 'lib/pivo_flow/base.rb', line 85

def reconfig
  KEYS_AND_QUESTIONS.each do |key, question|
    ask_question_and_force_update_config(question, key)
  end
  config_update_success
end

#runObject

This method is fired after initialization and should be overwritten by subclasses



76
77
# File 'lib/pivo_flow/base.rb', line 76

def run
end

#user_nameObject

Get full user name from git config, i.e. Adam Newman



80
81
82
# File 'lib/pivo_flow/base.rb', line 80

def user_name
  @options[:user_name] ||= @options[:repository].config['pivotal.full-name'] || @options[:repository].config['user.name']
end