BigW Consortium Gitlab

job_artifacts.rb 2.74 KB
Newer Older
1 2 3 4
module API
  class JobArtifacts < Grape::API
    before { authenticate_non_get! }

5 6 7 8 9 10 11
    # EE::API::JobArtifacts would override the following helpers
    helpers do
      def authorize_download_artifacts!
        authorize_read_builds!
      end
    end

12 13 14 15
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
    resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
16
      desc 'Download the artifacts archive from a job' do
17 18 19 20 21 22
        detail 'This feature was introduced in GitLab 8.10'
      end
      params do
        requires :ref_name, type: String, desc: 'The ref from repository'
        requires :job,      type: String, desc: 'The name for the job'
      end
23
      route_setting :authentication, job_token_allowed: true
24 25
      get ':id/jobs/artifacts/:ref_name/download',
        requirements: { ref_name: /.+/ } do
26
        authorize_download_artifacts!
27 28 29 30

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

31
        present_carrierwave_file!(latest_build.artifacts_file)
32 33
      end

34
      desc 'Download the artifacts archive from a job' do
35 36 37 38 39
        detail 'This feature was introduced in GitLab 8.5'
      end
      params do
        requires :job_id, type: Integer, desc: 'The ID of a job'
      end
40
      route_setting :authentication, job_token_allowed: true
41
      get ':id/jobs/:job_id/artifacts' do
42
        authorize_download_artifacts!
43 44 45

        build = find_build!(params[:job_id])

46
        present_carrierwave_file!(build.artifacts_file)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
      end

      desc 'Download a specific file from artifacts archive' do
        detail 'This feature was introduced in GitLab 10.0'
      end
      params do
        requires :job_id, type: Integer, desc: 'The ID of a job'
        requires :artifact_path, type: String, desc: 'Artifact path'
      end
      get ':id/jobs/:job_id/artifacts/*artifact_path', format: false do
        authorize_read_builds!

        build = find_build!(params[:job_id])
        not_found! unless build.artifacts?

        path = Gitlab::Ci::Build::Artifacts::Path
          .new(params[:artifact_path])
64
        bad_request! unless path.valid?
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

        send_artifacts_entry(build, path)
      end

      desc 'Keep the artifacts to prevent them from being deleted' do
        success Entities::Job
      end
      params do
        requires :job_id, type: Integer, desc: 'The ID of a job'
      end
      post ':id/jobs/:job_id/artifacts/keep' do
        authorize_update_builds!

        build = find_build!(params[:job_id])
        authorize!(:update_build, build)
        return not_found!(build) unless build.artifacts?

        build.keep_artifacts!

        status 200
        present build, with: Entities::Job
      end
    end
  end
end