2017-05-31 09:43:19 -04:00
|
|
|
require_relative '../support/test_env'
|
|
|
|
|
2014-07-31 13:13:27 -04:00
|
|
|
FactoryGirl.define do
|
2014-11-30 03:50:15 -05:00
|
|
|
# Project without repository
|
|
|
|
#
|
|
|
|
# Project does not have bare repository.
|
2016-07-05 18:24:35 -04:00
|
|
|
# Use this factory if you don't need repository in tests
|
2014-07-31 13:13:27 -04:00
|
|
|
factory :empty_project, class: 'Project' do
|
|
|
|
sequence(:name) { |n| "project#{n}" }
|
|
|
|
path { name.downcase.gsub(/\s/, '_') }
|
|
|
|
namespace
|
|
|
|
creator
|
|
|
|
|
2016-10-06 17:17:11 -04:00
|
|
|
# Behaves differently to nil due to cache_has_external_issue_tracker
|
|
|
|
has_external_issue_tracker false
|
|
|
|
|
2014-07-31 13:13:27 -04:00
|
|
|
trait :public do
|
|
|
|
visibility_level Gitlab::VisibilityLevel::PUBLIC
|
|
|
|
end
|
|
|
|
|
|
|
|
trait :internal do
|
|
|
|
visibility_level Gitlab::VisibilityLevel::INTERNAL
|
|
|
|
end
|
|
|
|
|
|
|
|
trait :private do
|
|
|
|
visibility_level Gitlab::VisibilityLevel::PRIVATE
|
|
|
|
end
|
2016-05-04 17:05:16 -04:00
|
|
|
|
2017-06-01 10:27:35 -04:00
|
|
|
trait :import_scheduled do
|
|
|
|
import_status :scheduled
|
|
|
|
end
|
|
|
|
|
|
|
|
trait :import_started do
|
|
|
|
import_status :started
|
|
|
|
end
|
|
|
|
|
|
|
|
trait :import_finished do
|
|
|
|
import_status :finished
|
|
|
|
end
|
|
|
|
|
|
|
|
trait :import_failed do
|
|
|
|
import_status :failed
|
|
|
|
end
|
|
|
|
|
2017-01-04 03:30:11 -05:00
|
|
|
trait :archived do
|
|
|
|
archived true
|
|
|
|
end
|
|
|
|
|
2016-11-11 07:51:50 -05:00
|
|
|
trait :access_requestable do
|
|
|
|
request_access_enabled true
|
|
|
|
end
|
|
|
|
|
2017-05-01 16:15:16 -04:00
|
|
|
trait :with_avatar do
|
|
|
|
avatar { File.open(Rails.root.join('spec/fixtures/dk.png')) }
|
|
|
|
end
|
|
|
|
|
2017-01-16 23:06:18 -05:00
|
|
|
trait :repository do
|
|
|
|
# no-op... for now!
|
|
|
|
end
|
|
|
|
|
2016-05-04 17:05:16 -04:00
|
|
|
trait :empty_repo do
|
|
|
|
after(:create) do |project|
|
2017-03-08 12:04:44 -05:00
|
|
|
raise "Failed to create repository!" unless project.create_repository
|
2017-03-02 13:55:30 -05:00
|
|
|
|
|
|
|
# We delete hooks so that gitlab-shell will not try to authenticate with
|
|
|
|
# an API that isn't running
|
|
|
|
FileUtils.rm_r(File.join(project.repository_storage_path, "#{project.path_with_namespace}.git", 'hooks'))
|
2016-05-04 17:05:16 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-01 18:31:21 -04:00
|
|
|
|
2016-09-23 03:42:07 -04:00
|
|
|
trait :broken_repo do
|
|
|
|
after(:create) do |project|
|
2017-03-08 12:04:44 -05:00
|
|
|
raise "Failed to create repository!" unless project.create_repository
|
2016-09-23 03:42:07 -04:00
|
|
|
|
|
|
|
FileUtils.rm_r(File.join(project.repository_storage_path, "#{project.path_with_namespace}.git", 'refs'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-14 19:59:04 -05:00
|
|
|
trait :test_repo do
|
|
|
|
after :create do |project|
|
2017-05-10 11:31:50 -04:00
|
|
|
TestEnv.copy_repo(project,
|
|
|
|
bare_repo: TestEnv.factory_repo_path_bare,
|
|
|
|
refs: TestEnv::BRANCH_SHA)
|
2016-12-14 19:59:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-02 05:10:25 -05:00
|
|
|
trait(:wiki_enabled) { wiki_access_level ProjectFeature::ENABLED }
|
|
|
|
trait(:wiki_disabled) { wiki_access_level ProjectFeature::DISABLED }
|
|
|
|
trait(:wiki_private) { wiki_access_level ProjectFeature::PRIVATE }
|
|
|
|
trait(:builds_enabled) { builds_access_level ProjectFeature::ENABLED }
|
|
|
|
trait(:builds_disabled) { builds_access_level ProjectFeature::DISABLED }
|
|
|
|
trait(:builds_private) { builds_access_level ProjectFeature::PRIVATE }
|
|
|
|
trait(:snippets_enabled) { snippets_access_level ProjectFeature::ENABLED }
|
|
|
|
trait(:snippets_disabled) { snippets_access_level ProjectFeature::DISABLED }
|
|
|
|
trait(:snippets_private) { snippets_access_level ProjectFeature::PRIVATE }
|
|
|
|
trait(:issues_disabled) { issues_access_level ProjectFeature::DISABLED }
|
|
|
|
trait(:issues_enabled) { issues_access_level ProjectFeature::ENABLED }
|
|
|
|
trait(:issues_private) { issues_access_level ProjectFeature::PRIVATE }
|
|
|
|
trait(:merge_requests_enabled) { merge_requests_access_level ProjectFeature::ENABLED }
|
|
|
|
trait(:merge_requests_disabled) { merge_requests_access_level ProjectFeature::DISABLED }
|
|
|
|
trait(:merge_requests_private) { merge_requests_access_level ProjectFeature::PRIVATE }
|
|
|
|
trait(:repository_enabled) { repository_access_level ProjectFeature::ENABLED }
|
|
|
|
trait(:repository_disabled) { repository_access_level ProjectFeature::DISABLED }
|
|
|
|
trait(:repository_private) { repository_access_level ProjectFeature::PRIVATE }
|
|
|
|
|
2016-08-01 18:31:21 -04:00
|
|
|
# Nest Project Feature attributes
|
|
|
|
transient do
|
|
|
|
wiki_access_level ProjectFeature::ENABLED
|
|
|
|
builds_access_level ProjectFeature::ENABLED
|
|
|
|
snippets_access_level ProjectFeature::ENABLED
|
|
|
|
issues_access_level ProjectFeature::ENABLED
|
|
|
|
merge_requests_access_level ProjectFeature::ENABLED
|
2016-09-16 15:15:39 -04:00
|
|
|
repository_access_level ProjectFeature::ENABLED
|
2016-08-01 18:31:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after(:create) do |project, evaluator|
|
2016-11-02 17:50:44 -04:00
|
|
|
# Builds and MRs can't have higher visibility level than repository access level.
|
|
|
|
builds_access_level = [evaluator.builds_access_level, evaluator.repository_access_level].min
|
|
|
|
merge_requests_access_level = [evaluator.merge_requests_access_level, evaluator.repository_access_level].min
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
project.project_feature
|
|
|
|
.update_attributes!(
|
2016-08-01 18:31:21 -04:00
|
|
|
wiki_access_level: evaluator.wiki_access_level,
|
2016-11-02 17:50:44 -04:00
|
|
|
builds_access_level: builds_access_level,
|
2016-08-01 18:31:21 -04:00
|
|
|
snippets_access_level: evaluator.snippets_access_level,
|
|
|
|
issues_access_level: evaluator.issues_access_level,
|
2016-11-02 17:50:44 -04:00
|
|
|
merge_requests_access_level: merge_requests_access_level,
|
2016-09-16 15:15:39 -04:00
|
|
|
repository_access_level: evaluator.repository_access_level
|
2016-08-01 18:31:21 -04:00
|
|
|
)
|
Use CTEs for nested groups and authorizations
This commit introduces the usage of Common Table Expressions (CTEs) to
efficiently retrieve nested group hierarchies, without having to rely on
the "routes" table (which is an _incredibly_ inefficient way of getting
the data). This requires a patch to ActiveRecord (found in the added
initializer) to work properly as ActiveRecord doesn't support WITH
statements properly out of the box.
Unfortunately MySQL provides no efficient way of getting nested groups.
For example, the old routes setup could easily take 5-10 seconds
depending on the amount of "routes" in a database. Providing vastly
different logic for both MySQL and PostgreSQL will negatively impact the
development process. Because of this the various nested groups related
methods return empty relations when used in combination with MySQL.
For project authorizations the logic is split up into two classes:
* Gitlab::ProjectAuthorizations::WithNestedGroups
* Gitlab::ProjectAuthorizations::WithoutNestedGroups
Both classes get the fresh project authorizations (= as they should be
in the "project_authorizations" table), including nested groups if
PostgreSQL is used. The logic of these two classes is quite different
apart from their public interface. This complicates development a bit,
but unfortunately there is no way around this.
This commit also introduces Gitlab::GroupHierarchy. This class can be
used to get the ancestors and descendants of a base relation, or both by
using a UNION. This in turn is used by methods such as:
* Namespace#ancestors
* Namespace#descendants
* User#all_expanded_groups
Again this class relies on CTEs and thus only works on PostgreSQL. The
Namespace methods will return an empty relation when MySQL is used,
while User#all_expanded_groups will return only the groups a user is a
direct member of.
Performance wise the impact is quite large. For example, on GitLab.com
Namespace#descendants used to take around 580 ms to retrieve data for a
particular user. Using CTEs we are able to reduce this down to roughly 1
millisecond, returning the exact same data.
== On The Fly Refreshing
Refreshing of authorizations on the fly (= when
users.authorized_projects_populated was not set) is removed with this
commit. This simplifies the code, and ensures any queries used for
authorizations are not mutated because they are executed in a Rails
scope (e.g. Project.visible_to_user).
This commit includes a migration to schedule refreshing authorizations
for all users, ensuring all of them have their authorizations in place.
Said migration schedules users in batches of 5000, with 5 minutes
between every batch to smear the load around a bit.
== Spec Changes
This commit also introduces some changes to various specs. For example,
some specs for ProjectTeam assumed that creating a personal project
would _not_ lead to the owner having access, which is incorrect. Because
we also no longer refresh authorizations on the fly for new users some
code had to be added to the "empty_project" factory. This chunk of code
ensures that the owner's permissions are refreshed after creating the
project, something that is normally done in Projects::CreateService.
2017-04-24 11:19:22 -04:00
|
|
|
|
|
|
|
# Normally the class Projects::CreateService is used for creating
|
|
|
|
# projects, and this class takes care of making sure the owner and current
|
|
|
|
# user have access to the project. Our specs don't use said service class,
|
|
|
|
# thus we must manually refresh things here.
|
|
|
|
owner = project.owner
|
|
|
|
|
|
|
|
if owner && owner.is_a?(User) && !project.pending_delete
|
|
|
|
project.members.create!(user: owner, access_level: Gitlab::Access::MASTER)
|
|
|
|
end
|
|
|
|
|
|
|
|
project.group&.refresh_members_authorized_projects
|
2016-08-01 18:31:21 -04:00
|
|
|
end
|
2014-07-31 13:13:27 -04:00
|
|
|
end
|
|
|
|
|
2014-11-30 03:50:15 -05:00
|
|
|
# Project with empty repository
|
|
|
|
#
|
|
|
|
# This is a case when you just created a project
|
|
|
|
# but not pushed any code there yet
|
|
|
|
factory :project_empty_repo, parent: :empty_project do
|
2016-05-04 17:05:16 -04:00
|
|
|
empty_repo
|
2014-11-30 03:50:15 -05:00
|
|
|
end
|
|
|
|
|
2016-09-23 03:42:07 -04:00
|
|
|
# Project with broken repository
|
|
|
|
#
|
|
|
|
# Project with an invalid repository state
|
|
|
|
factory :project_broken_repo, parent: :empty_project do
|
|
|
|
broken_repo
|
|
|
|
end
|
|
|
|
|
2014-11-30 03:50:15 -05:00
|
|
|
# Project with test repository
|
|
|
|
#
|
|
|
|
# Test repository source can be found at
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab-test
|
2014-07-31 13:13:27 -04:00
|
|
|
factory :project, parent: :empty_project do
|
|
|
|
path { 'gitlabhq' }
|
|
|
|
|
2016-12-14 19:59:04 -05:00
|
|
|
test_repo
|
2017-01-04 09:25:55 -05:00
|
|
|
|
2016-12-08 06:11:52 -05:00
|
|
|
transient do
|
|
|
|
create_template nil
|
|
|
|
end
|
|
|
|
|
|
|
|
after :create do |project, evaluator|
|
|
|
|
if evaluator.create_template
|
|
|
|
args = evaluator.create_template
|
|
|
|
|
|
|
|
project.add_user(args[:user], args[:access])
|
|
|
|
|
2017-02-15 18:28:29 -05:00
|
|
|
project.repository.create_file(
|
2016-12-08 06:11:52 -05:00
|
|
|
args[:user],
|
|
|
|
".gitlab/#{args[:path]}/bug.md",
|
|
|
|
'something valid',
|
|
|
|
message: 'test 3',
|
2017-02-15 18:28:29 -05:00
|
|
|
branch_name: 'master')
|
|
|
|
project.repository.create_file(
|
2016-12-08 06:11:52 -05:00
|
|
|
args[:user],
|
|
|
|
".gitlab/#{args[:path]}/template_test.md",
|
|
|
|
'template_test',
|
|
|
|
message: 'test 1',
|
2017-02-15 18:28:29 -05:00
|
|
|
branch_name: 'master')
|
|
|
|
project.repository.create_file(
|
2016-12-08 06:11:52 -05:00
|
|
|
args[:user],
|
|
|
|
".gitlab/#{args[:path]}/feature_proposal.md",
|
|
|
|
'feature_proposal',
|
|
|
|
message: 'test 2',
|
2017-02-15 18:28:29 -05:00
|
|
|
branch_name: 'master')
|
2016-12-08 06:11:52 -05:00
|
|
|
end
|
2014-07-31 13:13:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-13 01:27:45 -04:00
|
|
|
factory :forked_project_with_submodules, parent: :empty_project do
|
|
|
|
path { 'forked-gitlabhq' }
|
|
|
|
|
|
|
|
after :create do |project|
|
2017-05-10 11:31:50 -04:00
|
|
|
TestEnv.copy_repo(project,
|
|
|
|
bare_repo: TestEnv.forked_repo_path_bare,
|
|
|
|
refs: TestEnv::FORKED_BRANCH_SHA)
|
2015-04-13 01:27:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-31 13:13:27 -04:00
|
|
|
factory :redmine_project, parent: :project do
|
2016-10-06 17:17:11 -04:00
|
|
|
has_external_issue_tracker true
|
|
|
|
|
2015-01-26 18:59:40 -05:00
|
|
|
after :create do |project|
|
|
|
|
project.create_redmine_service(
|
|
|
|
active: true,
|
|
|
|
properties: {
|
2015-01-27 01:08:27 -05:00
|
|
|
'project_url' => 'http://redmine/projects/project_name_in_redmine',
|
2017-06-30 08:47:53 -04:00
|
|
|
'issues_url' => 'http://redmine/projects/project_name_in_redmine/issues/:id',
|
2015-01-27 01:08:27 -05:00
|
|
|
'new_issue_url' => 'http://redmine/projects/project_name_in_redmine/issues/new'
|
2015-01-26 18:59:40 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2014-07-31 13:13:27 -04:00
|
|
|
end
|
2015-04-29 14:28:24 -04:00
|
|
|
|
|
|
|
factory :jira_project, parent: :project do
|
2016-10-06 17:17:11 -04:00
|
|
|
has_external_issue_tracker true
|
2017-03-06 09:18:59 -05:00
|
|
|
jira_service
|
2015-04-29 14:28:24 -04:00
|
|
|
end
|
2016-12-08 11:36:26 -05:00
|
|
|
|
|
|
|
factory :kubernetes_project, parent: :empty_project do
|
2017-03-06 09:18:59 -05:00
|
|
|
kubernetes_service
|
2016-12-08 11:36:26 -05:00
|
|
|
end
|
2017-03-07 11:57:42 -05:00
|
|
|
|
|
|
|
factory :prometheus_project, parent: :empty_project do
|
|
|
|
after :create do |project|
|
|
|
|
project.create_prometheus_service(
|
|
|
|
active: true,
|
|
|
|
properties: {
|
|
|
|
api_url: 'https://prometheus.example.com'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2014-07-31 13:13:27 -04:00
|
|
|
end
|