-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Ruby 3.0] replace slack-notifier gem with internal library #18537
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eb8ab68
Implement FastlaneCore::Slack::LinkConverter
ainame d502696
Implment Slack#post_to_legacy_incoming_webhook
ainame 9a0d4e5
Rename FastlaneCore::Slack to Fastlane::Notification::Slack
ainame 765597f
Fix comment
ainame 5d72af0
Fix Fastlane::Notification::Slack
ainame 25fdbc3
Repalce slack-notifier with Fastlane::Notification::Slack
ainame a484d5a
Get rid of slack-notifier
ainame 191a9ae
Fix HTML_PATTERN
ainame c7dfc67
Add specs
ainame 0794a34
Fix format by rubocop
ainame 1541c8f
Apply suggestions from code review
ainame File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
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.
I almost forgot to mention that I like this namespace strategy a lot 馃槉