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 ce6f3bc..2555370 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,7 +1,6 @@ require "fastlane/action" require "fastlane_core/configuration/config_item" require "asana" -require "json" require_relative "../helper/ddg_apple_automation_helper" require_relative "../helper/github_actions_helper" diff --git a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_find_release_task_action.rb b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_find_release_task_action.rb index 9aa0aaa..11b14e9 100644 --- a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_find_release_task_action.rb +++ b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_find_release_task_action.rb @@ -1,8 +1,6 @@ require "fastlane/action" require "fastlane_core/configuration/config_item" require "asana" -require "httparty" -require "json" require "octokit" require "time" require_relative "../helper/ddg_apple_automation_helper" diff --git a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_upload_action.rb b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_upload_action.rb index ca181a5..ff68d49 100644 --- a/lib/fastlane/plugin/ddg_apple_automation/actions/asana_upload_action.rb +++ b/lib/fastlane/plugin/ddg_apple_automation/actions/asana_upload_action.rb @@ -1,6 +1,6 @@ require "fastlane/action" require "fastlane_core/configuration/config_item" -require "httparty" +require "asana" require_relative "../helper/ddg_apple_automation_helper" module Fastlane @@ -11,18 +11,15 @@ def self.run(params) token = params[:asana_access_token] file_name = params[:file_name] - begin - file = File.open(file_name) - url = Helper::DdgAppleAutomationHelper::ASANA_API_URL + "/tasks/#{task_id}/attachments" - response = HTTParty.post(url, - headers: { 'Authorization' => "Bearer #{token}" }, - body: { file: file }) + asana_client = Asana::Client.new do |c| + c.authentication(:access_token, token) + end - unless response.success? - UI.user_error!("Failed to upload file to Asana task: (#{response.code} #{response.message})") - end - rescue StandardError - UI.user_error!("Failed to open file: #{file_name}") + begin + asana_client.tasks.find_by_id(task_id).attach(filename: file_name, mime: "application/octet-stream") + rescue StandardError => e + UI.user_error!("Failed to upload file to Asana task: #{e}") + return end end @@ -51,7 +48,7 @@ def self.available_options optional: false, type: String), FastlaneCore::ConfigItem.new(key: :file_name, - description: "Path to the file that will be uploaded", + description: "Path to a file that will be uploaded", optional: false, type: String) ] diff --git a/spec/asana_upload_spec.rb b/spec/asana_upload_spec.rb index 339112a..1204af8 100644 --- a/spec/asana_upload_spec.rb +++ b/spec/asana_upload_spec.rb @@ -1,30 +1,25 @@ describe Fastlane::Actions::AsanaUploadAction do describe "#run" do + before do + @task = double("task") + @asana_client_tasks = double("asana_client_tasks") + 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) + end it "uploads a file successfully" do - allow(HTTParty).to receive(:post).and_return(double(success?: true)) - allow(File).to receive(:open).with("path/to/file.txt").and_return(double) + allow(@asana_client_tasks).to receive(:find_by_id).with("123").and_return(@task) + allow(@task).to receive(:attach).with(filename: "path/to/file.txt", mime: "application/octet-stream") - expect { test_action("12345", "path/to/file.txt") }.not_to raise_error + expect { test_action("123", "path/to/file.txt") }.not_to raise_error end - it "shows error if HTTP failure" do - allow(HTTParty).to receive(:post).and_return( - double( - success?: false, - code: 500, - message: "Internal Server Error" - ) - ) - allow(File).to receive(:open).with("path/to/file.txt").and_return(double) - - expect(Fastlane::UI).to receive(:user_error!).with("Failed to upload file to Asana task: (500 Internal Server Error)") - test_action("12345", "path/to/file.txt") - end + it "shows error if failure" do + allow(@asana_client_tasks).to receive(:find_by_id).with("123").and_return(@task) + allow(@task).to receive(:attach).and_raise(StandardError.new("API Error")) - it "shows error if file does not exist" do - expect(Fastlane::UI).to receive(:user_error!).with("Failed to open file: path/to/file.txt") - expect(HTTParty).not_to receive(:post) - test_action("12345", "path/to/file.txt") + expect(Fastlane::UI).to receive(:user_error!).with("Failed to upload file to Asana task: API Error") + test_action("123", "path/to/file.txt") end end