Check out locally PRs where the source/target branch were removed

parent 1aba0668
...@@ -71,11 +71,12 @@ module Gitlab ...@@ -71,11 +71,12 @@ module Gitlab
pull_requests = client.pull_requests(repo, state: :all, sort: :created, direction: :asc, per_page: 100) pull_requests = client.pull_requests(repo, state: :all, sort: :created, direction: :asc, per_page: 100)
pull_requests = pull_requests.map { |raw| PullRequestFormatter.new(project, raw) }.select(&:valid?) pull_requests = pull_requests.map { |raw| PullRequestFormatter.new(project, raw) }.select(&:valid?)
source_branches_removed = pull_requests.reject(&:source_branch_exists?).map { |pr| [pr.source_branch_name, pr.source_branch_sha] } source_branches_removed = pull_requests.reject(&:source_branch_exists?).map { |pr| [pr.source_branch_name, pr.number] }
target_branches_removed = pull_requests.reject(&:target_branch_exists?).map { |pr| [pr.target_branch_name, pr.target_branch_sha] } target_branches_removed = pull_requests.reject(&:target_branch_exists?).map { |pr| [pr.target_branch_name, pr.target_branch_sha] }
branches_removed = source_branches_removed | target_branches_removed branches_removed = source_branches_removed | target_branches_removed
restore_branches(branches_removed) restore_source_branches(source_branches_removed)
restore_target_branches(target_branches_removed)
pull_requests.each do |pull_request| pull_requests.each do |pull_request|
merge_request = pull_request.create! merge_request = pull_request.create!
...@@ -120,17 +121,20 @@ module Gitlab ...@@ -120,17 +121,20 @@ module Gitlab
end end
end end
def restore_branches(branches) def restore_source_branches(branches)
branches.each do |name, sha| branches.each do |name, number|
client.create_ref(repo, "refs/heads/#{name}", sha) project.repository.fetch_ref(repo_url, "pull/#{number}/head", name)
end
end end
project.repository.fetch_ref(repo_url, '+refs/heads/*', 'refs/heads/*') def restore_target_branches(branches)
branches.each do |name, sha|
project.repository.create_branch(name, sha)
end
end end
def clean_up_restored_branches(branches) def clean_up_restored_branches(branches)
branches.each do |name, _| branches.each do |name, _|
client.delete_ref(repo, "heads/#{name}")
project.repository.delete_branch(name) rescue Rugged::ReferenceError project.repository.delete_branch(name) rescue Rugged::ReferenceError
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment