-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add asana_upload_action and coressponding spec
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
lib/fastlane/plugin/ddg_apple_automation/actions/asana_upload_action.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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) | ||
rescue Errno::ENOENT | ||
UI.user_error!("Failed to open file: #{file_name}") | ||
end | ||
|
||
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 | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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)) | ||
expect(Fastlane::UI).to receive(:user_error!).with("Failed to open file: path/to/file.txt") | ||
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 |