Class: Gitlab::Triage::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/triage/engine.rb

Constant Summary collapse

FILTER_MAP =

This filter map is used to help make the filter_resource method smaller. We loop through each of the keys (conditions) and map that to the filters that will be used for it.

{
  date: {
    'branches' => Filters::BranchDateFilter,
    'issues' => Filters::IssueDateConditionsFilter,
    'merge_requests' => Filters::MergeRequestDateConditionsFilter,
    'work_items' => Filters::WorkItemDateConditionsFilter
  },
  protected: Filters::BranchProtectedFilter,
  assignee_member: Filters::AssigneeMemberConditionsFilter,
  author_member: Filters::AuthorMemberConditionsFilter,
  discussions: Filters::DiscussionsConditionsFilter,
  no_additional_labels: Filters::NoAdditionalLabelsConditionsFilter,
  ruby: Filters::RubyConditionsFilter,
  votes: Filters::VotesConditionsFilter,
  upvotes: Filters::VotesConditionsFilter,
  work_item_status: Filters::WorkItemStatusConditionsFilter
}.freeze
DEFAULT_NETWORK_ADAPTER =
Gitlab::Triage::NetworkAdapters::HttpartyAdapter
DEFAULT_GRAPHQL_ADAPTER =
Gitlab::Triage::NetworkAdapters::GraphqlAdapter
ALLOWED_STATE_VALUES =
{
  issues: %w[opened closed],
  merge_requests: %w[opened closed merged],
  work_items: %w[opened closed]
}.with_indifferent_access.freeze
MILESTONE_TIMEBOX_VALUES =
%w[none any upcoming started].freeze
ITERATION_SELECTION_VALUES =
%w[none any].freeze
EpicsTriagingForProjectImpossibleError =
Class.new(StandardError)
MultiPolicyInInjectionModeError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policies:, options:, network_adapter_class: DEFAULT_NETWORK_ADAPTER, graphql_network_adapter_class: DEFAULT_GRAPHQL_ADAPTER) ⇒ Engine

Returns a new instance of Engine.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gitlab/triage/engine.rb', line 79

def initialize(policies:, options:, network_adapter_class: DEFAULT_NETWORK_ADAPTER, graphql_network_adapter_class: DEFAULT_GRAPHQL_ADAPTER)
  options.host_url = policies.delete(:host_url) { options.host_url }
  options.api_version = policies.delete(:api_version) { 'v4' }
  options.dry_run = ENV['TEST'] == 'true' if options.dry_run.nil?

  @per_page = policies.delete(:per_page) { 100 }
  @policies = policies
  @options = options
  @network_adapter_class = network_adapter_class
  @graphql_network_adapter_class = graphql_network_adapter_class

  assert_options!

  @options.source = @options.source.to_s

  require_ruby_files
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



44
45
46
# File 'lib/gitlab/triage/engine.rb', line 44

def options
  @options
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



44
45
46
# File 'lib/gitlab/triage/engine.rb', line 44

def per_page
  @per_page
end

#policiesObject (readonly)

Returns the value of attribute policies.



44
45
46
# File 'lib/gitlab/triage/engine.rb', line 44

def policies
  @policies
end

Instance Method Details

#apply_work_item_status!(resource_type, expanded_conditions, rule_definition, resources, status_pre_filtered) ⇒ Object (private)



745
746
747
748
749
750
751
752
753
754
755
# File 'lib/gitlab/triage/engine.rb', line 745

def apply_work_item_status!(resource_type, expanded_conditions, rule_definition, resources, status_pre_filtered)
  return unless resource_type == 'issues' && needs_work_item_status?(expanded_conditions, rule_definition)

  status_values = Array(expanded_conditions[:work_item_status])

  if status_pre_filtered && status_values.size == 1
    resources.each { |r| r[:work_item_status] = status_values.first }
  else
    decorate_resources_with_work_item_status(resources)
  end
end

#assert_all!Object (private)

rubocop:disable Style/IfUnlessModifier



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gitlab/triage/engine.rb', line 151

def assert_all!
  return unless options.all

  if options.source
    raise ArgumentError, '--all-projects option cannot be used in conjunction with --source option!'
  end

  if options.source_id
    raise ArgumentError, '--all-projects option cannot be used in conjunction with --source-id option!'
  end

  if options.resource_reference # rubocop:disable Style/GuardClause
    raise ArgumentError, '--all-projects option cannot be used in conjunction with --resource-reference option!'
  end
end

#assert_epic_rule!(resource_type) ⇒ Object (private)



