Commit 203fe05a authored by Patrick Bajao's avatar Patrick Bajao

Allow finding project by alias

This uses the ProjectAlias model. If an alias matches
the project_name specified, the associated project
will then be returned.
parent d499d501
# frozen_string_literal: true
class ProjectAlias < ApplicationRecord
belongs_to :project
validates :project, presence: true
validates :name, presence: true, uniqueness: true
end
# frozen_string_literal: true
module EE
module Gitlab
module RepoPath
module ClassMethods
def find_project(project_path)
if project_alias = ProjectAlias.find_by_name(project_path)
[project_alias.project, false]
else
super(project_path)
end
end
end
end
end
end
FactoryBot.define do
factory :project_alias do
project
name { FFaker::Name.unique.name.parameterize }
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::RepoPath do
describe '.find_project' do
let(:project) { create(:project) }
context 'project_path matches a project alias' do
let(:project_alias) { create(:project_alias, project: project) }
it 'returns the project' do
expect(described_class.find_project(project_alias.name)).to eq([project, false])
end
end
context 'project_path does not match a project alias' do
context 'project path matches project full path' do
it 'returns the project' do
expect(described_class.find_project(project.full_path)).to eq([project, false])
end
end
context 'project path does not match an existing project full path' do
it 'returns nil' do
expect(described_class.find_project('some-project')).to eq([nil, nil])
end
end
end
end
end
require 'spec_helper'
describe ProjectAlias do
subject { build(:project_alias) }
it { is_expected.to belong_to(:project) }
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_uniqueness_of(:name) }
end
......@@ -38,3 +38,5 @@ module Gitlab
end
end
end
Gitlab::RepoPath.singleton_class.prepend(EE::Gitlab::RepoPath::ClassMethods)
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