Read about our upcoming Code of Conduct on this issue

merge_requests_closing_issues.rb 1.62 KB
Newer Older
1
2
# frozen_string_literal: true

3
class MergeRequestsClosingIssues < ApplicationRecord
4
5
  belongs_to :merge_request
  belongs_to :issue
6

7
8
  validates :merge_request_id, uniqueness: { scope: :issue_id }, presence: true
  validates :issue_id, presence: true
9

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  scope :with_issues, ->(ids) { where(issue_id: ids) }
  scope :with_merge_requests_enabled, -> do
    joins(:merge_request)
      .joins('INNER JOIN project_features ON merge_requests.target_project_id = project_features.project_id')
      .where('project_features.merge_requests_access_level >= :access', access: ProjectFeature::ENABLED)
  end

  scope :accessible_by, ->(user) do
    joins(:merge_request)
      .joins('INNER JOIN project_features ON merge_requests.target_project_id = project_features.project_id')
      .where('project_features.merge_requests_access_level >= :access OR EXISTS(:authorizations)',
             access: ProjectFeature::ENABLED,
             authorizations: user.authorizations_for_projects(min_access_level: Gitlab::Access::REPORTER, related_project_column: "merge_requests.target_project_id")
            )
  end

26
  class << self
27
    def count_for_collection(ids, current_user)
Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
28
      closing_merge_requests(ids, current_user).group(:issue_id).pluck('issue_id', Arel.sql('COUNT(*) as count'))
29
30
31
32
33
34
35
36
37
38
39
40
41
    end

    def count_for_issue(id, current_user)
      closing_merge_requests(id, current_user).count
    end

    private

    def closing_merge_requests(ids, current_user)
      return with_issues(ids) if current_user&.admin?
      return with_issues(ids).with_merge_requests_enabled if current_user.blank?

      with_issues(ids).accessible_by(current_user)
42
43
    end
  end
44
end