Skip to content

Commit

Permalink
Extract finding subtask into a separate method and improve handling b…
Browse files Browse the repository at this point in the history
…roken responses
  • Loading branch information
ayoy committed Sep 2, 2024
1 parent 12f6ec2 commit 84a819f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ def self.run(params)

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
# Fetch release task assignee and set GHA output.
# This is to match current GHA action behavior.
# 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')
automation_subtask_id = find_oldest_automation_subtask(response)
Helper::GitHubActionsHelper.set_output("asana_automation_task_id", automation_subtask_id)
automation_subtask_id
else
Expand Down Expand Up @@ -67,6 +64,13 @@ def self.available_options
def self.is_supported?(platform)
true
end

def self.find_oldest_automation_subtask(response)
response.parsed_response['data']
&.find_all { |hash| hash['name'] == 'Automation' }
&.min_by { |x| Time.parse(x['created_at']) } # Get the oldest 'Automation' subtask
&.dig('gid')
end
end
end
end
12 changes: 12 additions & 0 deletions spec/asana_get_release_automation_subtask_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
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(
Expand Down

0 comments on commit 84a819f

Please sign in to comment.