diff --git a/lib/modulesync/git_service/github.rb b/lib/modulesync/git_service/github.rb index 29303260..53deea87 100644 --- a/lib/modulesync/git_service/github.rb +++ b/lib/modulesync/git_service/github.rb @@ -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 diff --git a/lib/modulesync/git_service/gitlab.rb b/lib/modulesync/git_service/gitlab.rb index 42d53413..22df238e 100644 --- a/lib/modulesync/git_service/gitlab.rb +++ b/lib/modulesync/git_service/gitlab.rb @@ -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 diff --git a/spec/unit/module_sync/git_service/github_spec.rb b/spec/unit/module_sync/git_service/github_spec.rb index 506e774d..ef6b97fe 100644 --- a/spec/unit/module_sync/git_service/github_spec.rb +++ b/spec/unit/module_sync/git_service/github_spec.rb @@ -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', @@ -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 diff --git a/spec/unit/module_sync/git_service/gitlab_spec.rb b/spec/unit/module_sync/git_service/gitlab_spec.rb index e0221dbe..f5ae10c1 100644 --- a/spec/unit/module_sync/git_service/gitlab_spec.rb +++ b/spec/unit/module_sync/git_service/gitlab_spec.rb @@ -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', @@ -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