summaryrefslogtreecommitdiff
path: root/app/serializers/build_details_entity.rb
blob: 01a8a4ebea95dbbbb044f3454d06a5e38c6ca5c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# frozen_string_literal: true

class BuildDetailsEntity < JobEntity
  expose :coverage, :erased_at, :duration
  expose :tag_list, as: :tags
  expose :has_trace?, as: :has_trace
  expose :stage
  expose :stuck?, as: :stuck
  expose :user, using: UserEntity
  expose :runner, using: RunnerEntity
  expose :metadata, using: BuildMetadataEntity
  expose :pipeline, using: Ci::PipelineEntity

  expose :deployment_status, if: -> (*) { build.starts_environment? } do
    expose :deployment_status, as: :status
    expose :persisted_environment, as: :environment do |build, options|
      options.merge(deployment_details: false).yield_self do |opts|
        EnvironmentEntity.represent(build.persisted_environment, opts)
      end
    end
  end

  expose :deployment_cluster, if: -> (build) { build&.deployment&.cluster } do |build, options|
    # Until data is copied over from deployments.cluster_id, this entity must represent Deployment instead of DeploymentCluster
    # https://gitlab.com/gitlab-org/gitlab/issues/202628
    DeploymentClusterEntity.represent(build.deployment, options)
  end

  expose :artifact, if: -> (*) { can?(current_user, :read_job_artifacts, build) } do
    expose :download_path, if: -> (*) { build.locked_artifacts? || build.artifacts? } do |build|
      download_project_job_artifacts_path(project, build)
    end

    expose :browse_path, if: -> (*) { build.locked_artifacts? || build.browsable_artifacts? } do |build|
      browse_project_job_artifacts_path(project, build)
    end

    expose :keep_path, if: -> (*) { (build.has_expired_locked_archive_artifacts? || build.has_expiring_archive_artifacts?) && can?(current_user, :update_build, build) } do |build|
      keep_project_job_artifacts_path(project, build)
    end

    expose :expire_at, if: -> (*) { build.artifacts_expire_at.present? } do |build|
      build.artifacts_expire_at
    end

    expose :expired, if: -> (*) { build.artifacts_expire_at.present? } do |build|
      build.artifacts_expired?
    end

    expose :locked do |build|
      build.pipeline.artifacts_locked?
    end
  end

  expose :report_artifacts,
    as: :reports,
    using: JobArtifactReportEntity,
    if: -> (*) { can?(current_user, :read_build, build) }

  expose :erased_by, if: -> (*) { build.erased? }, using: UserEntity
  expose :erase_path, if: -> (*) { build.erasable? && can?(current_user, :erase_build, build) } do |build|
    erase_project_job_path(project, build)
  end

  expose :failure_reason, if: -> (*) { build.failed? }

  expose :terminal_path, if: -> (*) { can_create_build_terminal? } do |build|
    terminal_project_job_path(project, build)
  end

  expose :merge_request, if: -> (*) { can?(current_user, :read_merge_request, build.merge_request) } do
    expose :iid do |build|
      build.merge_request.iid
    end

    expose :path do |build|
      project_merge_request_path(build.merge_request.project,
                                 build.merge_request)
    end
  end

  expose :new_issue_path, if: -> (*) { can?(request.current_user, :create_issue, project) && build.failed? } do |build|
    new_project_issue_path(project, issue: build_failed_issue_options)
  end

  expose :raw_path do |build|
    raw_project_job_path(project, build)
  end

  expose :trigger, if: -> (*) { build.trigger_request } do
    expose :trigger_short_token, as: :short_token

    expose :trigger_variables, as: :variables, using: TriggerVariableEntity
  end

  expose :runners do
    expose :online do |build|
      build.any_runners_online?
    end

    expose :available do |build|
      project.any_active_runners?
    end

    expose :settings_path, if: -> (*) { can_admin_build? } do |build|
      project_runners_path(project)
    end
  end

  private

  def build_failed_issue_options
    { title: "Job Failed ##{build.id}",
      description: "Job [##{build.id}](#{project_job_url(project, build)}) failed for #{build.sha}:\n" }
  end

  def current_user
    request.current_user
  end

  def project
    build.project
  end

  def can_create_build_terminal?
    can?(current_user, :create_build_terminal, build) && build.has_terminal?
  end

  def can_admin_build?
    can?(request.current_user, :admin_build, project)
  end

  def callout_message
    return super unless build.failure_reason.to_sym == :missing_dependency_failure

    docs_url = "https://docs.gitlab.com/ee/ci/yaml/README.html#dependencies"

    [
      failure_message,
      help_message(docs_url).html_safe
    ].join("<br />")
  end

  def invalid_dependencies
    build.invalid_dependencies.map(&:name).join(', ')
  end

  def failure_message
    _("This job depends on other jobs with expired/erased artifacts: %{invalid_dependencies}") %
      { invalid_dependencies: invalid_dependencies }
  end

  def help_message(docs_url)
    html_escape(_("Please refer to %{docs_url}")) % { docs_url: "<a href=\"#{docs_url}\">#{html_escape(docs_url)}</a>".html_safe }
  end
end

BuildDetailsEntity.prepend_if_ee('::EE::BuildDetailEntity')