Skip to content

Commit

Permalink
merge from main
Browse files Browse the repository at this point in the history
  • Loading branch information
kshann committed Nov 22, 2024
2 parents a51b020 + 270faa3 commit f8a143d
Show file tree
Hide file tree
Showing 18 changed files with 392 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
require "fastlane/action"
require "fastlane_core/configuration/config_item"
require "yaml"
require "httparty"
require "json"
require_relative "../helper/ddg_apple_automation_helper"

module Fastlane
module Actions
class MattermostSendMessageAction < Action
def self.run(params)
github_handle = params[:github_handle]
template_name = params[:template_name]
mm_webhook_url = params[:mattermost_webhook_url]
args = (params[:template_args] || {}).merge(Hash(ENV).transform_keys { |key| key.downcase.gsub('-', '_') })
mapping_file = Helper::DdgAppleAutomationHelper.path_for_asset_file("mattermost_send_message/github-mattermost-user-id-mapping.yml")
user_mapping = YAML.load_file(mapping_file)
mattermost_user_handle = user_mapping[github_handle]

if mattermost_user_handle.nil? || mattermost_user_handle.to_s.empty?
UI.message("Mattermost user handle not known for #{github_handle}, skipping sending message")
return
end

text = process_template(template_name, args)
payload = {
"channel" => mattermost_user_handle,
"username" => "GitHub Actions",
"text" => text,
"icon_url" => "https://duckduckgo.com/assets/logo_header.v108.svg"
}

response = HTTParty.post(mm_webhook_url, {
headers: { 'Content-Type' => 'application/json' },
body: payload.to_json
})

# Check response status
if response.success?
UI.success("Message sent successfully!")
else
UI.user_error!("Failed to send message: #{response.body}")
end
end

def self.description
"This action sends a message to Mattermost, reporting the outcome to the user who triggered the workflow"
end

def self.authors
["DuckDuckGo"]
end

def self.return_value
""
end

def self.details
# Optional:
""
end

def self.process_template(template_name, args)
template_file = Helper::DdgAppleAutomationHelper.path_for_asset_file("mattermost_send_message/templates/#{template_name}.txt.erb")
Helper::DdgAppleAutomationHelper.process_erb_template(template_file, args)
end

def self.available_options
[
FastlaneCore::ConfigItem.new(key: :mattermost_webhook_url,
env_name: "MM_WEBHOOK_URL",
description: "Mattermost webhook URL",
optional: false,
type: String),
FastlaneCore::ConfigItem.new(key: :github_handle,
description: "Github user handle",
optional: false,
type: String),
FastlaneCore::ConfigItem.new(key: :template_args,
description: "Template arguments. For backward compatibility, environment variables are added to this hash",
optional: true,
type: Hash,
default_value: {}),
FastlaneCore::ConfigItem.new(key: :template_name,
description: "Name of a template file (without extension) for the message. Templates can be found in assets/mattermost_send_message/templates subdirectory.
The file is processed before being posted",
optional: false,
type: String)
]
end

def self.is_supported?(platform)
true
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.run(params)
options[:version] = new_version
options[:release_branch_name] = release_branch_name

release_task_id = Helper::AsanaHelper.create_release_task(options[:platform], options[:version], options[:asana_user_id], options[:asana_access_token], options[:is_hotfix])
release_task_id = Helper::AsanaHelper.create_release_task(options[:platform], options[:version], options[:asana_user_id], options[:asana_access_token])
options[:release_task_id] = release_task_id

Helper::AsanaHelper.update_asana_tasks_for_internal_release(options)
Expand Down Expand Up @@ -68,12 +68,7 @@ def self.available_options
FastlaneCore::ConfigItem.new(key: :target_section_id,
description: "Section ID in Asana where tasks included in the release should be moved",
optional: false,
type: String),
FastlaneCore::ConfigItem.new(key: :is_hotfix,
description: "Is this a hotfix release?",
optional: true,
type: Boolean,
default_value: false)
type: String)
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.run(params)
options = params.values
find_release_task_if_needed(options)

