From f9a7180050d344e622c7b74fb584d0216648dd01 Mon Sep 17 00:00:00 2001 From: Dominik Kapusta Date: Fri, 13 Sep 2024 14:48:53 +0200 Subject: [PATCH] Move asana_extract_task_id action body to a helper function --- .../actions/asana_add_comment_action.rb | 2 +- .../asana_create_action_item_action.rb | 2 +- .../actions/asana_extract_task_id_action.rb | 13 +----- ...et_release_automation_subtask_id_action.rb | 2 +- .../actions/asana_log_message_action.rb | 2 +- .../helper/ddg_apple_automation_helper.rb | 12 ++++++ spec/asana_add_comment_action_spec.rb | 10 ++--- spec/asana_create_action_item_action_spec.rb | 4 +- spec/asana_extract_task_id_action_spec.rb | 33 ++------------- spec/asana_log_message_action_spec.rb | 4 +- spec/ddg_apple_automation_helper_spec.rb | 40 +++++++++++++++++++ 11 files changed, 68 insertions(+), 56 deletions(-) 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 d2aaa13..13f3881 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 @@ -23,7 +23,7 @@ def self.run(params) return end - task_id = AsanaExtractTaskIdAction.run(task_url: task_url) if task_url + task_id = Helper::DdgAppleAutomationHelper.extract_asana_task_id(task_url) if task_url if template_name.to_s.empty? text = "#{comment}\n\nWorkflow URL: #{workflow_url}" diff --git a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_create_action_item_action.rb b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_create_action_item_action.rb index 040410c..f278ecc 100644 --- a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_create_action_item_action.rb +++ b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_create_action_item_action.rb @@ -23,7 +23,7 @@ def self.run(params) is_scheduled_release = params[:is_scheduled_release] github_handle = params[:github_handle] - task_id = AsanaExtractTaskIdAction.run(task_url: task_url) + task_id = Helper::DdgAppleAutomationHelper.extract_asana_task_id(task_url) automation_subtask_id = AsanaGetReleaseAutomationSubtaskIdAction.run(task_url: task_url, asana_access_token: token) if is_scheduled_release assignee_id = AsanaExtractTaskAssigneeAction.run(task_id: task_id, asana_access_token: token) diff --git a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_extract_task_id_action.rb b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_extract_task_id_action.rb index 15dce83..1204fa4 100644 --- a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_extract_task_id_action.rb +++ b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_extract_task_id_action.rb @@ -6,19 +6,8 @@ module Fastlane module Actions class AsanaExtractTaskIdAction < Action - TASK_URL_REGEX = %r{https://app.asana.com/[0-9]/[0-9]+/([0-9]+)(:/f)?} - ERROR_MESSAGE = "URL has incorrect format (attempted to match #{TASK_URL_REGEX})" - def self.run(params) - task_url = params[:task_url] - - if (match = task_url.match(TASK_URL_REGEX)) - task_id = match[1] - Helper::GitHubActionsHelper.set_output("asana_task_id", task_id) - task_id - else - UI.user_error!(ERROR_MESSAGE) - end + Helper::DdgAppleAutomationHelper.extract_asana_task_id(params[:task_url]) end def self.description diff --git a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_get_release_automation_subtask_id_action.rb b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_get_release_automation_subtask_id_action.rb index 8bb1eb5..21f2759 100644 --- a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_get_release_automation_subtask_id_action.rb +++ b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_get_release_automation_subtask_id_action.rb @@ -13,7 +13,7 @@ def self.run(params) task_url = params[:task_url] token = params[:asana_access_token] - task_id = AsanaExtractTaskIdAction.run(task_url: task_url, asana_access_token: token) + task_id = Helper::DdgAppleAutomationHelper.extract_asana_task_id(task_url) # Fetch release task assignee and set GHA output. # This is to match current GHA action behavior. 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 index 9a12a92..56393bf 100644 --- 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 @@ -22,7 +22,7 @@ def self.run(params) 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) + task_id = Helper::DdgAppleAutomationHelper.extract_asana_task_id(task_url) assignee_id = AsanaExtractTaskAssigneeAction.run(task_id: task_id, asana_access_token: token) else if github_handle.to_s.empty? 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 d8b30e8..37f18f1 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 @@ -1,5 +1,6 @@ require "fastlane_core/configuration/config_item" require "fastlane_core/ui/ui" +require_relative "github_actions_helper" module Fastlane UI = FastlaneCore::UI unless Fastlane.const_defined?(:UI) @@ -7,6 +8,7 @@ module Fastlane module Helper class DdgAppleAutomationHelper ASANA_APP_URL = "https://app.asana.com/0/0" + ASANA_TASK_URL_REGEX = %r{https://app.asana.com/[0-9]/[0-9]+/([0-9]+)(:/f)?} ERROR_ASANA_ACCESS_TOKEN_NOT_SET = "ASANA_ACCESS_TOKEN is not set" ERROR_GITHUB_TOKEN_NOT_SET = "GITHUB_TOKEN is not set" @@ -18,6 +20,16 @@ def self.asana_task_url(task_id) "#{ASANA_APP_URL}/#{task_id}/f" end + def self.extract_asana_task_id(task_url) + if (match = task_url.match(ASANA_TASK_URL_REGEX)) + task_id = match[1] + Helper::GitHubActionsHelper.set_output("asana_task_id", task_id) + task_id + else + UI.user_error!("URL has incorrect format (attempted to match #{ASANA_TASK_URL_REGEX})") + end + end + def self.path_for_asset_file(file) File.expand_path("../assets/#{file}", __dir__) end diff --git a/spec/asana_add_comment_action_spec.rb b/spec/asana_add_comment_action_spec.rb index 17f4aa3..e69dfb8 100644 --- a/spec/asana_add_comment_action_spec.rb +++ b/spec/asana_add_comment_action_spec.rb @@ -12,19 +12,17 @@ end it "does not call task id extraction if task id provided" do - allow(Fastlane::Actions::AsanaExtractTaskIdAction).to receive(:run) + allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:extract_asana_task_id) allow(@asana_client_stories).to receive(:create_story_for_task).and_return(double) test_action(task_id: "123", comment: "comment") - expect(Fastlane::Actions::AsanaExtractTaskIdAction).not_to have_received(:run) + expect(Fastlane::Helper::DdgAppleAutomationHelper).not_to have_received(:extract_asana_task_id) end it "extracts task id if task id not provided" do allow(@asana_client_stories).to receive(:create_story_for_task).and_return(double) - allow(Fastlane::Actions::AsanaExtractTaskIdAction).to receive(:run) + allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:extract_asana_task_id) test_action(task_url: "https://app.asana.com/0/753241/9999", comment: "comment") - expect(Fastlane::Actions::AsanaExtractTaskIdAction).to have_received(:run).with( - task_url: "https://app.asana.com/0/753241/9999" - ) + expect(Fastlane::Helper::DdgAppleAutomationHelper).to have_received(:extract_asana_task_id).with("https://app.asana.com/0/753241/9999") end it "shows error if both task id and task url are not provided" do diff --git a/spec/asana_create_action_item_action_spec.rb b/spec/asana_create_action_item_action_spec.rb index 2f0a300..ced0800 100644 --- a/spec/asana_create_action_item_action_spec.rb +++ b/spec/asana_create_action_item_action_spec.rb @@ -15,7 +15,7 @@ allow(Asana::Client).to receive(:new).and_return(asana_client) allow(asana_client).to receive(:tasks).and_return(@asana_client_tasks) - allow(Fastlane::Actions::AsanaExtractTaskIdAction).to receive(:run).and_return(task_id) + allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:extract_asana_task_id).and_return(task_id) allow(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run).and_return(assignee_id) allow(Fastlane::Actions::AsanaGetReleaseAutomationSubtaskIdAction).to receive(:run).with(task_url: task_url, asana_access_token: anything).and_return(automation_subtask_id) allow(Fastlane::Actions::AsanaGetUserIdForGithubHandleAction).to receive(:run).and_return(assignee_id) @@ -25,7 +25,7 @@ end it "extracts assignee id from release task when is scheduled release" do - expect(Fastlane::Actions::AsanaExtractTaskIdAction).to receive(:run).with(task_url: task_url) + expect(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:extract_asana_task_id).with(task_url) expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run).with( task_id: task_id, asana_access_token: anything diff --git a/spec/asana_extract_task_id_action_spec.rb b/spec/asana_extract_task_id_action_spec.rb index 427765a..b8fbd23 100644 --- a/spec/asana_extract_task_id_action_spec.rb +++ b/spec/asana_extract_task_id_action_spec.rb @@ -1,37 +1,10 @@ describe Fastlane::Actions::AsanaExtractTaskIdAction do describe "#run" do - it "extracts task ID" do + it "calls helper" do + expect(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:extract_asana_task_id) + .with("https://app.asana.com/0/0/0").and_return("0") expect(test_action("https://app.asana.com/0/0/0")).to eq("0") end - - it "extracts task ID when project ID is non-zero" do - expect(test_action("https://app.asana.com/0/753241/9999")).to eq("9999") - end - - it "extracts task ID when first digit is non-zero" do - expect(test_action("https://app.asana.com/4/753241/9999")).to eq("9999") - end - - it "extracts long task ID" do - expect(test_action("https://app.asana.com/0/0/12837864576817392")).to eq("12837864576817392") - end - - it "extracts task ID from a URL with a trailing /f" do - expect(test_action("https://app.asana.com/0/0/1234/f")).to eq("1234") - end - - it "sets GHA output" do - allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output) - - expect(test_action("https://app.asana.com/0/12837864576817392/3465387322")).to eq("3465387322") - expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_task_id", "3465387322") - end - - it "fails when garbage is passed" do - expect(Fastlane::UI).to receive(:user_error!).with(Fastlane::Actions::AsanaExtractTaskIdAction::ERROR_MESSAGE) - - test_action("not a URL") - end end def test_action(task_url) diff --git a/spec/asana_log_message_action_spec.rb b/spec/asana_log_message_action_spec.rb index c17a900..778286e 100644 --- a/spec/asana_log_message_action_spec.rb +++ b/spec/asana_log_message_action_spec.rb @@ -14,7 +14,7 @@ allow(asana_client).to receive(:tasks).and_return(@asana_client_tasks) allow(Fastlane::Actions::AsanaGetReleaseAutomationSubtaskIdAction).to receive(:run).and_return(automation_subtask_id) - allow(Fastlane::Actions::AsanaExtractTaskIdAction).to receive(:run).and_return(task_id) + allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:extract_asana_task_id).and_return(task_id) allow(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run).and_return(assignee_id) allow(Fastlane::Actions::AsanaGetUserIdForGithubHandleAction).to receive(:run).and_return(assignee_id) allow(@asana_client_tasks).to receive(:add_followers_for_task) @@ -22,7 +22,7 @@ end it "extracts assignee id from release task when is scheduled release" do - expect(Fastlane::Actions::AsanaExtractTaskIdAction).to receive(:run).with(task_url: task_url) + expect(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:extract_asana_task_id).with(task_url) expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run).with( task_id: task_id, asana_access_token: anything diff --git a/spec/ddg_apple_automation_helper_spec.rb b/spec/ddg_apple_automation_helper_spec.rb index 41b4563..5b983c0 100644 --- a/spec/ddg_apple_automation_helper_spec.rb +++ b/spec/ddg_apple_automation_helper_spec.rb @@ -18,6 +18,46 @@ def asana_task_url(task_id) end end + describe "#extract_asana_task_id" do + it "extracts task ID" do + expect(extract_asana_task_id("https://app.asana.com/0/0/0")).to eq("0") + end + + it "extracts task ID when project ID is non-zero" do + expect(extract_asana_task_id("https://app.asana.com/0/753241/9999")).to eq("9999") + end + + it "extracts task ID when first digit is non-zero" do + expect(extract_asana_task_id("https://app.asana.com/4/753241/9999")).to eq("9999") + end + + it "extracts long task ID" do + expect(extract_asana_task_id("https://app.asana.com/0/0/12837864576817392")).to eq("12837864576817392") + end + + it "extracts task ID from a URL with a trailing /f" do + expect(extract_asana_task_id("https://app.asana.com/0/0/1234/f")).to eq("1234") + end + + it "sets GHA output" do + allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output) + + expect(extract_asana_task_id("https://app.asana.com/0/12837864576817392/3465387322")).to eq("3465387322") + expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_task_id", "3465387322") + end + + it "fails when garbage is passed" do + expect(Fastlane::UI).to receive(:user_error!) + .with("URL has incorrect format (attempted to match #{Fastlane::Helper::DdgAppleAutomationHelper::ASANA_TASK_URL_REGEX})") + + extract_asana_task_id("not a URL") + end + + def extract_asana_task_id(task_url) + Fastlane::Helper::DdgAppleAutomationHelper.extract_asana_task_id(task_url) + end + end + describe "#load_file" do it "shows error if provided file does not exist" do allow(Fastlane::UI).to receive(:user_error!)