Class: Gitlab::ImportExport::MergeRequestParser
- Inherits:
-
Object
- Object
- Gitlab::ImportExport::MergeRequestParser
- Defined in:
- lib/gitlab/import_export/merge_request_parser.rb
Constant Summary collapse
- FORKED_PROJECT_ID =
nil
Instance Method Summary collapse
- #branch_exists?(branch_name) ⇒ Boolean
-
#create_source_branch ⇒ Object
When the exported MR was in a fork, the source branch does not exist in the imported bundle - although the commits usually do - so it must be created manually.
- #create_target_branch ⇒ Object
- #fork_merge_request? ⇒ Boolean
-
#initialize(project, diff_head_sha, merge_request, relation_hash) ⇒ MergeRequestParser
constructor
A new instance of MergeRequestParser.
- #parse! ⇒ Object
Constructor Details
#initialize(project, diff_head_sha, merge_request, relation_hash) ⇒ MergeRequestParser
Returns a new instance of MergeRequestParser.
8 9 10 11 12 13 |
# File 'lib/gitlab/import_export/merge_request_parser.rb', line 8 def initialize(project, diff_head_sha, merge_request, relation_hash) @project = project @diff_head_sha = diff_head_sha @merge_request = merge_request @relation_hash = relation_hash end |
Instance Method Details
#branch_exists?(branch_name) ⇒ Boolean
59 60 61 |
# File 'lib/gitlab/import_export/merge_request_parser.rb', line 59 def branch_exists?(branch_name) @project.repository.raw.branch_exists?(branch_name) end |
#create_source_branch ⇒ Object
When the exported MR was in a fork, the source branch does not exist in the imported bundle - although the commits usually do - so it must be created manually. Ignore failures so we get the merge request itself if the commits are missing.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gitlab/import_export/merge_request_parser.rb', line 41 def create_source_branch if @merge_request.open? @project.repository.create_branch(@merge_request.source_branch, @diff_head_sha) end rescue StandardError => err Gitlab::Import::Logger.warn( message: 'Import warning: Failed to create source branch', source_branch: @merge_request.source_branch, diff_head_sha: @diff_head_sha, merge_request_iid: @merge_request.iid, error: err. ) end |
#create_target_branch ⇒ Object
55 56 57 |
# File 'lib/gitlab/import_export/merge_request_parser.rb', line 55 def create_target_branch @project.repository.create_branch(@merge_request.target_branch, @merge_request.target_branch_sha) end |
#fork_merge_request? ⇒ Boolean
63 64 65 |
# File 'lib/gitlab/import_export/merge_request_parser.rb', line 63 def fork_merge_request? @relation_hash['source_project_id'] == FORKED_PROJECT_ID end |
#parse! ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/gitlab/import_export/merge_request_parser.rb', line 15 def parse! if fork_merge_request? && @diff_head_sha @merge_request.source_project_id = @relation_hash['project_id'] create_source_branch unless branch_exists?(@merge_request.source_branch) create_target_branch unless branch_exists?(@merge_request.target_branch) end # The merge_request_diff associated with the current @merge_request might # be invalid. Than means, when the @merge_request object is saved, the # @merge_request.merge_request_diff won't. This can leave the merge request # in an invalid state, because a merge request must have an associated # merge request diff. # In this change, if the associated merge request diff is invalid, we set # it to nil. This change, in association with the after callback # :ensure_merge_request_diff in the MergeRequest class, makes that # when the merge request is going to be created and it doesn't have # one, a default one will be generated. @merge_request.merge_request_diff = nil unless @merge_request.merge_request_diff&.valid? @merge_request end |