From 1ac6936e644b2a1bf5ba89d463411f30f1954fc9 Mon Sep 17 00:00:00 2001 From: Mikail Karimi Date: Tue, 13 Sep 2022 16:56:16 -0400 Subject: [PATCH] Add lock and archive functionality to the stack update API call Add more tests to guarantee right behiviour of API --- .../shipit/api/stacks_controller.rb | 26 +++++++++- .../controllers/api/stacks_controller_test.rb | 51 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/app/controllers/shipit/api/stacks_controller.rb b/app/controllers/shipit/api/stacks_controller.rb index 112a4effb..ffcec1215 100644 --- a/app/controllers/shipit/api/stacks_controller.rb +++ b/app/controllers/shipit/api/stacks_controller.rb @@ -46,9 +46,13 @@ def create accepts :ignore_ci, Boolean accepts :merge_queue_enabled, Boolean accepts :continuous_deployment, Boolean + accepts :archived, Boolean end def update - stack.update(params) + stack.update(update_params) + + update_archived + render_resource(stack) end @@ -78,6 +82,26 @@ def stack @stack ||= stacks.from_param!(params[:id]) end + def update_archived + if key?(:archived) + if params[:archived] + stack.archive!(nil) + elsif stack.archived? + stack.unarchive! + end + end + end + + def key?(key) + params.to_h.key?(key) + end + + def update_params + params.select do |key, _| + %i(environment branch deploy_url ignore_ci merge_queue_enabled continuous_deployment).include?(key) + end + end + def repository @repository ||= Repository.find_or_create_by(owner: repo_owner, name: repo_name) end diff --git a/test/controllers/api/stacks_controller_test.rb b/test/controllers/api/stacks_controller_test.rb index 5509d8e29..0586d221a 100644 --- a/test/controllers/api/stacks_controller_test.rb +++ b/test/controllers/api/stacks_controller_test.rb @@ -114,6 +114,57 @@ class StacksControllerTest < ApiControllerTestCase refute @stack.continuous_deployment end + test "#update does not perform archive when key is not provided" do + refute_predicate @stack, :archived? + refute_predicate @stack, :locked? + + patch :update, params: { id: @stack.to_param } + + @stack.reload + refute_predicate @stack, :archived? + refute_predicate @stack, :locked? + end + + test "#update does not perform unarchive when key is not provided" do + @stack.archive!(shipit_users(:walrus)) + assert_predicate @stack, :locked? + assert_predicate @stack, :archived? + + patch :update, params: { id: @stack.to_param } + + @stack.reload + assert_predicate @stack, :locked? + assert_predicate @stack, :archived? + end + + test "#update allows to archive the stack" do + refute_predicate @stack, :archived? + refute_predicate @stack, :locked? + + patch :update, params: { id: @stack.to_param, archived: true } + + @stack.reload + assert_predicate @stack, :locked? + assert_predicate @stack, :archived? + assert_instance_of AnonymousUser, @stack.lock_author + assert_equal "Archived", @stack.lock_reason + end + + test "#update allows to unarchive the stack" do + @stack.archive!(shipit_users(:walrus)) + assert_predicate @stack, :locked? + assert_predicate @stack, :archived? + + patch :update, params: { id: @stack.to_param, archived: false } + + @stack.reload + refute_predicate @stack, :archived? + refute_predicate @stack, :locked? + assert_nil @stack.locked_since + assert_nil @stack.lock_reason + assert_instance_of AnonymousUser, @stack.lock_author + end + test "#index returns a list of stacks" do stack = Stack.last get :index