From 7c24cea75c4f8cb9b41a0c1785a6a522ecd94e8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20=C5=81yp?= Date: Thu, 5 Sep 2024 21:04:07 +0200 Subject: [PATCH] Add asana log message action --- .../actions/asana_add_comment_action.rb | 16 ++-- .../actions/asana_log_message_action.rb | 90 +++++++++++++++++++ .../helper/ddg_apple_automation_helper.rb | 6 ++ spec/asana_add_comment_spec.rb | 8 +- 4 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 lib/fastlane/plugin/ddg_apple_automation/actions/asana_log_message_action.rb diff --git a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_add_comment_action.rb b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_add_comment_action.rb index d2698ad..411a940 100644 --- a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_add_comment_action.rb +++ b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_add_comment_action.rb @@ -13,10 +13,9 @@ def self.run(params) task_url = params[:task_url] template_name = params[:template_name] comment = params[:comment] - workflow_url = params[:workflow_url] begin - validate_params(task_id, task_url, comment, template_name, workflow_url) + validate_params(task_id, task_url, comment, template_name) rescue ArgumentError => e UI.user_error!(e.message) return @@ -25,10 +24,11 @@ def self.run(params) task_id = Fastlane::Actions::AsanaExtractTaskIdAction.run(task_url: task_url) if task_url if template_name.to_s.empty? - text = "#{comment}\n\nWorkflow URL: #{workflow_url}" + text = "#{comment}\n\nWorkflow URL: #{ENV.fetch('WORKFLOW_URL')}" create_story(asana_access_token, task_id, text: text) else - template_content = load_template_file(template_name) + template_file = Helper::DdgAppleAutomationHelper.path_for_asset_file("asana_add_comment/templates/#{template_name}.html") + template_content = Helper::DdgAppleAutomationHelper.load_template_file(template_file) return unless template_content html_text = process_template_content(template_content) @@ -71,10 +71,6 @@ def self.available_options FastlaneCore::ConfigItem.new(key: :template_name, description: "Name of a template file (without extension) for the comment. Templates can be found in assets/asana_add_comment/templates subdirectory. The file is processed before being sent to Asana", - optional: true, - type: String), - FastlaneCore::ConfigItem.new(key: :workflow_url, - description: "Workflow URL to include in the comment", optional: true, type: String) ] @@ -84,7 +80,7 @@ def self.is_supported?(platform) true end - def self.validate_params(task_id, task_url, comment, template_name, workflow_url) + def self.validate_params(task_id, task_url, comment, template_name) if task_id.to_s.empty? && task_url.to_s.empty? raise ArgumentError, "Both task_id and task_url cannot be empty. At least one must be provided." end @@ -93,7 +89,7 @@ def self.validate_params(task_id, task_url, comment, template_name, workflow_url raise ArgumentError, "Both comment and template_name cannot be empty. At least one must be provided." end - if comment && workflow_url.to_s.empty? + if comment && ENV.fetch('WORKFLOW_URL').to_s.empty? raise ArgumentError, "If comment is provided, workflow_url cannot be empty" end end diff --git a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_log_message_action.rb b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_log_message_action.rb new file mode 100644 index 0000000..6bec361 --- /dev/null +++ b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_log_message_action.rb @@ -0,0 +1,90 @@ +require "fastlane/action" +require "fastlane_core/configuration/config_item" +require "asana" +require_relative "../helper/ddg_apple_automation_helper" +require_relative "asana_add_comment_action" +require_relative "asana_get_release_automation_subtask_id_action" +require_relative "asana_get_user_id_for_github_handle_action" + +module Fastlane + module Actions + class AsanaLogMessageAction < Action + def self.run(params) + asana_access_token = params[:asana_access_token] + task_url = params[:task_url] + template_name = params[:template_name] + comment = params[:comment] + is_scheduled_release = params[:is_scheduled_release] || true + github_handle = params[:github_handle] + + automation_subtask_id = AsanaGetReleaseAutomationSubtaskIdAction.run(task_url: task_url, asana_access_token: token) + + if is_scheduled_release + task_id = AsanaExtractTaskIdAction.run(task_url: task_url) + assignee_id = AsanaExtractTaskAssigneeAction.run(task_id: task_id, asana_access_token: token) + else + assignee_id = AsanaGetUserIdForGithubHandleAction.run(github_handle: github_handle, asana_access_token: token) + end + + asana_client = Asana::Client.new do |c| + c.authentication(:access_token, asana_access_token) + end + + begin + asana_client.tasks.add_followers_for_task(task_gid: task_id, followers: [assignee_id]) + rescue StandardError => e + UI.user_error!("Failed to add a collaborator to the release task: #{e}") + end + + AsanaAddCommentAction.run(task_id: automation_subtask_id, comment: comment, template_name: template_name, asana_access_token: token) + end + + def self.description + "Add a Message to Asana Release Automation Task" + end + + def self.authors + ["DuckDuckGo"] + end + + def self.return_value + "" + end + + def self.details + "Adds a comment about release progress to the Asana release task's 'Automation' subtask" + end + + def self.available_options + [ + FastlaneCore::ConfigItem.asana_access_token, + FastlaneCore::ConfigItem.new(key: :task_url, + description: "Asana release task URL", + optional: false, + type: String), + FastlaneCore::ConfigItem.new(key: :comment, + description: "Comment to add to the Asana task", + optional: true, + type: String), + FastlaneCore::ConfigItem.new(key: :template_name, + description: "Name of a template file (without extension) for the comment. Templates can be found in assets/asana_add_comment/templates subdirectory. + The file is processed before being sent to Asana", + optional: true, + type: String), + FastlaneCore::ConfigItem.new(key: :github_handle, + description: "Github user handle", + optional: true, + type: String), + FastlaneCore::ConfigItem.new(key: :is_scheduled_release, + description: "Indicates whether the release was scheduled or started manually", + optional: true, + type: Boolean) + ] + end + + def self.is_supported?(platform) + true + end + end + end +end diff --git a/lib/fastlane/plugin/ddg_apple_automation/helper/ddg_apple_automation_helper.rb b/lib/fastlane/plugin/ddg_apple_automation/helper/ddg_apple_automation_helper.rb index d0f8320..8cf00f7 100644 --- a/lib/fastlane/plugin/ddg_apple_automation/helper/ddg_apple_automation_helper.rb +++ b/lib/fastlane/plugin/ddg_apple_automation/helper/ddg_apple_automation_helper.rb @@ -22,6 +22,12 @@ def self.asana_task_url(task_id) def self.path_for_asset_file(file) File.expand_path("../assets/#{file}", __dir__) end + + def self.load_template_file(template_file) + File.read(template_file) + rescue StandardError + UI.user_error!("Error: The file '#{template_file}' does not exist.") + end end end end diff --git a/spec/asana_add_comment_spec.rb b/spec/asana_add_comment_spec.rb index 3bc8a48..ad73522 100644 --- a/spec/asana_add_comment_spec.rb +++ b/spec/asana_add_comment_spec.rb @@ -36,8 +36,12 @@ end it "shows error if comment is provided but workflow_url is not" do - expect(Fastlane::UI).to receive(:user_error!).with("If comment is provided, workflow_url cannot be empty") - test_action(task_id: "123", comment: "comment") + ClimateControl.modify( + WORKFLOW_URL: '' + ) do + expect(Fastlane::UI).to receive(:user_error!).with("If comment is provided, workflow_url cannot be empty") + test_action(task_id: "123", comment: "comment") + end end it "shows error if provided template does not exist" do