Consider autoloading files in this project
Problem
Currently, require_relative
is used to load code.
This can lead to issues when removing or renaming files. For example: !2579 (merged).
Proposed solution
Use zeitwerk
to manage (auto) loading code in the project.
Directory structure
zeitwerk
is very flexible but it requires you to provide a common namespace.
This project provides two different places where code is located:
-
lib/
- no namespace at all -
triage/
- namespaceTriage
triage/
is sometimes loading code from lib/
and vice-versa.
zeitwerk
does support multiple loaders but we'd need at least namespace for code located in lib/
.
Unify the structure
- Move
lib/
tolib/triage/utils
and namespace it toTriage::Reactive
- Move
triage/
intolib/triage/reactive
- Move
triage/templates
intotemplates/
Before
lib
├── constants
├── generate
├── hierarchy
├── schedule
├── shared
└── triage
└── utils
└── generate
triage/
├── job
├── processor
│ ├── appsec
│ ├── community
│ ├── compliance_group
│ ├── database
│ ├── delivery
│ ├── docs
│ ├── engineering_productivity
│ ├── gitlab_internal_commands
│ ├── growth_team
│ ├── privacy
│ ├── support
│ ├── threat_insights
│ └── workflow
├── rack
├── resources
├── strings
├── templates
└── triage
├── change
├── concerns
├── pipeline_failure
│ └── config
└── react_to_changes
35 directories
After
templates
lib
└── triage
├── reactive
│ ├── change
│ ├── concerns
│ ├── job
│ ├── pipeline_failure
│ │ └── config
│ ├── processor
│ │ ├── appsec
│ │ ├── community
│ │ ├── compliance_group
│ │ ├── database
│ │ ├── delivery
│ │ ├── docs
│ │ ├── engineering_productivity
│ │ ├── gitlab_internal_commands
│ │ ├── growth_team
│ │ ├── privacy
│ │ ├── support
│ │ ├── threat_insights
│ │ └── workflow
│ ├── rack
│ ├── react_to_changes
│ ├── resources
│ └── strings
└── utils
├── constants
├── generate
├── hierarchy
├── schedule
└── shared
32 directories
Implementation guide
In order to keep sanity of both authors and reviewers, introducing autoloading via zeitwerk
will be done in several steps/MRs:
-
Fix class/modules names which cannot be inferred/inflected properly - For example:
"label_jihu_contribution"
should defineLabelJihuContribution
and notLabelJiHuContribution
(Jihu
instead ofJiHu
)
- For example:
-
Namespace code in lib
- Move code to
lib/triage/utils
and useTriage::Utils
-
lib/triage.rb
is now the main entry point - Consolidate
require_relative
statements to requirelib/triage
- Move code to
-
Autoload lib/triage/utils
- Add and configure
zeitwerk
- Add and configure
-
Move triage/triage
intolib/triage/reactive
to make it autoloadable- Adjust
zeitwerk
's configuration to handle collapsable namespaces (processor
,job
etc) - Move configuration files (files which do not define any classes) into
config/
, for example:triage/triage/sentry.rb
- Adjust
Discussion
The following discussion from !2579 (merged) should be addressed:
-
@rymai started a discussion: (+2 comments)
Perhaps, we should switch to zeitwerk
and use for_gem
?
Edited by Peter Leitzen