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 different types that access JobType

Instance Method Summary collapse

Methods inherited from BaseObject

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

Methods included from Gitlab::Graphql::Present

#present, #unpresented

Instance Method Details

#artifactsObject



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

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

#browse_artifacts_pathObject



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

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

#can_play_job?Boolean

Returns:

  • (Boolean)


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

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

#commit_pathObject



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

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

#coverageObject



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

def coverage
  object&.coverage
end

#downstream_pipelineObject



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

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

#exit_codeObject



281
282
283
# File 'app/graphql/types/ci/job_type.rb', line 281

def exit_code
  object.exit_code if object.build.respond_to?(:exit_code)
end

#idObject

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



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

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



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

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

  object.build.class
end

#manual_variablesObject



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'app/graphql/types/ci/job_type.rb', line 267

def manual_variables
  if object.try(:action?) && !object.is_a?(GenericCommitStatus)
    BatchLoader::GraphQL.for(object.id).batch do |job_ids, loader|
      variables_by_job_id = ::Ci::JobVariable.for_jobs(job_ids).group_by(&:job_id)

      job_ids.each do |id|
        loader.call(id, variables_by_job_id[id] || [])
      end
    end
  else
    ::Ci::JobVariable.none
  end
end

#pipelineObject



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

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

#play_pathObject



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

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

#previous_stage_jobsObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/graphql/types/ci/job_type.rb', line 189

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



179
180
181
182
183
184
185
186
187
# File 'app/graphql/types/ci/job_type.rb', line 179

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_pathObject



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

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

#retry_pathObject



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

def retry_path
  return unless retryable

  ::Gitlab::Routing.url_helpers.retry_project_job_path(object.project, object)
end

#retryableObject



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

def retryable
  object.build.retryable?
end

#runnerObject



209
210
211
# File 'app/graphql/types/ci/job_type.rb', line 209

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

#runner_managerObject



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/graphql/types/ci/job_type.rb', line 213

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)
    ::Ci::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

#sourceObject



171
172
173
# File 'app/graphql/types/ci/job_type.rb', line 171

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

#stageObject



205
206
207
# File 'app/graphql/types/ci/job_type.rb', line 205

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

#tagsObject



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

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

#traceObject



175
176
177
# File 'app/graphql/types/ci/job_type.rb', line 175

def trace
  object.trace if object.has_trace?
end

#web_pathObject



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

def web_path
  ::Gitlab::Routing.url_helpers.project_job_path(object.project, object) unless object.is_a?(::Ci::Bridge)
end