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 AsanaGetReleaseAutomationSubtaskIdAction #1

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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,72 @@
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)

# Fetch release task assignee and set GHA output
# TODO: To be reworked for local execution
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')
Copy link
Contributor

Choose a reason for hiding this comment

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

perhaps it makes sense to put into a separate private method, like so:

def self.find_oldest_automation_subtask(data)
data
.select { |hash| hash['name'] == 'Automation' }
&.min_by { |x| Time.parse(x['created_at']) }
&.dig('gid')
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

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
Loading