diff --git a/lib/git_reflow.rb b/lib/git_reflow.rb index 569ad4e..ef57a15 100644 --- a/lib/git_reflow.rb +++ b/lib/git_reflow.rb @@ -107,13 +107,8 @@ def deliver(options = {}) feature_branch = current_branch base_branch = options['base'] || 'master' - update_current_branch - fetch_destination(base_branch) - update_destination(feature_branch) - begin existing_pull_request = git_server.find_open_pull_request( :from => current_branch, :to => base_branch ) - if existing_pull_request.nil? say "No pull request exists for #{remote_user}:#{current_branch}\nPlease submit your branch for review first with \`git reflow review\`", :deliver_halted else @@ -125,34 +120,40 @@ def deliver(options = {}) end if existing_pull_request.good_to_merge?(force: options['skip_lgtm']) - puts "Merging pull request ##{existing_pull_request.number}: '#{existing_pull_request.title}', from '#{existing_pull_request.feature_branch_name}' into '#{existing_pull_request.base_branch_name}'" + puts "Merging pull request ##{existing_pull_request.number}: '#{existing_pull_request.title}', from '#{existing_pull_request.feature_branch_name}' into '#{GitReflow::Config.get("github.owner")}:#{base_branch}'" - update_destination(base_branch) - merge_feature_branch(feature_branch, + res = merge_feature_branch(feature_branch, :destination_branch => base_branch, :pull_request_number => existing_pull_request.number, :lgtm_authors => existing_pull_request.approvals, - :message => commit_message) - committed = run_command_with_label 'git commit', with_system: true + :title => existing_pull_request.title, + :message => commit_message, + :head => existing_pull_request.head) - if committed - say "Merge complete!", :success + if res.code == "200" + # if committed + say "Pull Request successfully merged.", :success # check if user always wants to push and cleanup, otherwise ask always_deploy_and_cleanup = GitReflow::Config.get('reflow.always-deploy-and-cleanup') == "true" deploy_and_cleanup = always_deploy_and_cleanup || (ask "Would you like to push this branch to your remote repo and cleanup your feature branch? ") =~ /^y/i if deploy_and_cleanup - run_command_with_label "git push origin #{base_branch}" - run_command_with_label "git push origin :#{feature_branch}" + # Pulls merged changes from remote base_branch + run_command_with_label "git pull origin #{base_branch}" run_command_with_label "git branch -D #{feature_branch}" puts "Nice job buddy." else puts "Cleanup halted. Local changes were not pushed to remote repo.".colorize(:red) puts "To reset and go back to your branch run \`git reset --hard origin/#{base_branch} && git checkout #{feature_branch}\`" end + + elsif res.code == "405" + say "Pull Request is not mergeable.", :deliver_halted + elsif res.code == "409" + say "Head branch was modified. Review and try the merge again.", :deliver_halted else - say "There were problems commiting your feature... please check the errors above and try again.", :error + say "There was another error. Please review the pull request and try again.", :deliver_halted end else say existing_pull_request.rejection_message, :deliver_halted @@ -185,7 +186,7 @@ def deploy(destination_server) end def git_server - @git_server ||= GitServer.connect provider: GitReflow::Config.get('reflow.git-server').strip, silent: true + @git_server ||= GitServer.connect provider: "#{GitReflow::Config.get('reflow.git-server')}".strip, silent: true end end diff --git a/lib/git_reflow/commands/setup.rb b/lib/git_reflow/commands/setup.rb index deb6990..d497963 100644 --- a/lib/git_reflow/commands/setup.rb +++ b/lib/git_reflow/commands/setup.rb @@ -25,5 +25,12 @@ GitReflow::Config.add "constants.minimumApprovals", ask("Set the minimum number of approvals (leaving blank will require approval from all commenters): "), local: reflow_options[:project_only] GitReflow::Config.add "constants.approvalRegex", GitReflow::GitServer::PullRequest::DEFAULT_APPROVAL_REGEX, local: reflow_options[:project_only] + say("Enter the follow information associated with your remote repository: (The information is used to run 'git reflow deliver'.") + GitReflow::Config.add "github.owner", ask("Enter the owner of the remote repository: "), local: reflow_options[:project_only] + GitReflow::Config.add "github.repo", ask("Enter the name of the remote repository: "), local: reflow_options[:project_only] + + # Sets the user's github auth token + GitReflow::Config.set "github.oauth-token", ask("Set the your local github authorization token: (You cannot run 'git reflow deliver' without it!) "), local: reflow_options[:project_only] + say("Thanks! Your settings are saved to #{GitReflow::Config::CONFIG_FILE_PATH}.") end end diff --git a/lib/git_reflow/git_helpers.rb b/lib/git_reflow/git_helpers.rb index 3c10388..2215d36 100644 --- a/lib/git_reflow/git_helpers.rb +++ b/lib/git_reflow/git_helpers.rb @@ -1,5 +1,8 @@ require 'git_reflow/config' require 'git_reflow/sandbox' +require 'net/http' +require 'uri' +require 'json' module GitReflow module GitHelpers @@ -60,10 +63,41 @@ def merge_feature_branch(feature_branch_name, options = {}) message << "\nLGTM given by: @#{lgtm_authors.join(', @')}\n" end - run_command_with_label "git checkout #{options[:destination_branch]}" - run_command_with_label "git merge --squash #{feature_branch_name}" + payload = { + "commit_title" => options[:title], + "commit_message" => options[:message], + "sha" => options[:head].sha, + "squash" => true + } + + res = post_github_api( + { + :payload => payload, + :pull_request_number => options[:pull_request_number] + } + ) append_to_squashed_commit_message(message) if message.length > 0 + return res + end + + def post_github_api(options = {}) + url = URI.parse("#{GitReflow::Config.get('github.endpoint')}/repos/#{GitReflow::Config.get('github.owner')}/#{GitReflow::Config.get('github.repo')}/pulls/#{options[:pull_request_number]}/merge") + + req = Net::HTTP::Put.new(url.to_s) + req.body = options[:payload].to_json + + # Sets Request Headers + req["Accept"] = "application/vnd.github.polaris-preview" + req["Content-Type"] = "application/json" + req["Authorization"] = "token #{GitReflow::Config.get('github.oauth-token')}" + + Net::HTTP.start(url.host, url.port, + :use_ssl => url.scheme == 'https') do |http| + res = http.request req # Net::HTTPResponse object + puts res.inspect + return res + end end def append_to_squashed_commit_message(message = '') diff --git a/spec/git_reflow_spec.rb b/spec/git_reflow_spec.rb index c2951f7..e3e09af 100644 --- a/spec/git_reflow_spec.rb +++ b/spec/git_reflow_spec.rb @@ -189,6 +189,7 @@ before do allow(GitReflow).to receive(:append_to_squashed_commit_message).and_return(true) + allow(GitReflow).to receive(:post_github_api).and_return({}) module Kernel def system(cmd) @@ -310,6 +311,7 @@ def system(cmd) allow(github).to receive(:find_open_pull_request).and_return(existing_pull_request) allow(GitReflow).to receive(:get_first_commit_message).and_return(first_commit_message) allow(existing_pull_request).to receive(:reviewers).and_return(lgtm_comment_authors) + allow(GitReflow).to receive(:post_github_api).and_return({}) end it "includes the first commit message for the new branch in the commit message of the merge" do @@ -324,7 +326,9 @@ def system(cmd) destination_branch: 'master', pull_request_number: existing_pull_request.number, lgtm_authors: ['nhance'], - message: existing_pull_request.body + title: existing_pull_request.title, + message: existing_pull_request.body, + head: existing_pull_request.head }) expect { subject }.to have_output "Merging pull request ##{existing_pull_request.number}: '#{existing_pull_request.title}', from '#{existing_pull_request.head.label}' into '#{existing_pull_request.base.label}'" @@ -470,6 +474,7 @@ def system(cmd) end it "merges and squashes the feature branch into the master branch" do + allow(GitReflow).to receive(:post_github_api).and_return({}) expect(GitReflow).to receive(:merge_feature_branch) subject end diff --git a/spec/lgtm_git_reflow_spec.rb b/spec/lgtm_git_reflow_spec.rb index fa6add1..57239bc 100644 --- a/spec/lgtm_git_reflow_spec.rb +++ b/spec/lgtm_git_reflow_spec.rb @@ -55,6 +55,7 @@ before do allow(GitReflow).to receive(:append_to_squashed_commit_message).and_return(true) + allow(GitReflow).to receive(:post_github_api).and_return({}) module Kernel def system(cmd) @@ -163,7 +164,9 @@ def system(cmd) destination_branch: 'master', pull_request_number: existing_pull_request.number, lgtm_authors: ['nhance', 'Simon'], - message: existing_pull_request.body + title: existing_pull_request.title, + message: existing_pull_request.body, + head: existing_pull_request.head }) expect { subject }.to_not have_output "Merging pull request ##{existing_pull_request.number}: '#{existing_pull_request.title}', from '#{existing_pull_request.head.label}' into '#{existing_pull_request.base.label}'" @@ -335,7 +338,9 @@ def system(cmd) destination_branch: 'master', pull_request_number: existing_pull_request.number, lgtm_authors: ['nhance', 'Simon'], - message: existing_pull_request.body + title: existing_pull_request.title, + message: existing_pull_request.body, + head: existing_pull_request.head }) expect { subject }.to have_output "Merging pull request ##{existing_pull_request.number}: '#{existing_pull_request.title}', from '#{existing_pull_request.head.label}' into '#{existing_pull_request.base.label}'"