-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Calling Github API to Squash Merge the PR as opposed doing it manually
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}" | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
# 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 | ||
This comment has been minimized.
Sorry, something went wrong.
pboling
|
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
This comment has been minimized.
Sorry, something went wrong.
pboling
|
||
|
||
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 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
simonzhu24
Author
Owner
|
||
return res | ||
end | ||
end | ||
|
||
def append_to_squashed_commit_message(message = '') | ||
|
5 comments
on commit 36f50e4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Things completed:
- Prompted Users for:
- Auth Token
- Owner Name
- Repo Name
- Remove Manual Merge Process
- Make HTTP Request to Github to squash merge PR
- Exception / Error Handling for HTTP Request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! One thing I notice is that we've lost bit bucket support with this. The merge_feature_branch
should move into a new PullRequest#merge!
method.
From a higher level, it would be nice to see something like this:
begin
existing_pull_request.merge!
rescue GitReflow::PullRequest::MergeFailure => e
say e.message, :deliver_halted
end
Also, the github_api gem already provides a way to merge ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codenamev I didn't know about that gem before! Definitely, lets do that instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note quite sure if we need something like this?
begin
existing_pull_request.merge!
rescue GitReflow::PullRequest::MergeFailure => e
say e.message, :deliver_halted
end
The standard if-else or case would suffice...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@simonzhu24 I think we need the rescue because on the failure to merge, if you go with the github_api
gem, then the error will be raised, and an if-else can't handle that:
Error: GitReflow::PullRequest::MergeFailure
This line was to delete the remote feature branch. We still want to do this (not the line above it, just this line with
git push origin :#{feature_branch}
).The
:
means delete the branch.