Skip to content

Commit

Permalink
Add AsanaGetReleaseAutomationSubtaskIdAction
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoy committed Aug 29, 2024
1 parent 5d267b6 commit 870a93c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require "fastlane/action"
require "fastlane_core/configuration/config_item"
require "httparty"
require "json"
require "time"
require_relative "../helper/ddg_apple_automation_helper"
require_relative "../helper/github_actions_helper"
require_relative "asana_extract_task_id_action"
require_relative "asana_extract_task_assignee_action"

module Fastlane
module Actions
class AsanaGetReleaseAutomationSubtaskIdAction < Action
def self.run(params)
task_url = params[:task_url]
token = params[:asana_access_token]

task_id = AsanaExtractTaskIdAction.run(task_url: task_url, asana_access_token: token)
AsanaExtractTaskAssigneeAction.run(task_id: task_id, asana_access_token: token)

url = Helper::DdgAppleAutomationHelper::ASANA_API_URL + "/tasks/#{task_id}/subtasks?opt_fields=name,created_at"
response = HTTParty.get(url, headers: { 'Authorization' => "Bearer #{token}" })

if response.success?
data = response.parsed_response['data']
automation_subtask_id = data
.find_all { |hash| hash['name'] == 'Automation' }
&.min_by { |x| Time.parse(x['created_at']) } # Get the oldest 'Automation' subtask
&.dig('gid')
Helper::GitHubActionsHelper.set_output("asana_automation_task_id", automation_subtask_id)
automation_subtask_id
else
UI.user_error!("Failed to fetch 'Automation' subtask: (#{response.code} #{response.message})")
end
end

def self.description
"This action finds 'Automation' subtask for the release task in Asana specified by the URL given as parameter"
end

def self.authors
["DuckDuckGo"]
end

def self.return_value
"The 'Automation' task ID for the specified release task"
end

def self.details
# Optional:
""
end

def self.available_options
[
FastlaneCore::ConfigItem.asana_access_token,
FastlaneCore::ConfigItem.new(key: :task_url,
description: "Asana task URL",
optional: false,
type: String)
]
end

def self.is_supported?(platform)
true
end
end
end
end
2 changes: 1 addition & 1 deletion lib/fastlane/plugin/ddg_apple_automation/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Fastlane
module DdgAppleAutomation
VERSION = "0.2.0"
VERSION = "0.3.0"
end
end
80 changes: 80 additions & 0 deletions spec/asana_get_release_automation_subtask_id_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
describe Fastlane::Actions::AsanaGetReleaseAutomationSubtaskIdAction do
describe "#run" do
it "returns the 'Automation' subtask ID when it exists in the Asana task" do
expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run)
expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: { 'data' => [
{ 'gid' => '12345', 'name' => 'Automation', 'created_at' => '2020-01-01T00:00:00.000Z' }
] }
)
)

expect(test_action("https://app.asana.com/0/0/0")).to eq("12345")
end

it "returns the oldest 'Automation' subtask when there are multiple subtasks with that name" do
expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run)
expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: { 'data' => [
{ 'gid' => '12345', 'name' => 'Automation', 'created_at' => '2020-01-01T00:00:00.000Z' },
{ 'gid' => '431', 'name' => 'Automation', 'created_at' => '2019-01-01T00:00:00.000Z' },
{ 'gid' => '12460', 'name' => 'Automation', 'created_at' => '2020-01-05T00:00:00.000Z' }
] }
)
)

expect(test_action("https://app.asana.com/0/0/0")).to eq("431")
end

it "returns nil when 'Automation' subtask does not exist in the Asana task" do
expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run)
expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: { 'data' => [] }
)
)

expect(test_action("https://app.asana.com/0/0/0")).to eq(nil)
end

it "shows error when failed to fetch task subtasks" do
expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run)
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 'Automation' subtask: (401 Unauthorized)")

test_action("https://app.asana.com/0/0/0")
end

it "sets GHA output" do
allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output)
expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run)
expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: { 'data' => [
{ 'gid' => '12345', 'name' => 'Automation', 'created_at' => '2020-01-01T00:00:00.000Z' }
] }
)
)

expect(test_action("https://app.asana.com/0/0/0")).to eq("12345")
expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_automation_task_id", "12345")
end
end

def test_action(task_url)
Fastlane::Actions::AsanaGetReleaseAutomationSubtaskIdAction.run(task_url: task_url)
end
end

0 comments on commit 870a93c

Please sign in to comment.