Optimize define_variables for create_deploy_token to avoid unnecessary data loading
Summary
The create_deploy_token action in Projects::Settings::RepositoryController currently uses a before_action :define_variables callback that loads several variables that are not needed for creating deploy tokens. This causes unnecessary database queries and performance overhead.
Problem
The define_variables method initializes:
-
@deploy_keys- DeployKeysPresenter - Deploy token variables (needed ✓)
-
tag_names- Repository tag names -
branch_names- Repository branch names - Protected refs (branches and tags with access levels)
-
remote_mirror- First or initialized remote mirror
However, the create_deploy_token action only needs the deploy token variables. All other variables are loaded unnecessarily, especially when the action returns JSON responses (which is the common case).
Current Code
before_action :define_variables, only: [:create_deploy_token]
def create_deploy_token
result = Projects::DeployTokens::CreateService.new(@project, current_user, deploy_token_params).execute
# ... handles result and responds with JSON or HTML
end
def define_variables
@deploy_keys = DeployKeysPresenter.new(@project, current_user: current_user)
define_deploy_token_variables
tag_names
branch_names
define_protected_refs
remote_mirror
end
Proposed Solution
Refactor to only load the variables actually needed by create_deploy_token:
- Extract deploy token-specific variable initialization into a separate method
- Update the
before_actionforcreate_deploy_tokento only call the minimal required initialization - Keep
define_variablesfor theshowaction which needs all variables
Benefits
- Reduced database queries for deploy token creation
- Improved performance, especially for API/JSON requests
- Clearer separation of concerns between different controller actions
Related
- Identified in MR !212444 (merged) review: !212444 (comment 2893579426)
Edited by 🤖 GitLab Bot 🤖