Commit fd97271d authored by Giorgenes Gelatti's avatar Giorgenes Gelatti

Storage creator in package model

- Make creator required
- Add tests for the different pkgs
- Adds share example
parent 671f008c
......@@ -5,6 +5,8 @@ class Packages::Package < ApplicationRecord
include UsageStatistics
belongs_to :project
belongs_to :creator, class_name: 'User'
# package_files must be destroyed by ruby code in order to properly remove carrierwave uploads and update project statistics
has_many :package_files, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :dependency_links, inverse_of: :package, class_name: 'Packages::DependencyLink'
......
......@@ -8,7 +8,9 @@ module Packages
project
.packages
.with_package_type(package_type)
.safe_find_or_create_by!(name: name, version: version)
.safe_find_or_create_by!(name: name, version: version) do |pkg|
pkg.creator = package_creator
end
end
def create_package!(package_type, attrs = {})
......@@ -22,9 +24,14 @@ module Packages
def package_attrs(attrs)
{
creator: package_creator,
name: params[:name],
version: params[:version]
}.merge(attrs)
end
def package_creator
current_user if current_user.is_a?(User)
end
end
end
---
title: Record package creator
merge_request: 41258
author:
type: added
......@@ -21,7 +21,7 @@ class Gitlab::Seeder::Packages
.gsub('1.0.1', version))
.with_indifferent_access
::Packages::Npm::CreatePackageService.new(project, project.owner, params).execute
::Packages::Npm::CreatePackageService.new(project, project.creator, params).execute
print '.'
end
......@@ -38,7 +38,7 @@ class Gitlab::Seeder::Packages
path: "#{name}/#{version}"
}
pkg = ::Packages::Maven::CreatePackageService.new(project, project.owner, params).execute
pkg = ::Packages::Maven::CreatePackageService.new(project, project.creator, params).execute
%w(maven-metadata.xml my-app-1.0-20180724.124855-1.pom my-app-1.0-20180724.124855-1.jar).each do |filename|
with_cloned_fixture_file('maven', filename) do |filepath|
......@@ -68,7 +68,7 @@ class Gitlab::Seeder::Packages
package_channel: 'stable'
}
pkg = ::Packages::Conan::CreatePackageService.new(project, project.owner, params).execute
pkg = ::Packages::Conan::CreatePackageService.new(project, project.creator, params).execute
fixtures = {
'recipe_files' => %w(conanfile.py conanmanifest.txt),
......@@ -103,7 +103,7 @@ class Gitlab::Seeder::Packages
name = "MyNugetApp.Package#{i}"
version = "4.2.#{i}"
pkg = ::Packages::Nuget::CreatePackageService.new(project, project.owner, {}).execute
pkg = ::Packages::Nuget::CreatePackageService.new(project, project.creator, {}).execute
# when using ::Packages::Nuget::CreatePackageService, packages have a fixed name and a fixed version.
pkg.update!(name: name, version: version)
......
......@@ -2,6 +2,7 @@
FactoryBot.define do
factory :package, class: 'Packages::Package' do
project
creator { project&.creator }
name { 'my/company/app/my-app' }
sequence(:version) { |n| "1.#{n}-SNAPSHOT" }
package_type { :maven }
......
......@@ -6,6 +6,7 @@ RSpec.describe Packages::Package, type: :model do
describe 'relationships' do
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:creator) }
it { is_expected.to have_many(:package_files).dependent(:destroy) }
it { is_expected.to have_many(:dependency_links).inverse_of(:package) }
it { is_expected.to have_many(:tags).inverse_of(:package) }
......
......@@ -37,12 +37,16 @@ RSpec.describe Packages::Composer::CreatePackageService do
expect(created_package.composer_metadatum.target_sha).to eq branch.target
expect(created_package.composer_metadatum.composer_json.to_json).to eq json
end
it_behaves_like 'assigns the package creator' do
let(:package) { created_package }
end
end
context 'with a tag' do
let(:tag) { project.repository.find_tag('v1.2.3') }
before do
before(:all) do
project.repository.add_tag(user, 'v1.2.3', 'master')
end
......@@ -54,6 +58,10 @@ RSpec.describe Packages::Composer::CreatePackageService do
expect(created_package.name).to eq package_name
expect(created_package.version).to eq '1.2.3'
end
it_behaves_like 'assigns the package creator' do
let(:package) { created_package }
end
end
end
......
......@@ -5,9 +5,11 @@ RSpec.describe Packages::Conan::CreatePackageService do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
subject { described_class.new(project, user, params) }
subject(:service) { described_class.new(project, user, params) }
describe '#execute' do
subject(:package) { service.execute }
context 'valid params' do
let(:params) do
{
......@@ -19,8 +21,6 @@ RSpec.describe Packages::Conan::CreatePackageService do
end
it 'creates a new package' do
package = subject.execute
expect(package).to be_valid
expect(package.name).to eq(params[:package_name])
expect(package.version).to eq(params[:package_version])
......@@ -28,6 +28,8 @@ RSpec.describe Packages::Conan::CreatePackageService do
expect(package.conan_metadatum.package_username).to eq(params[:package_username])
expect(package.conan_metadatum.package_channel).to eq(params[:package_channel])
end
it_behaves_like 'assigns the package creator'
end
context 'invalid params' do
......@@ -41,7 +43,7 @@ RSpec.describe Packages::Conan::CreatePackageService do
end
it 'fails' do
expect { subject.execute }.to raise_exception(ActiveRecord::RecordInvalid)
expect { package }.to raise_exception(ActiveRecord::RecordInvalid)
end
end
end
......
......@@ -34,6 +34,8 @@ RSpec.describe Packages::Maven::CreatePackageService do
end
it_behaves_like 'assigns build to package'
it_behaves_like 'assigns the package creator'
end
context 'without version' do
......@@ -57,6 +59,8 @@ RSpec.describe Packages::Maven::CreatePackageService do
expect(package.maven_metadatum.app_name).to eq(app_name)
expect(package.maven_metadatum.app_version).to be nil
end
it_behaves_like 'assigns the package creator'
end
context 'path is missing' do
......
......@@ -27,6 +27,10 @@ RSpec.describe Packages::Npm::CreatePackageService do
.and change { Packages::Tag.count }.by(1)
end
it_behaves_like 'assigns the package creator' do
let(:package) { subject }
end
it { is_expected.to be_valid }
it 'creates a package with name and version' do
......
......@@ -9,9 +9,10 @@ RSpec.describe Packages::Nuget::CreatePackageService do
describe '#execute' do
subject { described_class.new(project, user, params).execute }
let(:package) { Packages::Package.last }
it 'creates the package' do
expect { subject }.to change { Packages::Package.count }.by(1)
package = Packages::Package.last
expect(package).to be_valid
expect(package.name).to eq(Packages::Nuget::CreatePackageService::TEMPORARY_PACKAGE_NAME)
......@@ -23,12 +24,12 @@ RSpec.describe Packages::Nuget::CreatePackageService do
expect { subject }.to change { Packages::Package.count }.by(1)
expect { described_class.new(project, user, params).execute }.to change { Packages::Package.count }.by(1)
package = Packages::Package.last
expect(package).to be_valid
expect(package.name).to eq(Packages::Nuget::CreatePackageService::TEMPORARY_PACKAGE_NAME)
expect(package.version).to start_with(Packages::Nuget::CreatePackageService::PACKAGE_VERSION)
expect(package.package_type).to eq('nuget')
end
it_behaves_like 'assigns the package creator'
end
end
......@@ -47,6 +47,10 @@ RSpec.describe Packages::Pypi::CreatePackageService do
end
end
it_behaves_like 'assigns the package creator' do
let(:package) { created_package }
end
context 'with an existing package' do
before do
described_class.new(project, user, params).execute
......
......@@ -14,6 +14,14 @@ RSpec.shared_examples 'assigns build to package' do
end
end
RSpec.shared_examples 'assigns the package creator' do
it 'assigns the package creator' do
subject
expect(package.creator).to eq user
end
end
RSpec.shared_examples 'returns packages' do |container_type, user_type|
context "for #{user_type}" do
before do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment