From ccea3f4208edc3a4bdde549170f1fd7b5da15384 Mon Sep 17 00:00:00 2001 From: Dominik Kapusta Date: Mon, 5 Aug 2024 00:32:38 +0200 Subject: [PATCH] Add asana_extract_task_assignee action --- Gemfile | 2 + .../asana_extract_task_assignee_action.rb | 62 +++++++++++++++++++ .../actions/asana_extract_task_id_action.rb | 2 +- .../helper/ddg_release_automation_helper.rb | 9 +++ spec/asana_extract_task_id_action_spec.rb | 2 +- 5 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 lib/fastlane/plugin/ddg_release_automation/actions/asana_extract_task_assignee_action.rb diff --git a/Gemfile b/Gemfile index fc7fa4a..0d46c20 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,8 @@ gem 'rubocop-require_tools' # SimpleCov is a code coverage analysis tool for Ruby. gem 'simplecov' +gem 'httparty' + gemspec plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile') diff --git a/lib/fastlane/plugin/ddg_release_automation/actions/asana_extract_task_assignee_action.rb b/lib/fastlane/plugin/ddg_release_automation/actions/asana_extract_task_assignee_action.rb new file mode 100644 index 0000000..7091b94 --- /dev/null +++ b/lib/fastlane/plugin/ddg_release_automation/actions/asana_extract_task_assignee_action.rb @@ -0,0 +1,62 @@ +require "fastlane/action" +require "fastlane_core/configuration/config_item" +require "httparty" +require "json" +require_relative "../helper/ddg_release_automation_helper" +require_relative "../helper/github_actions_helper" + +module Fastlane + module Actions + class AsanaExtractTaskAssigneeAction < Action + def self.run(params) + task_id = params[:task_id] + token = Helper::DdgReleaseAutomationHelper.fetch_asana_token + url = Helper::DdgReleaseAutomationHelper::ASANA_API_URL + "/tasks/#{task_id}?opt_fields=assignee" + + response = HTTParty.get(url, headers: { 'Authorization' => "Bearer #{token}" }) + + if response.success? + assignee_id = response.parsed_response.dig('data', 'assignee', 'gid') + else + UI.user_error!("Failed to fetch task assignee: (#{response.code} #{response.message})") + end + + if Helper.is_ci? + Helper::GitHubActionsHelper.set_output("asana_assignee_id", assignee_id) + end + + assignee_id + end + + def self.description + "This action checks Asana task assignee ID for a provided task ID" + end + + def self.authors + ["DuckDuckGo"] + end + + def self.return_value + "The assignee ID extracted from the Asana task" + end + + def self.details + # Optional: + "" + end + + def self.available_options + [ + FastlaneCore::ConfigItem.new(key: :task_id, + description: "Asana task ID", + optional: false, + type: String) + ] + end + + def self.is_supported?(platform) + true + end + end + end +end diff --git a/lib/fastlane/plugin/ddg_release_automation/actions/asana_extract_task_id_action.rb b/lib/fastlane/plugin/ddg_release_automation/actions/asana_extract_task_id_action.rb index 8aed256..15ac412 100644 --- a/lib/fastlane/plugin/ddg_release_automation/actions/asana_extract_task_id_action.rb +++ b/lib/fastlane/plugin/ddg_release_automation/actions/asana_extract_task_id_action.rb @@ -16,7 +16,7 @@ def self.run(params) task_id = match[1] if Helper.is_ci? - Helper::GitHubActionsHelper.set_output("ASANA_TASK_ID", task_id) + Helper::GitHubActionsHelper.set_output("asana_task_id", task_id) end task_id diff --git a/lib/fastlane/plugin/ddg_release_automation/helper/ddg_release_automation_helper.rb b/lib/fastlane/plugin/ddg_release_automation/helper/ddg_release_automation_helper.rb index 6447bf5..7dee361 100644 --- a/lib/fastlane/plugin/ddg_release_automation/helper/ddg_release_automation_helper.rb +++ b/lib/fastlane/plugin/ddg_release_automation/helper/ddg_release_automation_helper.rb @@ -8,9 +8,18 @@ class DdgReleaseAutomationHelper # class methods that you define here become available in your action # as `Helper::DdgReleaseAutomationHelper.your_method` # + + ASANA_API_URL = "https://app.asana.com/api/1.0" + def self.show_message UI.message("Hello from the ddg_release_automation plugin helper!") end + + def self.fetch_asana_token + ENV.fetch("ASANA_ACCESS_TOKEN") + rescue KeyError + UI.user_error!("ASANA_ACCESS_TOKEN is not set") + end end end end diff --git a/spec/asana_extract_task_id_action_spec.rb b/spec/asana_extract_task_id_action_spec.rb index 341c40f..977df2d 100644 --- a/spec/asana_extract_task_id_action_spec.rb +++ b/spec/asana_extract_task_id_action_spec.rb @@ -33,7 +33,7 @@ allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output) expect(test_action("https://app.asana.com/0/12837864576817392/3465387322")).to eq("3465387322") - expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("ASANA_TASK_ID", "3465387322") + expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_task_id", "3465387322") end it "fails when garbage is passed" do