Skip to content
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
13 changes: 11 additions & 2 deletions lib/modulesync/git_service/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,17 @@ def _open_pull_request(repo_path:, namespace:, title:, message:, source_branch:,
base: target_branch,
head: head)
unless pull_requests.empty?
# Skip creating the PR if it exists already.
$stdout.puts "Skipped! #{pull_requests.length} PRs found for branch '#{source_branch}'"
# assumption: there should only be one PR for a given source and target branch, so we just take the first one
pr = pull_requests.first

if pr['title'] == title
$stdout.puts "Skipped! #{pull_requests.length} PRs found for branch '#{source_branch}'"
$stdout.puts "Skipped! Existing PR ##{pr['number']} already has title '#{title}'"
else
@api.update_pull_request(repo_path, pr['number'], title: title)
$stdout.puts "Updated title of existing PR ##{pr['number']} to '#{title}'"
end

return
end

Expand Down
13 changes: 11 additions & 2 deletions lib/modulesync/git_service/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@ def _open_pull_request(repo_path:, namespace:, title:, message:, source_branch:,
source_branch: source_branch,
target_branch: target_branch)
unless merge_requests.empty?
# Skip creating the MR if it exists already.
$stdout.puts "Skipped! #{merge_requests.length} MRs found for branch '#{source_branch}'"
# assumption: there should only be one MR for a given source and target branch, so we just take the first one
mr = merge_requests.first

if mr['title'] == title
$stdout.puts "Skipped! #{merge_requests.length} MRs found for branch '#{source_branch}'"
$stdout.puts "Skipped! Existing MR !#{mr['iid']} already has title '#{title}'"
else
@api.update_merge_request(repo_path, mr['iid'], title: title)
$stdout.puts "Updated title of existing MR !#{mr['iid']} to '#{title}'"
end

return
end

Expand Down
25 changes: 23 additions & 2 deletions spec/unit/module_sync/git_service/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
expect { @it.open_pull_request(**args) }.to output(/Submitted PR/).to_stdout
end

it 'skips submitting PR if one has already been issued' do
it 'updates the title if an existing PR has a different title' do
pr = {
'title' => 'Test title',
'html_url' => 'https://example.com/pulls/44',
Expand All @@ -66,7 +66,28 @@
state: 'open',
base: 'master',
head: "#{args[:namespace]}:#{args[:source_branch]}").and_return([pr])
expect { @it.open_pull_request(**args) }.to output("Skipped! 1 PRs found for branch 'test'\n").to_stdout
expect(@client).to receive(:update_pull_request)
.with(args[:repo_path], pr['number'], title: args[:title])
expect { @it.open_pull_request(**args) }
.to output("Updated title of existing PR #44 to 'Test PR is submitted'\n").to_stdout
end

it 'skips updating an existing PR if its title is unchanged' do
pr = {
'title' => args[:title],
'html_url' => 'https://example.com/pulls/44',
'number' => '44',
}

expect(@client).to receive(:pull_requests)
.with(args[:repo_path],
state: 'open',
base: 'master',
head: "#{args[:namespace]}:#{args[:source_branch]}").and_return([pr])
expect(@client).not_to receive(:update_pull_request)
expect { @it.open_pull_request(**args) }
.to output("Skipped! 1 PRs found for branch 'test'\n" \
"Skipped! Existing PR #44 already has title 'Test PR is submitted'\n").to_stdout
end

context 'when labels are set' do
Expand Down
24 changes: 22 additions & 2 deletions spec/unit/module_sync/git_service/gitlab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
expect { @it.open_pull_request(**args) }.to output(/Submitted MR/).to_stdout
end

it 'skips submitting MR if one has already been issued' do
it 'updates the title if an existing MR has a different title' do
mr = {
'title' => 'Test title',
'html_url' => 'https://example.com/pulls/44',
Expand All @@ -56,8 +56,28 @@
state: 'opened',
source_branch: args[:source_branch],
target_branch: 'master').and_return([mr])
expect(@client).to receive(:update_merge_request)
.with(args[:repo_path], mr['iid'], title: args[:title])
expect { @it.open_pull_request(**args) }
.to output("Updated title of existing MR !44 to 'Test MR is submitted'\n").to_stdout
end

it 'skips updating an existing MR if its title is unchanged' do
mr = {
'title' => args[:title],
'html_url' => 'https://example.com/pulls/44',
'iid' => '44',
}

expect { @it.open_pull_request(**args) }.to output("Skipped! 1 MRs found for branch 'test'\n").to_stdout
expect(@client).to receive(:merge_requests)
.with(args[:repo_path],
state: 'opened',
source_branch: args[:source_branch],
target_branch: 'master').and_return([mr])
expect(@client).not_to receive(:update_merge_request)
expect { @it.open_pull_request(**args) }
.to output("Skipped! 1 MRs found for branch 'test'\n" \
"Skipped! Existing MR !44 already has title 'Test MR is submitted'\n").to_stdout
end

context 'when labels are set' do
Expand Down
Loading