Commit 3eb3badc authored by Stan Hu's avatar Stan Hu

Fix pipelines for merge requests in project exports

Previously an import for a pipeline for merge request would fail because
the `project.json` did not export the `merge_request` hash in the tree.
The pipeline would fail to validate with, "Validation failed: Merge
request can't be blank".

Unlike https://gitlab.com/gitlab-org/gitlab/merge_requests/17799, this
change won't make existing exports importable, but it will properly
establish a relationship between a pipeline and a merge request.

Relates to https://gitlab.com/gitlab-org/gitlab/issues/31414
parent 15e068b9
---
title: Fix pipelines for merge requests in project exports
merge_request: 17844
author:
type: fixed
......@@ -66,6 +66,7 @@ tree:
- stages:
- :statuses
- :external_pull_request
- :merge_request
- :external_pull_requests
- :auto_devops
- :triggers
......@@ -168,6 +169,12 @@ excluded_attributes:
- :external_diff_size
issues:
- :milestone_id
merge_request:
- :milestone_id
- :ref_fetched
- :merge_jid
- :rebase_jid
- :latest_merge_request_diff_id
merge_requests:
- :milestone_id
- :ref_fetched
......
......@@ -38,7 +38,7 @@ module Gitlab
IMPORTED_OBJECT_MAX_RETRIES = 5.freeze
EXISTING_OBJECT_CHECK = %i[milestone milestones label labels project_label project_labels group_label group_labels project_feature].freeze
EXISTING_OBJECT_CHECK = %i[milestone milestones label labels project_label project_labels group_label group_labels project_feature merge_request].freeze
TOKEN_RESET_MODELS = %i[Project Namespace Ci::Trigger Ci::Build Ci::Runner ProjectHook].freeze
......@@ -293,6 +293,7 @@ module Gitlab
def find_or_create_object!
return relation_class.find_or_create_by(project_id: @project.id) if @relation_name == :project_feature
return find_or_create_merge_request! if @relation_name == :merge_request
# Can't use IDs as validation exists calling `group` or `project` attributes
finder_hash = parsed_relation_hash.tap do |hash|
......@@ -303,6 +304,11 @@ module Gitlab
GroupProjectObjectBuilder.build(relation_class, finder_hash)
end
def find_or_create_merge_request!
@project.merge_requests.find_by(iid: parsed_relation_hash['iid']) ||
relation_class.new(parsed_relation_hash)
end
end
end
end
......@@ -6161,7 +6161,7 @@
{
"id": 36,
"project_id": 5,
"ref": "master",
"ref": null,
"sha": "sha-notes",
"before_sha": null,
"push_data": null,
......@@ -6289,19 +6289,21 @@
{
"id": 37,
"project_id": 5,
"ref": null,
"ref": "master",
"sha": "048721d90c449b244b7b4c53a9186b04330174ec",
"before_sha": null,
"push_data": null,
"created_at": "2016-03-22T15:20:35.757Z",
"updated_at": "2016-03-22T15:20:35.757Z",
"tag": null,
"tag": false,
"yaml_errors": null,
"committed_at": null,
"status": "failed",
"started_at": null,
"finished_at": null,
"duration": null,
"source": "merge_request_event",
"merge_request_id": 27,
"stages": [
{
"id": 21,
......@@ -6372,7 +6374,32 @@
}
]
}
]
],
"merge_request": {
"id": 27,
"target_branch": "feature",
"source_branch": "feature_conflict",
"source_project_id": 999,
"author_id": 1,
"assignee_id": null,
"title": "MR1",
"created_at": "2016-06-14T15:02:36.568Z",
"updated_at": "2016-06-14T15:02:56.815Z",
"state": "opened",
"merge_status": "unchecked",
"target_project_id": 5,
"iid": 9,
"description": null,
"position": 0,
"updated_by_id": null,
"merge_error": null,
"diff_head_sha": "HEAD",
"source_branch_sha": "ABCD",
"target_branch_sha": "DCBA",
"merge_params": {
"force_remove_source_branch": null
}
}
},
{
"id": 38,
......
......@@ -12,9 +12,9 @@ describe 'Import/Export model configuration' do
# Remove duplicated or add missing models
# - project is not part of the tree, so it has to be added manually.
# - milestone, labels have both singular and plural versions in the tree, so remove the duplicates.
# - milestone, labels, merge_request have both singular and plural versions in the tree, so remove the duplicates.
# - User, Author... Models we do not care about for checking models
names.flatten.uniq - %w(milestones labels user author) + ['project']
names.flatten.uniq - %w(milestones labels user author merge_request) + ['project']
end
let(:all_models_yml) { 'spec/lib/gitlab/import_export/all_models.yml' }
let(:all_models_hash) { YAML.load_file(all_models_yml) }
......
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