diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-07-14 18:50:34 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-07-14 18:50:34 +0800 |
commit | 70f508f5d48a3541a430539d7f8b41dfa99127a1 (patch) | |
tree | 56fc4e64b2bb716831f8ff7750ba5ff91a894218 | |
parent | a9a8ceebcbe25cbe27bebe9fc63ab364b1dd41ee (diff) | |
download | gitlab-ce-70f508f5d48a3541a430539d7f8b41dfa99127a1.tar.gz |
Serve artifacts from Builds
-rw-r--r-- | lib/api/api.rb | 1 | ||||
-rw-r--r-- | lib/api/artifacts.rb | 34 | ||||
-rw-r--r-- | lib/api/builds.rb | 35 | ||||
-rw-r--r-- | spec/requests/api/artifacts_spec.rb | 6 | ||||
-rw-r--r-- | spec/requests/api/builds_spec.rb | 2 |
5 files changed, 35 insertions, 43 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index f18258bf5a3..3d7d67510a8 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -26,7 +26,6 @@ module API # Ensure the namespace is right, otherwise we might load Grape::API::Helpers helpers ::API::Helpers - mount ::API::Artifacts mount ::API::AwardEmoji mount ::API::Branches mount ::API::Builds diff --git a/lib/api/artifacts.rb b/lib/api/artifacts.rb deleted file mode 100644 index 6ce2bed8260..00000000000 --- a/lib/api/artifacts.rb +++ /dev/null @@ -1,34 +0,0 @@ -module API - # Projects artifacts API - class Artifacts < Grape::API - before do - authenticate! - authorize!(:read_build, user_project) - end - - resource :projects do - # Download the artifacts file from ref_name and build_name - # - # Parameters: - # id (required) - The ID of a project - # ref_name (required) - The ref from repository - # build_name (required) - The name for the build - # Example Request: - # GET /projects/:id/artifacts/:ref_name/:build_name - get ':id/artifacts/:ref_name/:build_name', - requirements: { ref_name: /.+/ } do - builds = user_project.builds_for( - params[:build_name], params[:ref_name]) - - latest_build = builds.success.latest.first - - if latest_build - redirect( - "/projects/#{user_project.id}/builds/#{latest_build.id}/artifacts") - else - not_found! - end - end - end - end -end diff --git a/lib/api/builds.rb b/lib/api/builds.rb index b3b28541382..505ba1a66fe 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -71,12 +71,27 @@ module API build = get_build!(params[:build_id]) artifacts_file = build.artifacts_file - if !artifacts_file.file_storage? - redirect_to(build.artifacts_file.url) + present_artifact!(artifacts_file) + end - elsif artifacts_file.exists? - present_file!(artifacts_file.path, artifacts_file.filename) + # Download the artifacts file from ref_name and build_name + # + # Parameters: + # id (required) - The ID of a project + # ref_name (required) - The ref from repository + # job (required) - The name for the build + # Example Request: + # GET /projects/:id/artifacts/:ref_name/:build_name + get ':id/builds/artifacts/:ref_name', + requirements: { ref_name: /.+/ } do + builds = user_project.builds_for( + params[:job], params[:ref_name]) + latest_build = builds.success.latest.first + + if latest_build + redirect( + "/projects/#{user_project.id}/builds/#{latest_build.id}/artifacts") else not_found! end @@ -191,6 +206,18 @@ module API get_build(id) || not_found! end + def present_artifact!(artifacts_file) + if !artifacts_file.file_storage? + redirect_to(build.artifacts_file.url) + + elsif artifacts_file.exists? + present_file!(artifacts_file.path, artifacts_file.filename) + + else + not_found! + end + end + def filter_builds(builds, scope) return builds if scope.nil? || scope.empty? diff --git a/spec/requests/api/artifacts_spec.rb b/spec/requests/api/artifacts_spec.rb index 393b8f85402..f1461f7bc53 100644 --- a/spec/requests/api/artifacts_spec.rb +++ b/spec/requests/api/artifacts_spec.rb @@ -1,14 +1,14 @@ require 'spec_helper' require_relative '../shared/artifacts_context' -describe API::API, api: true do +describe API::API, api: true do include ApiHelpers describe 'GET /projects/:id/artifacts/:ref_name/:build_name' do include_context 'artifacts from ref and build name' - def path_from_ref(ref = pipeline.sha, build_name = build.name, _ = '') - api("/projects/#{project.id}/artifacts/#{ref}/#{build_name}", user) + def path_from_ref(ref = pipeline.sha, job = build.name) + api("/projects/#{project.id}/builds/artifacts/#{ref}?job=#{job}", user) end context '401' do diff --git a/spec/requests/api/builds_spec.rb b/spec/requests/api/builds_spec.rb index f5b39c3d698..b661bf71545 100644 --- a/spec/requests/api/builds_spec.rb +++ b/spec/requests/api/builds_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe API::API, api: true do +describe API::API, api: true do include ApiHelpers let(:user) { create(:user) } |