217
218
219
220
221
# File 'lib/gitlab/triage/engine.rb', line 217

def assert_epic_rule!(resource_type)
  return if resource_type != 'epics' || options.source == 'groups'

  raise EpicsTriagingForProjectImpossibleError, "Epics can only be triaged at the group level. Please set the `--source groups` option."
end

#assert_options!Object (private)



143
144
145
146
147
148
# File 'lib/gitlab/triage/engine.rb', line 143

def assert_options!
  assert_all!
  assert_source!
  assert_source_id!
  assert_resource_reference!
end

#assert_resource_reference!Object (private)



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/gitlab/triage/engine.rb', line 182

def assert_resource_reference!
  return unless options.resource_reference

  if options.source == 'groups' && !options.resource_reference.start_with?('&')
    raise ArgumentError, "--resource-reference can only start with '&' when --source=groups is passed ('#{options.resource_reference}' passed)!"
  end

  if options.source == 'projects' && !options.resource_reference.start_with?('#', '!') # rubocop:disable Style/GuardClause
    raise(
      ArgumentError,
      "--resource-reference can only start with '#' or '!' when --source=projects is passed " \
        "('#{options.resource_reference}' passed)!"
    )
  end
end

#assert_source!Object (private)

rubocop:enable Style/IfUnlessModifier

Raises:

  • (ArgumentError)


168
169
170
171
172
173
# File 'lib/gitlab/triage/engine.rb', line 168

def assert_source!
  return if options.source
  return if options.all

  raise ArgumentError, 'A source is needed (pass it with the `--source` option)!'
end

#assert_source_id!Object (private)

Raises:

  • (ArgumentError)


175
176
177
178
179
180
# File 'lib/gitlab/triage/engine.rb', line 175

def assert_source_id!
  return if options.source_id
  return if options.all

  raise ArgumentError, 'A project or group ID is needed (pass it with the `--source-id` option)!'
end

#attach_resource_type(resources, resource_type) ⇒ Object (private)



509
510
511
# File 'lib/gitlab/triage/engine.rb', line 509

def attach_resource_type(resources, resource_type)
  resources.each { |resource| resource[:type] = resource_type }
end

#branches_resource_query(conditions) ⇒ Object (private)



706
707
708
709
710
# File 'lib/gitlab/triage/engine.rb', line 706

def branches_resource_query(conditions)
  [].tap do |condition_builders|
    condition_builders << APIQueryBuilders::SingleQueryParamBuilder.new('search', conditions[:name]) if conditions[:name]
  end
end

#build_get_url(resource_type, conditions) ⇒ Object (private)

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/gitlab/triage/engine.rb', line 594

def build_get_url(resource_type, conditions)
  # Example issues query with state and labels
  # https://gitlab.com/api/v4/projects/test-triage%2Fissue-project/issues?state=open&labels=project%20label%20with%20spaces,group_label_no_spaces
  params = {
    per_page: per_page
  }

  condition_builders = []
  condition_builders << APIQueryBuilders::SingleQueryParamBuilder.new('iids', options.resource_reference[1..]) if options.resource_reference
  author_username = conditions[:author_username]
  condition_builders << APIQueryBuilders::SingleQueryParamBuilder.new('author_username', author_username) if author_username

  condition_builders << APIQueryBuilders::MultiQueryParamBuilder.new('labels', conditions[:labels], ',') if conditions[:labels]

  if conditions[:forbidden_labels]
    condition_builders << APIQueryBuilders::MultiQueryParamBuilder.new('not[labels]', conditions[:forbidden_labels], ',')
  end

  if conditions[:state]
    condition_builders << APIQueryBuilders::SingleQueryParamBuilder.new(
      'state',
      conditions[:state],
      allowed_values: ALLOWED_STATE_VALUES[resource_type])
  end

  condition_builders << milestone_condition_builder(resource_type, conditions[:milestone]) if conditions[:milestone]

  if conditions[:date] && APIQueryBuilders::DateQueryParamBuilder.applicable?(conditions[:date]) && resource_type&.to_sym != :branches
    condition_builders << APIQueryBuilders::DateQueryParamBuilder.new(conditions.delete(:date))
  end

  case resource_type&.to_sym
  when :issues
    condition_builders.concat(issues_resource_query(conditions))
  when :merge_requests
    condition_builders.concat(merge_requests_resource_query(conditions))
  when :branches
    condition_builders.concat(branches_resource_query(conditions))
  end

  condition_builders.compact.each do |condition_builder|
    params[condition_builder.param_name] = condition_builder.param_content
  end

  url_builder_options = {
    network_options: options,
    all: options.all,
    source: options.source,
    source_id: options.source_id,
    resource_type: resource_type,
    params: params
  }

  # FIXME: Epics listing endpoint doesn't support filtering by `iids`, so instead we
  # get a single epic when `--resource-reference` is given for epics.
  url_builder_options[:resource_id] = options.resource_reference[1..] if options.resource_reference && resource_type == 'epics'

  UrlBuilders::UrlBuilder.new(url_builder_options).build
