2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Releases
|
2021-01-03 14:25:43 +05:30
|
|
|
class UpdateService < Releases::BaseService
|
2019-02-15 15:39:39 +05:30
|
|
|
def execute
|
2021-01-29 00:20:46 +05:30
|
|
|
if error = validate
|
|
|
|
return error
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
if param_for_milestones_provided?
|
|
|
|
previous_milestones = release.milestones.map(&:id)
|
2020-03-13 15:44:24 +05:30
|
|
|
params[:milestones] = milestones
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
# transaction needed as Rails applies `save!` to milestone_releases
|
|
|
|
# when it does assign_attributes instead of actual saving
|
|
|
|
# this leads to the validation error being raised
|
|
|
|
# see https://gitlab.com/gitlab-org/gitlab/-/merge_requests/43385
|
2021-10-27 15:23:28 +05:30
|
|
|
ApplicationRecord.transaction do
|
2021-01-03 14:25:43 +05:30
|
|
|
if release.update(params)
|
2021-01-29 00:20:46 +05:30
|
|
|
execute_hooks(release, 'update')
|
2021-01-03 14:25:43 +05:30
|
|
|
success(tag: existing_tag, release: release, milestones_updated: milestones_updated?(previous_milestones))
|
|
|
|
else
|
|
|
|
error(release.errors.messages || '400 Bad request', 400)
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
error(e.message || '400 Bad request', 400)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def validate
|
2022-11-25 23:54:43 +05:30
|
|
|
return error(_('Tag does not exist'), 404) unless existing_tag
|
|
|
|
return error(_('Release does not exist'), 404) unless release
|
|
|
|
return error(_('Access Denied'), 403) unless allowed?
|
|
|
|
return error(_('params is empty'), 400) if empty_params?
|
2023-04-23 21:23:45 +05:30
|
|
|
return error(format(_("Milestone(s) not found: %{milestones}"), milestones: inexistent_milestone_titles.join(', ')), 400) if inexistent_milestone_titles.any? # rubocop:disable Layout/LineLength
|
|
|
|
return error(format(_("Milestone id(s) not found: %{milestones}"), milestones: inexistent_milestone_ids.join(', ')), 400) if inexistent_milestone_ids.any? # rubocop:disable Layout/LineLength
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def allowed?
|
|
|
|
Ability.allowed?(current_user, :update_release, release)
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty_params?
|
|
|
|
params.except(:tag).empty?
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
def milestones_updated?(previous_milestones)
|
2023-04-23 21:23:45 +05:30
|
|
|
return false unless param_for_milestones_provided?
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
previous_milestones.to_set != release.milestones.map(&:id)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|