unless Helper::GitHelper.assert_branch_has_changes(options[:release_branch])
if params[:is_scheduled_release] && !Helper::GitHelper.assert_branch_has_changes(options[:release_branch])
UI.important("No changes to the release branch (or only changes to scripts and workflows). Skipping automatic release.")
Helper::GitHubActionsHelper.set_output("skip_release", true)
return
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
aataraxiaa: "@psmith"
afterxleep: "@dbernal"
alessandroboron: "@aboron"
amddg44: "@amallon"
ayoy: "@dkapusta"
brindy: "@brindy"
bwaresiak: "@bartek"
Bunn: "@fbunn"
dharb: "@dave"
diegoreymendez: "@dreymendez"
dus7: "@mariusz"
federicocappelli: "@fcappelli"
GioSensation: "@emanuele"
graeme: "@garthur"
jaceklyp: "@jlyp"
jonathanKingston: "@jkingston"
jotaemepereira: "@jpereira"
ladamski: "@ladamski"
mallexxx: "@amartemyanov"
miasma13: "@msmaga"
muodov: "@mtsoy"
quanganhdo: "@ado"
samsymons: "@ssymons"
shakyShane: "@sosbourne"
SabrinaTardio: "@stardio"
THISISDINOSAUR: "@esullivan"
tomasstrba: "@tom"
viktorjansson: "@viktor"
vinay-nadig-0042: "@vnadig"
kshann: "@kshannon"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:warning: **iOS release job failed** :thisisfine: | [:github: Workflow run summary](<%= workflow_url %>)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Notarized macOS app `<%= release_type %>` build is ready :goose_honk_tada: | [:github: Workflow run summary](<%= workflow_url %>)<% if defined?(asana_task_url) && !asana_task_url.to_s.strip.empty? %> | [:asana: Asana Task](<%= asana_task_url %>)<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:rotating_light: Notarized macOS app `<%= release_type %>` build failed | [:github: Workflow run summary](<%= workflow_url %>)<% if defined?(asana_task_url) && !asana_task_url.to_s.strip.empty? %> | [:asana: Asana Task](<%= asana_task_url %>)<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= platform %> app has been successfully uploaded to <%= destination %> :goose_honk_tada: | [:github: Workflow run summary](<%= workflow_url %>)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:rotating_light: <%= platform %> app <%= destination %> workflow failed | [:github: Workflow run summary](<%= workflow_url %>)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:rotating_light: macOS app variants workflow failed | [:github: Workflow run summary](<%= workflow_url %>)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
macOS app variants have been published successfully :goose_honk_tada: | [:github: Workflow run summary](<%= workflow_url %>)
40 changes: 10 additions & 30 deletions lib/fastlane/plugin/ddg_apple_automation/helper/asana_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def self.get_release_automation_subtask_id(task_url, asana_access_token)
end

def self.get_asana_user_id_for_github_handle(github_handle)
mapping_file = File.expand_path('../assets/github-asana-user-id-mapping.yml', __dir__)
mapping_file = Helper::DdgAppleAutomationHelper.path_for_asset_file('asana_get_user_id_for_github_handle/github-asana-user-id-mapping.yml')
user_mapping = YAML.load_file(mapping_file)
asana_user_id = user_mapping[github_handle]

Expand All @@ -129,7 +129,7 @@ def self.upload_file_to_asana_task(task_id, file_path, asana_access_token)
end
end

def self.release_template_task_id(platform, is_hotfix)
def self.release_template_task_id(platform, is_hotfix: false)
case platform
when "ios"
is_hotfix ? IOS_HOTFIX_TASK_TEMPLATE_ID : IOS_RELEASE_TASK_TEMPLATE_ID
Expand All @@ -140,7 +140,7 @@ def self.release_template_task_id(platform, is_hotfix)
end
end

def self.release_task_name(version, platform, is_hotfix)
def self.release_task_name(version, platform, is_hotfix: false)
case platform
when "ios"
is_hotfix ? "iOS App Hotfix Release #{version}" : "iOS App Release #{version}"
Expand Down Expand Up @@ -173,9 +173,9 @@ def self.release_section_id(platform)
end
end

def self.create_release_task(platform, version, assignee_id, asana_access_token, is_hotfix)
template_task_id = release_template_task_id(platform, is_hotfix)
task_name = release_task_name(version, platform, is_hotfix)
def self.create_release_task(platform, version, assignee_id, asana_access_token)
template_task_id = release_template_task_id(platform)
task_name = release_task_name(version, platform)
section_id = release_section_id(platform)

