-
Notifications
You must be signed in to change notification settings - Fork 34
[Spike] Automatically handle and enforce BlockKit limits #198
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
Closed
Closed
Changes from all commits
Commits
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
This file contains hidden or 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 hidden or 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,15 @@ | ||
| module Slack | ||
| module BlockKit | ||
| class Configuration | ||
| attr_accessor :autofix_invalid_blocks | ||
|
|
||
| def initialize | ||
| @autofix_invalid_blocks = false | ||
| end | ||
|
|
||
| def autofix_invalid_blocks? | ||
| !!@autofix_invalid_blocks | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| module Slack | ||
| module BlockKit | ||
| module Limiters | ||
| module BlockId | ||
| BLOCK_ID_LIMIT = 255 | ||
|
|
||
| def as_json | ||
| json = super | ||
|
|
||
| if Slack::BlockKit.config.autofix_invalid_blocks? && json[:block_id] | ||
| json[:block_id] = Utils.truncate(json[:block_id].to_s, length: BLOCK_ID_LIMIT, omission: "") | ||
| end | ||
|
|
||
| json | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,23 @@ | ||
| module Slack | ||
| module BlockKit | ||
| module Utils | ||
| # Truncate a string to a certain length, adding an optional omission | ||
| # string if the string is truncated. This is generally used to ensure | ||
| # that certain block limits do not exceed their maximum length when | ||
| # autofixing is enabled. | ||
| # | ||
| # See: https://api.rubyonrails.org/classes/String.html#method-i-truncate | ||
| def self.truncate(text, length:, omission: "...", separator: nil) | ||
| return text.dup unless text.length > length | ||
|
|
||
| omission ||= "" | ||
| length_with_room_for_omission = length - omission.length | ||
|
|
||
| stop = text.rindex(separator, length_with_room_for_omission) if separator | ||
| stop ||= length_with_room_for_omission | ||
|
|
||
| "#{text[0, stop]}#{omission}" | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
| require_relative '../limiters/block_id_helpers' | ||
|
|
||
| RSpec.describe Slack::BlockKit::Layout::Divider do | ||
| let(:instance) { described_class.new(**params) } | ||
|
|
||
| it_behaves_like 'a block that handles block_id length limits' | ||
|
|
||
| describe '#as_json' do | ||
| subject { instance.as_json } | ||
|
|
||
| let(:params) { {} } | ||
| let(:expected_json) { { type: 'divider' } } | ||
|
|
||
| it { is_expected.to eq expected_json } | ||
|
|
||
| context 'with block_id' do | ||
| let(:params) { { block_id: '1123' } } | ||
| let(:expected_json) do | ||
| { | ||
| type: 'divider', | ||
| block_id: '1123' | ||
| } | ||
| end | ||
|
|
||
| it { is_expected.to eq expected_json } | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,55 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
| require_relative '../limiters/block_id_helpers' | ||
|
|
||
| RSpec.describe Slack::BlockKit::Layout::Image do | ||
| let(:instance) { described_class.new(**params) } | ||
|
|
||
| it_behaves_like 'a block that handles block_id length limits', url: '__URL__', alt_text: '__ALT_TEXT__' | ||
|
|
||
| describe '#as_json' do | ||
| subject { instance.as_json } | ||
|
|
||
| let(:params) do | ||
| { | ||
| url: '__URL__', | ||
| alt_text: '__ALT_TEXT__' | ||
| } | ||
| end | ||
| let(:expected_json) do | ||
| { | ||
| type: 'image', | ||
| image_url: '__URL__', | ||
| alt_text: '__ALT_TEXT__' | ||
| } | ||
| end | ||
|
|
||
| it { is_expected.to eq expected_json } | ||
|
|
||
| context 'with all arguments' do | ||
| let(:params) do | ||
| { | ||
| url: '__URL__', | ||
| alt_text: '__ALT_TEXT__', | ||
| block_id: '1123', | ||
| title: 'This is a title', | ||
| } | ||
| end | ||
| let(:expected_json) do | ||
| { | ||
| type: 'image', | ||
| image_url: '__URL__', | ||
| alt_text: '__ALT_TEXT__', | ||
| block_id: '1123', | ||
| title: { | ||
| type: 'plain_text', | ||
| text: 'This is a title' | ||
| } | ||
| } | ||
| end | ||
|
|
||
| it { is_expected.to eq expected_json } | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
|
|
||
| RSpec.shared_examples_for 'a block that handles block_id length limits' do |**params| | ||
| let(:block) { described_class.new(block_id: block_id, **params) } | ||
| let(:block_id) { 'a' * (Slack::BlockKit::Limiters::BlockId::BLOCK_ID_LIMIT + 1) } | ||
|
|
||
| subject(:json) { block.as_json } | ||
|
|
||
| context 'when autofix is disabled' do | ||
| it 'does not truncate the block_id when converting to JSON' do | ||
| expect(json[:block_id]).to eq(block_id) | ||
| end | ||
| end | ||
|
|
||
| context 'when autofix is enabled' do | ||
| let(:block) { described_class.new(block_id: block_id, **params) } | ||
|
|
||
| around do |example| | ||
| Slack::BlockKit.configuration.autofix_invalid_blocks = true | ||
| example.run | ||
| Slack::BlockKit.configuration.autofix_invalid_blocks = false | ||
| end | ||
|
|
||
| it 'truncates the block_id when converting to JSON' do | ||
| expect(json[:block_id]).to eq('a' * Slack::BlockKit::Limiters::BlockId::BLOCK_ID_LIMIT) | ||
| end | ||
|
|
||
| context 'when the block_id is already within the limit' do | ||
| let(:block_id) { 'a' * (Slack::BlockKit::Limiters::BlockId::BLOCK_ID_LIMIT - 1) } | ||
|
|
||
| it 'does not truncate the block_id' do | ||
| expect(json[:block_id]).to eq(block_id) | ||
| end | ||
| end | ||
|
|
||
| context 'when no block_id is provided' do | ||
| let(:block) { described_class.new(**params) } | ||
| let(:block_id) { nil } | ||
|
|
||
| it 'does not include block_id in the JSON' do | ||
| expect(json).not_to have_key(:block_id) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Note: I used Rails' implementation of
truncatefor a couple reasonsblock_idandaction_iddon't need to truncate with an ellipsis at the end, though that's supposedly still valid), so theomissionandseparatoroptions give us controlautofix_invalid_blocksthat allow users to specify how they'd like text to truncate (e.g.Slack::BlockKit.config.truncate_invalid_text_with = "…"or something)