Class: Ci::Components::Usages::CreateService
- Inherits:
-
Object
- Object
- Ci::Components::Usages::CreateService
- Defined in:
- app/services/ci/components/usages/create_service.rb
Constant Summary collapse
- ValidationError =
Class.new(StandardError)
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(component, used_by_project:) ⇒ CreateService
constructor
A new instance of CreateService.
Constructor Details
#initialize(component, used_by_project:) ⇒ CreateService
Returns a new instance of CreateService.
9 10 11 12 |
# File 'app/services/ci/components/usages/create_service.rb', line 9 def initialize(component, used_by_project:) @component = component @used_by_project = used_by_project end |
Instance Method Details
#execute ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/services/ci/components/usages/create_service.rb', line 14 def execute component_usage = Ci::Catalog::Resources::Components::Usage.new( component: component, catalog_resource: component.catalog_resource, project: component.project, used_by_project_id: used_by_project.id ) component_last_usage = Ci::Catalog::Resources::Components::LastUsage.get_usage_for(component, used_by_project) if component_last_usage.new_record? component_last_usage.last_used_date = Time.current.to_date else component_last_usage.touch(:last_used_date) end component_last_usage.save # Save last usage regardless of component_usage if component_usage.save ServiceResponse.success(message: 'Usage recorded') else errors = component_usage.errors || component_last_usage.errors if errors.size == 1 && errors.first.type == :taken ServiceResponse.success(message: 'Usage already recorded for today') else exception = ValidationError.new(errors..join(', ')) Gitlab::ErrorTracking.track_exception(exception) ServiceResponse.error(message: exception.) end end end |