Class: Packages::Maven::FindOrCreatePackageService

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/packages/maven/find_or_create_package_service.rb

Constant Summary collapse

SNAPSHOT_TERM =
'-SNAPSHOT'

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#executeObject


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/services/packages/maven/find_or_create_package_service.rb', line 7

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

  unless Namespace::PackageSetting.duplicates_allowed?(package)
    return ServiceResponse.error(message: 'Duplicate package is not allowed') if target_package_is_duplicate?(package)
  end

  unless package
    # Maven uploads several files during `mvn deploy` in next order:
    #   - my-company/my-app/1.0-SNAPSHOT/my-app.jar
    #   - my-company/my-app/1.0-SNAPSHOT/my-app.pom
    #   - my-company/my-app/1.0-SNAPSHOT/maven-metadata.xml
    #   - my-company/my-app/maven-metadata.xml
    #
    # The last xml file does not have VERSION in URL because it contains
    # information about all versions. When uploading such file, we create
    # a package with a version set to `nil`. The xml file with a version
    # is only created and uploaded for snapshot versions.
    #
    # Gradle has a different upload order:
    #   - my-company/my-app/1.0-SNAPSHOT/maven-metadata.xml
    #   - my-company/my-app/1.0-SNAPSHOT/my-app.jar
    #   - my-company/my-app/1.0-SNAPSHOT/my-app.pom
    #   - my-company/my-app/maven-metadata.xml
    #
    # The first upload has to create the proper package (the one with the version set).
    if params[:file_name] == Packages::Maven::Metadata.filename && !params[:path]&.ends_with?(SNAPSHOT_TERM)
      package_name = params[:path]
      version = nil
    else
      package_name, _, version = params[:path].rpartition('/')
    end

    package_params = {
      name: package_name,
      path: params[:path],
      status: params[:status],
      version: version
    }

    package =
      ::Packages::Maven::CreatePackageService.new(project, current_user, package_params)
                                             .execute
  end

  package.create_build_infos!(params[:build])

  ServiceResponse.success(payload: { package: package })
end