Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
Add --force flag to submit command in order to create a PR from a bra…
Browse files Browse the repository at this point in the history
…nch without task
  • Loading branch information
marcioj committed Jan 10, 2017
1 parent b680399 commit a1e060f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 78 deletions.
43 changes: 25 additions & 18 deletions lib/assisted_workflow/addons/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
require "assisted_workflow/addons/base"

module AssistedWorkflow::Addons

class GitError < AssistedWorkflow::Error; end

class Git < Base

DESCRIPTION_LIMIT = 30

def initialize(output, options = {})
super
@command_options = {:raise_error => true}.merge(options)
end

# creates a new git branch based on story attributes
# the branch name format is:
# => story_onwer_username.story_id.story_name
Expand All @@ -23,7 +23,7 @@ def create_story_branch(story, username)
git "checkout -b #{branch}"
# git "push --set-upstream origin #{branch}"
end

# run all the git steps required for a clean pull request
def rebase_and_push
log "preparing local branch"
Expand All @@ -35,24 +35,24 @@ def rebase_and_push
git "rebase master"
git "push -u -f origin #{branch}"
end

# returns the current story id based on branch name
def current_story_id
current_branch.split(".")[1]
end

# returns the current local branch name
def current_branch
git("rev-parse --abbrev-ref HEAD", :silent => true)
end

# returns the repository name assigned to origin following the format:
# owner/project
def repository
url = git("config --get remote.origin.url", :error => "cannot find 'origin' remote repository url")
url.gsub("[email protected]:", "").gsub("https://github.com/", "").gsub(/\.git$/, "").chomp
end

# check if current branch is merged into master
def check_merged!
check_everything_commited!
Expand All @@ -66,7 +66,7 @@ def check_merged!
end
merged
end

# removes current branch and its remote version
def remove_branch
log "removing local and remote feature branches"
Expand All @@ -75,9 +75,16 @@ def remove_branch
git "checkout master"
git "branch -D #{branch}"
end


def current_feature_name
branch = current_branch
branch = branch[branch.index(".") + 1, branch.length].gsub(/[\W_]/, " ")
branch[0] = branch[0].upcase
branch
end

private #=================================================================

def git(command, options = {})
options = @command_options.merge(options)
puts "git #{command}" unless options[:silent] == true
Expand All @@ -88,24 +95,24 @@ def git(command, options = {})
end
result
end