UI.message("Creating release task for #{version}")
Expand Down Expand Up @@ -296,10 +296,6 @@ def self.update_asana_tasks_for_public_release(params)
Helper::ReleaseTaskHelper.construct_release_announcement_task_description(params[:version], release_notes, task_ids)
end

# Rertieves tasks between this release and the last internal release
#
# @param github_token [String] GitHub token
#
def self.get_tasks_in_last_internal_release(platform, github_token)
# 1. Find last internal release tag (last internal release is the second one, because the first one is the release that's just created)
UI.message("Fetching tasks in latest internal release")
Expand All @@ -325,7 +321,7 @@ def self.fetch_tasks_for_tag(tag_id, asana_access_token)
asana_client = make_asana_client(asana_access_token)
task_ids = []
begin
response = asana_client.tasks.get_tasks_for_tag(tag_gid: tag_id, options: { opt_fields: ["gid"] })
response = asana_client.tasks.get_tasks_for_tag(tag_gid: tag_id, options: { fields: ["gid"] })
loop do
task_ids += response.map(&:gid)
response = response.next_page
Expand All @@ -341,7 +337,7 @@ def self.fetch_subtasks(task_id, asana_access_token)
asana_client = make_asana_client(asana_access_token)
task_ids = []
begin
response = asana_client.tasks.get_subtasks_for_task(task_gid: task_id, options: { opt_fields: ["gid"] })
response = asana_client.tasks.get_subtasks_for_task(task_gid: task_id, options: { fields: ["gid"] })
loop do
task_ids += response.map(&:gid)
response = response.next_page
Expand Down Expand Up @@ -381,7 +377,7 @@ def self.complete_tasks(task_ids, asana_access_token)
next
end

projects_ids = asana_client.projects.get_projects_for_task(task_gid: task_id, options: { opt_fields: ["gid"] }).map(&:gid)
projects_ids = asana_client.projects.get_projects_for_task(task_gid: task_id, options: { fields: ["gid"] }).map(&:gid)
if projects_ids.include?(CURRENT_OBJECTIVES_PROJECT_ID)
UI.important("Not completing task #{task_id} because it's a Current Objective")
next
Expand All @@ -395,7 +391,7 @@ def self.complete_tasks(task_ids, asana_access_token)

def self.find_asana_release_tag(tag_name, release_task_id, asana_access_token)
asana_client = make_asana_client(asana_access_token)
release_task_tags = asana_client.tasks.get_task(task_gid: release_task_id, options: { opt_fields: ["tags"] }).tags
release_task_tags = asana_client.tasks.get_task(task_gid: release_task_id, options: { fields: ["tags"] }).tags

if (tag_id = release_task_tags.find { |t| t.name == tag_name }&.gid) && !tag_id.to_s.empty?
return tag_id
Expand Down Expand Up @@ -436,22 +432,6 @@ def self.sanitize_asana_html_notes(content)
.gsub(%r{<br\s*/?>}, "\n") # replace <br> tags with newlines
end

def self.get_task_ids_from_git_log(from_ref, to_ref = "HEAD")
git_log = `git log #{from_ref}..#{to_ref}`

git_log
.gsub("\n", " ")
.scan(%r{\bTask/Issue URL:.*?https://app\.asana\.com[/0-9f]+\b})
.map { |task_line| task_line.gsub(/.*(https.*)/, '\1') }
.map { |task_url| extract_asana_task_id(task_url, set_gha_output: false) }
end

def self.fetch_release_notes(release_task_id, asana_access_token, output_type: "asana")
asana_client = make_asana_client(asana_access_token)
release_task_body = asana_client.tasks.get_task(task_gid: release_task_id, options: { opt_fields: ["notes"] }).notes
ReleaseTaskHelper.extract_release_notes(release_task_body, output_type: output_type)
end

def self.construct_this_release_includes(task_ids)
return '' if task_ids.empty?

Expand Down
2 changes: 1 addition & 1 deletion lib/fastlane/plugin/ddg_apple_automation/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Fastlane
module DdgAppleAutomation
VERSION = "0.11.3"
VERSION = "0.11.7"
end
end
Loading

0 comments on commit f8a143d

Please sign in to comment.