Class: Types::Ci::JobType

Inherits:
BaseObject
  • Object
show all
Defined in:
app/graphql/types/ci/job_type.rb

Overview

rubocop: disable Graphql/AuthorizeTypes The permission is presented through ‘StageType` that has its own authorization

Instance Method Summary collapse

Methods inherited from BaseObject

accepts, assignable?, authorization, authorize, authorized?, #current_user

Methods included from Gitlab::Graphql::Present

#present, #unpresented

Instance Method Details

#artifactsObject



162
163
164
165
166
# File 'app/graphql/types/ci/job_type.rb', line 162

def artifacts
  if object.is_a?(::Ci::Build)
    object.job_artifacts
  end
end

#browse_artifacts_pathObject



250
251
252
# File 'app/graphql/types/ci/job_type.rb', line 250

def browse_artifacts_path
  ::Gitlab::Routing.url_helpers.browse_project_job_artifacts_path(object.project, object)
end

#can_play_job?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'app/graphql/types/ci/job_type.rb', line 132

def can_play_job?
  object.playable? && Ability.allowed?(current_user, :play_job, object)
end

#commit_pathObject



230
231
232
# File 'app/graphql/types/ci/job_type.rb', line 230

def commit_path
  ::Gitlab::Routing.url_helpers.project_commit_path(object.project, object.sha)
end

#coverageObject



254
255
256
# File 'app/graphql/types/ci/job_type.rb', line 254

def coverage
  object&.coverage
end

#detailed_statusObject



158
159
160
# File 'app/graphql/types/ci/job_type.rb', line 158

def detailed_status
  object.detailed_status(context[:current_user])
end

#downstream_pipelineObject



150
151
152
# File 'app/graphql/types/ci/job_type.rb', line 150

def downstream_pipeline
  object.downstream_pipeline if object.respond_to?(:downstream_pipeline)
end

#idObject

This class is a secret union! TODO: turn this into an actual union, so that fields can be referenced safely!



222
223
224
225
226
227
228
# File 'app/graphql/types/ci/job_type.rb', line 222

def id
  return unless object.id.present?

  model_name = object.type || ::CommitStatus.name
  id = object.id
  Gitlab::GlobalId.build(model_name: model_name, id: id)
end

#kindObject



136
137
138
139
140
# File 'app/graphql/types/ci/job_type.rb', line 136

def kind
  return ::Ci::Build unless [::Ci::Build, ::Ci::Bridge].include?(object.build.class)

  object.build.class
end

#manual_jobObject



258
259
260
# File 'app/graphql/types/ci/job_type.rb', line 258

def manual_job
  object.try(:action?)
end

#manual_variablesObject



266
267
268
269
270
271
272
# File 'app/graphql/types/ci/job_type.rb', line 266

def manual_variables
  if object.action? && object.respond_to?(:job_variables)
    object.job_variables
  else
    []
  end
end

#pipelineObject



146
147
148
# File 'app/graphql/types/ci/job_type.rb', line 146

def pipeline
  Gitlab::Graphql::Loaders::BatchModelLoader.new(::Ci::Pipeline, object.pipeline_id).find
end

#play_pathObject



246
247
248
# File 'app/graphql/types/ci/job_type.rb', line 246

def play_path
  ::Gitlab::Routing.url_helpers.play_project_job_path(object.project, object)
end

#previous_stage_jobsObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/graphql/types/ci/job_type.rb', line 182

def previous_stage_jobs
  BatchLoader::GraphQL.for([object.pipeline_id, object.stage_idx - 1]).batch(default_value: []) do |tuples, loader|
    pipeline_ids = tuples.map(&:first).uniq
    stage_idxs = tuples.map(&:second).uniq

    # This query can fetch unneeded jobs when querying for more than one pipeline.
    # It was decided that fetching and discarding the jobs is preferable to making a more complex query.
    jobs = CommitStatus.in_pipelines(pipeline_ids).for_stage(stage_idxs).latest
    grouped_jobs = jobs.group_by { |job| [job.pipeline_id, job.stage_idx] }

    tuples.each do |tuple|
      loader.call(tuple, grouped_jobs.fetch(tuple, []))
    end
  end
end

#previous_stage_jobs_or_needsObject



172
173
174
175
176
177
178
179
180
# File 'app/graphql/types/ci/job_type.rb', line 172

def previous_stage_jobs_or_needs
  if object.scheduling_type == 'stage'
    Gitlab::Graphql::Lazy.with_value(previous_stage_jobs) do |jobs|
      jobs
    end
  else
    object.needs
  end
end

#ref_nameObject



234
235
236
# File 'app/graphql/types/ci/job_type.rb', line 234

def ref_name
  object&.ref
end

#ref_pathObject



238
239
240
# File 'app/graphql/types/ci/job_type.rb', line 238

def ref_path
  ::Gitlab::Routing.url_helpers.project_commits_path(object.project, ref_name)
end

#retryableObject



142
143
144
# File 'app/graphql/types/ci/job_type.rb', line 142

def retryable
  object.build.retryable?
end

#runnerObject



202
203
204
# File 'app/graphql/types/ci/job_type.rb', line 202

def runner
  Gitlab::Graphql::Loaders::BatchModelLoader.new(::Ci::Runner, object.runner_id).find
end

#runner_managerObject



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/graphql/types/ci/job_type.rb', line 206

def runner_manager
  BatchLoader::GraphQL.for(object.id).batch(key: :runner_managers) do |build_ids, loader|
    plucked_build_to_runner_manager_ids =
      ::Ci::RunnerManagerBuild.for_build(build_ids).pluck_build_id_and_runner_manager_id
    runner_managers = ::Ci::RunnerManager.id_in(plucked_build_to_runner_manager_ids.values.uniq)
    Preloaders::RunnerManagerPolicyPreloader.new(runner_managers, current_user).execute
    runner_managers_by_id = runner_managers.index_by(&:id)

    build_ids.each do |build_id|
      loader.call(build_id, runner_managers_by_id[plucked_build_to_runner_manager_ids[build_id]])
    end
  end
end

#stageObject



198
199
200
# File 'app/graphql/types/ci/job_type.rb', line 198

def stage
  ::Gitlab::Graphql::Loaders::BatchModelLoader.new(::Ci::Stage, object.stage_id).find
end

#tagsObject



154
155
156
# File 'app/graphql/types/ci/job_type.rb', line 154

def tags
  object.tags.map(&:name) if object.is_a?(::Ci::Build)
end

#traceObject



168
169
170
# File 'app/graphql/types/ci/job_type.rb', line 168

def trace
  object.trace if object.has_trace?
end

#triggeredObject



262
263
264
# File 'app/graphql/types/ci/job_type.rb', line 262

def triggered
  object.try(:trigger_request)
end

#web_pathObject



242
243
244
# File 'app/graphql/types/ci/job_type.rb', line 242

def web_path
  ::Gitlab::Routing.url_helpers.project_job_path(object.project, object)
end