def system(command)
%x{#{command}}.chomp
end

def system_error?
$? != 0
end

def branch_name(story, username)
description = story.name.to_s.downcase.gsub(/\W/, "_").slice(0, DESCRIPTION_LIMIT)
[username, story.id, description].join(".").downcase
end

def not_commited_changes
git("status --porcelain", :silent => true).split("\n")
end

def check_everything_commited!
raise AssistedWorkflow::Error, "git: there are not commited changes" unless not_commited_changes.empty?
end
Expand Down
2 changes: 1 addition & 1 deletion lib/assisted_workflow/addons/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def create_pull_request(branch, story)
pull_request = if story.is_a? GithubStory
@client.create_pull_request_for_issue(@repo, base, branch, story.id)
else
title = "[##{story.id}] #{story.name}"
title = story.id ? "[##{story.id}] #{story.name}" : story.name
@client.create_pull_request(@repo, base, branch, title, story.description)
end

Expand Down
53 changes: 31 additions & 22 deletions lib/assisted_workflow/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class CLI < Thor
GLOBAL_CONFIG = File.expand_path(".awconfig", ENV["HOME"])
LOCAL_CONFIG = ".awconfig"
source_root(File.expand_path(File.join(__FILE__, "..", "templates")))

# tasks shortcuts
map ["-v", "--version"] => :version
map "s" => :start
map "u" => :submit
map "f" => :finish

desc "setup", "Setup initial configuration in current project directory"
def setup
copy_file "awconfig.global.tt", GLOBAL_CONFIG
Expand All @@ -32,7 +32,7 @@ def setup
c << "$ aw config pivotal.project_id=00001"
end
end

desc "start [STORY_ID]", "Start the pivotal story and create a new branch to receive the changes"
method_option :all, :type => :boolean, :default => false, :aliases => "-a", :desc => "Show started and pending stories when no story_id is provided"
method_option :estimate, :type => :numeric, :aliases => "-e", :desc => "Sets the story estimate when starting"
Expand All @@ -50,20 +50,29 @@ def start(story_id=nil)
out.next_command "after commiting your changes, submit a pull request using:", "$ aw submit"
end
end

desc "submit", "Submits the current story creating a new pull request"
method_option :force, :type => :boolean, :default => false, :aliases => "-f", :desc => "Create the pull request regardless of the current having an associated task or not"
def submit
check_awfile!
story_id = git.current_story_id

unless story = tracker.find_story(story_id)
raise AssistedWorkflow::Error, "story not found, make sure a feature branch in active"
unless options[:force]
raise AssistedWorkflow::Error, "story not found, make sure a feature branch is active or use --force to ignore it altogether"
end
end

git.rebase_and_push
pr_url = github.create_pull_request(git.current_branch, story)
tracker.finish_story(story, :note => pr_url)
pr_story = story || OpenStruct.new(name: git.current_feature_name)
pr_url = github.create_pull_request(git.current_branch, pr_story)

if story
tracker.finish_story(story, :note => pr_url)
end
out.next_command "after pull request approval, remove the feature branch using:", "$ aw finish"
end

desc "finish", "Check if the changes are merged into master, removing the current feature branch"
def finish
check_awfile!
Expand All @@ -74,12 +83,12 @@ def finish
git.remove_branch
out.next_command "well done! check your next stories using:", "$ aw start"
end

desc "version", "Display assisted_workflow gem version"
def version
say AssistedWorkflow::VERSION
end

desc "config group.key=value", "Set configuration keys in local config file"
method_option :global, :type => :boolean, :aliases => "-g", :desc => "Set configuration key in global configuration file (for all projects)"
def config(*args)
Expand All @@ -89,36 +98,36 @@ def config(*args)
config_file.parse(args).save!
end
end

desc "thanks", "Aw, Thanks!", :hide => true
def thanks
out.say "you're welcome!", :on_magenta
end


no_tasks do
def out
@out ||= Output.new(self.shell)
end

def tracker
@tracker ||= Addons.load_tracker(out, configuration) || github
end

def git
@git ||= Addons::Git.new(out)
end

def github
@github ||= Addons::Github.new(out,
@github ||= Addons::Github.new(out,
{"repository" => git.repository}.merge(configuration[:github])
)
end

def config_file
@config_file ||= ConfigFile.new(awfile)
end

# loads all configuration, merging global and local values
def configuration
@configuration ||= begin
Expand All @@ -128,7 +137,7 @@ def configuration
end
end
end

class << self
def start(given_args=ARGV, config={})
super
Expand All @@ -137,9 +146,9 @@ def start(given_args=ARGV, config={})
exit(1)
end
end

private ##################################################################

def check_awfile!
raise AssistedWorkflow::Error, "#{awfile} does not exist.\nmake sure you run `$ aw setup` in your project folder." unless File.exist?(awfile)
end
Expand Down
39 changes: 22 additions & 17 deletions spec/assisted_workflow/addons/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
stub(@git).system_error?{ false }
stub(@git).system("git rev-parse --abbrev-ref HEAD"){ "flavio.1234.new_feature"}
end

it "creates a story branch" do
mock(@git).system("git checkout -b flavio.1234.new_feature")
@git.create_story_branch(story, "flavio")
end

it "raises a git error when git command does not exit with success" do
mock(@git).system_error?{ true }
mock(@git).system("git checkout -b flavio.1234.new_feature")
proc { @git.create_story_branch(story, "flavio") }.must_raise AssistedWorkflow::Addons::GitError, "git command error"
end


it "rebases and push a feature branch" do
mock(@git).system("git status --porcelain"){ "" }
mock(@git).system("git checkout master")
Expand All @@ -29,57 +29,62 @@
mock(@git).system("git push -u -f origin flavio.1234.new_feature")
@git.rebase_and_push
end

it "raises when rebasing if there are not commited changes" do
mock(@git).system("git status --porcelain"){ "changed_file.rb" }
proc {
proc {
@git.rebase_and_push
}.must_raise AssistedWorkflow::Error, "git: there are not commited changes"
end

it "returns the story_id from branch name" do
@git.current_story_id.must_equal "1234"
end

it "return the current branch name" do
@git.current_branch.must_equal "flavio.1234.new_feature"
end

it "returns the repository name assigned to origin" do
mock(@git).system("git config --get remote.origin.url"){ "[email protected]:flaviogranero/assisted_workflow.git"}
@git.repository.must_equal "flaviogranero/assisted_workflow"
end


it "returns the feature name" do
stub(@git).system("git rev-parse --abbrev-ref HEAD"){ "marcioj.some-amazing_feature.that.i-did" }
@git.current_feature_name.must_equal "Some amazing feature that i did"
end

describe "#check_merged!" do

before do
mock(@git).system("git status --porcelain"){ "" }
mock(@git).system("git checkout flavio.1234.new_feature")
mock(@git).system("git checkout master")
mock(@git).system("git pull --rebase")
end

it "returns true if current branch is merged into master" do
mock(@git).system("git branch --merged"){ "flavio.1234.new_feature" }
@git.check_merged!.must_equal true
end

it "returns false if current branch is not merged into master" do
mock(@git).system("git branch --merged"){ "flavio.1234.other_feature" }
proc { @git.check_merged! }.must_raise AssistedWorkflow::Error, "this branch is not merged into master"

end
end

it "removes current branch and its remote version" do
mock(@git).system("git push origin :flavio.1234.new_feature")
mock(@git).system("git checkout master")
mock(@git).system("git branch -D flavio.1234.new_feature")
@git.remove_branch
end

private #==================================================================

def story
# stubs
@client = TrackerApi::Client.new(token: "mypivotaltoken")
Expand Down
Loading

0 comments on commit a1e060f

Please sign in to comment.