From 1e6ea914a1affef8490ca15338c885c11d54e17f Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 19 Mar 2019 10:07:05 -0700 Subject: [PATCH] GitHub import: Create new branches as project owner This avoids permission errors when importing merge requests whose authors don't have direct access to push to the main repository. --- .../sh-create-branch-as-project-owner-for-github-import.yml | 5 +++++ lib/gitlab/github_import/importer/pull_request_importer.rb | 2 +- .../github_import/importer/pull_request_importer_spec.rb | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/sh-create-branch-as-project-owner-for-github-import.yml diff --git a/changelogs/unreleased/sh-create-branch-as-project-owner-for-github-import.yml b/changelogs/unreleased/sh-create-branch-as-project-owner-for-github-import.yml new file mode 100644 index 00000000000..a3d484cbf05 --- /dev/null +++ b/changelogs/unreleased/sh-create-branch-as-project-owner-for-github-import.yml @@ -0,0 +1,5 @@ +--- +title: 'GitHub import: Create new branches as project owner' +merge_request: 26335 +author: +type: fixed diff --git a/lib/gitlab/github_import/importer/pull_request_importer.rb b/lib/gitlab/github_import/importer/pull_request_importer.rb index e294173f992..72451e5e01e 100644 --- a/lib/gitlab/github_import/importer/pull_request_importer.rb +++ b/lib/gitlab/github_import/importer/pull_request_importer.rb @@ -89,7 +89,7 @@ module Gitlab return if project.repository.branch_exists?(source_branch) - project.repository.add_branch(merge_request.author, source_branch, pull_request.source_branch_sha) + project.repository.add_branch(project.owner, source_branch, pull_request.source_branch_sha) rescue Gitlab::Git::CommandError => e Gitlab::Sentry.track_acceptable_exception(e, extra: { diff --git a/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb b/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb index 15e59718dce..37c3fae7cb7 100644 --- a/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb +++ b/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb @@ -273,6 +273,11 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi mr.state = 'opened' mr.save + # Ensure the project owner is creating the branches because the + # merge request author may not have access to push to this + # repository. + allow(project.repository).to receive(:add_branch).with(project.owner, anything, anything).and_call_original + importer.insert_git_data(mr, exists) expect(project.repository.branch_exists?(mr.source_branch)).to be_truthy