Commit 048dc0f5 authored by James Lopez's avatar James Lopez

Merge branch '197962-package-versions-api' into 'master'

Add versions array to package api response

See merge request gitlab-org/gitlab!31231
parents 911c4841 5b26c988
...@@ -188,7 +188,27 @@ Example response: ...@@ -188,7 +188,27 @@ Example response:
"name": "Administrator", "name": "Administrator",
"avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon" "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon"
} }
},
"versions": [
{
"id":2,
"version":"2.0-SNAPSHOT",
"created_at":"2020-04-28T04:42:11.573Z",
"pipeline": {
"id": 234,
"status": "pending",
"ref": "new-pipeline",
"sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a",
"web_url": "https://example.com/foo/bar/pipelines/58",
"created_at": "2016-08-11T11:28:34.085Z",
"updated_at": "2016-08-11T11:32:35.169Z",
"user": {
"name": "Administrator",
"avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon"
}
}
} }
]
} }
``` ```
......
...@@ -125,6 +125,18 @@ class Packages::Package < ApplicationRecord ...@@ -125,6 +125,18 @@ class Packages::Package < ApplicationRecord
end end
end end
def versions
project.packages
.with_name(name)
.where.not(version: version)
.with_package_type(package_type)
.order(:version)
end
def pipeline
build_info&.pipeline
end
private private
def valid_conan_package_recipe def valid_conan_package_recipe
......
---
title: Add versions array to the package API payload
merge_request: 31231
author:
type: added
...@@ -29,15 +29,13 @@ module EE ...@@ -29,15 +29,13 @@ module EE
expose :pipeline, if: ->(package) { package.build_info }, using: Package::Pipeline expose :pipeline, if: ->(package) { package.build_info }, using: Package::Pipeline
expose :versions, using: ::EE::API::Entities::PackageVersion
private private
def project_path def project_path
object.project.full_path object.project.full_path
end end
def pipeline
object.build_info.pipeline
end
end end
end end
end end
......
# frozen_string_literal: true
module EE
module API
module Entities
class PackageVersion < Grape::Entity
expose :id
expose :version
expose :created_at
expose :tags
expose :pipeline, if: ->(package) { package.build_info }, using: Package::Pipeline
end
end
end
end
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
"name", "name",
"version", "version",
"package_type", "package_type",
"_links" "_links",
"versions"
], ],
"properties": { "properties": {
"name": { "name": {
...@@ -29,6 +30,12 @@ ...@@ -29,6 +30,12 @@
}, },
"created_at": { "created_at": {
"type": "string" "type": "string"
},
"versions": {
"type": "array",
"items": {
"$ref": "package_version.json"
}
} }
} }
} }
{
"type": "object",
"required": [
"version",
"created_at",
"pipeline"
],
"properties": {
"version": {
"type": "string"
},
"created_at": {
"type": "string"
},
"pipeline": {
"$ref": "../pipeline.json"
}
}
}
...@@ -384,4 +384,41 @@ RSpec.describe Packages::Package, type: :model do ...@@ -384,4 +384,41 @@ RSpec.describe Packages::Package, type: :model do
expect(packages.pluck(:name)).to match_array([nuget_package.name, maven_package.name]) expect(packages.pluck(:name)).to match_array([nuget_package.name, maven_package.name])
end end
end end
describe '#versions' do
let_it_be(:project) { create(:project) }
let_it_be(:package) { create(:maven_package, project: project) }
let_it_be(:package2) { create(:maven_package, project: project) }
let_it_be(:package3) { create(:maven_package, project: project, name: 'foo') }
it 'returns other package versions of the same package name belonging to the project' do
expect(package.versions).to contain_exactly(package2)
end
it 'does not return different packages' do
expect(package.versions).not_to include(package3)
end
end
describe '#pipeline' do
let_it_be(:package) { create(:maven_package) }
context 'package without pipeline' do
it 'returns nil if there is no pipeline' do
expect(package.pipeline).to be_nil
end
end
context 'package with pipeline' do
let_it_be(:pipeline) { create(:ci_pipeline) }
before do
package.create_build_info!(pipeline: pipeline)
end
it 'returns the pipeline' do
expect(package.pipeline).to eq(pipeline)
end
end
end
end end
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