Skip to content

Commit

Permalink
Release 3356 | Raise error when error 413 is likely to happen (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
mooikos authored Jun 18, 2024
1 parent 8e63882 commit fc41f27
Show file tree
Hide file tree
Showing 22 changed files with 198 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: [ "2.7", "3.0", "3.1.2" ]
ruby-version: [ "3.1", "3.2", "3.3" ]
steps:
- uses: actions/checkout@v2

Expand Down
5 changes: 3 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
AllCops:
TargetRubyVersion: 2.7
TargetRubyVersion: 3.1

Metrics/BlockLength:
Exclude:
- 'spec/**/*'
- msteams_hermes.gemspec
- spec/**/*

Style/StringLiterals:
Enabled: true
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

## [1.1.1] - 2023-01-24
- Prevent causing 413 response from microsoft teams webhook

## [1.1.0] - 2023-01-24
- Add support for @mentions

Expand Down
4 changes: 0 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,3 @@ source "https://rubygems.org"

# Specify your gem's dependencies in msteams_hermes.gemspec
gemspec

gem "rake"
gem "rspec"
gem "rubocop"
87 changes: 57 additions & 30 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,59 +1,86 @@
PATH
remote: .
specs:
msteams_hermes (1.1.0)
msteams_hermes (1.1.1)

GEM
remote: https://rubygems.org/
specs:
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
diff-lcs (1.5.0)
json (2.6.2)
parallel (1.22.1)
parser (3.1.2.1)
bigdecimal (3.1.8)
byebug (11.1.3)
coderay (1.1.3)
crack (1.0.0)
bigdecimal
rexml
diff-lcs (1.5.1)
hashdiff (1.1.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
method_source (1.1.0)
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
pry-byebug (3.10.1)
byebug (~> 11.0)
pry (>= 0.13, < 0.15)
public_suffix (5.0.5)
racc (1.8.0)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.5.0)
rexml (3.2.5)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
rake (13.2.1)
regexp_parser (2.9.2)
rexml (3.2.8)
strscan (>= 3.0.9)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rubocop (1.36.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.63.5)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.1.2.1)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.20.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.21.0)
parser (>= 3.1.1.0)
ruby-progressbar (1.11.0)
unicode-display_width (2.2.0)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
strscan (3.1.0)
unicode-display_width (2.5.0)
webmock (3.23.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby
x86_64-darwin-20
x86_64-darwin-21

DEPENDENCIES
msteams_hermes!
pry-byebug
rake
rspec
rubocop
webmock (~> 3.23)

BUNDLED WITH
2.2.15
2.5.10
2 changes: 1 addition & 1 deletion complex_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@
]
)

