Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsanaUploadAction #2

Merged
merged 6 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "fastlane/action"
require "fastlane_core/configuration/config_item"
require "httparty"
require_relative "../helper/ddg_apple_automation_helper"

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]

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 })

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}")
end
end

def self.description
"Uploads a file to an Asana task"
end

def self.authors
["DuckDuckGo"]
end

def self.return_value
""
end

def self.details
# Optional:
""
end

def self.available_options
[
FastlaneCore::ConfigItem.asana_access_token,
FastlaneCore::ConfigItem.new(key: :task_id,
description: "Asana task ID",
optional: false,
type: String),
FastlaneCore::ConfigItem.new(key: :file_name,
description: "Path to the file that will be uploaded",
optional: false,
type: String)
]
end

def self.is_supported?(platform)
true
end
end
end
end
36 changes: 36 additions & 0 deletions spec/asana_upload_action_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
describe Fastlane::Actions::AsanaUploadAction do
describe "#run" do
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)

expect { test_action("12345", "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 file does not exist" do
allow(HTTParty).to receive(:post).and_return(double(success?: true))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not needed now 👍

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")
end
end

def test_action(task_id, file_name)
Fastlane::Actions::AsanaUploadAction.run(task_id: task_id,
file_name: file_name)
end
end
Loading