2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 11:21:25 -04:00
|
|
|
module Projects
|
|
|
|
# Service class for counting and caching the number of open issues of a
|
|
|
|
# project.
|
2017-11-15 09:47:10 -05:00
|
|
|
class OpenIssuesCountService < Projects::CountService
|
2018-05-17 15:39:48 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-06-15 13:32:37 -04:00
|
|
|
# Cache keys used to store issues count
|
2021-09-16 17:11:39 -04:00
|
|
|
# TOTAL_COUNT_KEY includes confidential and hidden issues (admin)
|
|
|
|
# TOTAL_COUNT_WITHOUT_HIDDEN_KEY includes confidential issues but not hidden issues (reporter and above)
|
|
|
|
# PUBLIC_COUNT_WITHOUT_HIDDEN_KEY does not include confidential or hidden issues (guest)
|
|
|
|
TOTAL_COUNT_KEY = 'project_open_issues_including_hidden_count'
|
|
|
|
TOTAL_COUNT_WITHOUT_HIDDEN_KEY = 'project_open_issues_without_hidden_count'
|
|
|
|
PUBLIC_COUNT_WITHOUT_HIDDEN_KEY = 'project_open_public_issues_without_hidden_count'
|
2018-06-15 13:32:37 -04:00
|
|
|
|
2018-05-17 15:39:48 -04:00
|
|
|
def initialize(project, user = nil)
|
|
|
|
@user = user
|
|
|
|
|
|
|
|
super(project)
|
|
|
|
end
|
|
|
|
|
2021-09-16 17:11:39 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def refresh_cache(&block)
|
|
|
|
if block_given?
|
|
|
|
super(&block)
|
|
|
|
else
|
|
|
|
update_cache_for_key(total_count_cache_key) do
|
|
|
|
issues_with_hidden
|
|
|
|
end
|
|
|
|
|
|
|
|
update_cache_for_key(public_count_without_hidden_cache_key) do
|
|
|
|
issues_without_hidden_without_confidential
|
|
|
|
end
|
|
|
|
|
|
|
|
update_cache_for_key(total_count_without_hidden_cache_key) do
|
|
|
|
issues_without_hidden_with_confidential
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def relation_for_count
|
|
|
|
self.class.query(@project, public_only: public_only?, include_hidden: include_hidden?)
|
|
|
|
end
|
|
|
|
|
2017-08-17 11:21:25 -04:00
|
|
|
def cache_key_name
|
2021-09-16 17:11:39 -04:00
|
|
|
if include_hidden?
|
|
|
|
TOTAL_COUNT_KEY
|
|
|
|
elsif public_only?
|
|
|
|
PUBLIC_COUNT_WITHOUT_HIDDEN_KEY
|
|
|
|
else
|
|
|
|
TOTAL_COUNT_WITHOUT_HIDDEN_KEY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_hidden?
|
|
|
|
user_is_admin?
|
2018-05-17 15:39:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def public_only?
|
|
|
|
!user_is_at_least_reporter?
|
|
|
|
end
|
|
|
|
|
2021-09-16 17:11:39 -04:00
|
|
|
def user_is_admin?
|
|
|
|
strong_memoize(:user_is_admin) do
|
|
|
|
@user&.can_admin_all_resources?
|
|
|
|
end
|
2018-05-17 15:39:48 -04:00
|
|
|
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 11:21:25 -04:00
|
|
|
end
|
2017-11-30 06:52:38 -05:00
|
|
|
|
2021-09-16 17:11:39 -04:00
|
|
|
def total_count_without_hidden_cache_key
|
|
|
|
cache_key(TOTAL_COUNT_WITHOUT_HIDDEN_KEY)
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_count_without_hidden_cache_key
|
|
|
|
cache_key(PUBLIC_COUNT_WITHOUT_HIDDEN_KEY)
|
2018-06-15 13:32:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def total_count_cache_key
|
|
|
|
cache_key(TOTAL_COUNT_KEY)
|
|
|
|
end
|
|
|
|
|
2021-09-16 17:11:39 -04:00
|
|
|
def issues_with_hidden
|
|
|
|
self.class.query(@project, public_only: false, include_hidden: true).count
|
|
|
|
end
|
2018-06-15 13:32:37 -04:00
|
|
|
|
2021-09-16 17:11:39 -04:00
|
|
|
def issues_without_hidden_without_confidential
|
|
|
|
self.class.query(@project, public_only: true, include_hidden: false).count
|
|
|
|
end
|
2018-06-15 13:32:37 -04:00
|
|
|
|
2021-09-16 17:11:39 -04:00
|
|
|
def issues_without_hidden_with_confidential
|
|
|
|
self.class.query(@project, public_only: false, include_hidden: false).count
|
2018-06-15 13:32:37 -04:00
|
|
|
end
|
|
|
|
|
2021-09-16 17:11:39 -04:00
|
|
|
# We only show total issues count for admins, who are allowed to view hidden issues.
|
|
|
|
# We also only show issues count including confidential for reporters, who are allowed to view confidential issues.
|
2018-05-17 15:39:48 -04:00
|
|
|
# This will still show a discrepancy on issues number but should be less than before.
|
2019-09-18 10:02:45 -04:00
|
|
|
# Check https://gitlab.com/gitlab-org/gitlab-foss/issues/38418 description.
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2020-09-17 05:09:32 -04:00
|
|
|
|
2021-09-16 17:11:39 -04:00
|
|
|
def self.query(projects, public_only: true, include_hidden: false)
|
|
|
|
if include_hidden
|
|
|
|
Issue.opened.with_issue_type(Issue::TYPES_FOR_LIST).where(project: projects)
|
|
|
|
elsif public_only
|
|
|
|
Issue.public_only.opened.with_issue_type(Issue::TYPES_FOR_LIST).where(project: projects)
|
2018-05-17 15:39:48 -04:00
|
|
|
else
|
2021-09-16 17:11:39 -04:00
|
|
|
Issue.without_hidden.opened.with_issue_type(Issue::TYPES_FOR_LIST).where(project: projects)
|
2018-05-17 15:39:48 -04:00
|
|
|
end
|
2017-11-30 06:52:38 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 11:21:25 -04:00
|
|
|
end
|
|
|
|
end
|