summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2019-02-12 11:05:22 +0000
committerGitLab Release Tools Bot <robert+release-tools@gitlab.com>2019-02-12 16:33:31 +0000
commit5535978520bf250276813e622b322fe69c06bc49 (patch)
treec3257dda64784ac1b1dcabed3985df7de4af71d6 /lib
parentbdc63ee179ecd6c9f2f5614ea2101c327e758159 (diff)
downloadgitlab-ce-5535978520bf250276813e622b322fe69c06bc49.tar.gz
Merge branch 'sh-import-source-branch-github-forks' into 'master'
Create the source branch for a GitHub import Closes #57370 See merge request gitlab-org/gitlab-ce!25064 (cherry picked from commit 6dfce6e786f4828f3d2b0ff63d2a2dea4c82471c) e34a3213 Create the source branch for a GitHub import
Diffstat (limited to 'lib')
-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