Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2019-02-09 15:28:23 +0300
committerStan Hu <stanhu@gmail.com>2019-02-12 09:00:05 +0300
commite34a321327cbbb1f615b9594c920dc04277bdb81 (patch)
tree61820dd5cde8cc0eacfe1f2c04aeabe78bffa97a /lib/gitlab/github_import
parent91b88e0be4a943aa7000c7d921e77fc200c53500 (diff)
Create the source branch for a GitHub import
When the GitHub importer creates a merge request, it retrieves the SHA but does not actually create the source branch. This makes it impossible to merge an open merge request, particularly if the source branch were from a forked project. In that case, the branch will never exist because the original `project-name:source-branch` name is never created, nor is it a valid branch name. To prevent possible branch name conflicts, forked source branches are now renamed `github/fork/project-name/source-branch` and created when necessary. Note that we only create the source branch if the merge request is open. For projects that have many merge requests, the project would end up with a lot of possibly dead branches. Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/57370
Diffstat (limited to 'lib/gitlab/github_import')
-rw-r--r--lib/gitlab/github_import/importer/pull_request_importer.rb30
-rw-r--r--lib/gitlab/github_import/representation/pull_request.rb4
2 files changed, 32 insertions, 2 deletions
diff --git a/lib/gitlab/github_import/importer/pull_request_importer.rb b/lib/gitlab/github_import/importer/pull_request_importer.rb
index ae7c4cf1b38..e294173f992 100644
--- a/lib/gitlab/github_import/importer/pull_request_importer.rb
+++ b/lib/gitlab/github_import/importer/pull_request_importer.rb
@@ -67,6 +67,36 @@ module Gitlab
def insert_git_data(merge_request, already_exists)
insert_or_replace_git_data(merge_request, pull_request.source_branch_sha, pull_request.target_branch_sha, already_exists)
+ # We need to create the branch after the merge request is
+ # populated to ensure the merge request is in the right state
+ # when the branch is created.
+ create_source_branch_if_not_exists(merge_request)
+ end
+
+ # An imported merge request will not be mergeable unless the
+ # source branch exists. For pull requests from forks, the source
+ # branch will be in the form of
+ # "github/fork/{project-name}/{source_branch}". This branch will never
+ # exist, so we create it here.
+ #
+ # Note that we only create the branch if the merge request is still open.
+ # For projects that have many pull requests, we assume that if it's closed
+ # the branch has already been deleted.
+ def create_source_branch_if_not_exists(merge_request)
+ return unless merge_request.open?
+
+ source_branch = pull_request.formatted_source_branch
+
+ return if project.repository.branch_exists?(source_branch)
+
+ project.repository.add_branch(merge_request.author, source_branch, pull_request.source_branch_sha)
+ rescue Gitlab::Git::CommandError => e
+ Gitlab::Sentry.track_acceptable_exception(e,
+ extra: {
+ source_branch: source_branch,
+ project_id: merge_request.project.id,
+ merge_request_id: merge_request.id
+ })
end
end
end
diff --git a/lib/gitlab/github_import/representation/pull_request.rb b/lib/gitlab/github_import/representation/pull_request.rb
index 593b491a837..0ccc4bfaed3 100644
--- a/lib/gitlab/github_import/representation/pull_request.rb
+++ b/lib/gitlab/github_import/representation/pull_request.rb
@@ -76,10 +76,10 @@ module Gitlab
# Returns a formatted source branch.
#
# For cross-project pull requests the branch name will be in the format
- # `owner-name:branch-name`.
+ # `github/fork/owner-name/branch-name`.
def formatted_source_branch
if cross_project? && source_repository_owner
- "#{source_repository_owner}:#{source_branch}"
+ "github/fork/#{source_repository_owner}/#{source_branch}"
elsif source_branch == target_branch
# Sometimes the source and target branch are the same, but GitLab
# doesn't support this. This can happen when both the user and