Class: Banzai::ReferenceParser::IssueParser

Inherits:
IssuableParser show all
Defined in:
lib/banzai/reference_parser/issue_parser.rb

Instance Method Summary collapse

Methods inherited from IssuableParser

#can_read_reference?, #referenced_by

Methods inherited from BaseParser

#can?, #collection_cache_key, #collection_objects_for_ids, data_attribute, #find_projects_for_hash_keys, #gather_attributes_per_project, #gather_references, #grouped_objects_for_nodes, #initialize, #nodes_user_can_reference, #process, #project_for_node, #projects_for_nodes, #referenced_by, #references_relation, #unique_attribute_values

Constructor Details

This class inherits a constructor from Banzai::ReferenceParser::BaseParser

Instance Method Details

#nodes_visible_to_user(user, nodes) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/banzai/reference_parser/issue_parser.rb', line 8

def nodes_visible_to_user(user, nodes)
  issues = records_for_nodes(nodes)
  issues_to_check, cross_project_issues = partition_issues(issues, user)

  readable_issues =
    Ability.issues_readable_by_user(issues_to_check, user).to_set

  nodes.select do |node|
    issue_in_node = issues[node]

    # We check the inclusion of readable issues first because it's faster.
    #
    # But we need to fall back to `read_issue_iid` if the user cannot read
    # cross project, since it might be possible the user can see the IID
    # but not the issue.
    if readable_issues.include?(issue_in_node)
      true
    elsif cross_project_issues.include?(issue_in_node)
      can_read_reference?(user, issue_in_node)
    else
      false
    end
  end
end

#partition_issues(issues, user) ⇒ Object

issues - A Hash mapping HTML nodes to their corresponding Issue

instances.

user - The current User.


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/banzai/reference_parser/issue_parser.rb', line 36

def partition_issues(issues, user)
  return [issues.values, []] if can?(user, :read_cross_project)

  issues_to_check = []
  cross_project_issues = []

  # We manually partition the data since our input is a Hash and our
  # output has to be an Array of issues; not an Array of (node, issue)
  # pairs.
  issues.each do |node, issue|
    target =
      if issue.project == project_for_node(node)
        issues_to_check
      else
        cross_project_issues
      end

    target << issue
  end

  [issues_to_check, cross_project_issues]
end

#records_for_nodes(nodes) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/banzai/reference_parser/issue_parser.rb', line 59

def records_for_nodes(nodes)
  @issues_for_nodes ||= grouped_objects_for_nodes(
    nodes,
    Issue.all.includes(
      :author,
      :assignees,
      {
        # These associations are primarily used for checking permissions.
        # Eager loading these ensures we don't end up running dozens of
        # queries in this process.
        project: [
          { namespace: :owner },
          { group: [:owners, :group_members] },
          :invited_groups,
          :project_members,
          :project_feature,
          :route
        ]
      }
    ),
    self.class.data_attribute
  )
end