2018-12-25 04:48:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Releases
|
2020-10-06 11:08:33 -04:00
|
|
|
class UpdateService < Releases::BaseService
|
2018-12-25 04:48:26 -05:00
|
|
|
def execute
|
2020-11-06 19:08:58 -05:00
|
|
|
if error = validate
|
|
|
|
return error
|
|
|
|
end
|
2019-09-03 05:38:59 -04:00
|
|
|
|
2020-01-10 13:07:43 -05:00
|
|
|
if param_for_milestone_titles_provided?
|
|
|
|
previous_milestones = release.milestones.map(&:title)
|
|
|
|
params[:milestones] = milestones
|
|
|
|
end
|
2018-12-25 04:48:26 -05:00
|
|
|
|
2020-10-18 20:08:50 -04:00
|
|
|
# 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-07-23 08:09:05 -04:00
|
|
|
ApplicationRecord.transaction do
|
2020-10-18 20:08:50 -04:00
|
|
|
if release.update(params)
|
2020-11-06 19:08:58 -05:00
|
|
|
execute_hooks(release, 'update')
|
2020-10-18 20:08:50 -04:00
|
|
|
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)
|
2018-12-25 04:48:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-11-06 19:08:58 -05:00
|
|
|
def validate
|
|
|
|
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?
|
|
|
|
return error("Milestone(s) not found: #{inexistent_milestones.join(', ')}", 400) if inexistent_milestones.any?
|
|
|
|
end
|
|
|
|
|
2018-12-25 04:48:26 -05:00
|
|
|
def allowed?
|
|
|
|
Ability.allowed?(current_user, :update_release, release)
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty_params?
|
|
|
|
params.except(:tag).empty?
|
|
|
|
end
|
2020-01-10 13:07:43 -05:00
|
|
|
|
|
|
|
def milestones_updated?(previous_milestones)
|
|
|
|
return false unless param_for_milestone_titles_provided?
|
|
|
|
|
|
|
|
previous_milestones.to_set != release.milestones.map(&:title)
|
|
|
|
end
|
2018-12-25 04:48:26 -05:00
|
|
|
end
|
|
|
|
end
|