Skip to content

Commit

Permalink
Add specs for GitHubActionsHelper and AsanaExtractTaskAssigneeAction
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoy committed Aug 28, 2024
1 parent 766668d commit 1898726
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ fastlane/README.md
fastlane/report.xml
coverage
test-results
.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@ module Actions
class AsanaExtractTaskAssigneeAction < Action
def self.run(params)
task_id = params[:task_id]
token = Helper::DdgAppleAutomationHelper.fetch_asana_token
url = Helper::DdgAppleAutomationHelper::ASANA_API_URL + "/tasks/#{task_id}?opt_fields=assignee"
token = params[:asana_access_token]

url = Helper::DdgAppleAutomationHelper::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')
Helper::GitHubActionsHelper.set_output("asana_assignee_id", assignee_id)
assignee_id
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
Expand All @@ -47,6 +43,7 @@ def self.details

def self.available_options
[
FastlaneCore::ConfigItem.asana_access_token,
FastlaneCore::ConfigItem.new(key: :task_id,
description: "Asana task ID",
optional: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ def self.run(params)

if (match = task_url.match(TASK_URL_REGEX))
task_id = match[1]

if Helper.is_ci?
Helper::GitHubActionsHelper.set_output("asana_task_id", task_id)
end

Helper::GitHubActionsHelper.set_output("asana_task_id", task_id)
task_id
else
UI.user_error!(ERROR_MESSAGE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
require "fastlane_core/configuration/config_item"
require 'fastlane_core/ui/ui'

module Fastlane
UI = FastlaneCore::UI unless Fastlane.const_defined?(:UI)

module Helper
class DdgAppleAutomationHelper
# class methods that you define here become available in your action
# as `Helper::DdgAppleAutomationHelper.your_method`
#

ASANA_API_URL = "https://app.asana.com/api/1.0"
ERROR_ASANA_ACCESS_TOKEN_NOT_SET = "ASANA_ACCESS_TOKEN is not set"
end
end
end

def self.show_message
UI.message("Hello from the ddg_apple_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
module FastlaneCore
class ConfigItem
def self.asana_access_token
FastlaneCore::ConfigItem.new(key: :asana_access_token,
env_name: "ASANA_ACCESS_TOKEN",
description: "Asana access token",
optional: false,
sensitive: true,
type: String,
verify_block: proc do |value|
UI.user_error!(Fastlane::Helper::DdgAppleAutomationHelper::ERROR_ASANA_ACCESS_TOKEN_NOT_SET) if value.to_s.length == 0
end)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ module Fastlane
module Helper
class GitHubActionsHelper
def self.set_output(key, value)
Action.sh("echo '#{key}=#{value}' >> #{ENV.fetch('GITHUB_OUTPUT', '/dev/null')}")
return unless Helper.is_ci?

if key.to_s.length == 0
UI.user_error!("Key cannot be empty")
elsif value.to_s.length > 0
Action.sh("echo '#{key}=#{value}' >> #{ENV.fetch('GITHUB_OUTPUT', '/dev/null')}")
end
end
end
end
Expand Down
57 changes: 57 additions & 0 deletions spec/asana_extract_task_assignee_action_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
describe Fastlane::Actions::AsanaExtractTaskAssigneeAction do
describe "#run" do
it "returns the assignee ID when Asana task is assigned" do
expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: { 'data' => { 'assignee' => { 'gid' => '67890' } } }
)
)

expect(test_action("12345")).to eq("67890")
end

it "returns nil when Asana task is not assigned" do
expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: { 'data' => { 'assignee' => nil } }
)
)

expect(test_action("12345")).to eq(nil)
end

it "shows error when failed to fetch task assignee" do
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 task assignee: (401 Unauthorized)")

test_action("12345")
end

it "sets GHA output" do
allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output)

expect(HTTParty).to receive(:get).and_return(
double(
success?: true,
parsed_response: { 'data' => { 'assignee' => { 'gid' => '67890' } } }
)
)

expect(test_action("12345")).to eq("67890")
expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("asana_assignee_id", "67890")
end
end

def test_action(task_id)
Fastlane::Actions::AsanaExtractTaskAssigneeAction.run(task_id: task_id)
end
end
11 changes: 1 addition & 10 deletions spec/asana_extract_task_id_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@
expect(test_action("https://app.asana.com/0/0/1234/f")).to eq("1234")
end

it "does not set GHA output when not in CI" do
allow(Fastlane::Helper).to receive(:is_ci?).and_return(false)
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).not_to have_received(:set_output)
end

it "sets GHA output in CI" do
allow(Fastlane::Helper).to receive(:is_ci?).and_return(true)
it "sets GHA output" do
allow(Fastlane::Helper::GitHubActionsHelper).to receive(:set_output)

expect(test_action("https://app.asana.com/0/12837864576817392/3465387322")).to eq("3465387322")
Expand Down
58 changes: 58 additions & 0 deletions spec/github_actions_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
describe Fastlane::Helper::GitHubActionsHelper do
describe "#set_output" do
it "sets output when in CI and value is not empty" do
allow(Fastlane::Helper).to receive(:is_ci?).and_return(true)
allow(Fastlane::Action).to receive(:sh)
allow(ENV).to receive(:fetch).with("GITHUB_OUTPUT", "/dev/null").and_return("/dev/null")

set_output("foo", "bar")
expect(Fastlane::Action).to have_received(:sh).with("echo 'foo=bar' >> /dev/null")
end

it "honors GITHUB_OUTPUT environment variable when in CI" do
allow(Fastlane::Helper).to receive(:is_ci?).and_return(true)
allow(Fastlane::Action).to receive(:sh)
allow(ENV).to receive(:fetch).with("GITHUB_OUTPUT", "/dev/null").and_return("/tmp/github_output")

set_output("foo", "bar")
expect(Fastlane::Action).to have_received(:sh).with("echo 'foo=bar' >> /tmp/github_output")
end

it "does not set output when in CI and value is empty" do
allow(Fastlane::Helper).to receive(:is_ci?).and_return(true)
allow(Fastlane::Action).to receive(:sh)

set_output("foo", "")
expect(Fastlane::Action).not_to have_received(:sh)
end

it "does not set output when in CI and value is nil" do
allow(Fastlane::Helper).to receive(:is_ci?).and_return(true)
allow(Fastlane::Action).to receive(:sh)

set_output("foo", nil)
expect(Fastlane::Action).not_to have_received(:sh)
end

it "does not set output when not in CI" do
allow(Fastlane::Helper).to receive(:is_ci?).and_return(false)
allow(Fastlane::Action).to receive(:sh)

set_output("foo", "bar")
expect(Fastlane::Action).not_to have_received(:sh)
end

it "fails when key is empty" do
allow(Fastlane::Helper).to receive(:is_ci?).and_return(true)
allow(Fastlane::Action).to receive(:sh)
expect(Fastlane::UI).to receive(:user_error!).with("Key cannot be empty")

set_output("", "bar")
expect(Fastlane::Action).not_to have_received(:sh)
end
end

def set_output(key, value)
Fastlane::Helper::GitHubActionsHelper.set_output(key, value)
end
end

0 comments on commit 1898726

Please sign in to comment.