MsTeamsHermes::Message.new(webhook_url: YOUR_WEBHOOK_URL, content: content).deliver
MsTeamsHermes::Message.new(webhook_url: YOUR_WEBHOOK_URL, content:).deliver
6 changes: 3 additions & 3 deletions lib/msteams_hermes/actions/open_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def initialize(url:, title: nil, tooltip: nil)
def to_hash
{
type: "Action.OpenUrl",
url: url,
title: title,
tooltip: tooltip
url:,
title:,
tooltip:
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/msteams_hermes/components/adaptive_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def to_hash
actions: actions&.map(&:to_hash),
msteams: {
entities: entities.map(&:to_hash),
width: width
width:
}
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/msteams_hermes/components/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(items:, width: "auto")
def to_hash
{
type: "Column",
width: width,
width:,
items: items.map(&:to_hash)
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/msteams_hermes/components/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(items:, style: Style::ContainerStyle::DEFAULT, select_action: nil
def to_hash
{
type: "Container",
style: style,
style:,
selectAction: select_action&.to_hash,
items: items.map(&:to_hash)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/msteams_hermes/components/fact_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize(facts:)
def to_hash
{
type: "FactSet",
facts: facts
facts:
}
end

Expand Down
14 changes: 7 additions & 7 deletions lib/msteams_hermes/components/text_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def initialize(text:,
def to_hash
{
type: "TextBlock",
text: text,
color: color,
size: size,
font_type: font_type,
weight: weight,
wrap: wrap,
is_subtle: is_subtle
text:,
color:,
size:,
font_type:,
weight:,
wrap:,
is_subtle:
}
end
end
Expand Down
43 changes: 42 additions & 1 deletion lib/msteams_hermes/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ module MsTeamsHermes
# https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL#send-adaptive-cards-using-an-incoming-webhook
##
class Message
##
# Raises when the message is larger than the latest known maximum size of a microsoft teams message
##
class MessageBodyTooLargeError < StandardError
def initialize(current_size)
super "Microsoft Teams Webhook answered with a 413 due to content size limitations" \
" (last check it was about #{MSTEAMS_MESSAGE_SIZE_LIMIT} bytes)." \
"\nYour message results in a size of about #{current_size} bytes which is too much."
end
end

##
# Raises when the response message changes from the successful response
##
class UnknownError < StandardError
def initialize(error)
super "Microsoft Teams Webhook had an unexpected response body:\n#{error}"
end
end

# Docu says: 'The message size limit is 28 KB',
# but testing aligned with 21KB.
# See: https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook
MSTEAMS_MESSAGE_SIZE_LIMIT = 21_000
MSTEAMS_MESSAGE_413_ERROR_TOKEN = "returned HTTP error 413"

attr_reader :webhook_url, :content

def initialize(content:, webhook_url: ENV["WEBHOOK_URL"])
Expand All @@ -19,6 +45,8 @@ def initialize(content:, webhook_url: ENV["WEBHOOK_URL"])
raise "Message `content` must be an AdaptiveCard" unless @content.is_a? Components::AdaptiveCard
end

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
##
# Sends a HTTP request to the webhook URL specified via
# either environment variable or when initializing the class
Expand All @@ -31,9 +59,22 @@ def deliver
req = Net::HTTP::Post.new(uri)
req.body = body_json
req["Content-Type"] = "application/json"
http.request(req)

response = http.request(req)

# For details see:
# https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL%2Ctext1#send-messages-using-curl-and-powershell
if response.body != "1"
raise MessageBodyTooLargeError, body_json.bytesize if response.body.include? MSTEAMS_MESSAGE_413_ERROR_TOKEN

raise UnknownError, response.body
end

response
end
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength

##
# Formats the JSON object to be set on the HTTP request
Expand Down
2 changes: 1 addition & 1 deletion lib/msteams_hermes/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MsTeamsHermes
VERSION = "1.1.0"
VERSION = "1.1.1"
end
10 changes: 7 additions & 3 deletions msteams_hermes.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ Gem::Specification.new do |spec|
"Microsoft's adaptive cards."
spec.homepage = "https://github.com/xing/msteams_hermes"
spec.license = "MIT"
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/xing/msteams_hermes"
spec.metadata["changelog_uri"] = "https://github.com/xing/msteams_hermes/CHANGELOG.md"

spec.required_ruby_version = Gem::Requirement.new(">= 3.1")

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
Expand All @@ -34,6 +35,9 @@ Gem::Specification.new do |spec|
# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"

# For more information and examples about making a new gem, checkout our
# guide at: https://bundler.io/guides/creating_gem.html
spec.add_development_dependency "pry-byebug"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rubocop"
spec.add_development_dependency "webmock", "~> 3.23"
end
4 changes: 2 additions & 2 deletions spec/msteams_hermes/actions/open_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

RSpec.describe MsTeamsHermes::Actions::OpenUrl do
describe "#to_hash" do
subject(:action) { MsTeamsHermes::Actions::OpenUrl.new(url: url, title: title, tooltip: tooltip) }
subject(:action) { MsTeamsHermes::Actions::OpenUrl.new(url:, title:, tooltip:) }

let(:url) { "https://foo" }
let(:title) { "any title" }
let(:tooltip) { "any tooltip" }

it "renders the hash object" do
expect(action.to_hash).to eq({ type: "Action.OpenUrl", url: url, title: title, tooltip: tooltip })
expect(action.to_hash).to eq({ type: "Action.OpenUrl", url:, title:, tooltip: })
end
end
end
2 changes: 1 addition & 1 deletion spec/msteams_hermes/components/adaptive_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

describe "#to_hash" do
subject(:component) do
MsTeamsHermes::Components::AdaptiveCard.new(body: [fact_set], actions: [action], width: width)
MsTeamsHermes::Components::AdaptiveCard.new(body: [fact_set], actions: [action], width:)
end

let(:fact_set) { MsTeamsHermes::Components::FactSet.new(facts: [fact]) }
Expand Down
4 changes: 2 additions & 2 deletions spec/msteams_hermes/components/column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

describe "#to_hash" do
subject(:component) { MsTeamsHermes::Components::Column.new(items: [fact_set], width: width) }
subject(:component) { MsTeamsHermes::Components::Column.new(items: [fact_set], width:) }

let(:fact_set) { MsTeamsHermes::Components::FactSet.new(facts: [fact]) }
let(:fact) { { title: "foo", value: "bar" } }
Expand All @@ -28,7 +28,7 @@
facts: [fact]
}
],
width: width
width:
}

expect(component.to_hash).to eq hash
Expand Down
4 changes: 2 additions & 2 deletions spec/msteams_hermes/components/container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
describe "#to_hash" do
subject(:component) do
MsTeamsHermes::Components::Container.new(items: [fact_set],
style: style,
style:,
select_action: action)
end

Expand All @@ -36,7 +36,7 @@
facts: [fact]
}
],
style: style,
style:,
selectAction: {
type: "Action.OpenUrl",
url: action_url,
Expand Down
Loading

0 comments on commit fc41f27

Please sign in to comment.