From 88ad09effe4149900cfbe0b1bd7342c331720587 Mon Sep 17 00:00:00 2001 From: Chris Zhu Date: Wed, 1 Jan 2025 13:31:47 -0800 Subject: [PATCH] updated environment variables via file paste --- .../parse_text_content.rb | 19 +++++++++++ app/actions/environment_variables/update.rb | 10 ++++++ .../environment_variables_controller.rb | 32 +++++++++--------- .../environment_variables_controller.js | 11 +++++++ app/javascript/controllers/index.js | 4 +++ .../environment_variables/index.html.erb | 33 +++++++++++++++++-- package.json | 1 + yarn.lock | 5 +++ 8 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 app/actions/environment_variables/parse_text_content.rb create mode 100644 app/actions/environment_variables/update.rb diff --git a/app/actions/environment_variables/parse_text_content.rb b/app/actions/environment_variables/parse_text_content.rb new file mode 100644 index 00000000..c27b8513 --- /dev/null +++ b/app/actions/environment_variables/parse_text_content.rb @@ -0,0 +1,19 @@ +class EnvironmentVariables::ParseTextContent + extend LightService::Action + + expects :params + promises :params + + executed do |context| + unless context.params[:text_content].present? + next + end + + context.params[:environment_variables] = context.params[:text_content].strip.split("\n").map do |line| + name, value = line.split("=") + # If the value is in quotes, remove them + value = value.gsub(/^"|"$/, "") if value.present? + { name:, value: } + end + end +end diff --git a/app/actions/environment_variables/update.rb b/app/actions/environment_variables/update.rb new file mode 100644 index 00000000..1cce5263 --- /dev/null +++ b/app/actions/environment_variables/update.rb @@ -0,0 +1,10 @@ +class EnvironmentVariables::Update + extend LightService::Organizer + + def self.call(project:, params:, current_user:) + with(project:, params:, current_user:).reduce( + EnvironmentVariables::ParseTextContent, + EnvironmentVariables::BulkUpdate + ) + end +end \ No newline at end of file diff --git a/app/controllers/projects/environment_variables_controller.rb b/app/controllers/projects/environment_variables_controller.rb index 66729fa0..a5e6e2be 100644 --- a/app/controllers/projects/environment_variables_controller.rb +++ b/app/controllers/projects/environment_variables_controller.rb @@ -8,17 +8,25 @@ class Projects::EnvironmentVariablesController < Projects::BaseController end def create - EnvironmentVariables::BulkUpdate.execute(project: @project, params:, current_user:) - @project.updated! + result = EnvironmentVariables::Update.call( + project: @project, + params:, + current_user: + ) + if result.success? + @project.updated! - if @project.current_deployment.present? - Projects::DeploymentJob.perform_later(@project.current_deployment) - @project.events.create(user: current_user, eventable: @project.last_build, event_action: :update) - redirect_to project_environment_variables_path(@project), - notice: "Deployment started to apply new environment variables." + if @project.current_deployment.present? + Projects::DeploymentJob.perform_later(@project.current_deployment) + @project.events.create(user: current_user, eventable: @project.last_build, event_action: :update) + redirect_to project_environment_variables_path(@project), + notice: "Restarting services with new environment variables." + else + redirect_to project_environment_variables_path(@project), + notice: "Environment variables will be applied on the next deployment." + end else - redirect_to project_environment_variables_path(@project), - notice: "Environment variables will be applied on the next deployment." + redirect_to project_environment_variables_path(@project), alert: "Failed to update environment variables." end end @@ -30,10 +38,4 @@ class Projects::EnvironmentVariablesController < Projects::BaseController end render turbo_stream: turbo_stream.remove("environment_variable_#{@environment_variable.id}") end - - private - - def environment_variable_params - params.require(:environment_variable).permit(:name, :value) - end end diff --git a/app/javascript/controllers/environment_variables_controller.js b/app/javascript/controllers/environment_variables_controller.js index 9b8d8a54..e8190a4b 100644 --- a/app/javascript/controllers/environment_variables_controller.js +++ b/app/javascript/controllers/environment_variables_controller.js @@ -19,6 +19,17 @@ export default class extends Controller { }) } + download() { + const vars = JSON.parse(this.varsValue) + const env = vars.map(v => `${v.name}=${v.value}`).join("\n") + const blob = new Blob([env], { type: "text/plain" }) + const url = URL.createObjectURL(blob) + const a = document.createElement("a") + a.href = url + a.download = ".env" + a.click() + } + add(e) { e.preventDefault(); this._add("", "") diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js index a538cbe0..1164b515 100644 --- a/app/javascript/controllers/index.js +++ b/app/javascript/controllers/index.js @@ -1,5 +1,9 @@ import { application } from "./application" +import TextareaAutogrow from 'stimulus-textarea-autogrow' + import controllers from './**/*_controller.js' + +application.register('textarea-autogrow', TextareaAutogrow) controllers.forEach((controller) => { application.register(controller.name, controller.module.default) }) diff --git a/app/views/projects/environment_variables/index.html.erb b/app/views/projects/environment_variables/index.html.erb index 8997e85e..36614622 100644 --- a/app/views/projects/environment_variables/index.html.erb +++ b/app/views/projects/environment_variables/index.html.erb @@ -7,8 +7,37 @@ > <%= form_with(url: project_environment_variables_path(@project), method: :post) do |form| %>
- - <%= form.submit "Save", class: "btn btn-primary" %> +
+
+ + <%= form.submit "Save", class: "btn btn-primary" %> +
+ +
<% end %> + +
+
+ +
Add from .env file
+
+
+ <%= form_with(url: project_environment_variables_path(@project), method: :post) do |form| %> + <%= form.text_area( + :text_content, + class: "textarea textarea-bordered w-full", + placeholder: "KEY1=value1\nKEY2=value2", + rows: 6, + data: { + controller: "textarea-autogrow", + 'textarea-autogrow-resize-debounce-delay-value': "500" + } + ) %> + <%= form.submit "Add variables", class: "btn btn-primary" %> + <% end %> +
+
+
+
<% end %> diff --git a/package.json b/package.json index c57f8293..fa69683d 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "prettier-plugin-tailwindcss": "^0.6.8", "sass": "^1.79.3", "stimulus-flatpickr": "^3.0.0-0", + "stimulus-textarea-autogrow": "^4.1.0", "tailwindcss": "^3.4.1", "tailwindcss-stimulus-components": "^5.1.0", "tippy.js": "^6.3.7", diff --git a/yarn.lock b/yarn.lock index 801c178a..f752f19a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1384,6 +1384,11 @@ stimulus-flatpickr@^3.0.0-0: dependencies: "@hotwired/stimulus" "^3.0.0" +stimulus-textarea-autogrow@^4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/stimulus-textarea-autogrow/-/stimulus-textarea-autogrow-4.1.0.tgz#faa2f7952062c4508b1566478dc203af7f24f632" + integrity sha512-hYz+xN3VZiO+eLl+zok4yvhH+mW5WkvOJEExyVP3J97dWQzc/qPDFdAM1EDJvfUIuRzJVq/WJynqqQwiied5iw== + "string-width-cjs@npm:string-width@^4.2.0": version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"