2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 15:21:25 +00:00
|
|
|
module Projects
|
|
|
|
# Service class for counting and caching the number of open issues of a
|
|
|
|
# project.
|
2017-11-15 14:47:10 +00:00
|
|
|
class OpenIssuesCountService < Projects::CountService
|
2018-05-17 19:39:48 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-06-15 17:32:37 +00:00
|
|
|
# Cache keys used to store issues count
|
|
|
|
PUBLIC_COUNT_KEY = 'public_open_issues_count'.freeze
|
|
|
|
TOTAL_COUNT_KEY = 'total_open_issues_count'.freeze
|
|
|
|
|
2018-05-17 19:39:48 +00:00
|
|
|
def initialize(project, user = nil)
|
|
|
|
@user = user
|
|
|
|
|
|
|
|
super(project)
|
|
|
|
end
|
|
|
|
|
2017-08-17 15:21:25 +00:00
|
|
|
def cache_key_name
|
2018-06-15 17:32:37 +00:00
|
|
|
public_only? ? PUBLIC_COUNT_KEY : TOTAL_COUNT_KEY
|
2018-05-17 19:39:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def public_only?
|
|
|
|
!user_is_at_least_reporter?
|
|
|
|
end
|
|
|
|
|
|
|
|
def relation_for_count
|
|
|
|
self.class.query(@project, public_only: public_only?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_is_at_least_reporter?
|
|
|
|
strong_memoize(:user_is_at_least_reporter) do
|
|
|
|
@user && @project.team.member?(@user, Gitlab::Access::REPORTER)
|
|
|
|
end
|
2017-08-17 15:21:25 +00:00
|
|
|
end
|
2017-11-30 11:52:38 +00:00
|
|
|
|
2018-06-15 17:32:37 +00:00
|
|
|
def public_count_cache_key
|
|
|
|
cache_key(PUBLIC_COUNT_KEY)
|
|
|
|
end
|
|
|
|
|
|
|
|
def total_count_cache_key
|
|
|
|
cache_key(TOTAL_COUNT_KEY)
|
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_cache(&block)
|
2018-06-22 17:17:03 +00:00
|
|
|
if block_given?
|
|
|
|
super(&block)
|
|
|
|
else
|
|
|
|
count_grouped_by_confidential = self.class.query(@project, public_only: false).group(:confidential).count
|
|
|
|
public_count = count_grouped_by_confidential[false] || 0
|
|
|
|
total_count = public_count + (count_grouped_by_confidential[true] || 0)
|
2018-06-15 17:32:37 +00:00
|
|
|
|
2018-06-22 17:17:03 +00:00
|
|
|
update_cache_for_key(public_count_cache_key) do
|
|
|
|
public_count
|
|
|
|
end
|
2018-06-15 17:32:37 +00:00
|
|
|
|
2018-06-22 17:17:03 +00:00
|
|
|
update_cache_for_key(total_count_cache_key) do
|
|
|
|
total_count
|
|
|
|
end
|
2018-06-15 17:32:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-17 19:39:48 +00:00
|
|
|
# We only show total issues count for reporters
|
|
|
|
# which are allowed to view confidential issues
|
|
|
|
# This will still show a discrepancy on issues number but should be less than before.
|
|
|
|
# Check https://gitlab.com/gitlab-org/gitlab-ce/issues/38418 description.
|
|
|
|
def self.query(projects, public_only: true)
|
|
|
|
if public_only
|
|
|
|
Issue.opened.public_only.where(project: projects)
|
|
|
|
else
|
|
|
|
Issue.opened.where(project: projects)
|
|
|
|
end
|
2017-11-30 11:52:38 +00:00
|
|
|
end
|
2017-08-17 15:21:25 +00:00
|
|
|
end
|
|
|
|
end
|