Skip to content

Commit

Permalink
Add rest of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaceklyp committed Sep 12, 2024
1 parent 8683bc1 commit cfdc0c3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,24 @@ def self.run(params)

Helper::GitHubActionsHelper.set_output("asana_assignee_id", assignee_id)

asana_client = Asana::Client.new do |c|
c.authentication(:access_token, token)
end

if template_name
template_file = Helper::DdgAppleAutomationHelper.path_for_asset_file("asana_create_action_item/templates/#{template_name}.yml")
template_content = YAML.safe_load(Helper::DdgAppleAutomationHelper.load_file(template_file))
task_name = Helper::DdgAppleAutomationHelper.sanitize_and_replace_env_vars(template_content["name"])
html_notes = Helper::DdgAppleAutomationHelper.sanitize_and_replace_env_vars(template_content["html_notes"])
end

subtask_options = {
task_gid: task_id,
assignee: assignee_id,
name: task_name
}
subtask_options[:notes] = notes unless notes.nil?
subtask_options[:html_notes] = html_notes unless html_notes.nil?
subtask = asana_client.tasks.create_subtask_for_task(**subtask_options)
begin
subtask = create_subtask(
task_id: task_id,
assignee_id: assignee_id,
task_name: task_name,
notes: notes,
html_notes: html_notes
)
rescue StandardError => e
UI.user_error!("Failed to create subtask for task: #{e}")
end

Helper::GitHubActionsHelper.set_output("new_task_id", subtask.gid) if subtask&.gid
end
Expand Down Expand Up @@ -114,6 +113,21 @@ def self.available_options
def self.is_supported?(platform)
true
end

def self.create_subtask(task_id:, assignee_id:, task_name:, notes: nil, html_notes: nil)
subtask_options = {
task_gid: task_id,
assignee: assignee_id,
name: task_name
}
subtask_options[:notes] = notes unless notes.nil?
subtask_options[:html_notes] = html_notes unless html_notes.nil?

asana_client = Asana::Client.new do |c|
c.authentication(:access_token, token)
end
asana_client.tasks.create_subtask_for_task(**subtask_options)
end
end
end
end
64 changes: 45 additions & 19 deletions spec/asana_create_action_item_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
let(:task_id) { "1" }
let(:assignee_id) { "11" }
let(:github_handle) { "user" }
let(:task_name) { "example name" }

let(:parsed_yaml_content) { { 'name' => 'test task', 'html_notes' => '<p>Some notes</p>' } }

before do
@asana_client_tasks = double
Expand Down Expand Up @@ -44,27 +47,50 @@
expect(Fastlane::Helper::GitHubActionsHelper).not_to have_received(:set_output)
end

# it "adds an assignee as follower to the automation task" do
# expect(@asana_client_tasks).to receive(:add_followers_for_task).with(task_gid: automation_subtask_id, followers: [assignee_id])
# test_action(task_url: task_url, comment: comment, is_scheduled_release: false, github_handle: github_handle)
# end
it "correctly builds payload if notes input is given" do
test_action(task_url: task_url, task_name: task_name, notes: "notes", is_scheduled_release: true)
expect(@asana_client_tasks).to have_received(:create_subtask_for_task).with(
task_gid: task_id,
name: task_name,
notes: "notes",
assignee: assignee_id
)
end

# it "raises an error if adding a collaborator fails" do
# allow(Fastlane::UI).to receive(:user_error!)
# allow(@asana_client_tasks).to receive(:add_followers_for_task).and_raise(StandardError, 'some error')
# test_action(task_url: task_url, comment: comment, is_scheduled_release: false, github_handle: github_handle)
# expect(Fastlane::UI).to have_received(:user_error!).with("Failed to add user 11 as collaborator on task 2: some error")
# end
it "correctly builds payload if html_notes input is given" do
test_action(task_url: task_url, task_name: task_name, html_notes: "html_notes", is_scheduled_release: true)
expect(@asana_client_tasks).to have_received(:create_subtask_for_task).with(
task_gid: task_id,
name: task_name,
html_notes: "html_notes",
assignee: assignee_id
)
end

# it "adds a comment to the automation subtask" do
# expect(Fastlane::Actions::AsanaAddCommentAction).to receive(:run).with(
# task_id: automation_subtask_id,
# comment: comment,
# template_name: nil,
# asana_access_token: anything
# )
# test_action(task_url: task_url, comment: comment, is_scheduled_release: false, github_handle: github_handle)
# end
it "correctly builds payload if template_name input is given" do
allow(File).to receive(:read)
allow(YAML).to receive(:safe_load).and_return(parsed_yaml_content)
test_action(task_url: task_url, task_name: task_name, template_name: "template_name", is_scheduled_release: true)
expect(@asana_client_tasks).to have_received(:create_subtask_for_task).with(
task_gid: task_id,
name: "test task",
html_notes: "<p>Some notes</p>",
assignee: assignee_id
)
end

it "raises an error if adding subtask fails" do
allow(Fastlane::UI).to receive(:user_error!)
allow(@asana_client_tasks).to receive(:create_subtask_for_task).and_raise(StandardError, 'API Error')
test_action(task_url: task_url, task_name: task_name, notes: "notes", is_scheduled_release: true)
expect(Fastlane::UI).to have_received(:user_error!).with("Failed to create subtask for task: API Error")
end

it "correctly sets output" do
allow(@asana_client_tasks).to receive(:create_subtask_for_task).and_return(double('subtask', gid: "42"))
test_action(task_url: task_url, task_name: task_name, notes: "notes", is_scheduled_release: true)
expect(Fastlane::Helper::GitHubActionsHelper).to have_received(:set_output).with("new_task_id", "42")
end
end

def test_action(task_url:, task_name: nil, notes: nil, html_notes: nil, template_name: nil, is_scheduled_release: false, github_handle: nil)
Expand Down

0 comments on commit cfdc0c3

Please sign in to comment.