end

#build_graphql_query(resource_type, conditions, graphql_only = false) ⇒ Object (private)



882
883
884
885
# File 'lib/gitlab/triage/engine.rb', line 882

def build_graphql_query(resource_type, conditions, graphql_only = false)
  Gitlab::Triage::GraphqlQueries::QueryBuilder
    .new(options.source, resource_type, conditions, graphql_only: graphql_only)
end

#decorate_resources_with_graphql_data(resources, graphql_resources) ⇒ Object (private)



513
514
515
516
517
518
# File 'lib/gitlab/triage/engine.rb', line 513

def decorate_resources_with_graphql_data(resources, graphql_resources)
  return if graphql_resources.nil?

  graphql_resources_by_id = graphql_resources.index_by { |resource| resource[:id] }
  resources.each { |resource| resource.merge!(graphql_resources_by_id[resource[:id]].to_h) }
end

#decorate_resources_with_work_item_status(resources) ⇒ Object (private)

Status is a work item capability not available on the IssueType GraphQL type or the REST API. We fetch it via a separate workItems query and merge it back by IID, similar to how decorate_resources_with_graphql_data works for other fields. We use widgets (not features) for backward compatibility with GitLab versions before the features field was introduced in 18.9.



814
815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/gitlab/triage/engine.rb', line 814

def decorate_resources_with_work_item_status(resources)
  return if resources.empty?

  iids = resources.filter_map { |r| (r['iid'] || r[:iid])&.to_s }
  return if iids.empty?

  status_by_iid = fetch_work_item_statuses_by_iid(iids)

  resources.each do |resource|
    iid = (resource['iid'] || resource[:iid]).to_s
    resource[:work_item_status] = status_by_iid[iid]
  end
end

#draft_condition_builder(draft_condittion) ⇒ Object (private)



712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'lib/gitlab/triage/engine.rb', line 712

def draft_condition_builder(draft_condittion)
  # Issues API only accepts 'yes' and 'no' as strings: https://docs.gitlab.com/ee/api/merge_requests.html
  wip =
    case draft_condittion
    when true
      'yes'
    when false
      'no'
    else
      raise ArgumentError, 'The "draft" condition only accepts true or false.'
    end

  APIQueryBuilders::SingleQueryParamBuilder.new('wip', wip)
end

#fetch_resources(resource_type, expanded_conditions, rule_definition) ⇒ Object (private)



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/gitlab/triage/engine.rb', line 444

def fetch_resources(resource_type, expanded_conditions, rule_definition)
  return fetch_work_items(expanded_conditions) if resource_type == 'work_items'

  resources = []

  if rule_definition[:api] == 'graphql'
    graphql_query_options = { source: source_full_path }

    if options.resource_reference
      expanded_conditions[:iids] = options.resource_reference[1..]
      graphql_query_options[:iids] = [expanded_conditions[:iids]]
    elsif expanded_conditions[:iids].present?
      graphql_query_options[:iids] = expanded_conditions[:iids]
    end

    graphql_query = build_graphql_query(resource_type, expanded_conditions, true)

    resources = graphql_network.query(graphql_query, **graphql_query_options)
  else
    # FIXME: Epics listing endpoint doesn't support filtering by `iids`, so instead we
    # get a single epic when `--resource-reference` is given for epics.
    # Because of that, the query could return a single epic, so we make sure we get an array.
    pre_filter_iids = expanded_conditions.delete(:iids)

    resources = Array(network.query_api(build_get_url(resource_type, expanded_conditions)))

    # Narrow the REST results to the pre-filtered IID set, when a pre-filter
    # ran. The contract is carried by the value itself: `nil` means no
    # pre-filter ran (keep all resources), while any array - including `[]` -
    # means "narrow to exactly these IIDs". An empty array therefore yields
    # zero resources, which is correct when a pre-filter (e.g. work_item_status)
    # ran and matched nothing. `present?` cannot express this because it is
    # false for `[]`.
    unless pre_filter_iids.nil?
      filtered_iids = pre_filter_iids.map(&:to_s)
      resources = resources.select { |r| filtered_iids.include?(r['iid'].to_s) }
    end

    iids = resources.pluck('iid').map(&:to_s)
    expanded_conditions[:iids] = iids

    graphql_query = build_graphql_query(resource_type, expanded_conditions)
    graphql_resources = graphql_network.query(graphql_query, source: source_full_path, iids: iids) if graphql_query.any?

    decorate_resources_with_graphql_data(resources, graphql_resources)
  end

  resources
