Commit 4ae411ff authored by Felipe Artur's avatar Felipe Artur

Preloads head pipeline for each merge request

parent 11ff9fc6
......@@ -47,7 +47,7 @@ module IssuableCollections
end
def merge_requests_collection
merge_requests_finder.execute.preload(:source_project, :target_project, :author, :assignee, :labels, :milestone, :merge_request_diff, target_project: :namespace)
merge_requests_finder.execute.preload(:source_project, :target_project, :author, :assignee, :labels, :milestone, :merge_request_diff, :head_pipeline, target_project: :namespace)
end
def issues_finder
......
......@@ -13,6 +13,8 @@ class MergeRequest < ActiveRecord::Base
has_one :merge_request_diff,
-> { order('merge_request_diffs.id DESC') }
belongs_to :head_pipeline, foreign_key: "head_pipeline_id", class_name: "Ci::Pipeline"
has_many :events, as: :target, dependent: :destroy
has_many :merge_requests_closing_issues, class_name: 'MergeRequestsClosingIssues', dependent: :delete_all
......@@ -829,12 +831,6 @@ class MergeRequest < ActiveRecord::Base
diverged_commits_count > 0
end
def head_pipeline
return unless diff_head_sha && source_project
@head_pipeline ||= source_project.pipeline_for(source_branch, diff_head_sha)
end
def all_pipelines
return Ci::Pipeline.none unless source_project
......
......@@ -47,7 +47,7 @@ module Ci
end
Ci::Pipeline.transaction do
pipeline.save
update_merge_requests_head_pipeline if pipeline.save
Ci::CreatePipelineBuildsService
.new(project, current_user)
......@@ -118,6 +118,12 @@ module Ci
origin_sha && origin_sha != Gitlab::Git::BLANK_SHA
end
def update_merge_requests_head_pipeline
merge_requests = MergeRequest.where(source_branch: @pipeline.ref, source_project: @pipeline.project)
merge_requests.update_all(head_pipeline_id: @pipeline.id) if merge_requests.any?
end
def error(message, save: false)
pipeline.errors.add(:base, message)
pipeline.drop if save
......
---
title: Preloads head pipeline for each merge request
merge_request:
author:
class AddHeadPipelineIdToMergeRequests < ActiveRecord::Migration
DOWNTIME = false
def change
add_column :merge_requests, :head_pipeline_id, :integer
end
end
......@@ -123,6 +123,7 @@ ActiveRecord::Schema.define(version: 20170506185517) do
t.integer "cached_markdown_version"
t.boolean "clientside_sentry_enabled", default: false, null: false
t.string "clientside_sentry_dsn"
t.string "default_artifacts_expire_in", default: "0", null: false
end
create_table "audit_events", force: :cascade do |t|
......@@ -690,6 +691,7 @@ ActiveRecord::Schema.define(version: 20170506185517) do
t.integer "cached_markdown_version"
t.datetime "last_edited_at"
t.integer "last_edited_by_id"
t.integer "head_pipeline_id"
end
add_index "merge_requests", ["assignee_id"], name: "index_merge_requests_on_assignee_id", using: :btree
......
......@@ -158,6 +158,7 @@ MergeRequest:
- time_estimate
- last_edited_at
- last_edited_by_id
- head_pipeline_id
MergeRequestDiff:
- id
- state
......
......@@ -34,6 +34,18 @@ describe Ci::CreatePipelineService, services: true do
it { expect(pipeline).to have_attributes(status: 'pending') }
it { expect(pipeline.builds.first).to be_kind_of(Ci::Build) }
it 'updates head pipeline of each merge request' do
merge_request_1 = create(:merge_request, source_branch: 'master', target_branch: "branch_1", source_project: project)
merge_request_2 = create(:merge_request, source_branch: 'master', target_branch: "branch_2", source_project: project)
merge_request_3 = create(:merge_request, source_branch: 'other_branch', target_branch: "branch_2", source_project: project)
head_pipeline = pipeline
expect(merge_request_1.reload.head_pipeline).to eq(head_pipeline)
expect(merge_request_2.reload.head_pipeline).to eq(head_pipeline)
expect(merge_request_3.reload.head_pipeline).to be_nil
end
context 'auto-cancel enabled' do
before do
project.update(auto_cancel_pending_pipelines: 'enabled')
......
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