summaryrefslogtreecommitdiff
path: root/lib/api/jobs.rb
blob: 723a5b0fa3af413045c20f538edfeb4d1fecd04b (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# frozen_string_literal: true

module API
  class Jobs < ::API::Base
    include PaginationParams
    before { authenticate! }

    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
      params do
        requires :id, type: String, desc: 'The ID of a project'
      end

      helpers do
        params :optional_scope do
          optional :scope, types: [String, Array[String]], desc: 'The scope of builds to show',
                           values: ::CommitStatus::AVAILABLE_STATUSES,
                           coerce_with: ->(scope) {
                             case scope
                             when String
                               [scope]
                             when ::Hash
                               scope.values
                             when ::Array
                               scope
                             else
                               ['unknown']
                             end
                           }
        end
      end

      desc 'Get a projects jobs' do
        success Entities::Ci::Job
      end
      params do
        use :optional_scope
        use :pagination
      end
      # rubocop: disable CodeReuse/ActiveRecord
      get ':id/jobs', feature_category: :continuous_integration do
        authorize_read_builds!

        builds = user_project.builds.order('id DESC')
        builds = filter_builds(builds, params[:scope])

        builds = builds.preload(:user, :job_artifacts_archive, :job_artifacts, :runner, :tags, pipeline: :project)
        present paginate(builds), with: Entities::Ci::Job
      end
      # rubocop: enable CodeReuse/ActiveRecord

      desc 'Get a specific job of a project' do
        success Entities::Ci::Job
      end
      params do
        requires :job_id, type: Integer, desc: 'The ID of a job'
      end
      get ':id/jobs/:job_id', feature_category: :continuous_integration do
        authorize_read_builds!

        build = find_build!(params[:job_id])

        present build, with: Entities::Ci::Job
      end

      # TODO: We should use `present_disk_file!` and leave this implementation for backward compatibility (when build trace
      #       is saved in the DB instead of file). But before that, we need to consider how to replace the value of
      #       `runners_token` with some mask (like `xxxxxx`) when sending trace file directly by workhorse.
      desc 'Get a trace of a specific job of a project'
      params do
        requires :job_id, type: Integer, desc: 'The ID of a job'
      end
      get ':id/jobs/:job_id/trace', feature_category: :continuous_integration do
        authorize_read_builds!

        build = find_build!(params[:job_id])

        authorize_read_build_trace!(build) if build

        header 'Content-Disposition', "infile; filename=\"#{build.id}.log\""
        content_type 'text/plain'
        env['api.format'] = :binary

        # The trace can be nil bu body method expects a string as an argument.
        trace = build.trace.raw || ''
        body trace
      end

      desc 'Cancel a specific job of a project' do
        success Entities::Ci::Job
      end
      params do
        requires :job_id, type: Integer, desc: 'The ID of a job'
      end
      post ':id/jobs/:job_id/cancel', feature_category: :continuous_integration do
        authorize_update_builds!

        build = find_build!(params[:job_id])
        authorize!(:update_build, build)

        build.cancel

        present build, with: Entities::Ci::Job
      end

      desc 'Retry a specific build of a project' do
        success Entities::Ci::Job
      end
      params do
        requires :job_id, type: Integer, desc: 'The ID of a build'
      end
      post ':id/jobs/:job_id/retry', feature_category: :continuous_integration do
        authorize_update_builds!

        build = find_build!(params[:job_id])
        authorize!(:update_build, build)
        break forbidden!('Job is not retryable') unless build.retryable?

        build = ::Ci::Build.retry(build, current_user)

        present build, with: Entities::Ci::Job
      end

      desc 'Erase job (remove artifacts and the trace)' do
        success Entities::Ci::Job
      end
      params do
        requires :job_id, type: Integer, desc: 'The ID of a build'
      end
      post ':id/jobs/:job_id/erase', feature_category: :continuous_integration do
        authorize_update_builds!

        build = find_build!(params[:job_id])
        authorize!(:erase_build, build)
        break forbidden!('Job is not erasable!') unless build.erasable?

        build.erase(erased_by: current_user)
        present build, with: Entities::Ci::Job
      end

      desc 'Trigger an actionable job (manual, delayed, etc)' do
        success Entities::Ci::JobBasic
        detail 'This feature was added in GitLab 8.11'
      end
      params do
        requires :job_id, type: Integer, desc: 'The ID of a Job'
      end

      post ":id/jobs/:job_id/play", feature_category: :continuous_integration do
        authorize_read_builds!

        job = find_job!(params[:job_id])

        authorize!(:play_job, job)

        bad_request!("Unplayable Job") unless job.playable?

        job.play(current_user)

        status 200

        if job.is_a?(::Ci::Build)
          present job, with: Entities::Ci::Job
        else
          present job, with: Entities::Ci::Bridge
        end
      end
    end

    resource :job do
      desc 'Get current project using job token' do
        success Entities::Ci::Job
      end
      route_setting :authentication, job_token_allowed: true
      get '', feature_category: :continuous_integration do
        validate_current_authenticated_job

        present current_authenticated_job, with: Entities::Ci::Job
      end
    end

    helpers do
      # rubocop: disable CodeReuse/ActiveRecord
      def filter_builds(builds, scope)
        return builds if scope.nil? || scope.empty?

        available_statuses = ::CommitStatus::AVAILABLE_STATUSES

        unknown = scope - available_statuses
        render_api_error!('Scope contains invalid value(s)', 400) unless unknown.empty?

        builds.where(status: available_statuses && scope)
      end
      # rubocop: enable CodeReuse/ActiveRecord

      def validate_current_authenticated_job
        # current_authenticated_job will be nil if user is using
        # a valid authentication (like PRIVATE-TOKEN) that is not CI_JOB_TOKEN
        not_found!('Job') unless current_authenticated_job
      end
    end
  end
end

API::Jobs.prepend_mod_with('API::Jobs')