end

#fetch_source_full_pathObject (private)

Raises:

  • (ArgumentError)


891
892
893
894
895
896
897
898
899
900
# File 'lib/gitlab/triage/engine.rb', line 891

def fetch_source_full_path
  return options.source_id unless /\A\d+\z/.match?(options.source_id)

  source_details = network.query_api(build_get_url(nil, {})).first
  full_path = source_details['full_path'] || source_details['path_with_namespace']

  raise ArgumentError, 'A source with given source_id was not found!' if full_path.blank?

  full_path
end

#fetch_work_item_iids_by_status(status_condition) ⇒ Object (private)

Pre-filter: query workItems with server-side status filtering to get only the IIDs that match, avoiding fetching the full resource set when most items won't pass the status filter.



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'lib/gitlab/triage/engine.rb', line 764

def fetch_work_item_iids_by_status(status_condition)
  status_names = Array(status_condition)
  all_iids = []

  status_names.each do |status_name|
    query = <<~GRAPHQL
      query($source: ID!, $after: String, $statusName: String!) {
        #{options.source.singularize}(fullPath: $source) {
          workItems(status: { name: $statusName }, after: $after, first: 100) {
            pageInfo {
              hasNextPage
              endCursor
            }
            nodes {
              iid
            }
          }
        }
      }
    GRAPHQL

    after_cursor = nil

    loop do
      response = graphql_network.adapter.query_raw(
        query,
        resource_path: [options.source.singularize, 'workItems'],
        variables: { source: source_full_path, after: after_cursor, statusName: status_name }
      )

      work_items = Array.wrap(response[:results])
      work_items.each do |wi|
        iid = wi.is_a?(Hash) ? (wi['iid'] || wi[:iid]) : wi.try(:iid)
        all_iids << iid.to_s if iid
      end

      break unless response[:more_pages]

      after_cursor = response[:end_cursor]
    end
  end

  all_iids.uniq
end

#fetch_work_item_statuses_by_iid(iids) ⇒ Object (private)

The workItems connection caps first at 100, so we request the status for the given IIDs in batches and paginate within each batch.



830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
# File 'lib/gitlab/triage/engine.rb', line 830

def fetch_work_item_statuses_by_iid(iids)
  query = <<~GRAPHQL
    query($source: ID!, $iids: [String!], $after: String) {
      #{options.source.singularize}(fullPath: $source) {
        workItems(iids: $iids, after: $after, first: 100) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            iid
            widgets(onlyTypes: [STATUS]) {
              ... on WorkItemWidgetStatus {
                status {
                  id
                  name
                }
              }
            }
          }
        }
      }
    }
  GRAPHQL

  status_by_iid = {}

  iids.each_slice(100) do |iids_batch|
    after_cursor = nil

    loop do
      response = graphql_network.adapter.query_raw(
        query,
        resource_path: [options.source.singularize, 'workItems'],
        variables: { source: source_full_path, iids: iids_batch, after: after_cursor }
      )

      Array.wrap(response[:results]).each do |wi|
        wi = wi.deep_transform_keys(&:underscore).with_indifferent_access
        status_widget = Array.wrap(wi[:widgets]).find { |w| w[:status].present? }
        status_by_iid[wi[:iid].to_s] = status_widget.dig(:status, :name) if status_widget
      end

      break unless response[:more_pages]

      after_cursor = response[:end_cursor]
    end
  end

  status_by_iid
end

#fetch_work_items(expanded_conditions) ⇒ Object (private)



494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/gitlab/triage/engine.rb', line 494

def fetch_work_items(expanded_conditions)
  conditions = expanded_conditions.dup
  conditions[:iids] = options.resource_reference[1..] if options.resource_reference

  # The work_items path bypasses build_get_url, so validate `state` here
  # the same way the URL builders do for the other resource types.
  if conditions[:state]
    ParamsValidator.new(
      [{ name: 'state', type: String, values: ALLOWED_STATE_VALUES[:work_items] }],
      { 'state' => conditions[:state] }).validate!
  end

  work_items_source.list(conditions)
