class Gitlab::Triage::CommandBuilders::TextContentBuilder

Constants

PLACEHOLDER_REGEX
SUPPORTED_PLACEHOLDERS

Public Class Methods

new( items, resource: nil, network: nil, redact_confidentials: true) click to toggle source
# File lib/gitlab/triage/command_builders/text_content_builder.rb, line 37
def initialize(
  items, resource: nil, network: nil, redact_confidentials: true)
  super(items, resource: resource, network: network)
  @redact_confidentials = redact_confidentials
end

Private Instance Methods

eval_interpolation(item) click to toggle source
# File lib/gitlab/triage/command_builders/text_content_builder.rb, line 55
def eval_interpolation(item)
  quoted_comment = "%Q{#{item}}"

  Resource::Context.build(
    resource,
    network: network,
    redact_confidentials: @redact_confidentials
  ).eval(quoted_comment)
end
extract_attributes(path) click to toggle source
# File lib/gitlab/triage/command_builders/text_content_builder.rb, line 95
def extract_attributes(path)
  redact_attributes(path, resource_dig_and_map(path.split('.')))
end
format_item(item) click to toggle source
# File lib/gitlab/triage/command_builders/text_content_builder.rb, line 49
def format_item(item)
  return item unless resource

  replace_placeholders(eval_interpolation(item))
end
redact_attributes(path, attributes) click to toggle source
# File lib/gitlab/triage/command_builders/text_content_builder.rb, line 119
def redact_attributes(path, attributes)
  return attributes unless redact_confidential_attributes?

  case path
  when 'web_url', 'items', 'type'
    attributes # No need to redact them
  else
    [Resource::Base::CONFIDENTIAL_TEXT]
  end
end
redact_confidential_attributes?() click to toggle source
# File lib/gitlab/triage/command_builders/text_content_builder.rb, line 130
def redact_confidential_attributes?
  @redact_confidentials && resource[:confidential]
end
replace_placeholders(item) click to toggle source
# File lib/gitlab/triage/command_builders/text_content_builder.rb, line 65
def replace_placeholders(item)
  SUPPORTED_PLACEHOLDERS.inject(item) do |comment, (placeholder, template)|
    next comment unless comment.include?("{{#{placeholder}}}")

    path = template[/.*#{PLACEHOLDER_REGEX}.*/, 1]
    attributes = extract_attributes(path)

    formatted_text = attributes.map do |attribute|
      template.sub(PLACEHOLDER_REGEX, attribute.to_s)
    end.join(', ')

    escaped_text =
      case placeholder
      when :items
        # We don't need to escape it because it's recursive,
        # which the contents should all be escaped already.
        # Or put it another way, items isn't an attribute
        # retrieved externally. It's a generated value which
        # should be safe to begin with. At some point we
        # may want to make this more distinguishable,
        # separating values from API and values generated.
        formatted_text
      else
        CGI.escape_html(formatted_text)
      end

    comment.gsub("{{#{placeholder}}}", escaped_text)
  end
end
resource_dig_and_map(indices) click to toggle source

If we don't have to map arrays, we can simply do:

resource.dig(*indices)

Thus this method name. The only array here is `assignees`

# File lib/gitlab/triage/command_builders/text_content_builder.rb, line 104
def resource_dig_and_map(indices)
  attributes = indices.inject(resource) do |result, index|
    break unless result

    case result
    when Array
      result.flat_map { |sub_resource| sub_resource[index] }
    else
      result[index]
    end
  end

  Array.wrap(attributes)
end
separator() click to toggle source
# File lib/gitlab/triage/command_builders/text_content_builder.rb, line 45
def separator
  "\n\n"
end