Skip to content

Commit

Permalink
Make commenting to PR on integrate opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-nelson-mn committed May 18, 2015
1 parent 6ef2893 commit 8f061be
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 59 deletions.
3 changes: 2 additions & 1 deletion lib/thegarage/gitx/cli/integrate_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class IntegrateCommand < BaseCommand
include Thegarage::Gitx::Github
desc 'integrate', 'integrate the current branch into one of the aggregate development branches (default = staging)'
method_option :resume, :type => :string, :aliases => '-r', :desc => 'resume merging of feature-branch'
method_option :comment, :type => :boolean, :aliases => '-c', :desc => 'add a comment to the pull request for this branch. Creates a new PR if none exists.'
def integrate(integration_branch = 'staging')
assert_aggregate_branch!(integration_branch)

Expand All @@ -26,7 +27,7 @@ def integrate(integration_branch = 'staging')
integrate_branch(branch, integration_branch) unless options[:resume]
checkout_branch branch

create_integrate_comment(branch) unless config.reserved_branch?(branch)
create_integrate_comment(branch) if options[:comment] && !config.reserved_branch?(branch)
end

private
Expand Down
134 changes: 76 additions & 58 deletions spec/thegarage/gitx/cli/integrate_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@
it 'defaults to staging branch' do
should meet_expectations
end
it 'posts comment to pull request' do
expect(WebMock).to have_requested(:post, "https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments").
with(body: {body: '[gitx] integrated into staging :twisted_rightwards_arrows:'})
it 'does not post comment to pull request' do
expect(WebMock).to_not have_requested(:post, "https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments")
end
end
context 'when current_branch == master' do
Expand All @@ -74,51 +73,6 @@
expect(WebMock).to_not have_requested(:post, "https://api.github.com/repos/thegarage/thegarage-gitx/pulls")
end
end
context 'when a pull request doesnt exist for the feature-branch' do
let(:authorization_token) { '123123' }
let(:changelog) { '* made some fixes' }
let(:new_pull_request) do
{
html_url: "https://path/to/html/pull/request",
issue_url: "https://api/path/to/issue/url",
number: 10,
head: {
ref: "branch_name"
}
}
end
before do
allow(cli).to receive(:ask_editor).and_return('description')
allow(cli).to receive(:authorization_token).and_return(authorization_token)
expect(cli).to receive(:execute_command).with(Thegarage::Gitx::Cli::UpdateCommand, :update).twice

expect(cli).to receive(:run_cmd).with("git fetch origin").ordered
expect(cli).to receive(:run_cmd).with("git branch -D staging", allow_failure: true).ordered
expect(cli).to receive(:run_cmd).with("git checkout staging").ordered
expect(cli).to receive(:run_cmd).with("git merge feature-branch").ordered
expect(cli).to receive(:run_cmd).with("git push origin HEAD").ordered
expect(cli).to receive(:run_cmd).with("git checkout feature-branch").ordered
expect(cli).to receive(:run_cmd).with('git checkout feature-branch').ordered
expect(cli).to receive(:run_cmd).with("git log master...feature-branch --reverse --no-merges --pretty=format:'* %s%n%b'").and_return("2013-01-01 did some stuff").ordered

stub_request(:post, 'https://api.github.com/repos/thegarage/thegarage-gitx/pulls').to_return(:status => 201, :body => new_pull_request.to_json, :headers => {'Content-Type' => 'application/json'})
stub_request(:post, 'https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments').to_return(:status => 201)

