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