From 91ae0b60ae3a0e6d762dda64115536e0e81b3979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20=C5=81yp?= Date: Thu, 5 Sep 2024 22:37:19 +0200 Subject: [PATCH] Add spec --- .../actions/asana_log_message_action.rb | 8 ++- spec/asana_log_message_spec.rb | 71 +++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 spec/asana_log_message_spec.rb 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 6bec361..06ccd5f 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 @@ -5,16 +5,18 @@ 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" +require_relative "asana_extract_task_id_action" +require_relative "asana_extract_task_assignee_action" module Fastlane module Actions class AsanaLogMessageAction < Action def self.run(params) - asana_access_token = params[:asana_access_token] + 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 + is_scheduled_release = params[:is_scheduled_release] || false github_handle = params[:github_handle] automation_subtask_id = AsanaGetReleaseAutomationSubtaskIdAction.run(task_url: task_url, asana_access_token: token) @@ -31,7 +33,7 @@ def self.run(params) end begin - asana_client.tasks.add_followers_for_task(task_gid: task_id, followers: [assignee_id]) + asana_client.tasks.add_followers_for_task(task_gid: automation_subtask_id, followers: [assignee_id]) rescue StandardError => e UI.user_error!("Failed to add a collaborator to the release task: #{e}") end diff --git a/spec/asana_log_message_spec.rb b/spec/asana_log_message_spec.rb new file mode 100644 index 0000000..f88a6e8 --- /dev/null +++ b/spec/asana_log_message_spec.rb @@ -0,0 +1,71 @@ +describe Fastlane::Actions::AsanaLogMessageAction do + describe "#run" do + let(:task_url) { "https://example.com" } + let(:task_id) { "1" } + let(:automation_subtask_id) { "2" } + let(:assignee_id) { "11" } + let(:comment) { "comment" } + let(:github_handle) { "user" } + + 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(Fastlane::Actions::AsanaGetReleaseAutomationSubtaskIdAction).to receive(:run).and_return(automation_subtask_id) + allow(Fastlane::Actions::AsanaExtractTaskIdAction).to receive(:run).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) + allow(Fastlane::Actions::AsanaAddCommentAction).to receive(:run) + end + + it "does extract assignee id from release task when is scheduled release" do + expect(Fastlane::Actions::AsanaExtractTaskIdAction).to receive(:run).with(task_url: task_url) + expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run).with( + task_id: task_id, + asana_access_token: anything + ) + test_action(task_url: task_url, comment: comment, is_scheduled_release: true) + end + + it "does takes assignee id from github handle when is manual release" do + expect(Fastlane::Actions::AsanaGetUserIdForGithubHandleAction).to receive(:run).with( + github_handle: github_handle, + asana_access_token: anything + ) + test_action(task_url: task_url, comment: comment, is_scheduled_release: false, github_handle: github_handle) + end + + it "adds a assignee as a follower to the automation task" do + expect(@asana_client_tasks).to receive(:add_followers_for_task).with(task_gid: automation_subtask_id, followers: [assignee_id]) + test_action(task_url: task_url, comment: comment, is_scheduled_release: false, github_handle: github_handle) + end + + it "raises an error if adding a collaborator fails" do + allow(Fastlane::UI).to receive(:user_error!) + allow(@asana_client_tasks).to receive(:add_followers_for_task).and_raise(StandardError, 'some error') + test_action(task_url: task_url, comment: comment, is_scheduled_release: false, github_handle: github_handle) + expect(Fastlane::UI).to have_received(:user_error!).with("Failed to add a collaborator to the release task: some error") + end + + it "adds a comment to the automation subtask" do + expect(Fastlane::Actions::AsanaAddCommentAction).to receive(:run).with( + task_id: automation_subtask_id, + comment: comment, + template_name: nil, + asana_access_token: anything + ) + test_action(task_url: task_url, comment: comment, is_scheduled_release: false, github_handle: github_handle) + end + end + + def test_action(task_url:, github_handle: nil, comment: nil, template_name: nil, is_scheduled_release: false) + Fastlane::Actions::AsanaLogMessageAction.run(task_url: task_url, + comment: comment, + template_name: template_name, + is_scheduled_release: is_scheduled_release, + github_handle: github_handle) + end +end