end

#filter_resource(resource, conditions) ⇒ Object (private)



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/gitlab/triage/engine.rb', line 534

def filter_resource(resource, conditions)
  results = []

  FILTER_MAP.each do |condition_key, filter_value|
    # Skips to the next key value pair if the condition is not applicable
    next if conditions[condition_key].nil?

    case filter_value
    when Hash
      filter_in_ruby = conditions[condition_key].dig(:filter_in_ruby)
      merged_at = conditions[condition_key].dig(:attribute) == 'merged_at'
      filter_branch = conditions.dig(:date) && resource[:type] == 'branches'

      # Set the filter to the resource type
      if filter_in_ruby || merged_at || filter_branch
        filter = filter_value[resource[:type]]
        results << filter.new(resource, conditions[condition_key]).calculate
      end
    else
      # The `filter_value` set is not of type `hash`
      filter = filter_value

      # If the :ruby condition exists then filter based off of conditions
      # else we base off of the `conditions[condition_key]`.

      result =
        if condition_key.to_s == 'no_additional_labels'
          filter.new(resource, conditions[:labels]).calculate
        elsif condition_key.to_s == 'protected'
          filter.new(resource, conditions[:protected]).calculate
        elsif filter.instance_method(:initialize).arity == 2
          filter.new(resource, conditions[condition_key]).calculate
        else
          filter.new(resource, conditions[condition_key], network).calculate
        end

      results << result
    end
  end
  results.all?
end

#filter_resources(resources, conditions) ⇒ Object (private)



528
529
530
531
532
# File 'lib/gitlab/triage/engine.rb', line 528

def filter_resources(resources, conditions)
  resources.select do |resource|
    filter_resource(resource, conditions)
  end
end

#graphql_networkObject



132
133
134
# File 'lib/gitlab/triage/engine.rb', line 132

def graphql_network
  @graphql_network ||= GraphqlNetwork.new(graphql_network_adapter)
end

#graphql_network_adapterObject (private)



235
236
237
# File 'lib/gitlab/triage/engine.rb', line 235

def graphql_network_adapter
  @graphql_network_adapter ||= @graphql_network_adapter_class.new(options, tracker: tracker)
end

#issues_resource_query(conditions) ⇒ Object (private)



684
685
686
687
688
689
690
691
# File 'lib/gitlab/triage/engine.rb', line 684

def issues_resource_query(conditions)
  [].tap do |condition_builders|
    condition_builders << APIQueryBuilders::SingleQueryParamBuilder.new('weight', conditions[:weight]) if conditions[:weight]
    condition_builders << iteration_condition_builder(conditions[:iteration]) if conditions[:iteration]
    condition_builders << APIQueryBuilders::SingleQueryParamBuilder.new('health_status', conditions[:health_status]) if conditions[:health_status]
    condition_builders << APIQueryBuilders::SingleQueryParamBuilder.new('issue_type', conditions[:issue_type]) if conditions[:issue_type]
  end
end

#iteration_condition_builder(iteration_value) ⇒ Object (private)



672
673
674
675
676
677
678
679
680
681
682
# File 'lib/gitlab/triage/engine.rb', line 672

def iteration_condition_builder(iteration_value)
  # Issues API should use the `iteration_id` param for timebox values, and `iteration_title` for iteration title
  args =
    if ITERATION_SELECTION_VALUES.include?(iteration_value.downcase)
      ['iteration_id', iteration_value.titleize] # The API only accepts titleized values.
    else
      ['iteration_title', iteration_value]
    end

  APIQueryBuilders::SingleQueryParamBuilder.new(*args)
end

#limit_resources(resources, limits) ⇒ Object (private)



576
577
578
579
580
581
582
# File 'lib/gitlab/triage/engine.rb', line 576

def limit_resources(resources, limits)
  if limits.empty?
    resources
  else
    Limiters::DateFieldLimiter.new(resources, limits).limit
  end
end

#merge_requests_resource_query(conditions) ⇒ Object (private)



693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/gitlab/triage/engine.rb', line 693

def merge_requests_resource_query(conditions)
  [].tap do |condition_builders|
    [
      :source_branch,
      :target_branch,
      :reviewer_id
    ].each do |key|
      condition_builders << APIQueryBuilders::SingleQueryParamBuilder.new(key.to_s, conditions[key]) if conditions[key]
    end
    condition_builders << draft_condition_builder(conditions[:draft]) if conditions.key?(:draft)
  end
