Skip to content
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

Add support for section fields #123

Merged
merged 10 commits into from
Oct 27, 2023
Merged

Conversation

lf-
Copy link
Contributor

@lf- lf- commented Dec 15, 2022

Before submitting your PR, check that you've:

After submitting your PR:

  • Update the Changelog.md file with a link to your PR
  • Bumped the version number if there isn't an (unreleased) on the Changelog
  • Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)

@lf-
Copy link
Contributor Author

lf- commented Dec 16, 2022

I am not going to be able to finish this PR. Here's the context: CircleCI jobs send messages which only have "fields" but no "text". Currently we require "text" to exist, so we get a parse error when we parse such a message.

This PR attempts to rectify this, but it involves some API changes, and more generally I think that slack-web needs more of the pattern Persistent and http-client use where they don't allow you to construct a SlackSection or whatever, but instead provide a default which you override. This would reduce the API breakage of fixing things like this.

@maxhallinan maxhallinan changed the title fix circleci message parsing Add support for section fields Oct 26, 2023
, slackSectionFields :: Maybe [SlackText]
-- ^ Required if 'slackSectionText' is not provided.
, slackSectionAccessory :: Maybe SlackAccessory
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slackSectionText and slackSectionFields should really be SlackTextObject, but using that type would break the library's API even further.

The consequence of using SlackText is that the library only supports mrkdwn text. This is an incomplete implementation of Slack's API, which accepts both plain_text and mrkdwn for the section text and fields attributes.

Error _ ->
Nothing
Success slackAccessory ->
Just slackAccessory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no good options. This seems like the least bad.

@@ -374,9 +413,7 @@ data SlackBlock
deriving stock (Eq)

instance Show SlackBlock where
Copy link
Contributor

@maxhallinan maxhallinan Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I briefly reformed this Show instance but realized that doing so invalidates 8 golden files. In the interest of keeping this PR small, I reverted.

@@ -163,22 +163,43 @@ truncateSlackMessage (SlackMessage blocks) =
in (SlackMessage truncatedBlocks, or isTruncateds)

truncateSlackBlock :: SlackBlock -> (SlackBlock, Bool)
truncateSlackBlock sb@(SlackBlockSection (SlackText texts) mAccessory) =
let messageLength = sum $ map T.length texts
truncateSlackBlock sb@(SlackBlockSection SlackSection {..}) =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire premise of this function is bizarre and I think it should be removed from the library. Userland should be responsible for handling messages that exceed the length limit.

For that reason, I am not taking the fields text into account here. It would be more trouble than it's worth, imo.

prefixFirstSlackBlockSection prefix (SlackBlockSection text mAccessory : sbs) = (SlackBlockSection (message prefix <> text) mAccessory : sbs, True)
prefixFirstSlackBlockSection prefix (sb : sbs) = let (prefixedSbs, match) = prefixFirstSlackBlockSection prefix sbs in (sb : prefixedSbs, match)
prefixFirstSlackBlockSection _ [] = ([], False)
prefixFirstSlackBlockSection prefix (SlackBlockSection SlackSection {..} : sbs) =
Copy link
Contributor

@maxhallinan maxhallinan Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper is also bizarre in its specificity and seems to belong to some application's use-case. Not a good fit for a library.

@maxhallinan maxhallinan marked this pull request as ready for review October 26, 2023 16:35
Copy link

@levinean levinean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxhallinan Is there any way to test this pr with our backend? Or does it have to be merged before it can be used?

@maxhallinan
Copy link
Contributor

@maxhallinan Is there any way to test this pr with our backend? Or does it have to be merged before it can be used?

You would have to install slack-web from this branch. This PR contains some breaking changes and mwb will have to be migrated before it compiles.

@levinean
Copy link

This PR contains some breaking changes and mwb will have to be migrated before it compiles.
So how do we know that this works? From the golden tests?

@maxhallinan
Copy link
Contributor

@levinean I can add a test that it encodes to valid Slack block JSON.

@maxhallinan
Copy link
Contributor

Screenshot 2023-10-26 at 2 43 50 PM

@levinean you can copy/paste tests/golden/BlockKitBuilderMessage/simple_blocks.golden.json into the Block Kit Builder to see the fields JSON above.

Copy link

@levinean levinean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to test FromJSON as well? We don't really use it but I want to make sure in case we start

Copy link

@levinean levinean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure FromJSON works great, we don't use it anyways, and I feel like I have been blocking this for too long

@maxhallinan maxhallinan merged commit affd233 into master Oct 27, 2023
4 checks passed
@maxhallinan maxhallinan deleted the fix-circleci-messages-wip branch October 27, 2023 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants