Skip to content

Commit

Permalink
Refactor asana_upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jaceklyp committed Sep 6, 2024
1 parent c573fd6 commit 94af858
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
]
Expand Down
35 changes: 15 additions & 20 deletions spec/asana_upload_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 94af858

Please sign in to comment.