From 61c5c6a1b6d04e33c01458078159eaa3600c8d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20=C5=81yp?= Date: Thu, 12 Sep 2024 14:09:08 +0200 Subject: [PATCH] First part of spec --- .../asana_create_action_item_action.rb | 2 +- spec/asana_create_action_item_action_spec.rb | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 spec/asana_create_action_item_action_spec.rb 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 908ef2c..143c38c 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 @@ -56,7 +56,7 @@ def self.run(params) subtask_options[:html_notes] = html_notes unless html_notes.nil? subtask = asana_client.tasks.create_subtask_for_task(**subtask_options) - Helper::GitHubActionsHelper.set_output("new_task_id", subtask.gid) if subtask.gid + Helper::GitHubActionsHelper.set_output("new_task_id", subtask.gid) if subtask&.gid end def self.description diff --git a/spec/asana_create_action_item_action_spec.rb b/spec/asana_create_action_item_action_spec.rb new file mode 100644 index 0000000..6b19b40 --- /dev/null +++ b/spec/asana_create_action_item_action_spec.rb @@ -0,0 +1,81 @@ +describe Fastlane::Actions::AsanaCreateActionItemAction do + describe "#run" do + let(:task_url) { "https://app.asana.com/4/753241/9999" } + let(:task_id) { "1" } + let(:assignee_id) { "11" } + 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::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(:create_subtask_for_task) + + allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output) + 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::Actions::AsanaExtractTaskAssigneeAction).to receive(:run).with( + task_id: task_id, + asana_access_token: anything + ) + test_action(task_url: task_url, is_scheduled_release: true) + expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_assignee_id", "11") + end + + it "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, is_scheduled_release: false, github_handle: github_handle) + expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_assignee_id", "11") + end + + it "raises an error when github handle is empty and is manual release" do + expect(Fastlane::UI).to receive(:user_error!).with("Github handle cannot be empty for manual release") + test_action(task_url: task_url, is_scheduled_release: false, github_handle: "") + expect(Fastlane::Helper::GitHubActionsHelper).not_to have_received(:set_output) + end + + # it "adds an assignee as 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 user 11 as collaborator on task 2: 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:, task_name: nil, notes: nil, html_notes: nil, template_name: nil, is_scheduled_release: false, github_handle: nil) + Fastlane::Actions::AsanaCreateActionItemAction.run( + task_url: task_url, + task_name: task_name, + notes: notes, + html_notes: html_notes, + template_name: template_name, + is_scheduled_release: is_scheduled_release, + github_handle: github_handle + ) + end +end