Skip to content

Commit

Permalink
Update specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jaceklyp committed Sep 6, 2024
1 parent b45fef2 commit 8b72456
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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"
Expand Down Expand Up @@ -33,7 +31,7 @@ def self.run(params)
return
end

automation_subtask_id = find_oldest_automation_subtask(subtasks).gid
automation_subtask_id = find_oldest_automation_subtask(subtasks)&.gid
Helper::GitHubActionsHelper.set_output("asana_automation_task_id", automation_subtask_id)
automation_subtask_id
end
Expand Down Expand Up @@ -70,14 +68,9 @@ def self.is_supported?(platform)
end

def self.find_oldest_automation_subtask(subtasks)
automation_subtask = subtasks
.find_all { |task| task.name == 'Automation' }
&.min_by { |task| Time.parse(task.created_at) }
if automation_subtask.nil?
UI.user_error!("There is no 'Automation' subtask in task: #{task_id}")
return
end
automation_subtask
subtasks
.find_all { |task| task.name == 'Automation' }
&.min_by { |task| Time.parse(task.created_at) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/asana_extract_task_assignee_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
describe "#run" do
before do
@asana_client_tasks = double
asana_client = double("Asana::Client")
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)
Expand Down
2 changes: 1 addition & 1 deletion spec/asana_find_release_task_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ def validate_semver(version)
end

it "shows error" do
allow(Fastlane::UI).to receive(:user_error!)
expect(Fastlane::Actions::AsanaFindReleaseTaskAction).not_to receive(:find_hotfix_task_in_response)
expect(Fastlane::Actions::AsanaFindReleaseTaskAction).not_to receive(:find_release_task_in_response)
allow(Fastlane::UI).to receive(:user_error!)

find_release_task("1.0.0")

Expand Down
85 changes: 19 additions & 66 deletions spec/asana_get_release_automation_subtask_id_action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,88 +1,41 @@
describe Fastlane::Actions::AsanaGetReleaseAutomationSubtaskIdAction do
describe "#run" do
it "returns the 'Automation' subtask ID when it exists in the Asana task" do
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_subtasks_for_task)
end
it "returns the 'Automation' subtask ID and sets GHA output when the subtask exists in the Asana task" 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(@asana_client_tasks).to receive(:get_subtasks_for_task).and_return(
[double(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

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(@asana_client_tasks).to receive(:get_subtasks_for_task).and_return(
[double(gid: "12345", name: "Automation", created_at: "2020-01-01T00:00:00.000Z"),
double(gid: "431", name: "Automation", created_at: "2019-01-01T00:00:00.000Z"),
double(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
allow(Fastlane::UI).to receive(:user_error!)
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 "returns nil when response is empty" do
expect(Fastlane::Actions::AsanaExtractTaskAssigneeAction).to receive(:run)
expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: {}
)
)

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)")
expect(@asana_client_tasks).to receive(:get_subtasks_for_task).and_raise(StandardError, "API error")

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")
expect(Fastlane::UI).to have_received(:user_error!).with("Failed to fetch 'Automation' subtasks for task 0: API error")
end
end

Expand Down

0 comments on commit 8b72456

Please sign in to comment.