Verified Commit d2f55f9a authored by Dmytro Zaporozhets (DZ)'s avatar Dmytro Zaporozhets (DZ) 🌴
Browse files

Add instance-level maven endpoint for download

parent b3bb56d4
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -119,6 +119,31 @@ on the home page of your project.
If you have a self-hosted GitLab installation, replace `gitlab.com` with your
domain name.

## Instance level Maven endpoint

> [Introduced](https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/8274) in GitLab Premium 11.6.

If you rely on many packages, it might be inefficient to include the `repository` section 
with a unique URL for each package. Instead, you can use the instance level endpoint for 
all maven packages stored in GitLab. Only packages you have access to 
will be available for download. Here's how the relevant `repository` section of 
your `pom.xml` would look like:

```xml
<repositories>
  <repository>
    <id>gitlab-maven</id>
    <url>https://gitlab.com/api/v4/packages/maven</url>
  </repository>
</repositories>
```

If you have a self-hosted GitLab installation, replace `gitlab.com` with your
domain name.

You still need a project specific URL for uploading a package 
in the `distributionManagement` section.

## Uploading packages

Once you have set up the [authorization](#authorizing-with-the-gitlab-maven-repository)
+12 −4
Original line number Diff line number Diff line
# frozen_string_literal: true
class Packages::MavenPackageFinder
  attr_reader :project, :path
  attr_reader :path, :project

  def initialize(project, path)
    @project = project
  def initialize(path, project = nil)
    @path = path
    @project = project
  end

  def execute
@@ -17,9 +17,17 @@ def execute!

  private

  def scope
    if project
      project.packages
    else
      ::Packages::Package.all
    end
  end

  # rubocop: disable CodeReuse/ActiveRecord
  def packages
    project.packages.joins(:maven_metadatum)
    scope.joins(:maven_metadatum)
      .where(packages_maven_metadata: { path: path })
  end
  # rubocop: enable CodeReuse/ActiveRecord
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ class FindOrCreateMavenPackageService < BaseService

    def execute
      package = ::Packages::MavenPackageFinder
        .new(project, params[:path]).execute
        .new(params[:path], project).execute

      unless package
        if params[:file_name] == MAVEN_METADATA_FILE
+5 −0
Original line number Diff line number Diff line
---
title: Add an instance-level endpoint for downloading maven packages
merge_request: 8274
author:
type: added
+35 −2
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ class MavenPackages < Grape::API
    before do
      require_packages_enabled!
      authenticate_non_get!
      authorize_packages_feature!
    end

    helpers do
@@ -54,10 +53,44 @@ def verify_package_file(package_file, uploaded_file)
      end
    end

    desc 'Download the maven package file at instance level' do
      detail 'This feature was introduced in GitLab 11.6'
    end
    params do
      requires :path, type: String, desc: 'Package path'
      requires :file_name, type: String, desc: 'Package file name'
    end
    route_setting :authentication, job_token_allowed: true
    get 'packages/maven/*path/:file_name', requirements: MAVEN_ENDPOINT_REQUIREMENTS do
      file_name, format = extract_format(params[:file_name])

      package = ::Packages::MavenPackageFinder.new(params[:path]).execute!

      forbidden! unless package.project.feature_available?(:packages)

      authorize!(:read_package, package.project)

      package_file = ::Packages::PackageFileFinder
        .new(package, file_name).execute!

      case format
      when 'md5'
        package_file.file_md5
      when 'sha1'
        package_file.file_sha1
      when nil
        present_carrierwave_file!(package_file.file)
      end
    end

    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
    resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
      before do
        authorize_packages_feature!
      end

      desc 'Download the maven package file' do
        detail 'This feature was introduced in GitLab 11.3'
      end
@@ -72,7 +105,7 @@ def verify_package_file(package_file, uploaded_file)
        file_name, format = extract_format(params[:file_name])

        package = ::Packages::MavenPackageFinder
          .new(user_project, params[:path]).execute!
          .new(params[:path], user_project).execute!

        package_file = ::Packages::PackageFileFinder
          .new(package, file_name).execute!
Loading