Skip to content

Commit e80313f

Browse files
committed
Conditionally destroy a ressource
1 parent 998afa5 commit e80313f

24 files changed

+71
-114
lines changed

lib/api/access_requests.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,12 @@ class AccessRequests < Grape::API
6767
end
6868
delete ":id/access_requests/:user_id" do
6969
source = find_source(source_type, params[:id])
70-
member = source.public_send(:requesters).find_by!(user_id: params[:user_id])
70+
member = source.requesters.find_by!(user_id: params[:user_id])
7171

72-
check_unmodified_since(member.updated_at)
73-
74-
status 204
75-
::Members::DestroyService.new(source, current_user, params)
76-
.execute(:requesters)
72+
destroy_conditionally!(member) do
73+
::Members::DestroyService.new(source, current_user, params)
74+
.execute(:requesters)
75+
end
7776
end
7877
end
7978
end

lib/api/award_emoji.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ class AwardEmoji < Grape::API
8585
end
8686
delete "#{endpoint}/:award_id" do
8787
award = awardable.award_emoji.find(params[:award_id])
88-
check_unmodified_since(award.updated_at)
8988

9089
unauthorized! unless award.user == current_user || current_user.admin?
9190

92-
status 204
93-
award.destroy
91+
destroy_conditionally!(award)
9492
end
9593
end
9694
end

lib/api/boards.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,13 @@ def board_lists
122122
end
123123
delete "/lists/:list_id" do
124124
authorize!(:admin_list, user_project)
125-
126125
list = board_lists.find(params[:list_id])
127-
check_unmodified_since(list.updated_at)
128-
129-
service = ::Boards::Lists::DestroyService.new(user_project, current_user)
130126

131-
unless service.execute(list)
132-
render_api_error!({ error: 'List could not be deleted!' }, 400)
127+
destroy_conditionally!(list) do |list|
128+
service = ::Boards::Lists::DestroyService.new(user_project, current_user)
129+
unless service.execute(list)
130+
render_api_error!({ error: 'List could not be deleted!' }, 400)
131+
end
133132
end
134133
end
135134
end

lib/api/broadcast_messages.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ def find_message
9090
end
9191
delete ':id' do
9292
message = find_message
93-
check_unmodified_since(message.updated_at)
9493

95-
status 204
96-
message.destroy
94+
destroy_conditionally!(message)
9795
end
9896
end
9997
end

lib/api/deploy_keys.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ class DeployKeys < Grape::API
125125
key = user_project.deploy_keys_projects.find_by(deploy_key_id: params[:key_id])
126126
not_found!('Deploy Key') unless key
127127

128-
check_unmodified_since(key.updated_at)
129-
130-
status 204
131-
key.destroy
128+
destroy_conditionally!(key)
132129
end
133130
end
134131
end

lib/api/environments.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ class Environments < Grape::API
7878
authorize! :update_environment, user_project
7979

8080
environment = user_project.environments.find(params[:environment_id])
81-
check_unmodified_since(environment.updated_at)
8281

83-
status 204
84-
environment.destroy
82+
destroy_conditionally!(environment)
8583
end
8684

8785
desc 'Stops an existing environment' do

lib/api/groups.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,10 @@ def present_groups(groups, options = {})
117117
delete ":id" do
118118
group = find_group!(params[:id])
119119
authorize! :admin_group, group
120-
121-
check_unmodified_since(group.updated_at)
122120

123-
status 204
124-
::Groups::DestroyService.new(group, current_user).execute
121+
destroy_conditionally!(group) do |group|
122+
::Groups::DestroyService.new(group, current_user).execute
123+
end
125124
end
126125

127126
desc 'Get a list of projects in this group.' do

lib/api/helpers.rb

+14-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,25 @@ def declared_params(options = {})
1111
declared(params, options).to_h.symbolize_keys
1212
end
1313

14-
def check_unmodified_since(last_modified)
15-
if_unmodified_since = Time.parse(headers['If-Unmodified-Since']) if headers.key?('If-Unmodified-Since') rescue nil
14+
def check_unmodified_since!(last_modified)
15+
if_unmodified_since = Time.parse(headers['If-Unmodified-Since']) rescue nil
1616

17-
if if_unmodified_since && if_unmodified_since < last_modified
17+
if if_unmodified_since && last_modified > if_unmodified_since
1818
render_api_error!('412 Precondition Failed', 412)
1919
end
2020
end
2121

22+
def destroy_conditionally!(resource, last_update_field: :updated_at)
23+
check_unmodified_since!(resource.public_send(last_update_field))
24+
25+
status 204
26+
if block_given?
27+
yield resource
28+
else
29+
resource.destroy
30+
end
31+
end
32+
2233
def current_user
2334
return @current_user if defined?(@current_user)
2435

lib/api/issues.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,8 @@ def find_issues(args = {})
230230
not_found!('Issue') unless issue
231231

232232
authorize!(:destroy_issue, issue)
233-
check_unmodified_since(issue.updated_at)
234233

235-
status 204
236-
issue.destroy
234+
destroy_conditionally!(issue)
237235
end
238236

239237
desc 'List merge requests closing issue' do

lib/api/labels.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ class Labels < Grape::API
5656
label = user_project.labels.find_by(title: params[:name])
5757
not_found!('Label') unless label
5858

59-
check_unmodified_since(label.updated_at)
60-
61-
status 204
62-
label.destroy
59+
destroy_conditionally!(label)
6360
end
6461

6562
desc 'Update an existing label. At least one optional parameter is required.' do

