2020-10-30 18:08:56 +00:00
---
stage: none
group: unassigned
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers
---
2017-12-06 15:30:03 +00:00
# Guidelines for implementing Enterprise Edition features
2017-10-27 14:29:36 +00:00
- **Write the code and the tests.**: As with any code, EE features should have
good test coverage to prevent regressions.
- **Write documentation.**: Add documentation to the `doc/` directory. Describe
2020-11-12 21:09:19 +00:00
the feature and include screenshots, if applicable. Indicate [what editions ](documentation/styleguide/index.md#product-badges )
2020-05-05 18:09:43 +00:00
the feature applies to.
2017-10-27 14:29:36 +00:00
- **Submit a MR to the `www-gitlab-com` project.**: Add the new feature to the
2018-06-30 08:19:10 +00:00
[EE features list ](https://about.gitlab.com/features/ ).
2017-10-27 14:29:36 +00:00
## Act as CE when unlicensed
2020-04-15 15:09:17 +00:00
Since the implementation of
2020-05-21 06:08:25 +00:00
[GitLab CE features to work with unlicensed EE instance ](https://gitlab.com/gitlab-org/gitlab/-/issues/2500 )
2017-10-27 14:29:36 +00:00
GitLab Enterprise Edition should work like GitLab Community Edition
when no license is active. So EE features always should be guarded by
`project.feature_available?` or `group.feature_available?` (or
`License.feature_available?` if it is a system-wide feature).
CE specs should remain untouched as much as possible and extra specs
should be added for EE. Licensed features can be stubbed using the
spec helper `stub_licensed_features` in `EE::LicenseHelpers` .
2019-09-20 09:06:17 +00:00
You can force GitLab to act as CE by either deleting the `ee/` directory or by
2019-10-16 18:08:01 +00:00
setting the [`FOSS_ONLY` environment variable ](https://gitlab.com/gitlab-org/gitlab/blob/master/config/helpers/is_ee_env.js )
to something that evaluates as `true` . The same works for running tests
(for example `FOSS_ONLY=1 yarn jest` ).
2019-04-08 12:06:08 +00:00
2020-09-15 06:09:32 +00:00
## CI pipelines in a FOSS context
By default, merge request pipelines for development run in an EE-context only. If you are
developing features that differ between FOSS and EE, you may wish to run pipelines in a
FOSS context as well.
To run pipelines in both contexts, include `RUN AS-IF-FOSS` in the merge request title.
See the [As-if-FOSS jobs ](pipelines.md#as-if-foss-jobs ) pipelines documentation for more information.
2017-10-27 14:29:36 +00:00
## Separation of EE code
2019-09-26 15:06:18 +00:00
All EE code should be put inside the `ee/` top-level directory. The
2018-02-13 14:41:47 +00:00
rest of the code should be as close to the CE files as possible.
2017-10-27 14:29:36 +00:00
### EE-only features
If the feature being developed is not present in any form in CE, we don't
2019-09-26 15:06:18 +00:00
need to put the code under the `EE` namespace. For example, an EE model could
2017-10-27 14:29:36 +00:00
go into: `ee/app/models/awesome.rb` using `Awesome` as the class name. This
is applied not only to models. Here's a list of other examples:
- `ee/app/controllers/foos_controller.rb`
- `ee/app/finders/foos_finder.rb`
- `ee/app/helpers/foos_helper.rb`
- `ee/app/mailers/foos_mailer.rb`
- `ee/app/models/foo.rb`
- `ee/app/policies/foo_policy.rb`
- `ee/app/serializers/foo_entity.rb`
- `ee/app/serializers/foo_serializer.rb`
- `ee/app/services/foo/create_service.rb`
- `ee/app/validators/foo_attr_validator.rb`
- `ee/app/workers/foo_worker.rb`
2018-05-23 12:56:11 +00:00
- `ee/app/views/foo.html.haml`
- `ee/app/views/foo/_bar.html.haml`
2017-10-27 14:29:36 +00:00
2019-09-26 15:06:18 +00:00
This works because for every path that is present in CE's eager-load/auto-load
2020-04-15 15:09:17 +00:00
paths, we add the same `ee/` -prepended path in [`config/application.rb` ](https://gitlab.com/gitlab-org/gitlab/blob/925d3d4ebc7a2c72964ce97623ae41b8af12538d/config/application.rb#L42-52 ).
2018-05-23 12:56:11 +00:00
This also applies to views.
2018-03-01 10:16:06 +00:00
2020-08-13 12:09:50 +00:00
#### Testing EE-only features
To test an EE class that doesn't exist in CE, create the spec file as you normally
would in the `ee/spec` directory, but without the second `ee/` subdirectory.
For example, a class `ee/app/models/vulnerability.rb` would have its tests in `ee/spec/models/vulnerability_spec.rb` .
2017-10-27 14:29:36 +00:00
### EE features based on CE features
2018-11-13 12:33:02 +00:00
For features that build on existing CE features, write a module in the `EE`
2019-07-30 13:00:53 +00:00
namespace and inject it in the CE class, on the last line of the file that the
class resides in. This makes conflicts less likely to happen during CE to EE
merges because only one line is added to the CE class - the line that injects
the module. For example, to prepend a module into the `User` class you would use
the following approach:
2018-11-13 12:33:02 +00:00
```ruby
class User < ActiveRecord::Base
# ... lots of code here ...
end
2019-07-30 13:00:53 +00:00
User.prepend_if_ee('EE::User')
2018-11-13 12:33:02 +00:00
```
2017-10-27 14:29:36 +00:00
2019-07-30 13:00:53 +00:00
Do not use methods such as `prepend` , `extend` , and `include` . Instead, use
`prepend_if_ee` , `extend_if_ee` , or `include_if_ee` . These methods take a
_String_ containing the full module name as the argument, not the module itself.
2017-10-27 14:29:36 +00:00
Since the module would require an `EE` namespace, the file should also be
put in an `ee/` sub-directory. For example, we want to extend the user model
in EE, so we have a module called `::EE::User` put inside
`ee/app/models/ee/user.rb` .
This is also not just applied to models. Here's a list of other examples:
- `ee/app/controllers/ee/foos_controller.rb`
- `ee/app/finders/ee/foos_finder.rb`
- `ee/app/helpers/ee/foos_helper.rb`
- `ee/app/mailers/ee/foos_mailer.rb`
- `ee/app/models/ee/foo.rb`
- `ee/app/policies/ee/foo_policy.rb`
- `ee/app/serializers/ee/foo_entity.rb`
- `ee/app/serializers/ee/foo_serializer.rb`
- `ee/app/services/ee/foo/create_service.rb`
- `ee/app/validators/ee/foo_attr_validator.rb`
- `ee/app/workers/ee/foo_worker.rb`
2020-08-13 12:09:50 +00:00
#### Testing EE features based on CE features
To test an `EE` namespaced module that extends a CE class with EE features,
create the spec file as you normally would in the `ee/spec` directory, including the second `ee/` subdirectory.
For example, an extension `ee/app/models/ee/user.rb` would have its tests in `ee/app/models/ee/user_spec.rb` .
In the `RSpec.describe` call, use the CE class name where the EE module would be used.
For example, in `ee/app/models/ee/user_spec.rb` , the test would start with:
```ruby
RSpec.describe User do
describe 'ee feature added through extension'
end
```
2017-10-27 14:29:36 +00:00
#### Overriding CE methods
To override a method present in the CE codebase, use `prepend` . It
lets you override a method in a class with a method from a module, while
still having access the class's implementation with `super` .
There are a few gotchas with it:
2019-09-18 18:06:14 +00:00
- you should always [`extend ::Gitlab::Utils::Override` ](utilities.md#override ) and use `override` to
2017-12-25 10:20:50 +00:00
guard the "overrider" method to ensure that if the method gets renamed in
CE, the EE override won't be silently forgotten.
2017-10-27 14:29:36 +00:00
- when the "overrider" would add a line in the middle of the CE
implementation, you should refactor the CE method and split it in
smaller methods. Or create a "hook" method that is empty in CE,
and with the EE-specific implementation in EE.
- when the original implementation contains a guard clause (e.g.
2020-04-22 18:09:52 +00:00
`return unless condition` ), we cannot easily extend the behavior by
2017-10-27 14:29:36 +00:00
overriding the method, because we can't know when the overridden method
(i.e. calling `super` in the overriding method) would want to stop early.
In this case, we shouldn't just override it, but update the original method
to make it call the other method we want to extend, like a [template method
pattern](https://en.wikipedia.org/wiki/Template_method_pattern).
For example, given this base:
2019-07-09 07:16:17 +00:00
```ruby
class Base
def execute
return unless enabled?
2018-10-15 13:59:00 +00:00
2019-07-09 07:16:17 +00:00
# ...
# ...
2017-10-27 14:29:36 +00:00
end
2019-07-09 07:16:17 +00:00
end
```
2017-10-27 14:29:36 +00:00
2019-07-09 07:16:17 +00:00
Instead of just overriding `Base#execute` , we should update it and extract
2020-04-22 18:09:52 +00:00
the behavior into another method:
2017-10-27 14:29:36 +00:00
2019-07-09 07:16:17 +00:00
```ruby
class Base
def execute
return unless enabled?
2018-10-15 13:59:00 +00:00
2019-07-09 07:16:17 +00:00
do_something
end
2018-10-15 13:59:00 +00:00
2019-07-09 07:16:17 +00:00
private
2018-10-15 13:59:00 +00:00
2019-07-09 07:16:17 +00:00
def do_something
# ...
# ...
2017-10-27 14:29:36 +00:00
end
2019-07-09 07:16:17 +00:00
end
```
2018-09-06 16:52:18 +00:00
2019-07-09 07:16:17 +00:00
Then we're free to override that `do_something` without worrying about the
guards:
2018-09-06 16:52:18 +00:00
2019-07-09 07:16:17 +00:00
```ruby
module EE::Base
extend ::Gitlab::Utils::Override
2018-10-15 13:59:00 +00:00
2019-07-09 07:16:17 +00:00
override :do_something
def do_something
# Follow the above pattern to call super and extend it
2017-10-27 14:29:36 +00:00
end
2019-07-09 07:16:17 +00:00
end
```
2018-10-15 13:59:00 +00:00
2017-10-27 14:29:36 +00:00
When prepending, place them in the `ee/` specific sub-directory, and
wrap class or module in `module EE` to avoid naming conflicts.
For example to override the CE implementation of
`ApplicationController#after_sign_out_path_for` :
```ruby
def after_sign_out_path_for(resource)
current_application_settings.after_sign_out_path.presence || new_user_session_path
end
```
Instead of modifying the method in place, you should add `prepend` to
the existing file:
```ruby
class ApplicationController < ActionController::Base
# ...
def after_sign_out_path_for(resource)
current_application_settings.after_sign_out_path.presence || new_user_session_path
end
# ...
end
2018-11-13 12:33:02 +00:00
2019-07-30 13:00:53 +00:00
ApplicationController.prepend_if_ee('EE::ApplicationController')
2017-10-27 14:29:36 +00:00
```
And create a new file in the `ee/` sub-directory with the altered
implementation:
```ruby
module EE
2017-12-25 10:20:50 +00:00
module ApplicationController
extend ::Gitlab::Utils::Override
2017-10-27 14:29:36 +00:00
2017-12-25 10:20:50 +00:00
override :after_sign_out_path_for
def after_sign_out_path_for(resource)
2017-10-27 14:29:36 +00:00
if Gitlab::Geo.secondary?
Gitlab::Geo.primary_node.oauth_logout_url(@geo_logout_state)
else
super
end
end
end
end
```
2018-08-17 10:44:06 +00:00
##### Overriding CE class methods
The same applies to class methods, except we want to use
`ActiveSupport::Concern` and put `extend ::Gitlab::Utils::Override`
within the block of `class_methods` . Here's an example:
```ruby
module EE
module Groups
module GroupMembersController
extend ActiveSupport::Concern
class_methods do
extend ::Gitlab::Utils::Override
override :admin_not_required_endpoints
def admin_not_required_endpoints
super.concat(%i[update override])
end
end
end
end
end
```
2017-10-27 14:29:36 +00:00
#### Use self-descriptive wrapper methods
2019-11-06 18:06:29 +00:00
When it's not possible/logical to modify the implementation of a method, then
wrap it in a self-descriptive method and use that method.
2017-10-27 14:29:36 +00:00
2019-11-06 18:06:29 +00:00
For example, in GitLab-FOSS, the only user created by the system is `User.ghost`
but in EE there are several types of bot-users that aren't really users. It would
be incorrect to override the implementation of `User#ghost?` , so instead we add
a method `#internal?` to `app/models/user.rb` . The implementation will be:
2017-10-27 14:29:36 +00:00
```ruby
2019-11-06 18:06:29 +00:00
def internal?
ghost?
2017-10-27 14:29:36 +00:00
end
```
In EE, the implementation `ee/app/models/ee/users.rb` would be:
```ruby
2019-11-06 18:06:29 +00:00
override :internal?
def internal?
super || bot?
2017-10-27 14:29:36 +00:00
end
```
2018-10-15 13:59:00 +00:00
### Code in `config/routes`
2018-10-17 13:29:39 +00:00
When we add `draw :admin` in `config/routes.rb` , the application will try to
load the file located in `config/routes/admin.rb` , and also try to load the
file located in `ee/config/routes/admin.rb` .
2018-10-15 13:59:00 +00:00
2018-10-23 12:48:02 +00:00
In EE, it should at least load one file, at most two files. If it cannot find
any files, an error will be raised. In CE, since we don't know if there will
be an EE route, it will not raise any errors even if it cannot find anything.
2018-10-17 13:29:39 +00:00
This means if we want to extend a particular CE route file, just add the same
file located in `ee/config/routes` . If we want to add an EE only route, we
2018-10-23 12:48:02 +00:00
could still put `draw :ee_only` in both CE and EE, and add
`ee/config/routes/ee_only.rb` in EE, similar to `render_if_exists` .
2018-10-15 13:59:00 +00:00
2017-10-27 14:29:36 +00:00
### Code in `app/controllers/`
In controllers, the most common type of conflict is with `before_action` that
has a list of actions in CE but EE adds some actions to that list.
The same problem often occurs for `params.require` / `params.permit` calls.
**Mitigations**
Separate CE and EE actions/keywords. For instance for `params.require` in
`ProjectsController` :
```ruby
def project_params
params.require(:project).permit(project_params_attributes)
end
# Always returns an array of symbols, created however best fits the use case.
# It _should_ be sorted alphabetically.
def project_params_attributes
%i[
description
name
path
]
end
```
In the `EE::ProjectsController` module:
```ruby
def project_params_attributes
super + project_params_attributes_ee
end
def project_params_attributes_ee
%i[
approvals_before_merge
approver_group_ids
approver_ids
...
]
end
```
### Code in `app/models/`
EE-specific models should `extend EE::Model` .
For example, if EE has a specific `Tanuki` model, you would
place it in `ee/app/models/ee/tanuki.rb` .
### Code in `app/views/`
It's a very frequent problem that EE is adding some specific view code in a CE
view. For instance the approval code in the project's settings page.
**Mitigations**
Blocks of code that are EE-specific should be moved to partials. This
2018-11-19 13:51:54 +00:00
avoids conflicts with big chunks of HAML code that are not fun to
2017-10-27 14:29:36 +00:00
resolve when you add the indentation to the equation.
2018-05-23 12:56:11 +00:00
EE-specific views should be placed in `ee/app/views/` , using extra
2017-10-27 14:29:36 +00:00
sub-directories if appropriate.
2020-01-22 06:08:33 +00:00
### Code in `lib/gitlab/background_migration/`
When you create EE-only background migrations, you have to plan for users that
downgrade GitLab EE to CE. In other words, every EE-only migration has to be present in
CE code but with no implementation, instead you need to extend it on EE side.
GitLab CE:
```ruby
# lib/gitlab/background_migration/prune_orphaned_geo_events.rb
module Gitlab
module BackgroundMigration
class PruneOrphanedGeoEvents
def perform(table_name)
end
end
end
end
Gitlab::BackgroundMigration::PruneOrphanedGeoEvents.prepend_if_ee('EE::Gitlab::BackgroundMigration::PruneOrphanedGeoEvents')
```
GitLab EE:
```ruby
# ee/lib/ee/gitlab/background_migration/prune_orphaned_geo_events.rb
module EE
module Gitlab
module BackgroundMigration
module PruneOrphanedGeoEvents
extend ::Gitlab::Utils::Override
override :perform
def perform(table_name = EVENT_TABLES.first)
return if ::Gitlab::Database.read_only?
deleted_rows = prune_orphaned_rows(table_name)
table_name = next_table(table_name) if deleted_rows.zero?
::BackgroundMigrationWorker.perform_in(RESCHEDULE_DELAY, self.class.name.demodulize, table_name) if table_name
end
end
end
end
end
```
2020-10-29 09:08:38 +00:00
### Code in `app/graphql/`
EE-specific mutations, resolvers, and types should be added to
`ee/app/graphql/{mutations,resolvers,types}` .
To override a CE mutation, resolver, or type, create the file in
`ee/app/graphql/ee/{mutations,resolvers,types}` and add new code to a
`prepended` block.
For example, if CE has a mutation called `Mutations::Tanukis::Create` and you
wanted to add a new argument, place the EE override in
`ee/app/graphql/ee/mutations/tanukis/create.rb` :
```ruby
module EE
module Mutations
module Tanukis
module Create
extend ActiveSupport::Concern
prepended do
argument :name,
GraphQL::STRING_TYPE,
required: false,
description: 'Tanuki name'
end
end
end
end
end
```
2018-05-28 14:15:29 +00:00
#### Using `render_if_exists`
2018-05-23 12:56:11 +00:00
Instead of using regular `render` , we should use `render_if_exists` , which
will not render anything if it cannot find the specific partial. We use this
so that we could put `render_if_exists` in CE, keeping code the same between
CE and EE.
The advantages of this:
2019-09-26 15:06:18 +00:00
- Very clear hints about where we're extending EE views while reading CE code.
2018-05-23 12:56:11 +00:00
The disadvantage of this:
- If we have typos in the partial name, it would be silently ignored.
2019-05-16 15:06:40 +00:00
##### Caveats
2019-05-16 10:05:33 +00:00
The `render_if_exists` view path argument must be relative to `app/views/` and `ee/app/views` .
Resolving an EE template path that is relative to the CE view path will not work.
```haml
- # app/views/projects/index.html.haml
2019-05-16 15:06:40 +00:00
= render_if_exists 'button' # Will not render `ee/app/views/projects/_button` and will quietly fail
2019-05-16 10:05:33 +00:00
= render_if_exists 'projects/button' # Will render `ee/app/views/projects/_button`
```
2018-05-28 14:15:29 +00:00
#### Using `render_ce`
For `render` and `render_if_exists` , they search for the EE partial first,
and then CE partial. They would only render a particular partial, not all
partials with the same name. We could take the advantage of this, so that
the same partial path (e.g. `shared/issuable/form/default_templates` ) could
be referring to the CE partial in CE (i.e.
`app/views/shared/issuable/form/_default_templates.html.haml` ), while EE
partial in EE (i.e.
`ee/app/views/shared/issuable/form/_default_templates.html.haml` ). This way,
we could show different things between CE and EE.
However sometimes we would also want to reuse the CE partial in EE partial
because we might just want to add something to the existing CE partial. We
could workaround this by adding another partial with a different name, but it
would be tedious to do so.
In this case, we could as well just use `render_ce` which would ignore any EE
partials. One example would be
`ee/app/views/shared/issuable/form/_default_templates.html.haml` :
``` haml
- if @project .feature_available?(:issuable_default_templates)
= render_ce 'shared/issuable/form/default_templates'
- elsif show_promotions?
= render 'shared/promotions/promote_issue_templates'
```
In the above example, we can't use
`render 'shared/issuable/form/default_templates'` because it would find the
same EE partial, causing infinite recursion. Instead, we could use `render_ce`
so it ignores any partials in `ee/` and then it would render the CE partial
(i.e. `app/views/shared/issuable/form/_default_templates.html.haml` )
for the same path (i.e. `shared/issuable/form/default_templates` ). This way
we could easily wrap around the CE partial.
2017-10-27 14:29:36 +00:00
### Code in `lib/`
Place EE-specific logic in the top-level `EE` module namespace. Namespace the
class beneath the `EE` module just as you would normally.
For example, if CE has LDAP classes in `lib/gitlab/ldap/` then you would place
EE-specific LDAP classes in `ee/lib/ee/gitlab/ldap` .
2018-03-06 17:10:55 +00:00
### Code in `lib/api/`
2019-07-30 13:00:53 +00:00
It can be very tricky to extend EE features by a single line of `prepend_if_ee` ,
and for each different [Grape ](https://github.com/ruby-grape/grape ) feature, we
might need different strategies to extend it. To apply different strategies
2018-03-06 17:10:55 +00:00
easily, we would use `extend ActiveSupport::Concern` in the EE module.
2018-03-26 07:42:19 +00:00
Put the EE module files following
[EE features based on CE features ](#ee-features-based-on-ce-features ).
2018-03-06 17:10:55 +00:00
#### EE API routes
2018-03-23 21:52:20 +00:00
For EE API routes, we put them in a `prepended` block:
2018-03-06 17:10:55 +00:00
2018-09-06 16:52:18 +00:00
```ruby
2018-03-06 17:10:55 +00:00
module EE
module API
module MergeRequests
extend ActiveSupport::Concern
prepended do
params do
requires :id, type: String, desc: 'The ID of a project'
end
2018-11-08 12:18:17 +00:00
resource :projects, requirements: ::API::API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2018-03-06 17:10:55 +00:00
# ...
end
end
end
end
end
```
2018-03-23 21:52:20 +00:00
Note that due to namespace differences, we need to use the full qualifier for some
constants.
2018-03-06 17:10:55 +00:00
2020-03-27 03:07:56 +00:00
#### EE parameters
2018-03-06 17:10:55 +00:00
2018-03-26 07:42:19 +00:00
We can define `params` and utilize `use` in another `params` definition to
2020-03-27 03:07:56 +00:00
include parameters defined in EE. However, we need to define the "interface" first
2018-03-23 21:52:20 +00:00
in CE in order for EE to override it. We don't have to do this in other places
2019-07-30 13:00:53 +00:00
due to `prepend_if_ee` , but Grape is complex internally and we couldn't easily
do that, so we'll follow regular object-oriented practices that we define the
2018-03-23 21:52:20 +00:00
interface first here.
2018-03-06 17:10:55 +00:00
2020-03-27 03:07:56 +00:00
For example, suppose we have a few more optional parameters for EE. We can move the
2020-06-29 21:09:07 +00:00
parameters out of the `Grape::API::Instance` class to a helper module, so we can inject it
2019-05-27 15:53:19 +00:00
before it would be used in the class.
2018-03-06 17:10:55 +00:00
2018-09-06 16:52:18 +00:00
```ruby
2018-03-06 17:10:55 +00:00
module API
2020-06-29 21:09:07 +00:00
class Projects < Grape::API::Instance
2019-05-27 15:53:19 +00:00
helpers Helpers::ProjectsHelpers
end
end
```
Given this CE API `params` :
```ruby
module API
module Helpers
module ProjectsHelpers
extend ActiveSupport::Concern
extend Grape::API::Helpers
params :optional_project_params_ce do
# CE specific params go here...
2018-03-06 17:10:55 +00:00
end
2019-05-27 15:53:19 +00:00
params :optional_project_params_ee do
end
2018-03-06 17:10:55 +00:00
2019-05-27 15:53:19 +00:00
params :optional_project_params do
use :optional_project_params_ce
use :optional_project_params_ee
end
2018-03-06 17:10:55 +00:00
end
end
end
2018-11-13 12:33:02 +00:00
2019-07-30 13:00:53 +00:00
API::Helpers::ProjectsHelpers.prepend_if_ee('EE::API::Helpers::ProjectsHelpers')
2018-03-06 17:10:55 +00:00
```
2019-05-27 15:53:19 +00:00
We could override it in EE module:
2018-03-06 17:10:55 +00:00
2018-09-06 16:52:18 +00:00
```ruby
2018-03-06 17:10:55 +00:00
module EE
module API
2019-05-27 15:53:19 +00:00
module Helpers
module ProjectsHelpers
extend ActiveSupport::Concern
2018-03-06 17:10:55 +00:00
2019-05-27 15:53:19 +00:00
prepended do
params :optional_project_params_ee do
2018-03-06 17:10:55 +00:00
# EE specific params go here...
end
end
end
end
end
end
```
#### EE helpers
2018-03-23 21:52:20 +00:00
To make it easy for an EE module to override the CE helpers, we need to define
those helpers we want to extend first. Try to do that immediately after the
class definition to make it easy and clear:
2018-03-06 17:10:55 +00:00
2018-09-06 16:52:18 +00:00
```ruby
2018-03-06 17:10:55 +00:00
module API
2020-06-29 21:09:07 +00:00
class JobArtifacts < Grape::API::Instance
2018-03-06 17:10:55 +00:00
# EE::API::JobArtifacts would override the following helpers
helpers do
def authorize_download_artifacts!
authorize_read_builds!
end
end
end
end
2018-11-13 12:33:02 +00:00
2019-07-30 13:00:53 +00:00
API::JobArtifacts.prepend_if_ee('EE::API::JobArtifacts')
2018-03-06 17:10:55 +00:00
```
2018-03-23 21:52:20 +00:00
And then we can follow regular object-oriented practices to override it:
2018-03-06 17:10:55 +00:00
2018-09-06 16:52:18 +00:00
```ruby
2018-03-06 17:10:55 +00:00
module EE
module API
module JobArtifacts
extend ActiveSupport::Concern
prepended do
helpers do
def authorize_download_artifacts!
super
check_cross_project_pipelines_feature!
end
end
end
end
end
end
```
2020-04-22 18:09:52 +00:00
#### EE-specific behavior
2018-03-06 17:10:55 +00:00
2020-04-22 18:09:52 +00:00
Sometimes we need EE-specific behavior in some of the APIs. Normally we could
2018-03-23 21:52:20 +00:00
use EE methods to override CE methods, however API routes are not methods and
therefore can't be simply overridden. We need to extract them into a standalone
method, or introduce some "hooks" where we could inject behavior in the CE
route. Something like this:
2018-03-06 17:10:55 +00:00
2018-09-06 16:52:18 +00:00
```ruby
2018-03-06 17:10:55 +00:00
module API
2020-06-29 21:09:07 +00:00
class MergeRequests < Grape::API::Instance
2018-03-06 17:10:55 +00:00
helpers do
# EE::API::MergeRequests would override the following helpers
def update_merge_request_ee(merge_request)
end
end
put ':id/merge_requests/:merge_request_iid/merge' do
merge_request = find_project_merge_request(params[:merge_request_iid])
# ...
update_merge_request_ee(merge_request)
# ...
end
end
end
2018-11-13 12:33:02 +00:00
2019-07-30 13:00:53 +00:00
API::MergeRequests.prepend_if_ee('EE::API::MergeRequests')
2018-03-06 17:10:55 +00:00
```
2018-03-23 21:52:20 +00:00
Note that `update_merge_request_ee` doesn't do anything in CE, but
2018-03-06 17:10:55 +00:00
then we could override it in EE:
2018-09-06 16:52:18 +00:00
```ruby
2018-03-06 17:10:55 +00:00
module EE
module API
module MergeRequests
extend ActiveSupport::Concern
prepended do
helpers do
def update_merge_request_ee(merge_request)
# ...
end
end
end
end
end
end
```
2018-03-23 21:52:20 +00:00
#### EE `route_setting`
2018-03-06 17:10:55 +00:00
2018-03-23 21:52:20 +00:00
It's very hard to extend this in an EE module, and this is simply storing
2018-03-06 17:10:55 +00:00
some meta-data for a particular route. Given that, we could simply leave the
EE `route_setting` in CE as it won't hurt and we are just not going to use
those meta-data in CE.
2018-03-23 21:52:20 +00:00
We could revisit this policy when we're using `route_setting` more and whether
or not we really need to extend it from EE. For now we're not using it much.
2018-03-06 17:10:55 +00:00
2018-03-23 21:52:20 +00:00
#### Utilizing class methods for setting up EE-specific data
2018-03-06 17:10:55 +00:00
2018-03-23 21:52:20 +00:00
Sometimes we need to use different arguments for a particular API route, and we
can't easily extend it with an EE module because Grape has different context in
2018-11-13 12:33:02 +00:00
different blocks. In order to overcome this, we need to move the data to a class
method that resides in a separate module or class. This allows us to extend that
2019-07-30 13:00:53 +00:00
module or class before its data is used, without having to place a
`prepend_if_ee` in the middle of CE code.
2018-03-06 17:10:55 +00:00
For example, in one place we need to pass an extra argument to
2018-03-23 21:52:20 +00:00
`at_least_one_of` so that the API could consider an EE-only argument as the
2018-11-13 12:33:02 +00:00
least argument. We would approach this as follows:
2018-03-06 17:10:55 +00:00
2018-09-06 16:52:18 +00:00
```ruby
2018-11-13 12:33:02 +00:00
# api/merge_requests/parameters.rb
2018-03-06 17:10:55 +00:00
module API
2020-06-29 21:09:07 +00:00
class MergeRequests < Grape::API::Instance
2018-11-13 12:33:02 +00:00
module Parameters
def self.update_params_at_least_one_of
%i[
assignee_id
description
]
end
2018-03-06 17:10:55 +00:00
end
2018-11-13 12:33:02 +00:00
end
end
2018-03-06 17:10:55 +00:00
2019-07-30 13:00:53 +00:00
API::MergeRequests::Parameters.prepend_if_ee('EE::API::MergeRequests::Parameters')
2018-03-06 17:10:55 +00:00
2018-11-13 12:33:02 +00:00
# api/merge_requests.rb
module API
2020-06-29 21:09:07 +00:00
class MergeRequests < Grape::API::Instance
2018-03-06 17:10:55 +00:00
params do
2018-11-13 12:33:02 +00:00
at_least_one_of(*Parameters.update_params_at_least_one_of)
2018-03-06 17:10:55 +00:00
end
end
end
```
2018-03-23 21:52:20 +00:00
And then we could easily extend that argument in the EE class method:
2018-03-06 17:10:55 +00:00
2018-09-06 16:52:18 +00:00
```ruby
2018-03-06 17:10:55 +00:00
module EE
module API
module MergeRequests
2018-11-13 12:33:02 +00:00
module Parameters
extend ActiveSupport::Concern
2018-03-06 17:10:55 +00:00
2018-11-13 12:33:02 +00:00
class_methods do
extend ::Gitlab::Utils::Override
2018-08-17 10:44:06 +00:00
2018-11-13 12:33:02 +00:00
override :update_params_at_least_one_of
def update_params_at_least_one_of
super.push(*%i[
squash
])
end
2018-03-06 17:10:55 +00:00
end
end
end
end
end
```
It could be annoying if we need this for a lot of routes, but it might be the
simplest solution right now.
2018-11-13 12:33:02 +00:00
This approach can also be used when models define validations that depend on
class methods. For example:
```ruby
# app/models/identity.rb
class Identity < ActiveRecord::Base
def self.uniqueness_scope
[:provider]
end
2019-07-30 13:00:53 +00:00
prepend_if_ee('EE::Identity')
2018-11-13 12:33:02 +00:00
validates :extern_uid,
allow_blank: true,
uniqueness: { scope: uniqueness_scope, case_sensitive: false }
end
# ee/app/models/ee/identity.rb
module EE
module Identity
extend ActiveSupport::Concern
class_methods do
extend ::Gitlab::Utils::Override
def uniqueness_scope
[*super, :saml_provider_id]
end
end
end
end
```
Instead of taking this approach, we would refactor our code into the following:
```ruby
# ee/app/models/ee/identity/uniqueness_scopes.rb
module EE
module Identity
module UniquenessScopes
extend ActiveSupport::Concern
class_methods do
extend ::Gitlab::Utils::Override
def uniqueness_scope
[*super, :saml_provider_id]
end
end
end
end
end
# app/models/identity/uniqueness_scopes.rb
class Identity < ActiveRecord::Base
module UniquenessScopes
def self.uniqueness_scope
[:provider]
end
end
end
2019-07-30 13:00:53 +00:00
Identity::UniquenessScopes.prepend_if_ee('EE::Identity::UniquenessScopes')
2018-11-13 12:33:02 +00:00
# app/models/identity.rb
class Identity < ActiveRecord::Base
validates :extern_uid,
allow_blank: true,
uniqueness: { scope: Identity::UniquenessScopes.scopes, case_sensitive: false }
end
```
2017-10-27 14:29:36 +00:00
### Code in `spec/`
When you're testing EE-only features, avoid adding examples to the
2019-09-26 15:06:18 +00:00
existing CE specs. Also do not change existing CE examples, since they
2017-10-27 14:29:36 +00:00
should remain working as-is when EE is running without a license.
2018-02-13 14:41:47 +00:00
Instead place EE specs in the `ee/spec` folder.
2017-10-27 14:29:36 +00:00
2019-02-12 12:39:48 +00:00
### Code in `spec/factories`
Use `FactoryBot.modify` to extend factories already defined in CE.
Note that you cannot define new factories (even nested ones) inside the `FactoryBot.modify` block. You can do so in a
separate `FactoryBot.define` block as shown in the example below:
```ruby
# ee/spec/factories/notes.rb
FactoryBot.modify do
factory :note do
trait :on_epic do
noteable { create(:epic) }
project nil
end
end
end
FactoryBot.define do
factory :note_on_epic, parent: :note, traits: [:on_epic]
end
```
2017-10-27 14:29:36 +00:00
## JavaScript code in `assets/javascripts/`
2018-03-12 20:39:39 +00:00
To separate EE-specific JS-files we should also move the files into an `ee` folder.
2017-10-27 14:29:36 +00:00
For example there can be an
`app/assets/javascripts/protected_branches/protected_branches_bundle.js` and an
EE counterpart
`ee/app/assets/javascripts/protected_branches/protected_branches_bundle.js` .
2019-01-30 14:02:44 +00:00
The corresponding import statement would then look like this:
```javascript
// app/assets/javascripts/protected_branches/protected_branches_bundle.js
import bundle from '~/protected_branches/protected_branches_bundle.js';
// ee/app/assets/javascripts/protected_branches/protected_branches_bundle.js
// (only works in EE)
import bundle from 'ee/protected_branches/protected_branches_bundle.js';
// in CE: app/assets/javascripts/protected_branches/protected_branches_bundle.js
// in EE: ee/app/assets/javascripts/protected_branches/protected_branches_bundle.js
import bundle from 'ee_else_ce/protected_branches/protected_branches_bundle.js';
```
2017-10-27 14:29:36 +00:00
2019-06-05 10:10:28 +00:00
See the frontend guide [performance section ](fe_guide/performance.md ) for
2019-08-27 08:44:07 +00:00
information on managing page-specific JavaScript within EE.
2017-10-27 14:29:36 +00:00
2019-02-28 15:39:20 +00:00
## Vue code in `assets/javascript`
2019-06-25 04:34:55 +00:00
2019-03-01 10:53:01 +00:00
### script tag
#### Child Component only used in EE
2019-06-25 04:34:55 +00:00
2019-05-20 14:11:44 +00:00
To separate Vue template differences we should [async import the components ](https://vuejs.org/v2/guide/components-dynamic-async.html#Async-Components ).
2019-02-28 15:39:20 +00:00
2020-01-21 12:08:31 +00:00
Doing this allows for us to load the correct component in EE while in CE
2019-02-28 15:39:20 +00:00
we can load a empty component that renders nothing. This code **should**
exist in the CE repository as well as the EE repository.
```html
< script >
export default {
components: {
EEComponent: () => import('ee_component/components/test.vue'),
},
};
< / script >
< template >
< div >
< ee-component / >
< / div >
< / template >
```
2020-07-03 09:08:53 +00:00
#### For JS code that is EE only, like props, computed properties, methods, etc
2019-06-25 04:34:55 +00:00
2020-07-03 09:08:53 +00:00
- Please do not use mixins unless ABSOLUTELY NECESSARY. Please try to find an alternative pattern.
2019-03-01 10:53:01 +00:00
2020-08-13 06:10:02 +00:00
##### Recommended alternative approach (named/scoped slots)
2019-06-25 04:34:55 +00:00
2020-07-03 09:08:53 +00:00
- We can use slots and/or scoped slots to achieve the same thing as we did with mixins. If you only need an EE component there is no need to create the CE component.
2020-08-27 15:10:21 +00:00
1. First, we have a CE component that can render a slot in case we need EE template and functionality to be decorated on top of the CE base.
2019-03-01 10:53:01 +00:00
2020-07-03 09:08:53 +00:00
```vue
// ./ce/my_component.vue
< script >
export default {
props: {
tooltipDefaultText: {
type: String,
},
},
computed: {
tooltipText() {
return this.tooltipDefaultText || "5 issues please";
}
},
2019-03-31 22:35:41 +00:00
}
2020-07-03 09:08:53 +00:00
< / script >
< template >
< span v-gl-tooltip :title = "tooltipText" class = "ce-text" > Community Edition Only Text< / span >
< slot name = "ee-specific-component" >
< / template >
2019-03-31 22:35:41 +00:00
```
2019-04-16 09:35:49 +00:00
2020-07-03 09:08:53 +00:00
1. Next, we render the EE component, and inside of the EE component we render the CE component and add additional content in the slot.
```vue
// ./ee/my_component.vue
< script >
export default {
computed: {
tooltipText() {
if (this.weight) {
return "5 issues with weight 10";
}
}
},
methods: {
submit() {
// do something.
}
},
}
< / script >
< template >
< my-component :tooltipDefaultText = "tooltipText" >
< template #ee -specific-component >
< span class = "some-ee-specific" > EE Specific Value< / span >
< button @click =" submit " > Click Me</ button >
< / template >
< / my-component >
< / template >
```
1. Finally, wherever the component is needed we can require it like so
`import MyComponent from 'ee_else_ce/path/my_component'.vue`
2019-03-01 10:53:01 +00:00
2020-07-03 09:08:53 +00:00
- this way the correct component will be included for either the ce or ee implementation
2019-03-01 10:53:01 +00:00
2020-07-03 09:08:53 +00:00
**For EE components that need different results for the same computed values, we can pass in props to the CE wrapper as seen in the example.**
2019-06-25 04:34:55 +00:00
2019-07-01 03:36:23 +00:00
- **EE Child components**
2019-03-01 10:53:01 +00:00
- Since we are using the async loading to check which component to load, we'd still use the component's name, check [this example ](#child-component-only-used-in-ee ).
2019-07-01 03:36:23 +00:00
- **EE extra HTML**
2019-04-16 09:35:49 +00:00
- For the templates that have extra HTML in EE we should move it into a new component and use the `ee_else_ce` dynamic import
2019-03-01 10:53:01 +00:00
### Non Vue Files
2019-06-25 04:34:55 +00:00
2019-03-01 10:53:01 +00:00
For regular JS files, the approach is similar.
2019-05-19 23:27:22 +00:00
1. We will keep using the [`ee_else_ce` ](../development/ee_features.md#javascript-code-in-assetsjavascripts ) helper, this means that EE only code should be inside the `ee/` folder.
2019-06-25 04:34:55 +00:00
1. An EE file should be created with the EE only code, and it should extend the CE counterpart.
1. For code inside functions that can't be extended, the code should be moved into a new file and we should use `ee_else_ce` helper:
2019-03-01 10:53:01 +00:00
2019-08-22 08:50:31 +00:00
#### Example
2019-03-01 10:53:01 +00:00
```javascript
2019-04-16 09:35:49 +00:00
import eeCode from 'ee_else_ce/ee_code';
2019-03-01 10:53:01 +00:00
2019-04-16 09:35:49 +00:00
function test() {
const test = 'a';
eeCode();
return test;
}
2019-03-01 10:53:01 +00:00
```
2017-10-27 14:29:36 +00:00
## SCSS code in `assets/stylesheets`
2019-09-26 15:06:18 +00:00
If a component you're adding styles for is limited to EE, it is better to have a
separate SCSS file in an appropriate directory within `app/assets/stylesheets` .
2017-10-27 14:29:36 +00:00
In some cases, this is not entirely possible or creating dedicated SCSS file is an overkill,
e.g. a text style of some component is different for EE. In such cases,
2020-08-27 15:10:21 +00:00
styles are usually kept in a stylesheet that is common for both CE and EE, and it is wise
2017-10-27 14:29:36 +00:00
to isolate such ruleset from rest of CE rules (along with adding comment describing the same)
to avoid conflicts during CE to EE merge.
2019-07-08 11:07:07 +00:00
### Bad
2019-06-25 04:34:55 +00:00
2017-10-27 14:29:36 +00:00
```scss
.section-body {
.section-title {
background: $gl-header-color;
}
& .ee-section-body {
.section-title {
background: $gl-header-color-cyan;
}
}
}
```
2019-07-08 11:07:07 +00:00
### Good
2019-06-25 04:34:55 +00:00
2017-10-27 14:29:36 +00:00
```scss
.section-body {
.section-title {
background: $gl-header-color;
}
}
2018-03-28 16:05:14 +00:00
// EE-specific start
2017-10-27 14:29:36 +00:00
.section-body.ee-section-body {
.section-title {
background: $gl-header-color-cyan;
}
}
2018-03-28 16:05:14 +00:00
// EE-specific end
2017-10-27 14:29:36 +00:00
```
2017-12-05 16:49:49 +00:00
2019-08-27 08:44:07 +00:00
## GitLab-svgs
2017-12-05 16:49:49 +00:00
Conflicts in `app/assets/images/icons.json` or `app/assets/images/icons.svg` can
be resolved simply by regenerating those assets with
[`yarn run svg` ](https://gitlab.com/gitlab-org/gitlab-svgs ).