From 822679696ed9b70f9534fecee3178d0fa3716607 Mon Sep 17 00:00:00 2001 From: Oleksiy Kovyrin Date: Thu, 14 Dec 2023 15:01:31 -0500 Subject: [PATCH 1/3] Allow overrides for default task values via URL parameters --- app/controllers/shipit/tasks_controller.rb | 1 + app/models/shipit/task_definition.rb | 8 ++++++++ app/models/shipit/variable_definition.rb | 8 ++++++++ app/views/shipit/_variables.html.erb | 2 +- test/controllers/tasks_controller_test.rb | 9 ++++++++- 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/controllers/shipit/tasks_controller.rb b/app/controllers/shipit/tasks_controller.rb index 6a95d84b7..18bdd337d 100644 --- a/app/controllers/shipit/tasks_controller.rb +++ b/app/controllers/shipit/tasks_controller.rb @@ -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 diff --git a/app/models/shipit/task_definition.rb b/app/models/shipit/task_definition.rb index bc814709b..4d1bd2704 100644 --- a/app/models/shipit/task_definition.rb +++ b/app/models/shipit/task_definition.rb @@ -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) + variables.each do |var| + if params.key?(var.name) + var.override_value(params[var.name]) + end + end + end + def allow_concurrency? @allow_concurrency end diff --git a/app/models/shipit/variable_definition.rb b/app/models/shipit/variable_definition.rb index 5093e81f2..6d0e0a32a 100644 --- a/app/models/shipit/variable_definition.rb +++ b/app/models/shipit/variable_definition.rb @@ -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, diff --git a/app/views/shipit/_variables.html.erb b/app/views/shipit/_variables.html.erb index c87397154..63b8b66ce 100644 --- a/app/views/shipit/_variables.html.erb +++ b/app/views/shipit/_variables.html.erb @@ -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 %> <% end %> <%= field.label variable.name, variable.title || variable.name %>

diff --git a/test/controllers/tasks_controller_test.rb b/test/controllers/tasks_controller_test.rb index adbd9355c..ff5fc08c2 100644 --- a/test/controllers/tasks_controller_test.rb +++ b/test/controllers/tasks_controller_test.rb @@ -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 From 4b5ac2b3d9d7d9d8ce49a2559fca46d9548a8495 Mon Sep 17 00:00:00 2001 From: Oleksiy Kovyrin Date: Thu, 14 Dec 2023 15:41:40 -0500 Subject: [PATCH 2/3] Update app/views/shipit/_variables.html.erb Co-authored-by: Jean byroot Boussier --- app/views/shipit/_variables.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/shipit/_variables.html.erb b/app/views/shipit/_variables.html.erb index 63b8b66ce..a43dd6c93 100644 --- a/app/views/shipit/_variables.html.erb +++ b/app/views/shipit/_variables.html.erb @@ -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.value %> + <%= field.text_field variable.name, value: params[variable.name].presence || variable.default %> <% end %> <%= field.label variable.name, variable.title || variable.name %>

From d9785f7a43c5cfd1aa1c34a63c6798c8c3a7f3e5 Mon Sep 17 00:00:00 2001 From: Oleksiy Kovyrin Date: Thu, 14 Dec 2023 15:43:13 -0500 Subject: [PATCH 3/3] Allow override in the view --- app/controllers/shipit/tasks_controller.rb | 1 - app/models/shipit/task_definition.rb | 8 -------- app/models/shipit/variable_definition.rb | 8 -------- 3 files changed, 17 deletions(-) diff --git a/app/controllers/shipit/tasks_controller.rb b/app/controllers/shipit/tasks_controller.rb index 18bdd337d..6a95d84b7 100644 --- a/app/controllers/shipit/tasks_controller.rb +++ b/app/controllers/shipit/tasks_controller.rb @@ -16,7 +16,6 @@ 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 diff --git a/app/models/shipit/task_definition.rb b/app/models/shipit/task_definition.rb index 4d1bd2704..bc814709b 100644 --- a/app/models/shipit/task_definition.rb +++ b/app/models/shipit/task_definition.rb @@ -40,14 +40,6 @@ 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) - variables.each do |var| - if params.key?(var.name) - var.override_value(params[var.name]) - end - end - end - def allow_concurrency? @allow_concurrency end diff --git a/app/models/shipit/variable_definition.rb b/app/models/shipit/variable_definition.rb index 6d0e0a32a..5093e81f2 100644 --- a/app/models/shipit/variable_definition.rb +++ b/app/models/shipit/variable_definition.rb @@ -15,14 +15,6 @@ 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,