-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
lib/fastlane/plugin/ddg_apple_automation/actions/asana_get_release_automation_subtask_id.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!