debian-mirror-gitlab/app/services/projects/open_issues_count_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2.1 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Projects
# Service class for counting and caching the number of open issues of a
# project.
class OpenIssuesCountService < Projects::CountService
2018-11-08 19:23:39 +05:30
include Gitlab::Utils::StrongMemoize
# Cache keys used to store issues count
2022-07-16 23:28:13 +05:30
PUBLIC_COUNT_KEY = 'public_open_issues_count'
TOTAL_COUNT_KEY = 'total_open_issues_count'
2018-11-08 19:23:39 +05:30
def initialize(project, user = nil)
@user = user
super(project)
end
2018-03-17 18:26:18 +05:30
def cache_key_name
2022-07-16 23:28:13 +05:30
public_only? ? PUBLIC_COUNT_KEY : TOTAL_COUNT_KEY
2018-11-08 19:23:39 +05:30
end
def public_only?
!user_is_at_least_reporter?
end
def user_is_at_least_reporter?
strong_memoize(:user_is_at_least_reporter) do
@user && @project.team.member?(@user, Gitlab::Access::REPORTER)
end
end
2022-07-16 23:28:13 +05:30
def relation_for_count
self.class.query(@project, public_only: public_only?)
2021-11-11 11:23:49 +05:30
end
2022-07-16 23:28:13 +05:30
def public_count_cache_key
cache_key(PUBLIC_COUNT_KEY)
2018-11-08 19:23:39 +05:30
end
def total_count_cache_key
cache_key(TOTAL_COUNT_KEY)
end
2022-07-16 23:28:13 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def refresh_cache(&block)
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-11-08 19:23:39 +05:30
2022-07-16 23:28:13 +05:30
update_cache_for_key(public_count_cache_key) do
public_count
end
2018-11-08 19:23:39 +05:30
2022-07-16 23:28:13 +05:30
update_cache_for_key(total_count_cache_key) do
total_count
end
2018-03-17 18:26:18 +05:30
end
2022-07-16 23:28:13 +05:30
# We only show issues count including confidential for reporters, who are allowed to view confidential issues.
2018-11-08 19:23:39 +05:30
# This will still show a discrepancy on issues number but should be less than before.
2019-12-04 20:38:33 +05:30
# Check https://gitlab.com/gitlab-org/gitlab-foss/issues/38418 description.
2022-07-16 23:28:13 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2022-07-16 23:28:13 +05:30
def self.query(projects, public_only: true)
2022-07-23 23:45:48 +05:30
open_issues = Issue.opened
2020-11-24 15:15:51 +05:30
2022-07-16 23:28:13 +05:30
if public_only
2022-07-23 23:45:48 +05:30
open_issues.public_only.where(project: projects)
2018-11-08 19:23:39 +05:30
else
2022-07-23 23:45:48 +05:30
open_issues.where(project: projects)
2018-11-08 19:23:39 +05:30
end
2018-03-17 18:26:18 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
end
end