-
Notifications
You must be signed in to change notification settings - Fork 0
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
Send mattermost action #14
Open
kshann
wants to merge
8
commits into
main
Choose a base branch
from
jacek/send-mattermost-msg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+280
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
87be851
Add send mattermost message action
jaceklyp 5e63a3b
Merge branch 'main' into jacek/send-mattermost-msg
kshann af9c412
Mattermost action unit tests (#13)
kshann 2820fed
add new user
kshann ac0e2ec
PR feedback
kshann 0c0932a
Add template_args to mattermost_send_message_action and make asana_ta…
ayoy 1fa2d52
remove yaml
kshann 6078fe2
Update templates and add specs to verify messages content
ayoy 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
97 changes: 97 additions & 0 deletions
97
lib/fastlane/plugin/ddg_apple_automation/actions/mattermost_send_message_action.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,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 |
File renamed without changes.
30 changes: 30 additions & 0 deletions
30
...ddg_apple_automation/assets/mattermost_send_message/github-mattermost-user-id-mapping.yml
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,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" |
1 change: 1 addition & 0 deletions
1
.../ddg_apple_automation/assets/mattermost_send_message/templates/ios-release-failed.txt.erb
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 @@ | ||
:warning: **iOS release job failed** :thisisfine: | [:github: Workflow run summary](<%= workflow_url %>) |
1 change: 1 addition & 0 deletions
1
...pple_automation/assets/mattermost_send_message/templates/notarized-build-complete.txt.erb
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 @@ | ||
Notarized macOS app `<%= release_type %>` build is ready :goose_honk_tada: | [:github: Workflow run summary](<%= workflow_url %>)<% if defined?(asana_task_url) %> | [:asana: Asana Task](<%= asana_task_url %>)<% end %> |
1 change: 1 addition & 0 deletions
1
..._apple_automation/assets/mattermost_send_message/templates/notarized-build-failed.txt.erb
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 @@ | ||
:rotating_light: Notarized macOS app `<%= release_type %>` build failed | [:github: Workflow run summary](<%= workflow_url %>)<% if defined?(asana_task_url) %> | [:asana: Asana Task](<%= asana_task_url %>)<% end %> |
1 change: 1 addition & 0 deletions
1
...apple_automation/assets/mattermost_send_message/templates/public-release-complete.txt.erb
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 @@ | ||
<%= app_platform %> app has been successfully uploaded to <%= destination %> :goose_honk_tada: | [:github: Workflow run summary](<%= workflow_url %>) |
1 change: 1 addition & 0 deletions
1
...g_apple_automation/assets/mattermost_send_message/templates/public-release-failed.txt.erb
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 @@ | ||
:rotating_light: macOS app <%= destination %> workflow failed | [:github: Workflow run summary](<%= workflow_url %>) |
1 change: 1 addition & 0 deletions
1
...apple_automation/assets/mattermost_send_message/templates/variants-release-failed.txt.erb
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 @@ | ||
:rotating_light: macOS app variants workflow failed | [:github: Workflow run summary](<%= workflow_url %>) |
1 change: 1 addition & 0 deletions
1
...le_automation/assets/mattermost_send_message/templates/variants-release-published.txt.erb
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 @@ | ||
macOS app variants have been published successfully :goose_honk_tada: | [:github: Workflow run summary](<%= workflow_url %>) |
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,146 @@ | ||
describe Fastlane::Actions::MattermostSendMessageAction do | ||
describe "run" do | ||
let(:params) do | ||
{ | ||
mattermost_webhook_url: "http://example.com/webhook", | ||
github_handle: "user", | ||
template_name: "test_template" | ||
} | ||
end | ||
|
||
let(:user_mapping) { { "user" => "@mattermost_user" } } | ||
let(:template_content) { { "text" => "Hello <%= name %>" } } | ||
let(:processed_template) { "Hello World" } | ||
|
||
before do | ||
allow(YAML).to receive(:load_file).with(anything).and_return(user_mapping) | ||
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:path_for_asset_file).and_return("mock_path") | ||
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:process_erb_template).and_return(processed_template) | ||
allow(YAML).to receive(:safe_load).and_return("text" => processed_template) | ||
allow(HTTParty).to receive(:post).and_return(double(success?: true)) | ||
|
||
allow(ENV).to receive(:[]).with("NAME").and_return("World") | ||
end | ||
|
||
it "sends a message to Mattermost with correct payload" do | ||
expected_payload = { | ||
"channel" => "@mattermost_user", | ||
"username" => "GitHub Actions", | ||
"text" => "Hello World", | ||
"icon_url" => "https://duckduckgo.com/assets/logo_header.v108.svg" | ||
} | ||
|
||
expect(HTTParty).to receive(:post).with( | ||
"http://example.com/webhook", | ||
hash_including( | ||
headers: { 'Content-Type' => 'application/json' }, | ||
body: expected_payload.to_json | ||
) | ||
).and_return(double(success?: true)) | ||
|
||
Fastlane::Actions::MattermostSendMessageAction.run(params) | ||
end | ||
|
||
it "skips sending if Mattermost user handle is unknown" do | ||
allow(YAML).to receive(:load_file).and_return({}) | ||
|
||
expect(HTTParty).not_to receive(:post) | ||
expect(FastlaneCore::UI).to receive(:message).with("Mattermost user handle not known for user, skipping sending message") | ||
|
||
Fastlane::Actions::MattermostSendMessageAction.run(params) | ||
end | ||
|
||
it "handles unsuccessful HTTP response" do | ||
allow(HTTParty).to receive(:post).and_return(double(success?: false, body: "Error message")) | ||
|
||
expect { Fastlane::Actions::MattermostSendMessageAction.run(params) }.to raise_error(FastlaneCore::Interface::FastlaneError, "Failed to send message: Error message") | ||
end | ||
end | ||
|
||
describe "process_template" do | ||
it "processes ios-release-failed template" do | ||
expected = ":warning: **iOS release job failed** :thisisfine: | [:github: Workflow run summary](https://workflow.com)" | ||
|
||
expect(process_template("ios-release-failed", { | ||
"workflow_url" => "https://workflow.com" | ||
})).to eq(expected) | ||
end | ||
|
||
it "processes notarized-build-complete template" do | ||
expected = "Notarized macOS app `release` build is ready :goose_honk_tada: | [:github: Workflow run summary](https://workflow.com)" | ||
|
||
expect(process_template("notarized-build-complete", { | ||
"release_type" => "release", | ||
"workflow_url" => "https://workflow.com" | ||
})).to eq(expected) | ||
end | ||
|
||
it "processes notarized-build-complete template with Asana task URL" do | ||
expected = "Notarized macOS app `release` build is ready :goose_honk_tada: | [:github: Workflow run summary](https://workflow.com) | [:asana: Asana Task](https://asana.com)" | ||
|
||
expect(process_template("notarized-build-complete", { | ||
"asana_task_url" => "https://asana.com", | ||
"release_type" => "release", | ||
"workflow_url" => "https://workflow.com" | ||
})).to eq(expected) | ||
end | ||
|
||
it "processes notarized-build-failed template" do | ||
expected = ":rotating_light: Notarized macOS app `release` build failed | [:github: Workflow run summary](https://workflow.com)" | ||
|
||
expect(process_template("notarized-build-failed", { | ||
"release_type" => "release", | ||
"workflow_url" => "https://workflow.com" | ||
})).to eq(expected) | ||
end | ||
|
||
it "processes notarized-build-failed template with Asana task URL" do | ||
expected = ":rotating_light: Notarized macOS app `release` build failed | [:github: Workflow run summary](https://workflow.com) | [:asana: Asana Task](https://asana.com)" | ||
|
||
expect(process_template("notarized-build-failed", { | ||
"asana_task_url" => "https://asana.com", | ||
"release_type" => "release", | ||
"workflow_url" => "https://workflow.com" | ||
})).to eq(expected) | ||
end | ||
|
||
it "processes public-release-complete template" do | ||
expected = "macOS app has been successfully uploaded to testflight :goose_honk_tada: | [:github: Workflow run summary](https://workflow.com)" | ||
|
||
expect(process_template("public-release-complete", { | ||
"app_platform" => "macOS", | ||
"destination" => "testflight", | ||
"workflow_url" => "https://workflow.com" | ||
})).to eq(expected) | ||
end | ||
|
||
it "processes public-release-failed template" do | ||
expected = ":rotating_light: macOS app testflight workflow failed | [:github: Workflow run summary](https://workflow.com)" | ||
|
||
expect(process_template("public-release-failed", { | ||
"destination" => "testflight", | ||
"workflow_url" => "https://workflow.com" | ||
})).to eq(expected) | ||
end | ||
|
||
it "processes variants-release-failed template" do | ||
expected = ":rotating_light: macOS app variants workflow failed | [:github: Workflow run summary](https://workflow.com)" | ||
|
||
expect(process_template("variants-release-failed", { | ||
"workflow_url" => "https://workflow.com" | ||
})).to eq(expected) | ||
end | ||
|
||
it "processes variants-release-published template" do | ||
expected = "macOS app variants have been published successfully :goose_honk_tada: | [:github: Workflow run summary](https://workflow.com)" | ||
|
||
expect(process_template("variants-release-published", { | ||
"workflow_url" => "https://workflow.com" | ||
})).to eq(expected) | ||
end | ||
|
||
def process_template(template_name, args) | ||
Fastlane::Actions::MattermostSendMessageAction.process_template(template_name, args) | ||
end | ||
end | ||
end |
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.
This gets a bit verbose so we usually provide a helper function in the spec called e.g.
run
, callingFastlane::Actions::MattermostSendMessageAction.run
inside. Seeprocess_template
at the bottom of the file.