Set project_id when creating Packages::Dependency
What does this MR do and why?
In Add project_id column to packages_dependencies ... (!160830 - merged) we added a new column project_id to the packages_dependencies table.
This is the second MR in the series that changes the Packages::CreateDependencyService to set project_id column when creating a new Packages::Dependency entry and to use project_id when fetching the existing entries.
Additionally, it changes Packages::Rubygems::CreateDependenciesService to re-use Packages::CreateDependencyService with the updated logic, instead of changing already problematic safe_find_or_create_by! to use project_id.
Note: Backfilling project_id for existing entries will be added in the separate MR.
MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Screenshots or screen recordings
No.
How to set up and validate locally
The Packages::CreateDependencyService is used to create a new dependency for npm, nuget and rubygems (experiment support packages. Let's check all of them.
npm package
-
Create a new or choose an existing project where npm package will be published.
-
Create a new npm package
mkdir test_package && cd test_package npm init -y -
Change the package's name to include a scope. Example:
"name": "@gitlab-org/test_package". More about the naming convention is here. -
Change the
.npmrcto set theregistryforscopeand add an authentication token in case of none public page docs. Use project level endpoint. -
Add dependency to the package
npm i axios -
Publish the package
npm publish -
Check that the new dependency for
axioswas created and it contains correctproject_id.# In `rails console` Packages::Dependency.where(name: 'axios')
Now let's check that the existing dependency without project_id is re-used
-
Create a new dependency using
rails consolePackages::Dependency.create(name: "momentjs", version_pattern: "^2.0.0") -
Add
momentjsto ourtest_packagenpm i momentjs@2.0.0 -
Bump the version of package and publish it.
-
Check the last
Packages::Dependencywith thename: "momentjs", it should be still withoutproject_id. -
Check that the
Packages::DependencyLinkwas created and linked tomomentjsand our package.
nuget package
-
Create a new nuget package
mkdir pineapple && cd pineapple nuget spec -
Check the dependencies inside
Package.nuspec, usually it should haveSampleDependency. If there're none, addSampleDependencyas following:<?xml version="1.0" encoding="utf-8"?> <package> <metadata> ... <dependencies> <group targetFramework=".NETStandard2.1"> <dependency id="SampleDependency" version="1.0.0" /> </group> </dependencies> </metadata> </package> -
Add nuget source to setup authentication docs
-
Generate package and publish it
nuget pack nuget push Package.1.0.0.nupkg -Source gitlab -
Check that the new dependency for
SampleDependencywas created and it contains correctproject_id.# In `rails console` Packages::Dependency.where(name: 'SampleDependency')
Now let's check that the existing dependency without project_id is re-used
-
Create a new dependency using
rails consolePackages::Dependency.create(name: "JunitXml.TestLogger", version_pattern: "4.0.254") -
Add
JunitXml.TestLoggerversion4.0.254to ourpackage<dependencies> <group targetFramework=".NETStandard2.1"> ... <dependency id="JunitXml.TestLogger" version="4.0.254" /> </group> </dependencies> -
Bump the version of package and publish it.
-
Check the last
Packages::Dependencywith thename: "JunitXml.TestLogger", it should be still withoutproject_id. -
Check that the
Packages::DependencyLinkwas created and linked toJunitXml.TestLoggerand our package.
rubygems package
-
Enable the feature flag
Feature.enable(:rubygem_packages) -
Create a new ruby package with
prydependencymkdir my_gem && cd my_gem tee -a my_gem.gemspec <<END Gem::Specification.new do |s| s.name = 'my_gem' s.version = '0.0.1' s.summary = "This is an example!" s.authors = ["Ruby Coder"] s.add_development_dependency "pry" end END gem build my_gem.gemspec -
Setup the credentials docs
-
Publish the package
gem push my_gem-0.0.1.gem --host <RUBYGEMS_API_ENDPOINT>RUBYGEMS_API_ENDPOINT is defined in
~/.gem/credentials -
Check that the new dependency for
prywas created and it contains correctproject_id.# In `rails console` Packages::Dependency.where(name: 'pry')
Now let's check that the existing dependency without project_id is re-used
-
Create a new dependency using
rails consolePackages::Dependency.create(name: "rake", version_pattern: ">= 0") -
Add new dependency to
my_gems.add_development_dependency "rake" -
Bump the version, build the gem and publish
-
Check the last
Packages::Dependencywith thename: "rake", it should be still withoutproject_id. -
Check that the
Packages::DependencyLinkwas created and linked torakeand our package.
Related to #465276 (closed)