lib/api/members.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,11 @@ class Members < Grape::API
9393
end
9494
delete ":id/members/:user_id" do
9595
source = find_source(source_type, params[:id])
96-
# Ensure that memeber exists
9796
member = source.members.find_by!(user_id: params[:user_id])
98-
check_unmodified_since(member.updated_at)
9997

100-
status 204
101-
::Members::DestroyService.new(source, current_user, declared_params).execute
98+
destroy_conditionally!(member) do
99+
::Members::DestroyService.new(source, current_user, declared_params).execute
100+
end
102101
end
103102
end
104103
end

lib/api/merge_requests.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,8 @@ def handle_merge_request_errors!(errors)
164164
merge_request = find_project_merge_request(params[:merge_request_iid])
165165

166166
authorize!(:destroy_merge_request, merge_request)
167-
check_unmodified_since(merge_request.updated_at)
168167

169-
status 204
170-
merge_request.destroy
168+
destroy_conditionally!(merge_request)
171169
end
172170

173171
params do

lib/api/notes.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ class Notes < Grape::API
131131
note = user_project.notes.find(params[:note_id])
132132

133133
authorize! :admin_note, note
134-
check_unmodified_since(note.updated_at)
135134

136-
status 204
137-
::Notes::DestroyService.new(user_project, current_user).execute(note)
135+
destroy_conditionally!(note) do |note|
136+
::Notes::DestroyService.new(user_project, current_user).execute(note)
137+
end
138138
end
139139
end
140140
end

lib/api/project_hooks.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ class ProjectHooks < Grape::API
9696
delete ":id/hooks/:hook_id" do
9797
hook = user_project.hooks.find(params.delete(:hook_id))
9898

99-
check_unmodified_since(hook.updated_at)
100-
101-
status 204
102-
hook.destroy
99+
destroy_conditionally!(hook)
103100
end
104101
end
105102
end

lib/api/project_snippets.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,8 @@ def snippets_for_current_user
116116
not_found!('Snippet') unless snippet
117117

118118
authorize! :admin_project_snippet, snippet
119-
check_unmodified_since(snippet.updated_at)
120119

121-
status 204
122-
snippet.destroy
120+
destroy_conditionally!(snippet)
123121
end
124122

125123
desc 'Get a raw project snippet'

lib/api/projects.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,10 @@ def present_projects(options = {})
334334
desc 'Remove a project'
335335
delete ":id" do
336336
authorize! :remove_project, user_project
337-
check_unmodified_since(user_project.updated_at)
338337

339-
::Projects::DestroyService.new(user_project, current_user, {}).async_execute
338+
destroy_conditionally!(user_project) do
339+
::Projects::DestroyService.new(user_project, current_user, {}).async_execute
340+
end
340341

341342
accepted!
342343
end

lib/api/runner.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ class Runner < Grape::API
4545
end
4646
delete '/' do
4747
authenticate_runner!
48-
status 204
49-
Ci::Runner.find_by_token(params[:token]).destroy
48+
49+
runner = Ci::Runner.find_by_token(params[:token])
50+
51+
destroy_conditionally!(runner)
5052
end
5153

5254
desc 'Validates authentication credentials' do

lib/api/runners.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ class Runners < Grape::API
7979
runner = get_runner(params[:id])
8080

8181
authenticate_delete_runner!(runner)
82-
check_unmodified_since(runner.updated_at)
8382

84-
status 204
85-
runner.destroy!
83+
destroy_conditionally!(runner)
8684
end
8785
end
8886

@@ -137,8 +135,7 @@ class Runners < Grape::API
137135
runner = runner_project.runner
138136
forbidden!("Only one project associated with the runner. Please remove the runner instead") if runner.projects.count == 1
139137

140-
status 204
141-
runner_project.destroy
138+
destroy_conditionally!(runner_project)
142139
end
143140
end
144141

lib/api/services.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ def service_attributes(service)
655655
end
656656
delete ":id/services/:service_slug" do
657657
service = user_project.find_or_initialize_service(params[:service_slug].underscore)
658-
# Todo, not sure
659-
check_unmodified_since(service.updated_at)
658+
# Todo: Check if this done the right way
659+
check_unmodified_since!(service.updated_at)
660660

661661
attrs = service_attributes(service).inject({}) do |hash, key|
662662
hash.merge!(key => nil)

lib/api/snippets.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ def public_snippets
122122
return not_found!('Snippet') unless snippet
123123

124124
authorize! :destroy_personal_snippet, snippet
125-
check_unmodified_since(snippet.updated_at)
126125

127-
status 204
128-
snippet.destroy
126+
destroy_conditionally!(snippet)
129127
end
130128

131129
desc 'Get a raw snippet' do

lib/api/system_hooks.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ class SystemHooks < Grape::API
6666
hook = SystemHook.find_by(id: params[:id])
6767
not_found!('System hook') unless hook
6868

69-
check_unmodified_since(hook.updated_at)
70-
71-
status 204
72-
hook.destroy
69+
destroy_conditionally!(hook)
7370
end
7471
end
7572
end

lib/api/triggers.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,7 @@ class Triggers < Grape::API
140140
trigger = user_project.triggers.find(params.delete(:trigger_id))
141141
return not_found!('Trigger') unless trigger
142142

143-
check_unmodified_since(trigger.updated_at)
144-
145-
status 204
146-
trigger.destroy
143+
destroy_conditionally!(trigger)
147144
end
148145
end
149146
end

0 commit comments

Comments
 (0)