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 9a5e067..d41420c 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,14 +23,14 @@ def self.run(params) return end - task_id = Fastlane::Actions::AsanaExtractTaskIdAction.run(task_url: task_url) if task_url + task_id = AsanaExtractTaskIdAction.run(task_url: task_url) if task_url if template_name.to_s.empty? text = "#{comment}\n\nWorkflow URL: #{workflow_url}" create_story(asana_access_token, task_id, text: text) else template_file = Helper::DdgAppleAutomationHelper.path_for_asset_file("asana_add_comment/templates/#{template_name}.html") - template_content = Helper::DdgAppleAutomationHelper.load_template_file(template_file) + template_content = Helper::DdgAppleAutomationHelper.load_file(template_file) return unless template_content html_text = process_template_content(template_content) @@ -96,13 +96,6 @@ def self.validate_params(task_id, task_url, comment, template_name, workflow_url end end - def self.load_template_file(template_name) - template_file = Helper::DdgAppleAutomationHelper.path_for_asset_file("asana_add_comment/templates/#{template_name}.html") - File.read(template_file) - rescue StandardError - UI.user_error!("Error: The file '#{template_name}.html' does not exist.") - end - def self.create_story(asana_access_token, task_id, text: nil, html_text: nil) client = Asana::Client.new do |c| c.authentication(:access_token, asana_access_token) diff --git a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_extract_task_assignee_action.rb b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_extract_task_assignee_action.rb index 8e78096..ce6f3bc 100644 --- a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_extract_task_assignee_action.rb +++ b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_extract_task_assignee_action.rb @@ -1,6 +1,6 @@ require "fastlane/action" require "fastlane_core/configuration/config_item" -require "httparty" +require "asana" require "json" require_relative "../helper/ddg_apple_automation_helper" require_relative "../helper/github_actions_helper" @@ -12,16 +12,20 @@ def self.run(params) task_id = params[:task_id] token = params[:asana_access_token] - url = Helper::DdgAppleAutomationHelper::ASANA_API_URL + "/tasks/#{task_id}?opt_fields=assignee" - response = HTTParty.get(url, headers: { 'Authorization' => "Bearer #{token}" }) + client = Asana::Client.new do |c| + c.authentication(:access_token, token) + end - if response.success? - assignee_id = response.parsed_response.dig('data', 'assignee', 'gid') - Helper::GitHubActionsHelper.set_output("asana_assignee_id", assignee_id) - assignee_id - else - UI.user_error!("Failed to fetch task assignee: (#{response.code} #{response.message})") + begin + task = client.tasks.get_task(task_gid: task_id) + rescue StandardError => e + UI.user_error!("Failed to fetch task assignee: #{e}") + return end + + assignee_id = task.assignee.gid + Helper::GitHubActionsHelper.set_output("asana_assignee_id", assignee_id) + assignee_id end def self.description 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 8cf00f7..3f64f76 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 @@ -23,10 +23,10 @@ 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) + def self.load_file(file) + File.read(file) rescue StandardError - UI.user_error!("Error: The file '#{template_file}' does not exist.") + UI.user_error!("Error: The file '#{file}' does not exist.") end end end diff --git a/spec/asana_extract_task_assignee_spec.rb b/spec/asana_extract_task_assignee_spec.rb index 3258a90..47df518 100644 --- a/spec/asana_extract_task_assignee_spec.rb +++ b/spec/asana_extract_task_assignee_spec.rb @@ -1,54 +1,37 @@ describe Fastlane::Actions::AsanaExtractTaskAssigneeAction do describe "#run" do - it "returns the assignee ID when Asana task is assigned" do - expect(HTTParty).to receive(:get).and_return( - double( - success?: true, - parsed_response: { 'data' => { 'assignee' => { 'gid' => '67890' } } } - ) + 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_task) + end + + it "returns the assignee ID and sets GHA output when Asana task is assigned" do + allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output) + expect(@asana_client_tasks).to receive(:get_task).and_return( + double(assignee: double(gid: "67890")) ) expect(test_action("12345")).to eq("67890") + expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_assignee_id", "67890") end it "returns nil when Asana task is not assigned" do - expect(HTTParty).to receive(:get).and_return( - double( - success?: true, - parsed_response: { 'data' => { 'assignee' => nil } } - ) + expect(@asana_client_tasks).to receive(:get_task).and_return( + double(assignee: double(gid: nil)) ) expect(test_action("12345")).to eq(nil) end it "shows error when failed to fetch task assignee" do - 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 task assignee: (401 Unauthorized)") + expect(@asana_client_tasks).to receive(:get_task).and_raise(StandardError, "API error") + expect(Fastlane::UI).to receive(:user_error!).with("Failed to fetch task assignee: API error") test_action("12345") end - - it "sets GHA output" do - allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output) - - expect(HTTParty).to receive(:get).and_return( - double( - success?: true, - parsed_response: { 'data' => { 'assignee' => { 'gid' => '67890' } } } - ) - ) - - expect(test_action("12345")).to eq("67890") - expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_assignee_id", "67890") - end end def test_action(task_id)