-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ruby 3.0] replace slack-notifier gem with internal library (#18537)
* Implement FastlaneCore::Slack::LinkConverter * Implment Slack#post_to_legacy_incoming_webhook * Rename FastlaneCore::Slack to Fastlane::Notification::Slack * Fix comment * Fix Fastlane::Notification::Slack * Repalce slack-notifier with Fastlane::Notification::Slack * Get rid of slack-notifier * Fix HTML_PATTERN * Add specs * Fix format by rubocop * Apply suggestions from code review Co-authored-by: Roger Oba <rogerluan.oba@gmail.com> Co-authored-by: Roger Oba <rogerluan.oba@gmail.com>
- Loading branch information
Showing
5 changed files
with
103 additions
and
20 deletions.
There are no files selected for viewing
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
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
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
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,56 @@ | ||
module Fastlane | ||
module Notification | ||
class Slack | ||
def initialize(webhook_url) | ||
@webhook_url = webhook_url | ||
@client = Faraday.new do |conn| | ||
conn.use(Faraday::Response::RaiseError) | ||
end | ||
end | ||
|
||
# Overriding channel, icon_url and username is only supported in legacy incoming webhook. | ||
# Also note that the use of attachments has been discouraged by Slack, in favor of Block Kit. | ||
# https://api.slack.com/legacy/custom-integrations/messaging/webhooks | ||
def post_to_legacy_incoming_webhook(channel:, username:, attachments:, link_names:, icon_url:) | ||
@client.post(@webhook_url) do |request| | ||
request.headers['Content-Type'] = 'application/json' | ||
request.body = { | ||
channel: channel, | ||
username: username, | ||
icon_url: icon_url, | ||
attachments: attachments, | ||
link_names: link_names | ||
}.to_json | ||
end | ||
end | ||
|
||
# This class was inspired by `LinkFormatter` in `slack-notifier` gem | ||
# https://github.com/stevenosloan/slack-notifier/blob/4bf6582663dc9e5070afe3fdc42d67c14a513354/lib/slack-notifier/util/link_formatter.rb | ||
class LinkConverter | ||
HTML_PATTERN = %r{<a.*?href=['"](?<link>#{URI.regexp})['"].*?>(?<label>.+?)<\/a>} | ||
MARKDOWN_PATTERN = /\[(?<label>[^\[\]]*?)\]\((?<link>#{URI.regexp}|mailto:#{URI::MailTo::EMAIL_REGEXP})\)/ | ||
|
||
def self.convert(string) | ||
convert_markdown_to_slack_link(convert_html_to_slack_link(string.scrub)) | ||
end | ||
|
||
def self.convert_html_to_slack_link(string) | ||
string.gsub(HTML_PATTERN) do |match| | ||
slack_link(Regexp.last_match[:link], Regexp.last_match[:label]) | ||
end | ||
end | ||
|
||
def self.convert_markdown_to_slack_link(string) | ||
string.gsub(MARKDOWN_PATTERN) do |match| | ||
slack_link(Regexp.last_match[:link], Regexp.last_match[:label]) | ||
end | ||
end | ||
|
||
def self.slack_link(href, text) | ||
return "<#{href}>" if text.nil? || text.empty? | ||
"<#{href}|#{text}>" | ||
end | ||
end | ||
end | ||
end | ||
end |
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,28 @@ | ||
describe Fastlane::Notification::Slack do | ||
describe Fastlane::Notification::Slack::LinkConverter do | ||
it 'should convert HTML anchor tag to Slack link format' do | ||
{ | ||
%|Hello <a href="https://fastlane.tools">fastlane</a>| => 'Hello <https://fastlane.tools|fastlane>', | ||
%|Hello <a href='https://fastlane.tools'>fastlane</a>| => 'Hello <https://fastlane.tools|fastlane>', | ||
%|Hello <a id="foo" href="https://fastlane.tools">fastlane</a>| => 'Hello <https://fastlane.tools|fastlane>', | ||
%|Hello <a href="https://fastlane.tools">fastlane</a> <a href="https://github.com/fastlane">GitHub</a>| => 'Hello <https://fastlane.tools|fastlane> <https://github.com/fastlane|GitHub>' | ||
}.each do |input, output| | ||
expect(described_class.convert(input)).to eq(output) | ||
end | ||
end | ||
|
||
it 'should convert Markdown link to Slack link format' do | ||
{ | ||
%|Hello [fastlane](https://fastlane.tools)| => 'Hello <https://fastlane.tools|fastlane>', | ||
%|Hello [fastlane](mailto:fastlane@fastlane.tools)| => 'Hello <mailto:fastlane@fastlane.tools|fastlane>', | ||
%|Hello [fastlane](https://fastlane.tools) [GitHub](https://github.com/fastlane)| => 'Hello <https://fastlane.tools|fastlane> <https://github.com/fastlane|GitHub>', | ||
%|Hello [[fastlane](https://fastlane.tools) in brackets]| => 'Hello [<https://fastlane.tools|fastlane> in brackets]', | ||
%|Hello [](https://fastlane.tools)| => 'Hello <https://fastlane.tools>', | ||
%|Hello ([fastlane](https://fastlane.tools) in parens)| => 'Hello (<https://fastlane.tools|fastlane> in parens)', | ||
%|Hello ([fastlane(:rocket:)](https://fastlane.tools) in parens)| => 'Hello (<https://fastlane.tools|fastlane(:rocket:)> in parens)' | ||
}.each do |input, output| | ||
expect(described_class.convert(input)).to eq(output) | ||
end | ||
end | ||
end | ||
end |