Commit f7bd0d2e authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '8681-api-endpoint-for-package-repositories' into 'master'

Resolve "API endpoint for Package repositories"

See merge request gitlab-org/gitlab-ee!9259
parents b403ff3b 49f4c684
......@@ -48,6 +48,7 @@ The following API resources are available:
- [Namespaces](namespaces.md)
- [Notes](notes.md) (comments)
- [Notification settings](notification_settings.md)
- [Packages](packages.md) **[PREMIUM]**
- [Pages domains](pages_domains.md)
- [Pipelines](pipelines.md)
- [Pipeline schedules](pipeline_schedules.md)
......
# Packages API **[PREMIUM]**
This is the API docs of [GitLab Packages](../administration/packages.md).
## List project packages
> [Introduced](https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/9259) in GitLab 11.8.
Get a list of project packages. Both Maven and NPM packages are included in results.
When accessed without authentication, only packages of public projects are returned.
```
GET /projects/:id/packages
```
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
| `id` | integer/string | yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user. |
```bash
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/projects/:id/packages
```
Example response:
```json
[
{
"id": 1,
"name": "com/mycompany/my-app",
"version": "1.0-SNAPSHOT",
"package_type": "maven"
},
{
"id": 2,
"name": "@foo/bar",
"version": "1.0.3",
"package_type": "npm"
}
]
```
By default, the `GET` request will return 20 results, since the API is [paginated](README.md#pagination).
---
title: Add API endpoint for project packages
merge_request: 9259
author:
type: added
......@@ -14,6 +14,7 @@ module API
def authorize_download_package!
authorize!(:read_package, user_project)
end
alias_method :authorize_read_package!, :authorize_download_package!
def authorize_create_package!
authorize!(:create_package, user_project)
......
# frozen_string_literal: true
module API
class Packages < Grape::API
include PaginationParams
before do
require_packages_enabled!
authorize_packages_feature!
authorize_read_package!
end
helpers ::API::Helpers::PackagesHelpers
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Get all project packages' do
detail 'This feature was introduced in GitLab 11.8'
success EE::API::Entities::Package
end
params do
use :pagination
end
get ':id/packages' do
packages = user_project.packages
present paginate(packages), with: EE::API::Entities::Package
end
end
end
end
......@@ -22,6 +22,7 @@ module EE
mount ::API::ProjectPushRule
mount ::API::MavenPackages
mount ::API::NpmPackages
mount ::API::Packages
end
end
end
......
......@@ -484,6 +484,13 @@ module EE
expose :name
expose :versions
end
class Package < Grape::Entity
expose :id
expose :name
expose :version
expose :package_type
end
end
end
end
{
"type": "array",
"items": {
"type": "object",
"required" : ["name", "version", "packages_type"],
"properties" : {
"name": { "type": "string" },
"version": { "type": "string" },
"packages_type": { "type": "string" }
}
}
}
# frozen_string_literal: true
require 'spec_helper'
describe API::Packages do
let(:user) { create(:user) }
let(:project) { create(:project, :public) }
let(:package) { create(:npm_package, project: project) }
before do
project.add_developer(user)
end
describe 'GET /projects/:id/packages' do
let(:url) { "/projects/#{project.id}/packages" }
context 'packages feature enabled' do
before do
stub_licensed_features(packages: true)
end
context 'project is public' do
it 'returns 200' do
get api(url)
expect(response).to have_gitlab_http_status(200)
end
end
context 'project is private' do
let(:project) { create(:project, :private) }
it 'returns 404 for non authenticated user' do
get api(url)
expect(response).to have_gitlab_http_status(404)
end
it 'returns 404 for a user without access to the project' do
project.team.truncate
get api(url, user)
expect(response).to have_gitlab_http_status(404)
end
it 'returns 200 and valid response schema' do
get api(url, user)
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('public_api/v4/packages/packages', dir: 'ee')
end
end
context 'with pagination params' do
let(:per_page) { 2 }
let!(:package1) { create(:npm_package, project: project) }
let!(:package2) { create(:npm_package, project: project) }
let!(:package3) { create(:maven_package, project: project) }
before do
stub_licensed_features(packages: true)
end
context 'when viewing the first page' do
it 'returns first 2 packages' do
get api(url, user), params: { page: 1, per_page: per_page }
expect_paginated_array_response([package1.id, package2.id])
end
end
context 'viewing the second page' do
it 'returns the last package' do
get api(url, user), params: { page: 2, per_page: per_page }
expect_paginated_array_response([package3.id])
end
end
end
end
context 'packages feature disabled' do
before do
stub_licensed_features(packages: false)
end
it 'returns 403' do
get api(url, user)
expect(response).to have_gitlab_http_status(403)
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