Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1282 from Shopify/add-lock-and-archive-in-stack-u…
…pdate-api

Add lock and archive functionality to the stack update API call
  • Loading branch information
xiaoxipang committed Sep 15, 2022
2 parents bcf8adf + 1ac6936 commit 1695e86
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
26 changes: 25 additions & 1 deletion app/controllers/shipit/api/stacks_controller.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions test/controllers/api/stacks_controller_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 1695e86

Please sign in to comment.