Commit f7f4c7e1 authored by Patrick Bajao's avatar Patrick Bajao

Add ProjectAliases API endpoints

This endpoints are only accessible by an admin.
parent 203fe05a
......@@ -76,6 +76,8 @@ module EE
has_many :operations_feature_flags, class_name: 'Operations::FeatureFlag'
has_one :operations_feature_flags_client, class_name: 'Operations::FeatureFlagsClient'
has_many :project_aliases
scope :with_shared_runners_limit_enabled, -> { with_shared_runners.non_public_only }
scope :mirror, -> { where(mirror: true) }
......
# frozen_string_literal: true
module API
class ProjectAliases < Grape::API
before { authenticated_as_admin! }
helpers do
def project_alias
ProjectAlias.find_by_name!(params[:name])
end
def project
find_project!(params[:project_id])
end
end
resource :project_aliases do
desc 'Get a list of all project aliases' do
success EE::API::Entities::ProjectAlias
end
get do
present ProjectAlias.all, with: EE::API::Entities::ProjectAlias
end
desc 'Get info of specific project alias by name' do
success EE::API::Entities::ProjectAlias
end
get ':name' do
present project_alias, with: EE::API::Entities::ProjectAlias
end
desc 'Create a project alias'
params do
requires :project_id, type: String, desc: 'The ID or URL-encoded path of the project'
requires :name, type: String, desc: 'The alias of the project'
end
post do
project_alias = project.project_aliases.create(name: params[:name])
if project_alias.valid?
present project_alias, with: EE::API::Entities::ProjectAlias
else
render_validation_error!(project_alias)
end
end
desc 'Delete a project alias by name'
delete ':name' do
project_alias.destroy
status 204
end
end
end
end
......@@ -31,6 +31,7 @@ module EE
mount ::API::ProjectApprovals
mount ::API::Vulnerabilities
mount ::API::MergeRequestApprovals
mount ::API::ProjectAliases
version 'v3', using: :path do
# Although the following endpoints are kept behind V3 namespace,
......
......@@ -729,6 +729,10 @@ module EE
class ManagedLicense < Grape::Entity
expose :id, :name, :approval_status
end
class ProjectAlias < Grape::Entity
expose :id, :project_id, :name
end
end
end
end
{
"type": "object",
"allOf": [
{
"required" : [
"id",
"project_id",
"name"
],
"properties": {
"id": { "type": "integer" },
"project_id": { "type": "integer" },
"name": { "type": "string" }
}
}
]
}
{
"type": "array",
"items": { "$ref": "./project_alias.json" }
}
......@@ -34,6 +34,7 @@ describe Project do
it { is_expected.to have_many(:package_files).class_name('Packages::PackageFile') }
it { is_expected.to have_one(:github_service) }
it { is_expected.to have_many(:project_aliases) }
end
context 'scopes' do
......
require 'spec_helper'
describe API::ProjectAliases, api: true do
set(:user) { create(:user) }
set(:admin) { create(:admin) }
describe 'GET /project_aliases' do
context 'anonymous user' do
before do
get api('/project_aliases')
end
it 'returns 401' do
expect(response).to have_gitlab_http_status(401)
end
end
context 'regular user' do
before do
get api('/project_aliases', user)
end
it 'returns 403' do
expect(response).to have_gitlab_http_status(403)
end
end
context 'admin' do
let!(:project_alias_1) { create(:project_alias) }
let!(:project_alias_2) { create(:project_alias) }
before do
get api('/project_aliases', admin)
end
it 'returns the project aliases list' do
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('public_api/v4/project_aliases', dir: 'ee')
end
end
end
describe 'GET /project_aliases/:name' do
let(:project_alias) { create(:project_alias) }
context 'anonymous user' do
before do
get api("/project_aliases/#{project_alias.name}")
end
it 'returns 401' do
expect(response).to have_gitlab_http_status(401)
end
end
context 'regular user' do
before do
get api("/project_aliases/#{project_alias.name}", user)
end
it 'returns 403' do
expect(response).to have_gitlab_http_status(403)
end
end
context 'admin' do
context 'existing project alias' do
before do
get api("/project_aliases/#{project_alias.name}", admin)
end
it 'returns the project alias' do
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('public_api/v4/project_alias', dir: 'ee')
end
end
context 'non-existent project alias' do
before do
get api("/project_aliases/some-project", admin)
end
it 'returns 404' do
expect(response).to have_gitlab_http_status(404)
end
end
end
end
describe 'POST /project_aliases' do
let(:project) { create(:project) }
context 'anonymous user' do
before do
post api("/project_aliases")
end
it 'returns 401' do
expect(response).to have_gitlab_http_status(401)
end
end
context 'regular user' do
before do
post api("/project_aliases", user)
end
it 'returns 403' do
expect(response).to have_gitlab_http_status(403)
end
end
context 'admin' do
context 'existing project alias' do
let(:project_alias) { create(:project_alias) }
before do
post api("/project_aliases", admin), params: { project_id: project.id, name: project_alias.name }
end
it 'returns 400' do
expect(response).to have_gitlab_http_status(400)
end
end
context 'non-existent project alias' do
before do
post api("/project_aliases", admin), params: { project_id: project.id, name: 'some-project' }
end
it 'returns 200' do
expect(response).to have_gitlab_http_status(201)
expect(response).to match_response_schema('public_api/v4/project_alias', dir: 'ee')
end
end
end
end
describe 'DELETE /project_aliases/:name' do
let(:project_alias) { create(:project_alias) }
context 'anonymous user' do
before do
delete api("/project_aliases/#{project_alias.name}")
end
it 'returns 401' do
expect(response).to have_gitlab_http_status(401)
end
end
context 'regular user' do
before do
delete api("/project_aliases/#{project_alias.name}", user)
end
it 'returns 403' do
expect(response).to have_gitlab_http_status(403)
end
end
context 'admin' do
context 'existing project alias' do
before do
delete api("/project_aliases/#{project_alias.name}", admin)
end
it 'returns 204' do
expect(response).to have_gitlab_http_status(204)
end
end
context 'non-existent project alias' do
before do
delete api("/project_aliases/some-project", admin)
end
it 'returns 404' do
expect(response).to have_gitlab_http_status(404)
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