Skip to content

Commit

Permalink
Move upload_file_to_asana_task to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoy committed Sep 14, 2024
1 parent 8306e39 commit c9994b6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,7 @@ module Fastlane
module Actions
class AsanaUploadAction < Action
def self.run(params)
task_id = params[:task_id]
token = params[:asana_access_token]
file_name = params[:file_name]

asana_client = Asana::Client.new do |c|
c.authentication(:access_token, token)
end

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
Helper::DdgAppleAutomationHelper.upload_file_to_asana_task(params[:task_id], params[:file_name], params[:asana_access_token])
end

def self.description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ def self.get_asana_user_id_for_github_handle(github_handle)
end
end

def self.upload_file_to_asana_task(task_id, file_path, asana_access_token)
asana_client = Asana::Client.new do |c|
c.authentication(:access_token, asana_access_token)
end

begin
asana_client.tasks.find_by_id(task_id).attach(filename: file_path, mime: "application/octet-stream")
rescue StandardError => e
UI.user_error!("Failed to upload file to Asana task: #{e}")
return
end
end

def self.path_for_asset_file(file)
File.expand_path("../assets/#{file}", __dir__)
end
Expand Down
33 changes: 9 additions & 24 deletions spec/asana_upload_action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
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)
it "calls helper" do
task_id = "12345"
file_name = "file.txt"
asana_access_token = "secret-token"
expect(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:upload_file_to_asana_task)
.with(task_id, file_name, asana_access_token)
test_action(task_id, file_name, asana_access_token)
end

it "uploads a file successfully" do
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("123", "path/to/file.txt") }.not_to raise_error
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"))

expect(Fastlane::UI).to receive(:user_error!).with("Failed to upload file to Asana task: API Error")
test_action("123", "path/to/file.txt")
def test_action(task_id, file_name, asana_access_token)
Fastlane::Actions::AsanaUploadAction.run(task_id: task_id, file_name: file_name, asana_access_token: asana_access_token)
end
end

def test_action(task_id, file_name)
Fastlane::Actions::AsanaUploadAction.run(task_id: task_id,
file_name: file_name)
end
end
29 changes: 29 additions & 0 deletions spec/ddg_apple_automation_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,35 @@ def get_asana_user_id_for_github_handle(github_handle)
end
end

describe "#upload_file_to_asana_task" 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(@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 { upload_file_to_asana_task("123", "path/to/file.txt") }.not_to raise_error
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"))

expect(Fastlane::UI).to receive(:user_error!).with("Failed to upload file to Asana task: API Error")
upload_file_to_asana_task("123", "path/to/file.txt")
end

def upload_file_to_asana_task(task_id, file_path)
Fastlane::Helper::DdgAppleAutomationHelper.upload_file_to_asana_task(task_id, file_path, anything)
end
end

describe "#load_file" do
it "shows error if provided file does not exist" do
allow(Fastlane::UI).to receive(:user_error!)
Expand Down

0 comments on commit c9994b6

Please sign in to comment.