BigW Consortium Gitlab

jobs.rb 7.11 KB
Newer Older
1
module API
2
  class Jobs < Grape::API
3 4
    include PaginationParams

5 6
    before { authenticate! }

Robert Schilling committed
7 8 9
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
10
    resource :projects, requirements: { id: %r{[^/]+} } do
Robert Schilling committed
11 12 13
      helpers do
        params :optional_scope do
          optional :scope, types: [String, Array[String]], desc: 'The scope of builds to show',
14
                           values: ::CommitStatus::AVAILABLE_STATUSES,
Robert Schilling committed
15
                           coerce_with: ->(scope) {
16 17
                             case scope
                             when String
Robert Schilling committed
18
                               [scope]
19
                             when Hashie::Mash
Robert Schilling committed
20
                               scope.values
21 22
                             when Hashie::Array
                               scope
Robert Schilling committed
23 24 25 26 27 28 29
                             else
                               ['unknown']
                             end
                           }
        end
      end

30 31
      desc 'Get a projects jobs' do
        success Entities::Job
Robert Schilling committed
32 33 34
      end
      params do
        use :optional_scope
35
        use :pagination
Robert Schilling committed
36
      end
37
      get ':id/jobs' do
38 39
        builds = user_project.builds.order('id DESC')
        builds = filter_builds(builds, params[:scope])
40

41
        present paginate(builds), with: Entities::Job
42
      end
43

44 45 46 47 48 49 50 51 52 53 54 55 56
      desc 'Get pipeline jobs' do
        success Entities::Job
      end
      params do
        requires :pipeline_id, type: Integer,  desc: 'The pipeline ID'
        use :optional_scope
        use :pagination
      end
      get ':id/pipelines/:pipeline_id/jobs' do
        pipeline = user_project.pipelines.find(params[:pipeline_id])
        builds = pipeline.builds
        builds = filter_builds(builds, params[:scope])

57
        present paginate(builds), with: Entities::Job
58 59
      end

60 61
      desc 'Get a specific job of a project' do
        success Entities::Job
Robert Schilling committed
62 63
      end
      params do
64
        requires :job_id, type: Integer, desc: 'The ID of a job'
Robert Schilling committed
65
      end
66
      get ':id/jobs/:job_id' do
67 68
        authorize_read_builds!

69
        build = get_build!(params[:job_id])
70

71
        present build, with: Entities::Job
72 73
      end

74
      desc 'Download the artifacts file from a job' do
Robert Schilling committed
75 76 77
        detail 'This feature was introduced in GitLab 8.5'
      end
      params do
78
        requires :job_id, type: Integer, desc: 'The ID of a job'
Robert Schilling committed
79
      end
80
      get ':id/jobs/:job_id/artifacts' do
81 82
        authorize_read_builds!

83
        build = get_build!(params[:job_id])
84

85
        present_artifacts!(build.artifacts_file)
86
      end
87

88
      desc 'Download the artifacts file from a job' do
Robert Schilling committed
89 90 91 92
        detail 'This feature was introduced in GitLab 8.10'
      end
      params do
        requires :ref_name, type: String, desc: 'The ref from repository'
93
        requires :job,      type: String, desc: 'The name for the job'
Robert Schilling committed
94
      end
95
      get ':id/jobs/artifacts/:ref_name/download',
96
        requirements: { ref_name: /.+/ } do
97 98
        authorize_read_builds!

99 100
        builds = user_project.latest_successful_builds_for(params[:ref_name])
        latest_build = builds.find_by!(name: params[:job])
101

102
        present_artifacts!(latest_build.artifacts_file)
103 104
      end

105 106 107
      # TODO: We should use `present_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.
108
      desc 'Get a trace of a specific job of a project'
Robert Schilling committed
109
      params do
110
        requires :job_id, type: Integer, desc: 'The ID of a job'
Robert Schilling committed
111
      end
112
      get ':id/jobs/:job_id/trace' do