end

#milestone_condition_builder(resource_type, milestone_condition) ⇒ Object (private)

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity



657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/gitlab/triage/engine.rb', line 657

def milestone_condition_builder(resource_type, milestone_condition)
  milestone_value = Array(milestone_condition)[0].to_s # back-compatibility
  return if milestone_value.empty?

  # Issues API should use the `milestone_id` param for timebox values, and `milestone` for milestone title
  args =
    if resource_type.to_sym == :issues && MILESTONE_TIMEBOX_VALUES.include?(milestone_value.downcase)
      ['milestone_id', milestone_value.titleize] # The API only accepts titleized values.
    else
      ['milestone', milestone_value]
    end

  APIQueryBuilders::SingleQueryParamBuilder.new(*args)
end

#needs_work_item_status?(conditions, _rule_definition) ⇒ Boolean (private)

Returns:

  • (Boolean)


757
758
759
# File 'lib/gitlab/triage/engine.rb', line 757

def needs_work_item_status?(conditions, _rule_definition)
  conditions.key?(:work_item_status)
end

#networkObject



124
125
126
# File 'lib/gitlab/triage/engine.rb', line 124

def network
  @network ||= Network.new(restapi: restapi_network, graphql: graphql_network)
end

#network_adapterObject (private)



231
232
233
# File 'lib/gitlab/triage/engine.rb', line 231

def network_adapter
  @network_adapter ||= @network_adapter_class.new(options, tracker: tracker)
end

#performObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gitlab/triage/engine.rb', line 97

def perform
  Validators::PolicyValidator.new(resource_rules).validate!

  puts "Performing a dry run.\n\n" if options.dry_run

  unless options.quiet
    puts Gitlab::Triage::UI.header("Triaging the `#{options.source_id}` #{options.source.singularize}", char: '=')
    puts
  end

  resource_rules.each do |resource_type, policy_definition|
    next unless right_resource_type_for_resource_option?(resource_type)

    assert_epic_rule!(resource_type)

    unless options.quiet
      puts Gitlab::Triage::UI.header("Processing summaries & rules for #{resource_type}", char: '-')
      puts
    end

    process_summaries(resource_type, policy_definition[:summaries])
    process_rules(resource_type, policy_definition[:rules])
  end

  tracker.print_summary if options.summary
end

#pre_filter_by_work_item_status!(resource_type, expanded_conditions) ⇒ Object (private)

When filtering by work_item_status, query workItems with server-side status filtering first to narrow the IID set before the main fetch. This avoids fetching all resources just to discard most of them.



730
731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/gitlab/triage/engine.rb', line 730

def pre_filter_by_work_item_status!(resource_type, expanded_conditions)
  return false unless resource_type == 'issues' && expanded_conditions.key?(:work_item_status)

  pre_filter_iids = fetch_work_item_iids_by_status(expanded_conditions[:work_item_status])

  expanded_conditions[:iids] =
    if expanded_conditions[:iids].present?
      Array(expanded_conditions[:iids]).map(&:to_s) & pre_filter_iids
    else
      pre_filter_iids
    end

  true
end

#process_action(policy) ⇒ Object (private)



520
521
522
523
524
525
526
# File 'lib/gitlab/triage/engine.rb', line 520

def process_action(policy)
  Action.process(
    policy: policy,
    network: network,
    dry: options.dry_run)
  puts unless options.quiet
end

#process_rules(resource_type, rule_definitions) ⇒ nil (private)

Process an array of rule_definitions.

Examples:

Example of an array of rule definitions.


[{ name: "New issues", conditions: { state: opened }, limits: { most_recent: 2 }, actions: { labels: ["needs attention"] } }]

Parameters:

  • rule_definitions (Array<Hash>)

    An array usually given as YAML in a triage policy file.

Returns:

  • (nil)


293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/gitlab/triage/engine.rb', line 293

def process_rules(resource_type, rule_definitions)
  return if rule_definitions.blank?

  rule_definitions.each do |rule_definition|
    resources_for_rule(resource_type, rule_definition, in_summary: false) do |resources|
      policy = Policies::RulePolicy.new(
        resource_type, rule_definition, resources, network)

      process_action(policy)
    end
  end
end

#process_summaries(resource_type, summary_definitions) ⇒ nil (private)

Process an array of summary_definitions.

Examples:

Example of an array of summary definitions (shown as YAML for readability).


