Skip to content

Commit

Permalink
Replace httpclient with asana client in asana_extract_task_assignee
Browse files Browse the repository at this point in the history
  • Loading branch information
jaceklyp committed Sep 6, 2024
1 parent 4a6f810 commit c573fd6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ def self.run(params)
return
end

task_id = Fastlane::Actions::AsanaExtractTaskIdAction.run(task_url: task_url) if task_url
task_id = AsanaExtractTaskIdAction.run(task_url: task_url) if task_url

if template_name.to_s.empty?
text = "#{comment}\n\nWorkflow URL: #{workflow_url}"
create_story(asana_access_token, task_id, text: text)
else
template_file = Helper::DdgAppleAutomationHelper.path_for_asset_file("asana_add_comment/templates/#{template_name}.html")
template_content = Helper::DdgAppleAutomationHelper.load_template_file(template_file)
template_content = Helper::DdgAppleAutomationHelper.load_file(template_file)
return unless template_content

html_text = process_template_content(template_content)
Expand Down Expand Up @@ -96,13 +96,6 @@ def self.validate_params(task_id, task_url, comment, template_name, workflow_url
end
end

def self.load_template_file(template_name)
template_file = Helper::DdgAppleAutomationHelper.path_for_asset_file("asana_add_comment/templates/#{template_name}.html")
File.read(template_file)
rescue StandardError
UI.user_error!("Error: The file '#{template_name}.html' does not exist.")
end

def self.create_story(asana_access_token, task_id, text: nil, html_text: nil)
client = Asana::Client.new do |c|
c.authentication(:access_token, asana_access_token)
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 "json"
require_relative "../helper/ddg_apple_automation_helper"
require_relative "../helper/github_actions_helper"
Expand All @@ -12,16 +12,20 @@ def self.run(params)
task_id = params[:task_id]
token = params[:asana_access_token]

url = Helper::DdgAppleAutomationHelper::ASANA_API_URL + "/tasks/#{task_id}?opt_fields=assignee"
response = HTTParty.get(url, headers: { 'Authorization' => "Bearer #{token}" })
client = Asana::Client.new do |c|
c.authentication(:access_token, token)
end

if response.success?
assignee_id = response.parsed_response.dig('data', 'assignee', 'gid')
Helper::GitHubActionsHelper.set_output("asana_assignee_id", assignee_id)
assignee_id
else
UI.user_error!("Failed to fetch task assignee: (#{response.code} #{response.message})")
begin
task = client.tasks.get_task(task_gid: task_id)
rescue StandardError => e
UI.user_error!("Failed to fetch task assignee: #{e}")
return
end

assignee_id = task.assignee.gid
Helper::GitHubActionsHelper.set_output("asana_assignee_id", assignee_id)
assignee_id
end

def self.description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def self.path_for_asset_file(file)
File.expand_path("../assets/#{file}", __dir__)
end

def self.load_template_file(template_file)
File.read(template_file)
def self.load_file(file)
File.read(file)
rescue StandardError
UI.user_error!("Error: The file '#{template_file}' does not exist.")
UI.user_error!("Error: The file '#{file}' does not exist.")
end
end
end
Expand Down
51 changes: 17 additions & 34 deletions spec/asana_extract_task_assignee_spec.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,37 @@
describe Fastlane::Actions::AsanaExtractTaskAssigneeAction do
describe "#run" do
it "returns the assignee ID when Asana task is assigned" do
expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: { 'data' => { 'assignee' => { 'gid' => '67890' } } }
)
before do
@asana_client_tasks = double
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)
allow(@asana_client_tasks).to receive(:get_task)
end

it "returns the assignee ID and sets GHA output when Asana task is assigned" do
allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output)
expect(@asana_client_tasks).to receive(:get_task).and_return(
double(assignee: double(gid: "67890"))
)

expect(test_action("12345")).to eq("67890")
expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_assignee_id", "67890")
end

it "returns nil when Asana task is not assigned" do
expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: { 'data' => { 'assignee' => nil } }
)
expect(@asana_client_tasks).to receive(:get_task).and_return(
double(assignee: double(gid: nil))
)

expect(test_action("12345")).to eq(nil)
end

it "shows error when failed to fetch task assignee" do
expect(HTTParty).to receive(:get).and_return(
double(
success?: false,
code: 401,
message: "Unauthorized"
)
)

expect(Fastlane::UI).to receive(:user_error!).with("Failed to fetch task assignee: (401 Unauthorized)")
expect(@asana_client_tasks).to receive(:get_task).and_raise(StandardError, "API error")
expect(Fastlane::UI).to receive(:user_error!).with("Failed to fetch task assignee: API error")

test_action("12345")
end

it "sets GHA output" do
allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output)

expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: { 'data' => { 'assignee' => { 'gid' => '67890' } } }
)
)

expect(test_action("12345")).to eq("67890")
expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_assignee_id", "67890")
end
end

def test_action(task_id)
Expand Down

0 comments on commit c573fd6

Please sign in to comment.