From 8b724567388659f7ec02574f7dcd297bafcd2ebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20=C5=81yp?= Date: Fri, 6 Sep 2024 18:40:10 +0200 Subject: [PATCH] Update specs --- ...et_release_automation_subtask_id_action.rb | 15 +--- ...asana_extract_task_assignee_action_spec.rb | 2 +- spec/asana_find_release_task_action_spec.rb | 2 +- ...lease_automation_subtask_id_action_spec.rb | 85 +++++-------------- 4 files changed, 25 insertions(+), 79 deletions(-) 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 d1f2068..8bb1eb5 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 @@ -1,7 +1,5 @@ require "fastlane/action" require "fastlane_core/configuration/config_item" -require "httparty" -require "json" require "time" require_relative "../helper/ddg_apple_automation_helper" require_relative "../helper/github_actions_helper" @@ -33,7 +31,7 @@ def self.run(params) return end - automation_subtask_id = find_oldest_automation_subtask(subtasks).gid + automation_subtask_id = find_oldest_automation_subtask(subtasks)&.gid Helper::GitHubActionsHelper.set_output("asana_automation_task_id", automation_subtask_id) automation_subtask_id end @@ -70,14 +68,9 @@ def self.is_supported?(platform) end def self.find_oldest_automation_subtask(subtasks) - automation_subtask = subtasks - .find_all { |task| task.name == 'Automation' } - &.min_by { |task| Time.parse(task.created_at) } - if automation_subtask.nil? - UI.user_error!("There is no 'Automation' subtask in task: #{task_id}") - return - end - automation_subtask + subtasks + .find_all { |task| task.name == 'Automation' } + &.min_by { |task| Time.parse(task.created_at) } end end end diff --git a/spec/asana_extract_task_assignee_action_spec.rb b/spec/asana_extract_task_assignee_action_spec.rb index 47df518..fd5e30d 100644 --- a/spec/asana_extract_task_assignee_action_spec.rb +++ b/spec/asana_extract_task_assignee_action_spec.rb @@ -2,7 +2,7 @@ describe "#run" do before do @asana_client_tasks = double - asana_client = double("Asana::Client") + asana_client = double("asana_client") allow(Asana::Client).to receive(:new).and_return(asana_client) allow(asana_client).to receive(:tasks).and_return(@asana_client_tasks) allow(@asana_client_tasks).to receive(:get_task) diff --git a/spec/asana_find_release_task_action_spec.rb b/spec/asana_find_release_task_action_spec.rb index 805a9ab..db6400b 100644 --- a/spec/asana_find_release_task_action_spec.rb +++ b/spec/asana_find_release_task_action_spec.rb @@ -141,9 +141,9 @@ def validate_semver(version) end it "shows error" do + allow(Fastlane::UI).to receive(:user_error!) expect(Fastlane::Actions::AsanaFindReleaseTaskAction).not_to receive(:find_hotfix_task_in_response) expect(Fastlane::Actions::AsanaFindReleaseTaskAction).not_to receive(:find_release_task_in_response) - allow(Fastlane::UI).to receive(:user_error!) find_release_task("1.0.0") diff --git a/spec/asana_get_release_automation_subtask_id_action_spec.rb b/spec/asana_get_release_automation_subtask_id_action_spec.rb index 136f1dc..57fcd17 100644 --- a/spec/asana_get_release_automation_subtask_id_action_spec.rb +++ b/spec/asana_get_release_automation_subtask_id_action_spec.rb @@ -1,88 +1,41 @@ describe Fastlane::Actions::AsanaGetReleaseAutomationSubtaskIdAction do describe "#run" do - it "returns the 'Automation' subtask ID when it exists in the Asana task" do + before do + @asana_client_tasks = double + asana_client = double("asana_client") + allow(Asana::Client).to receive(:new).and_return(asana_client) + allow(asana_client).to receive(:tasks).and_return(@asana_client_tasks) + allow(@asana_client_tasks).to receive(:get_subtasks_for_task) + end + it "returns the 'Automation' subtask ID and sets GHA output when the subtask exists in the Asana task" do + allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output) expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run) - expect(HTTParty).to receive(:get).and_return( - double( - success?: true, - parsed_response: { 'data' => [ - { 'gid' => '12345', 'name' => 'Automation', 'created_at' => '2020-01-01T00:00:00.000Z' } - ] } - ) + expect(@asana_client_tasks).to receive(:get_subtasks_for_task).and_return( + [double(gid: "12345", name: "Automation", created_at: "2020-01-01T00:00:00.000Z")] ) expect(test_action("https://app.asana.com/0/0/0")).to eq("12345") + expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_automation_task_id", "12345") end it "returns the oldest 'Automation' subtask when there are multiple subtasks with that name" do expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run) - expect(HTTParty).to receive(:get).and_return( - double( - success?: true, - parsed_response: { 'data' => [ - { 'gid' => '12345', 'name' => 'Automation', 'created_at' => '2020-01-01T00:00:00.000Z' }, - { 'gid' => '431', 'name' => 'Automation', 'created_at' => '2019-01-01T00:00:00.000Z' }, - { 'gid' => '12460', 'name' => 'Automation', 'created_at' => '2020-01-05T00:00:00.000Z' } - ] } - ) + expect(@asana_client_tasks).to receive(:get_subtasks_for_task).and_return( + [double(gid: "12345", name: "Automation", created_at: "2020-01-01T00:00:00.000Z"), + double(gid: "431", name: "Automation", created_at: "2019-01-01T00:00:00.000Z"), + double(gid: "12460", name: "Automation", created_at: "2020-01-05T00:00:00.000Z")] ) expect(test_action("https://app.asana.com/0/0/0")).to eq("431") end it "returns nil when 'Automation' subtask does not exist in the Asana task" do + allow(Fastlane::UI).to receive(:user_error!) expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run) - expect(HTTParty).to receive(:get).and_return( - double( - success?: true, - parsed_response: { 'data' => [] } - ) - ) - - expect(test_action("https://app.asana.com/0/0/0")).to eq(nil) - end - - it "returns nil when response is empty" do - expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run) - expect(HTTParty).to receive(:get).and_return( - double( - success?: true, - parsed_response: {} - ) - ) - - expect(test_action("https://app.asana.com/0/0/0")).to eq(nil) - end - - it "shows error when failed to fetch task subtasks" do - expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run) - expect(HTTParty).to receive(:get).and_return( - double( - success?: false, - code: 401, - message: "Unauthorized" - ) - ) - - expect(Fastlane::UI).to receive(:user_error!).with("Failed to fetch 'Automation' subtask: (401 Unauthorized)") + expect(@asana_client_tasks).to receive(:get_subtasks_for_task).and_raise(StandardError, "API error") test_action("https://app.asana.com/0/0/0") - end - - it "sets GHA output" do - allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output) - expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run) - expect(HTTParty).to receive(:get).and_return( - double( - success?: true, - parsed_response: { 'data' => [ - { 'gid' => '12345', 'name' => 'Automation', 'created_at' => '2020-01-01T00:00:00.000Z' } - ] } - ) - ) - - expect(test_action("https://app.asana.com/0/0/0")).to eq("12345") - expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_automation_task_id", "12345") + expect(Fastlane::UI).to have_received(:user_error!).with("Failed to fetch 'Automation' subtasks for task 0: API error") end end