Class: Projects::OpenIssuesCountService

Inherits:
CountService show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/projects/open_issues_count_service.rb

Overview

Service class for counting and caching the number of open issues of a project.

Constant Summary collapse

PUBLIC_COUNT_KEY =

Cache keys used to store issues count

'public_open_issues_count'
TOTAL_COUNT_KEY =
'total_open_issues_count'

Constants inherited from CountService

CountService::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from CountService

#cache_key

Methods inherited from BaseCountService

#cache_key, #cache_options, #count, #count_stored?, #delete_cache, #raw?, #uncached_count, #update_cache_for_key

Constructor Details

#initialize(project, user = nil) ⇒ OpenIssuesCountService

Returns a new instance of OpenIssuesCountService.


13
14
15
16
17
# File 'app/services/projects/open_issues_count_service.rb', line 13

def initialize(project, user = nil)
  @user = user

  super(project)
end

Class Method Details

.query(projects, public_only: true) ⇒ Object

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 gitlab.com/gitlab-org/gitlab-foss/issues/38418 description. rubocop: disable CodeReuse/ActiveRecord


70
71
72
73
74
75
76
# File 'app/services/projects/open_issues_count_service.rb', line 70

def self.query(projects, public_only: true)
  if public_only
    Issue.opened.public_only.where(project: projects)
  else
    Issue.opened.where(project: projects)
  end
end

Instance Method Details

#cache_key_nameObject


19
20
21
# File 'app/services/projects/open_issues_count_service.rb', line 19

def cache_key_name
  public_only? ? PUBLIC_COUNT_KEY : TOTAL_COUNT_KEY
end

#public_count_cache_keyObject


37
38
39
# File 'app/services/projects/open_issues_count_service.rb', line 37

def public_count_cache_key
  cache_key(PUBLIC_COUNT_KEY)
end

#public_only?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'app/services/projects/open_issues_count_service.rb', line 23

def public_only?
  !user_is_at_least_reporter?
end

#refresh_cache(&block) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/projects/open_issues_count_service.rb', line 46

def refresh_cache(&block)
  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)

    update_cache_for_key(public_count_cache_key) do
      public_count
    end

    update_cache_for_key(total_count_cache_key) do
      total_count
    end
  end
end

#relation_for_countObject


27
28
29
# File 'app/services/projects/open_issues_count_service.rb', line 27

def relation_for_count
  self.class.query(@project, public_only: public_only?)
end

#total_count_cache_keyObject


41
42
43
# File 'app/services/projects/open_issues_count_service.rb', line 41

def total_count_cache_key
  cache_key(TOTAL_COUNT_KEY)
end

#user_is_at_least_reporter?Boolean

Returns:

  • (Boolean)

31
32
33
34
35
# File 'app/services/projects/open_issues_count_service.rb', line 31

def user_is_at_least_reporter?
  strong_memoize(:user_is_at_least_reporter) do
    @user && @project.team.member?(@user, Gitlab::Access::REPORTER)
  end
end