1# frozen_string_literal: true
2
3module Resolvers
4  class ProjectPipelineResolver < BaseResolver
5    include LooksAhead
6
7    type ::Types::Ci::PipelineType, null: true
8
9    alias_method :project, :object
10
11    argument :iid, GraphQL::Types::ID,
12             required: false,
13             description: 'IID of the Pipeline. For example, "1".'
14
15    argument :sha, GraphQL::Types::String,
16             required: false,
17             description: 'SHA of the Pipeline. For example, "dyd0f15ay83993f5ab66k927w28673882x99100b".'
18
19    def ready?(iid: nil, sha: nil, **args)
20      unless iid.present? ^ sha.present?
21        raise Gitlab::Graphql::Errors::ArgumentError, 'Provide one of an IID or SHA'
22      end
23
24      super
25    end
26
27    def resolve(iid: nil, sha: nil, **args)
28      self.lookahead = args.delete(:lookahead)
29
30      if iid
31        BatchLoader::GraphQL.for(iid).batch(key: project) do |iids, loader|
32          finder = ::Ci::PipelinesFinder.new(project, current_user, iids: iids)
33
34          apply_lookahead(finder.execute).each { |pipeline| loader.call(pipeline.iid.to_s, pipeline) }
35        end
36      else
37        BatchLoader::GraphQL.for(sha).batch(key: project) do |shas, loader|
38          finder = ::Ci::PipelinesFinder.new(project, current_user, sha: shas)
39
40          apply_lookahead(finder.execute).each { |pipeline| loader.call(pipeline.sha.to_s, pipeline) }
41        end
42      end
43    end
44
45    def unconditional_includes
46      [
47        { statuses: [:needs] }
48      ]
49    end
50  end
51end
52