summaryrefslogtreecommitdiff
path: root/lib/api
diff options
context:
space:
mode:
authorLin Jen-Shin <godfat@godfat.org>2016-09-14 20:38:32 +0800
committerLin Jen-Shin <godfat@godfat.org>2016-09-14 20:38:32 +0800
commitba84a428287a16e99bd0239bccc20c5a786d3549 (patch)
treeec3007c0151c95589aca1ca95f87c1ccd2fd2bf6 /lib/api
parent15bb44fc6357ca57e344d7b4818ac74ffb0a4dea (diff)
parent72e9f63ff722585ba75abbf4635f649e88709a70 (diff)
downloadgitlab-ce-ba84a428287a16e99bd0239bccc20c5a786d3549.tar.gz
Merge remote-tracking branch 'upstream/master' into pipeline-emails
* upstream/master: (64 commits) Update the 8.11 to 8.12 update documentation to use gitlab-shell 3.5.0 Update GITLAB_SHELL_VERSION Move pushes_since_gc to Redis Fix Gitlab::Popen.popen thread-safety issue Allow trailing newline in secret base64 data Improve .haml-lint.yml, simplify the haml_lint task and remove CHANGELOG entry Fix a typo in documentation Restore SSH Key title auto-population behavior Add test with inactive error use case for 'POST /ci/api/v1/builds/register.json' Fix rubocop offences Update API documentation Update CHANGELOG Change response for /ci/api/v1/builds/register.json from 404 to 204 Document the fact that merge requests from private forks can be checked out locally Change method name to #reopenable? Improve grammar Fix not working test with execute Add CHANGELOG Improve grammar Add #can_reopen? and tests ...
Diffstat (limited to 'lib/api')
-rw-r--r--lib/api/commit_statuses.rb54
-rw-r--r--lib/api/helpers.rb4
2 files changed, 32 insertions, 26 deletions
diff --git a/lib/api/commit_statuses.rb b/lib/api/commit_statuses.rb
index 5e3c9563703..dfbdd597d29 100644
--- a/lib/api/commit_statuses.rb
+++ b/lib/api/commit_statuses.rb
@@ -37,7 +37,7 @@ module API
# id (required) - The ID of a project
# sha (required) - The commit hash
# ref (optional) - The ref
- # state (required) - The state of the status. Can be: pending, running, success, error or failure
+ # state (required) - The state of the status. Can be: pending, running, success, failed or canceled
# target_url (optional) - The target URL to associate with this status
# description (optional) - A short description of the status
# name or context (optional) - A string label to differentiate this status from the status of other systems. Default: "default"
@@ -46,7 +46,7 @@ module API
post ':id/statuses/:sha' do
authorize! :create_commit_status, user_project
required_attributes! [:state]
- attrs = attributes_for_keys [:ref, :target_url, :description, :context, :name]
+ attrs = attributes_for_keys [:target_url, :description]
commit = @project.commit(params[:sha])
not_found! 'Commit' unless commit
@@ -58,36 +58,38 @@ module API
# the first found branch on that commit
ref = params[:ref]
- unless ref
- branches = @project.repository.branch_names_contains(commit.sha)
- not_found! 'References for commit' if branches.none?
- ref = branches.first
- end
+ ref ||= @project.repository.branch_names_contains(commit.sha).first
+ not_found! 'References for commit' unless ref
+
+ name = params[:name] || params[:context] || 'default'
pipeline = @project.ensure_pipeline(ref, commit.sha, current_user)
- name = params[:name] || params[:context]
- status = GenericCommitStatus.running_or_pending.find_by(pipeline: pipeline, name: name, ref: params[:ref])
- status ||= GenericCommitStatus.new(project: @project, pipeline: pipeline, user: current_user)
- status.update(attrs)
+ status = GenericCommitStatus.running_or_pending.find_or_initialize_by(
+ project: @project, pipeline: pipeline,
+ user: current_user, name: name, ref: ref)
+ status.attributes = attrs
- case params[:state].to_s
- when 'running'
- status.run
- when 'success'
- status.success
- when 'failed'
- status.drop
- when 'canceled'
- status.cancel
- else
- status.status = params[:state].to_s
- end
+ begin
+ case params[:state].to_s
+ when 'pending'
+ status.enqueue!
+ when 'running'
+ status.enqueue
+ status.run!
+ when 'success'
+ status.success!
+ when 'failed'
+ status.drop!
+ when 'canceled'
+ status.cancel!
+ else
+ render_api_error!('invalid state', 400)
+ end
- if status.save
present status, with: Entities::CommitStatus
- else
- render_validation_error!(status)
+ rescue StateMachines::InvalidTransition => e
+ render_api_error!(e.message, 400)
end
end
end
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index 6a20ba95a79..150875ed4f0 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -269,6 +269,10 @@ module API
render_api_error!('304 Not Modified', 304)
end
+ def no_content!
+ render_api_error!('204 No Content', 204)
+ end
+
def render_validation_error!(model)
if model.errors.any?
render_api_error!(model.errors.messages || '400 Bad Request', 400)