Commit d5f7e542 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Add job update API

parent fb8210ad
...@@ -39,13 +39,22 @@ module API ...@@ -39,13 +39,22 @@ module API
(Time.now - current_runner.contacted_at) >= contacted_at_max_age (Time.now - current_runner.contacted_at) >= contacted_at_max_age
end end
def build_not_found! def job_not_found!
if headers['User-Agent'].to_s.match(/gitlab(-ci-multi)?-runner \d+\.\d+\.\d+(~beta\.\d+\.g[0-9a-f]+)? /) if headers['User-Agent'].to_s.match(/gitlab(-ci-multi)?-runner \d+\.\d+\.\d+(~beta\.\d+\.g[0-9a-f]+)? /)
no_content! no_content!
else else
not_found! not_found!
end end
end end
def validate_job!(job)
not_found! unless job
yield if block_given?
forbidden!('Project has been deleted!') unless job.project
forbidden!('Job has been erased!') if job.erased?
end
end end
end end
end end
...@@ -66,7 +66,7 @@ module API ...@@ -66,7 +66,7 @@ module API
if current_runner.is_runner_queue_value_latest?(params[:last_update]) if current_runner.is_runner_queue_value_latest?(params[:last_update])
header 'X-GitLab-Last-Update', params[:last_update] header 'X-GitLab-Last-Update', params[:last_update]
Gitlab::Metrics.add_event(:build_not_found_cached) Gitlab::Metrics.add_event(:build_not_found_cached)
return build_not_found! return job_not_found!
end end
new_update = current_runner.ensure_runner_queue_value new_update = current_runner.ensure_runner_queue_value
...@@ -80,7 +80,7 @@ module API ...@@ -80,7 +80,7 @@ module API
else else
Gitlab::Metrics.add_event(:build_not_found) Gitlab::Metrics.add_event(:build_not_found)
header 'X-GitLab-Last-Update', new_update header 'X-GitLab-Last-Update', new_update
build_not_found! job_not_found!
end end
else else
# We received build that is invalid due to concurrency conflict # We received build that is invalid due to concurrency conflict
...@@ -88,6 +88,32 @@ module API ...@@ -88,6 +88,32 @@ module API
conflict! conflict!
end end
end end
desc 'Updates a job' do
http_codes [[200, 'Job was updated'], [403, 'Forbidden']]
end
params do
requires :token, type: String, desc: %q(Job's authentication token)
requires :id, type: Fixnum, desc: %q(Job's ID)
optional :trace, type: String, desc: %q(Job's full trace)
optional :state, type: String, desc: %q(Job's status: success, failed)
end
put '/:id' do
job = Ci::Build.find_by_id(params[:id])
authenticate_job!(job)
job.update_attributes(trace: params[:trace]) if params[:trace]
Gitlab::Metrics.add_event(:update_build,
project: job.project.path_with_namespace)
case params[:state].to_s
when 'success'
job.success
when 'failed'
job.drop
end
end
end end
end end
end end
...@@ -432,5 +432,53 @@ describe API::Runner do ...@@ -432,5 +432,53 @@ describe API::Runner do
end end
end end
end end
describe 'PUT /api/v4/jobs/:id' do
let(:job) { create(:ci_build, :pending, :trace, pipeline: pipeline, runner_id: runner.id) }
before { job.run! }
context 'when status is given' do
it 'mark job as succeeded' do
update_job(state: 'success')
expect(job.reload.status).to eq 'success'
end
it 'mark job as failed' do
update_job(state: 'failed')
expect(job.reload.status).to eq 'failed'
end
end
context 'when tace is given' do
it 'updates a running build' do
update_job(trace: 'BUILD TRACE UPDATED')
expect(response).to have_http_status(200)
expect(job.reload.trace).to eq 'BUILD TRACE UPDATED'
end
end
context 'when no trace is given' do
it 'does not override trace information' do
update_job
expect(job.reload.trace).to eq 'BUILD TRACE'
end
end
context 'when job has been erased' do
let(:job) { create(:ci_build, runner_id: runner.id, erased_at: Time.now) }
it 'responds with forbidden' do
update_job
expect(response).to have_http_status(403)
end
end
def update_job(token = job.token, **params)
new_params = params.merge(token: token)
put api("/jobs/#{job.id}"), new_params
end
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment