Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overrides for default task values via URL parameters #1320

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/shipit/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def index
def new
@definition = stack.find_task_definition(params[:definition_id])
@task = stack.tasks.build(definition: @definition)
@task.definition.override_variables(params)
end

def show
Expand Down
8 changes: 8 additions & 0 deletions app/models/shipit/task_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def render_title(env)
"This task (title: #{@title}) cannot be shown due to an incorrect variable name. Check your shipit.yml file"
end

def override_variables(params)
kovyrin marked this conversation as resolved.
Show resolved Hide resolved
variables.each do |var|
if params.key?(var.name)
var.override_value(params[var.name])
end
end
end

def allow_concurrency?
@allow_concurrency
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/shipit/variable_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def default_provided?
@default_provided
end

def override_value(value)
@override = value.to_s
end

def value
@override.presence || default
end

def to_h
{
'name' => @name,
Expand Down
2 changes: 1 addition & 1 deletion app/views/shipit/_variables.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<% if variable.select %>
<%= field.select variable.name, options_for_select([["Please select...", { disabled: "disabled" }]] + variable.select, variable.default || "Please select...") %>
<% else %>
<%= field.text_field variable.name, value: variable.default %>
<%= field.text_field variable.name, value: variable.value %>
kovyrin marked this conversation as resolved.
Show resolved Hide resolved
<% end %>
<%= field.label variable.name, variable.title || variable.name %>
</p>
Expand Down
9 changes: 8 additions & 1 deletion test/controllers/tasks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ class TasksControllerTest < ActionController::TestCase
session[:user_id] = shipit_users(:walrus).id
end

test "tasks defined in the shipit.yml can be displayed" do
test "tasks defined in the shipit.yml can be displayed with default variable values" do
get :new, params: { stack_id: @stack, definition_id: @definition.id }
assert_response :ok
assert_select 'input[name="task[env][FOO]"][value="1"]'
end

test "it is possible to provide a default value override for a task" do
get :new, params: { stack_id: @stack, definition_id: @definition.id, FOO: '42' }
assert_response :ok
assert_select 'input[name="task[env][FOO]"][value="42"]'
end

test "tasks defined in the shipit.yml can't be triggered if the stack is being deployed" do
Expand Down