VCR.use_cassette('pull_request_does_not_exist') do
cli.integrate
end
end
it 'creates github pull request' do
should meet_expectations
end
it 'creates github comment for integration' do
expect(WebMock).to have_requested(:post, "https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments").
with(body: {body: '[gitx] integrated into staging :twisted_rightwards_arrows:'})
end
it 'runs expected commands' do
should meet_expectations
end
end
context 'when staging branch does not exist remotely' do
let(:authorization_token) { '123123' }
let(:remote_branch_names) { [] }
Expand Down Expand Up @@ -146,9 +100,6 @@
it 'creates remote aggregate branch' do
should meet_expectations
end
it 'posts comment to pull request' do
expect(WebMock).to have_requested(:post, "https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments")
end
end
context 'when integration branch == prototype and remote branch exists' do
let(:authorization_token) { '123123' }
Expand All @@ -173,9 +124,6 @@
it 'runs expected commands' do
should meet_expectations
end
it 'posts comment to pull request' do
expect(WebMock).to have_requested(:post, "https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments")
end
end
context 'when integration branch is not an aggregate branch' do
it 'raises an error' do
Expand Down Expand Up @@ -235,9 +183,6 @@
it 'does not delete local staging branch' do
should meet_expectations
end
it 'posts comment to pull request' do
expect(WebMock).to have_requested(:post, "https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments")
end
end
context 'with --resume "feature-branch" flag when feature-branch does not exist' do
let(:options) do
Expand All @@ -264,8 +209,81 @@
it 'asks user for feature-branch name' do
should meet_expectations
end
end
context 'for default integration (to staging) with --comment flag' do
let(:options) do
{ comment: true }
end
let(:authorization_token) { '123123' }
let(:remote_branch_names) { ['origin/staging'] }
before do
allow(cli).to receive(:authorization_token).and_return(authorization_token)
expect(cli).to receive(:execute_command).with(Thegarage::Gitx::Cli::UpdateCommand, :update)

expect(cli).to receive(:run_cmd).with("git fetch origin").ordered
expect(cli).to receive(:run_cmd).with("git branch -D staging", allow_failure: true).ordered
expect(cli).to receive(:run_cmd).with("git checkout staging").ordered
expect(cli).to receive(:run_cmd).with("git merge feature-branch").ordered
expect(cli).to receive(:run_cmd).with("git push origin HEAD").ordered
expect(cli).to receive(:run_cmd).with("git checkout feature-branch").ordered

stub_request(:post, /.*api.github.com.*/).to_return(:status => 201)

VCR.use_cassette('pull_request_does_exist_with_success_status') do
cli.integrate
end
end
it 'defaults to staging branch' do
should meet_expectations
end
it 'posts comment to pull request' do
expect(WebMock).to have_requested(:post, "https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments")
expect(WebMock).to have_requested(:post, "https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments").
with(body: {body: '[gitx] integrated into staging :twisted_rightwards_arrows:'})
end
end
context 'with --comment flag when a pull request doesn\'t exist for the feature-branch' do
let(:options) do
{ comment: true }
end
let(:authorization_token) { '123123' }
let(:changelog) { '* made some fixes' }
let(:new_pull_request) do
{
html_url: "https://path/to/html/pull/request",
issue_url: "https://api/path/to/issue/url",
number: 10,
head: {
ref: "branch_name"
}
}
end
before do
allow(cli).to receive(:ask_editor).and_return('description')
allow(cli).to receive(:authorization_token).and_return(authorization_token)
expect(cli).to receive(:execute_command).with(Thegarage::Gitx::Cli::UpdateCommand, :update).twice

expect(cli).to receive(:run_cmd).with("git fetch origin").ordered
expect(cli).to receive(:run_cmd).with("git branch -D staging", allow_failure: true).ordered
expect(cli).to receive(:run_cmd).with("git checkout staging").ordered
expect(cli).to receive(:run_cmd).with("git merge feature-branch").ordered
expect(cli).to receive(:run_cmd).with("git push origin HEAD").ordered
expect(cli).to receive(:run_cmd).with("git checkout feature-branch").ordered
expect(cli).to receive(:run_cmd).with('git checkout feature-branch').ordered
expect(cli).to receive(:run_cmd).with("git log master...feature-branch --reverse --no-merges --pretty=format:'* %s%n%b'").and_return("2013-01-01 did some stuff").ordered

stub_request(:post, 'https://api.github.com/repos/thegarage/thegarage-gitx/pulls').to_return(:status => 201, :body => new_pull_request.to_json, :headers => {'Content-Type' => 'application/json'})
stub_request(:post, 'https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments').to_return(:status => 201)

VCR.use_cassette('pull_request_does_not_exist') do
cli.integrate
end
end
it 'creates github pull request' do
should meet_expectations
end
it 'creates github comment for integration' do
expect(WebMock).to have_requested(:post, "https://api.github.com/repos/thegarage/thegarage-gitx/issues/10/comments").
with(body: {body: '[gitx] integrated into staging :twisted_rightwards_arrows:'})
end
end
end
Expand Down

0 comments on commit 8f061be

Please sign in to comment.