- name: Newest and oldest issues summary
  rules:
    - name: New issues
      conditions:
        state: opened
      limits:
        most_recent: 2
      actions:
        summarize:
          item: "- [ ] [{{title}}]({{web_url}}) {{labels}}"
          summary: |
            Please triage the following new {{type}}:
            {{items}}
  actions:
    summarize:
      title: "Newest and oldest {{type}} summary"
      summary: |
        Please triage the following {{type}}:
        {{items}}
        Please take care of them before the end of #{7.days.from_now.strftime('%Y-%m-%d')}
        /label ~"needs attention"

Parameters:

  • summary_definitions (Array<Hash>)

    An array usually given as YAML in a triage policy file.

Returns:

  • (nil)


276
277
278
279
280
281
282
# File 'lib/gitlab/triage/engine.rb', line 276

def process_summaries(resource_type, summary_definitions)
  return if summary_definitions.blank?

  summary_definitions.each do |summary_definition|
    process_summary(resource_type, summary_definition)
  end
end

#process_summary(resource_type, summary_definition) ⇒ nil (private)

Process a summary_definition.

Examples:

Example of a summary definition hash (shown as YAML for readability).


name: Newest and oldest issues summary
rules:
  - name: New issues
    conditions:
      state: opened
    limits:
      most_recent: 2
    actions:
      summarize:
        item: "- [ ] [{{title}}]({{web_url}}) {{labels}}"
        summary: |
          Please triage the following new {{type}}:
          {{items}}
actions:
  summarize:
    title: "Newest and oldest {{type}} summary"
    summary: |
      Please triage the following {{type}}:
      {{items}}
      Please take care of them before the end of #{7.days.from_now.strftime('%Y-%m-%d')}
      /label ~"needs attention"

Parameters:

  • resource_type (String)

    The resource type, e.g. issues or merge_requests.

  • summary_definition (Hash)

    A hash usually given as YAML in a triage policy file:

Returns:

  • (nil)


336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/gitlab/triage/engine.rb', line 336

def process_summary(resource_type, summary_definition)
  unless options.quiet
    puts Gitlab::Triage::UI.header("Processing summary: **#{summary_definition[:name]}**", char: '~')
    puts
  end

  summary_parts_for_rules(resource_type, summary_definition[:rules]) do |summary_resources|
    policy = Policies::SummaryPolicy.new(
      resource_type, summary_definition, summary_resources, network)

    process_action(policy)
  end
end

#require_ruby_filesObject (private)



198
199
200
# File 'lib/gitlab/triage/engine.rb', line 198

def require_ruby_files
  options.require_files.each(&method(:require))
end

#resource_rulesObject (private)



223
224
225
# File 'lib/gitlab/triage/engine.rb', line 223

def resource_rules
  @resource_rules ||= policies.delete(:resource_rules) { {} }
end

#resources_for_rule(resource_type, rule_definition, in_summary: false) {|rule_resources, expanded_conditions| ... } ⇒ nil (private)

Transform a non-expanded rule_definition into a PoliciesResources::RuleResources.new(resources) object.

Examples:

Example of a rule definition hash.


{ name: "New issues", conditions: { state: opened }, limits: { most_recent: 2 }, actions: { labels: ["needs attention"] } }

Parameters:

  • resource_type (String)

    The resource type, e.g. issues or merge_requests.

  • rule_definition (Hash)

    A rule definition, e.g. { name: 'Foo', conditions: { milestone: 'v1' } }.

  • in_summary (Boolean) (defaults to: false)

    Whether this rule is part of a summary definition.

Yield Parameters:

  • rule_resources (PoliciesResources::RuleResources)

    An object which contains an array of resources.

  • expanded_conditions (Hash)

    A hash of expanded conditions.

Yield Returns:

  • (nil)

Returns:

  • (nil)


393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/gitlab/triage/engine.rb', line 393