113 114
        authorize_read_builds!

115
        build = get_build!(params[:job_id])
116 117 118 119

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

121
        trace = build.trace.raw
122
        body trace
123
      end
124

125 126
      desc 'Cancel a specific job of a project' do
        success Entities::Job
Robert Schilling committed
127 128
      end
      params do
129
        requires :job_id, type: Integer, desc: 'The ID of a job'
Robert Schilling committed
130
      end
131
      post ':id/jobs/:job_id/cancel' do
132
        authorize_update_builds!
133

134
        build = get_build!(params[:job_id])
135
        authorize!(:update_build, build)
136 137 138

        build.cancel

139
        present build, with: Entities::Job
140 141
      end

Robert Schilling committed
142
      desc 'Retry a specific build of a project' do
143
        success Entities::Job
Robert Schilling committed
144 145
      end
      params do
146
        requires :job_id, type: Integer, desc: 'The ID of a build'
Robert Schilling committed
147
      end
148
      post ':id/jobs/:job_id/retry' do
149
        authorize_update_builds!
150

151
        build = get_build!(params[:job_id])
152
        authorize!(:update_build, build)
153
        return forbidden!('Job is not retryable') unless build.retryable?
154

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

157
        present build, with: Entities::Job
158
      end
159

160 161
      desc 'Erase job (remove artifacts and the trace)' do
        success Entities::Job
Robert Schilling committed
162 163
      end
      params do
164
        requires :job_id, type: Integer, desc: 'The ID of a build'
Robert Schilling committed
165
      end
166
      post ':id/jobs/:job_id/erase' do
167
        authorize_update_builds!
168

169
        build = get_build!(params[:job_id])
170
        authorize!(:update_build, build)
171
        return forbidden!('Job is not erasable!') unless build.erasable?
172

173
        build.erase(erased_by: current_user)
174
        present build, with: Entities::Job
175
      end
176

Robert Schilling committed
177
      desc 'Keep the artifacts to prevent them from being deleted' do
178
        success Entities::Job
Robert Schilling committed
179 180
      end
      params do
181
        requires :job_id, type: Integer, desc: 'The ID of a job'
Robert Schilling committed
182
      end
183
      post ':id/jobs/:job_id/artifacts/keep' do
184 185
        authorize_update_builds!

186
        build = get_build!(params[:job_id])
187
        authorize!(:update_build, build)
188
        return not_found!(build) unless build.artifacts?
189 190 191 192

        build.keep_artifacts!

        status 200
193
        present build, with: Entities::Job
194
      end
195

196 197
      desc 'Trigger a manual job' do
        success Entities::Job
198 199 200
        detail 'This feature was added in GitLab 8.11'
      end
      params do
201
        requires :job_id, type: Integer, desc: 'The ID of a Job'
202
      end
203
      post ":id/jobs/:job_id/play" do
204 205
        authorize_read_builds!

206
        build = get_build!(params[:job_id])
207

208
        authorize!(:update_build, build)
Filipa Lacerda committed
209
        bad_request!("Unplayable Job") unless build.playable?
Z.J. van de Weg committed
210 211 212 213

        build.play(current_user)

        status 200
214
        present build, with: Entities::Job
215
      end
216 217 218
    end

    helpers do
219
      def find_build(id)
220
        user_project.builds.find_by(id: id.to_i)
221
      end
222

223
      def get_build!(id)
224
        find_build(id) || not_found!
225 226
      end

227
      def filter_builds(builds, scope)
228 229
        return builds if scope.nil? || scope.empty?

230
        available_statuses = ::CommitStatus::AVAILABLE_STATUSES
231

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

235
        builds.where(status: available_statuses && scope)
236
      end
237

238 239 240 241 242 243
      def authorize_read_builds!
        authorize! :read_build, user_project
      end

      def authorize_update_builds!
        authorize! :update_build, user_project
244
      end
245 246 247
    end
  end
end