Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry pull-request creation if there are errors #226

Merged
merged 1 commit into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/git_reflow/git_server/git_hub/pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def build_status

def commit_author
begin
username, branch = base.label.split(':')
username, _ = base.label.split(':')
first_commit = GitReflow.git_server.connection.pull_requests.commits(username, GitReflow.git_server.class.remote_repo_name, number.to_s).first
"#{first_commit.commit.author.name} <#{first_commit.commit.author.email}>".strip
rescue Github::Error::NotFound
Expand Down
16 changes: 12 additions & 4 deletions lib/git_reflow/workflows/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,18 @@ def self.load_raw_workflow(workflow_string)
end

if create_pull_request
pull_request = GitReflow.git_server.create_pull_request(title: params[:title] || params[:message],
body: params[:message],
head: "#{GitReflow.remote_user}:#{GitReflow.current_branch}",
base: params[:base])
begin
retries ||= 0
pull_request = GitReflow.git_server.create_pull_request(
title: params[:title] || params[:message],
body: params[:message],
head: "#{GitReflow.remote_user}:#{GitReflow.current_branch}",
base: params[:base]
)
rescue Github::Error::UnprocessableEntity
retry if (retries += 1) < 3
raise
end

say "Successfully created pull request ##{pull_request.number}: #{pull_request.title}\nPull Request URL: #{pull_request.html_url}\n", :success
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
:method => :post,
:body => "{
\"errors\" : [{
\"code\" : \"invalid\",
\"field\" : \"head\",
\"message\" : \"head The branch reenhanced:banana does not exist.\",
\"resource\" : \"PullRequest\" }],
\"message\" : \"Validation Failed\"}",
:url => "https://api.github.com/repos/reenhanced/gitreflow/pulls?access_token=12345",
:request_headers => {
"Content-Type" => "application/json",
"Authorization" => "Token token=\"12345\""
},
:parallel_manager => nil,
:request => {:proxy => nil},
:ssl => {},
:status => 422,
:response_headers => {
"server" => "nginx/1.0.13",
"date" => "Fri, 27 Apr 2012 13:02:49 GMT",
"content-type" => "application/json; charset=utf-8",
"connection" => "close",
"status" => "422 Unprocessable Entity",
"x-ratelimit-limit" => "5000",
"etag" => "\"ebdeb717fe19444c308e608728569d5a\"",
"x-oauth-scopes" => "repo",
"x-ratelimit-remaining" => "4996",
"x-accepted-oauth-scopes" => "repo",
"content-length" => "192"},
:response => ""
}
18 changes: 18 additions & 0 deletions spec/lib/git_reflow/workflows/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,24 @@ class CoreWorkflow < GitReflow::Workflows::Core
expect(GitReflow.git_server).to receive(:create_pull_request).and_return(existing_gh_pull_request)
expect{ subject }.to have_said "Successfully created pull request ##{existing_gh_pull_request.number}: #{existing_gh_pull_request.title}\nPull Request URL: #{existing_gh_pull_request.html_url}\n", :success
end

context "when pull request creation fails" do
let(:github_error) { Github::Error::UnprocessableEntity.new(eval(Fixture.new('pull_requests/pull_request_branch_nonexistent_error.json').to_s)) }

it "retries creating a pull request" do
call_count = 0
allow(GitReflow.git_server).to receive(:create_pull_request) do
call_count += 1
(call_count <= 1) ? raise(github_error) : existing_gh_pull_request
end
expect{ subject }.to have_said "Successfully created pull request ##{existing_gh_pull_request.number}: #{existing_gh_pull_request.title}\nPull Request URL: #{existing_gh_pull_request.html_url}\n", :success
end

it "reports failures even after retrying" do
allow(GitReflow.git_server).to receive(:create_pull_request).and_raise github_error
expect { subject }.to have_said "Github Error: #{github_error.to_s}", :error
end
end
end

context "aborting during PR template review" do
Expand Down