def resources_for_rule(resource_type, rule_definition, in_summary: false)
  puts Gitlab::Triage::UI.header("Gathering resources for rule: **#{rule_definition[:name]}**", char: '-') unless options.quiet

  # Skip resource fetching for custom type rules
  if rule_definition[:type] == 'custom'
    unless in_summary
      raise ArgumentError, "type: custom can only be used in summary rules, not in top-level rules (rule: #{rule_definition[:name]})"
    end

    puts "\n* Skipping resource fetch for custom type rule" unless options.quiet
    yield(PoliciesResources::RuleResources.new([]), {})
    return
  end

  ExpandCondition.perform(rule_conditions(rule_definition)) do |expanded_conditions|
    status_pre_filtered = pre_filter_by_work_item_status!(resource_type, expanded_conditions)

    # retrieving the resources for every rule is inefficient
    # however, previous rules may affect those upcoming
    resources = options.resources ||
      fetch_resources(resource_type, expanded_conditions, rule_definition)

    apply_work_item_status!(resource_type, expanded_conditions, rule_definition, resources, status_pre_filtered)

    # In some filters/actions we want to know which resource type it is
    attach_resource_type(resources, resource_type)

    unless options.quiet
      puts "\n\n* Found #{resources.count} resources..."
      print "* Filtering resources..."
    end

    resources = filter_resources(resources, expanded_conditions)

    unless options.quiet
      puts "\n* Total after filtering: #{resources.count} resources"
      print "* Limiting resources..."
    end

    resources = limit_resources(resources, rule_limits(rule_definition))
    unless options.quiet
      puts "\n* Total after limiting: #{resources.count} resources"
      puts
    end

    resources = sanitize_resources(resources)

    yield(PoliciesResources::RuleResources.new(resources), expanded_conditions)
  end
end

#restapi_networkObject



128
129
130
# File 'lib/gitlab/triage/engine.rb', line 128

def restapi_network
  @restapi_network ||= RestAPINetwork.new(network_adapter)
end

#right_resource_type_for_resource_option?(resource_type) ⇒ Boolean (private)

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/gitlab/triage/engine.rb', line 202

def right_resource_type_for_resource_option?(resource_type)
  return true unless options.resource_reference

  resource_reference = options.resource_reference

  case resource_type
  when 'issues', 'work_items'
    resource_reference.start_with?('#')
  when 'merge_requests'
    resource_reference.start_with?('!')
  when 'epics'
    resource_reference.start_with?('&')
  end
end

#rule_conditions(rule) ⇒ Object (private)



239
240
241
# File 'lib/gitlab/triage/engine.rb', line 239

def rule_conditions(rule)
  rule.fetch(:conditions) { {} }
end

#rule_limits(rule) ⇒ Object (private)



243
244
245
# File 'lib/gitlab/triage/engine.rb', line 243

def rule_limits(rule)
  rule.fetch(:limits) { {} }
end

#sanitize_resources(resources) ⇒ Object (private)



584
585
586
587
588
589
# File 'lib/gitlab/triage/engine.rb', line 584

def sanitize_resources(resources)
  resources.each do |resource|
    # Titles should not contain newlines. Translate them to spaces.
    resource[:title] = resource[:title]&.tr("\r\n", '  ')
  end
end

#source_full_pathObject (private)



887
888
889
# File 'lib/gitlab/triage/engine.rb', line 887

def source_full_path
  @source_full_path ||= fetch_source_full_path
end

#summary_parts_for_rules(resource_type, rule_definitions) {|summary_resources| ... } ⇒ nil (private)

Transform an array of rule_definitions into a PoliciesResources::SummaryResources.new(rule => rule_resources) object.

Examples:

Example of an array of rule definitions.


[{ name: "New issues", conditions: { state: opened }, limits: { most_recent: 2 }, actions: { labels: ["needs attention"] } }]

Parameters:

  • resource_type (String)

    The resource type, e.g. issues or merge_requests.

  • rule_definitions (Array<Hash>)

    An array of rule definitions, e.g. [{ name: 'Foo', conditions: { milestone: 'v1' } }, { name: 'Foo', conditions: { state: 'opened' } }].

Yield Parameters:

Yield Returns:

  • (nil)

Returns:

  • (nil)


364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/gitlab/triage/engine.rb', line 364

def summary_parts_for_rules(resource_type, rule_definitions)
  # { summary_rule => resources }
  parts = rule_definitions.each_with_object({}) do |rule_definition, result|
    to_enum(:resources_for_rule, resource_type, rule_definition, in_summary: true).each do |rule_resources, expanded_conditions|
      # We replace the non-expanded rule conditions with the expanded ones
      result.merge!(rule_definition.merge(conditions: expanded_conditions) => rule_resources)
    end

    result
  end

  yield(PoliciesResources::SummaryResources.new(parts))
end

#trackerObject (private)



227
228
229
# File 'lib/gitlab/triage/engine.rb', line 227

def tracker
  @tracker ||= options.summary ? APIObservability::Tracker.new : nil
end

#work_items_sourceObject



136
137
138
139
# File 'lib/gitlab/triage/engine.rb', line 136

def work_items_source
  @work_items_source ||= Sources::WorkItemsRestSource.new(
    network: network, options: options, per_page: per_page)
end