Module: ScrumNinjaGitCli

Defined in:
lib/scrum_ninja_git_cli.rb

Constant Summary collapse

HelpText =
<<-EOF

The 'sgc' command is used to support RF's normal release workflow.

Available commands:

  [Can be run from any branch]
    sgc 
    sgc list                       list all stories
    sgc info 12345                 show only the one story
    sgc xml 12345                  show full XML dump (from ScrumNinja API) for one story
    sgc own 12345[, new_owner_id]  Set owner of the given story (defaults to value in .scrumninja.yml)

  [Must be run from master, with clean working state]
    sgc start 12345           Check out a story branch and change ownership [if not already set]
    sgc join 12345            Check out a story branch without trying to change ownership
    sgc dev-task "some task"  Check out a dev-task branch (no ScrumNinja interaction)

  [Must be run from story branch, with clean working state]
    sgc push-branch        git:  pull --rebase; push origin story_branch
    sgc merge-from-master  git:  checkout master; pull; checkout story_branch; merge master
    sgc deliver            merge your story branch into master, then push the result.
                           (DOES NOT update ScrumNinja.)

Configuration:

  Information required to interact with ScrumNinja will be read from a
  file named .scrumninja.yml.  The command will search for this file in
  both the current dir and your home dir.  Values defined in ./.scrumninja.yml
  will override those in ~/.scrumninja.yml.

  Example YAML for this file:
    api_key: abc123     # generate this at:  https://scrumninja.com/user/edit
    project_id: 4493    # swipe this from the address bar
    user_id: 4256       # edit a story card, view source, find element #story_owner_user_id

EOF
Messages =
{
  :git_version         => 'You must be running Git 1.7 or newer.',
  :clean_working_state => 'Working state must be clean.',
  :must_be_on_master   => "Branch 'master' must be checked out.",
  :wrong_branch        => "Your story branch must be checked out.",
  :rake_failure        => "Rake failed.  Aborting.",
}

Class Method Summary collapse

Class Method Details

.add(*args) ⇒ Object



126
127
128
# File 'lib/scrum_ninja_git_cli.rb', line 126

def add(*args)
  git_wrapper.add(args.join(" "))
end

.commitObject



130
131
132
133
134
135
136
137
# File 'lib/scrum_ninja_git_cli.rb', line 130

def commit
  commit_msg_parts = git_wrapper.current_branch_name \
    .split('-').map(&:capitalize)
  commit_msg_parts.unshift(commit_msg_parts.shift + ":") \
    .unshift('Story')
  commit_msg = commit_msg_parts.join(" ")
  git_wrapper.commit(commit_msg)
end

.deliverObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/scrum_ninja_git_cli.rb', line 114

def deliver
  if 'master' == git_wrapper.current_branch_name
    output Messages[:wrong_branch] and return
  end

  git_wrapper.push_branch
  git_wrapper.merge_from_master
  return unless rake
  git_wrapper.merge_branch_into_master
  git_wrapper.push_master
end

.dev_task(description) ⇒ Object



97
98
99
# File 'lib/scrum_ninja_git_cli.rb', line 97

def dev_task(description)
  start_work_on_branch('dev-' + GitWrapper.string_hyphenize(description))
end

.git_wrapperObject

Support functions



141
142
143
# File 'lib/scrum_ninja_git_cli.rb', line 141

def git_wrapper
  GitWrapper
end

.helpObject

Commands



65
66
67
# File 'lib/scrum_ninja_git_cli.rb', line 65

def help
  output HelpText
end

.info(story_id) ⇒ Object



74
75
76
# File 'lib/scrum_ninja_git_cli.rb', line 74

def info(story_id)
  output session.get_story(story_id)
end

.join(story_id) ⇒ Object



87
88
89
90
# File 'lib/scrum_ninja_git_cli.rb', line 87

def join(story_id)
  story = session.get_story(story_id)
  start_work_on_branch story.branch_name
end

.listObject



69
70
71
72
# File 'lib/scrum_ninja_git_cli.rb', line 69

def list
  stories = session.get_stories
  output stories.map(&:to_s).join("\n")  # TODO: can we drop the #map?
end

.merge_from_masterObject



108
109
110
111
112
# File 'lib/scrum_ninja_git_cli.rb', line 108

def merge_from_master
  git_wrapper.merge_from_master
rescue GitWrapper::WrongBranchException
  print_message :wrong_branch
end

.output(*args) ⇒ Object



153
154
155
156
# File 'lib/scrum_ninja_git_cli.rb', line 153

def output(*args)
  puts *args
  true
end

.own(story_id, new_owner_id = nil, opts = {}) ⇒ Object



82
83
84
# File 'lib/scrum_ninja_git_cli.rb', line 82

def own(story_id, new_owner_id = nil, opts={})
  session.update_ownership(story_id, new_owner_id, opts)
end


149
150
151
# File 'lib/scrum_ninja_git_cli.rb', line 149

def print_message(message)
  output Messages[message]
end

.push_branchObject



102
103
104
105
106
# File 'lib/scrum_ninja_git_cli.rb', line 102

def push_branch
  git_wrapper.push_branch
rescue GitWrapper::WorkingFolderDirtyException
  print_message :clean_working_state
end

.rakeObject



158
159
160
161
162
163
164
165
# File 'lib/scrum_ninja_git_cli.rb', line 158

def rake
  if 0 == ShellCmd.run('rake')
    true
  else
    print_message :rake_failure
    false
  end
end

.run_command(*args) ⇒ Object

Simple command-line argument dispatching (see bottom of file)



57
58
59
60
61
62
# File 'lib/scrum_ninja_git_cli.rb', line 57

def run_command(*args)
  cmd = args.shift.to_s.gsub('-', '_')
  send(cmd, *args)
rescue ArgumentError
  help
end

.sessionObject



145
146
147
# File 'lib/scrum_ninja_git_cli.rb', line 145

def session
  @session ||= ScrumNinja::Session.new(:project_dir => git_wrapper.locate_git_repo_root)
end

.start(story_id) ⇒ Object



92
93
94
95
# File 'lib/scrum_ninja_git_cli.rb', line 92

def start(story_id)
  join story_id
  own story_id, nil, :abort_if_already_owned => true
end

.start_work_on_branch(branch_name) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/scrum_ninja_git_cli.rb', line 167

def start_work_on_branch(branch_name)
  unless git_wrapper.is_git_current_enough?
    output Messages[:git_version] and return
  end

  unless git_wrapper.is_state_clean?
    output Messages[:clean_working_state] and return
  end

  unless 'master' == git_wrapper.current_branch_name
    output Messages[:must_be_on_master] and return
  end

  git_wrapper.checkout(branch_name)
end

.xml(story_id) ⇒ Object



78
79
80
# File 'lib/scrum_ninja_git_cli.rb', line 78

def xml(story_id)
  output session.get_story_xml(story_id)
end