diff options
author | Robert Schilling <rschilling@student.tugraz.at> | 2017-08-04 11:32:21 +0200 |
---|---|---|
committer | Robert Schilling <rschilling@student.tugraz.at> | 2017-08-04 11:32:21 +0200 |
commit | 7291f5d7ea0e89dc9fa69dc0e9704f4ec4d7cece (patch) | |
tree | 3519b92a3cd25f9ddead93a091c13b9c04b334c9 | |
parent | 8f9b658e3a30e28189f5ef626d32661e08cf23aa (diff) | |
download | gitlab-ce-grapify-ci-builds-api.tar.gz |
Grapify the CI builds APIgrapify-ci-builds-api
-rw-r--r-- | lib/ci/api/builds.rb | 181 |
1 files changed, 93 insertions, 88 deletions
diff --git a/lib/ci/api/builds.rb b/lib/ci/api/builds.rb index 79058c02ce5..0d6f68b3556 100644 --- a/lib/ci/api/builds.rb +++ b/lib/ci/api/builds.rb @@ -1,18 +1,16 @@ module Ci module API - # Builds API class Builds < Grape::API resource :builds do - # Runs oldest pending build by runner - Runners only - # - # Parameters: - # token (required) - The uniq token of runner - # - # Example Request: - # POST /builds/register + desc 'Register a runner' do + detail 'Runners only' + end + params do + requires :token, type: String, desc: 'The build authorization token' + end post "register" do authenticate_runner! - required_attributes! [:token] + not_found! unless current_runner.active? update_runner_info @@ -46,14 +44,14 @@ module Ci end end - # Update an existing build - Runners only - # - # Parameters: - # id (required) - The ID of a project - # state (optional) - The state of a build - # trace (optional) - The trace of a build - # Example Request: - # PUT /builds/:id + desc 'Update an existing build' do + detail 'Runners only' + end + params do + requires :id, type: Integer, desc: 'The ID of a build' + optional :state, type: String, values: ['success', 'failed'], desc: 'The state of a build' + optional :trace, type: String, desc: 'The trace of a build' + end put ":id" do authenticate_runner! build = Ci::Build.where(runner_id: current_runner.id).running.find(params[:id]) @@ -65,8 +63,7 @@ module Ci Gitlab::Metrics.add_event(:update_build, project: build.project.full_path) - - case params[:state].to_s + case params[:state] when 'success' build.success when 'failed' @@ -74,17 +71,21 @@ module Ci end end - # Send incremental log update - Runners only - # - # Parameters: - # id (required) - The ID of a build - # Body: - # content of logs to append - # Headers: - # Content-Range (required) - range of content that was sent - # BUILD-TOKEN (required) - The build authorization token - # Example Request: - # PATCH /builds/:id/trace.txt + desc 'Send incremental log update' do + detail 'Runners only' + headers 'Content-Range' => { + description: 'Range of content that was sent', + required: true + }, + 'HTTP_BUILD_TOKEN' => { + description: 'The build authorization token', + required: false + } + end + params do + requires :id, type: Integer, desc: 'The ID of a build' + optional :token, type: String, desc: 'The build authorization token' + end patch ":id/trace.txt" do build = authenticate_build! @@ -102,14 +103,18 @@ module Ci header 'Range', "0-#{stream_size}" end - # Authorize artifacts uploading for build - Runners only - # - # Parameters: - # id (required) - The ID of a build - # token (required) - The build authorization token - # filesize (optional) - the size of uploaded file - # Example Request: - # POST /builds/:id/artifacts/authorize + desc 'Authorize artifacts uploading for a build' do + detail 'Runners only' + headers 'HTTP_BUILD_TOKEN' => { + description: 'The build authorization token', + required: false + } + end + params do + requires :id, type: Integer, desc: 'The ID of a build' + optional :token, type: String, desc: 'The build authorization token' + optional :filesize, type: Integer, desc: 'The size of the uploaded file' + end post ":id/artifacts/authorize" do require_gitlab_workhorse! Gitlab::Workhorse.verify_api_request!(headers) @@ -118,7 +123,7 @@ module Ci forbidden!('build is not running') unless build.running? if params[:filesize] - file_size = params[:filesize].to_i + file_size = params[:filesize] file_to_large! unless file_size < max_artifacts_size end @@ -127,32 +132,31 @@ module Ci Gitlab::Workhorse.artifact_upload_ok end - # Upload artifacts to build - Runners only - # - # Parameters: - # id (required) - The ID of a build - # token (required) - The build authorization token - # file (required) - Artifacts file - # expire_in (optional) - Specify when artifacts should expire (ex. 7d) - # Parameters (accelerated by GitLab Workhorse): - # file.path - path to locally stored body (generated by Workhorse) - # file.name - real filename as send in Content-Disposition - # file.type - real content type as send in Content-Type - # metadata.path - path to locally stored body (generated by Workhorse) - # metadata.name - filename (generated by Workhorse) - # Headers: - # BUILD-TOKEN (required) - The build authorization token, the same as token - # Body: - # The file content - # - # Example Request: - # POST /builds/:id/artifacts + desc 'Upload artifacts to build' do + detail 'Runners only' + headers 'HTTP_BUILD_TOKEN' => { + description: 'The build authorization token', + required: false + } + success Entities::BuildDetails + end + params do + requires :id, type: Integer, desc: 'The ID of a build' + optional :token, type: String, desc: 'The build authorization token' + #requires 'file.path', type: String, desc: 'Artifacts file' + #requires 'file.name', type: String, desc: 'Artifacts file' + optional :expire_in, type: String, + default: Gitlab::CurrentSettings.current_application_settings.default_artifacts_expire_in, + desc: 'Specify when artifacts should expire (ex. 7d)' + end post ":id/artifacts" do require_gitlab_workhorse! not_allowed! unless Gitlab.config.artifacts.enabled build = authenticate_build! forbidden!('Build is not running!') unless build.running? + pp params + artifacts_upload_path = ArtifactUploader.artifacts_upload_path artifacts = uploaded_file(:file, artifacts_upload_path) metadata = uploaded_file(:metadata, artifacts_upload_path) @@ -162,10 +166,9 @@ module Ci build.artifacts_file = artifacts build.artifacts_metadata = metadata - build.artifacts_expire_in = - params['expire_in'] || - Gitlab::CurrentSettings.current_application_settings - .default_artifacts_expire_in + build.artifacts_expire_in = params['expire_in'] + + if build.save present(build, with: Entities::BuildDetails) @@ -174,39 +177,41 @@ module Ci end end - # Download the artifacts file from build - Runners only - # - # Parameters: - # id (required) - The ID of a build - # token (required) - The build authorization token - # Headers: - # BUILD-TOKEN (required) - The build authorization token, the same as token - # Example Request: - # GET /builds/:id/artifacts + desc 'Download the artifacts file from build' do + detail 'Runners only' + headers 'HTTP_BUILD_TOKEN' => { + description: 'The build authorization token', + required: false + } + end + params do + requires :id, type: Integer, desc: 'The ID of a build' + optional :token, type: String, desc: 'The build authorization token' + end get ":id/artifacts" do build = authenticate_build! artifacts_file = build.artifacts_file - unless artifacts_file.exists? - not_found! - end + not_found! unless artifacts_file.exists? - unless artifacts_file.file_storage? - return redirect_to build.artifacts_file.url + if artifacts_file.file_storage? + present_file!(artifacts_file.path, artifacts_file.filename) + else + redirect_to build.artifacts_file.url end - - present_file!(artifacts_file.path, artifacts_file.filename) end - # Remove the artifacts file from build - Runners only - # - # Parameters: - # id (required) - The ID of a build - # token (required) - The build authorization token - # Headers: - # BUILD-TOKEN (required) - The build authorization token, the same as token - # Example Request: - # DELETE /builds/:id/artifacts + desc 'Remove the artifacts file from build' do + detail 'Runners only' + headers 'HTTP_BUILD_TOKEN' => { + description: 'The build authorization token', + required: false + } + end + params do + requires :id, type: Integer, desc: 'The ID of a build' + optional :token, type: String, desc: 'The build authorization token' + end delete ":id/artifacts" do build = authenticate_build! |