|
| 1 | +{- |
| 2 | +Copyright 2023 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +-} |
| 16 | + |
| 17 | +-- | |
| 18 | +-- Description: Format messages for GitHub |
| 19 | +-- Copyright: Copyright 2023 Google LLC |
| 20 | +-- License: Apache-2.0 |
| 21 | + |
| 22 | +-- |
| 23 | +-- Format message text in result objects for GitHub. |
| 24 | +-- |
| 25 | +-- GitHub only supports the `text` property in a `message` object inside a `result` object. |
| 26 | +-- It does not support other properties such as `markdown`. |
| 27 | +-- Theoretically this means that GitHub should only be able to render messages |
| 28 | +-- as pure text, which is unfortunate because it doesn't actually do so |
| 29 | +-- and ignores spaces at the beginning of lines, which makes messages |
| 30 | +-- much less readable. |
| 31 | +-- |
| 32 | +-- On the flip side, the reason it ignores spaces in the beginning is |
| 33 | +-- GitHub renders the messages with Markdown, although unfortunately it |
| 34 | +-- renders a restricted and /undocumented/ subset of the full Markdown syntax. |
| 35 | +-- |
| 36 | +-- SARIF output from HLint should support tools in general, |
| 37 | +-- so it would not be appropriate for HLint to add Markdown formatting |
| 38 | +-- to the text messages. Instead, this module will rewrite the text messages |
| 39 | +-- to add formatting to make them easier to read, since this program |
| 40 | +-- is specialized for uploading SARIF files to GitHub. |
| 41 | +module Format (formatMessages) where |
| 42 | + |
| 43 | +import Data.Aeson |
| 44 | +import Data.Aeson.KeyMap hiding (map) |
| 45 | +import Data.Text qualified as Text |
| 46 | + |
| 47 | +-- | Format text messages in result objects to be better readable on GitHub. |
| 48 | +formatMessages :: Value -> Value |
| 49 | +formatMessages (Object v) = Object $ mapWithKey formatRuns v |
| 50 | +formatMessages v = v |
| 51 | + |
| 52 | +formatRuns :: Key -> Value -> Value |
| 53 | +formatRuns "runs" (Array vs) = Array $ fmap formatRun vs |
| 54 | +formatRuns _ v = v |
| 55 | + |
| 56 | +formatRun :: Value -> Value |
| 57 | +formatRun (Object v) = Object $ mapWithKey formatResults v |
| 58 | +formatRun v = v |
| 59 | + |
| 60 | +formatResults :: Key -> Value -> Value |
| 61 | +formatResults "results" (Array vs) = Array $ fmap formatResult vs |
| 62 | +formatResults _ v = v |
| 63 | + |
| 64 | +formatResult :: Value -> Value |
| 65 | +formatResult (Object v) = Object $ mapWithKey formatMessage v |
| 66 | +formatResult v = v |
| 67 | + |
| 68 | +formatMessage :: Key -> Value -> Value |
| 69 | +formatMessage "message" (Object v) = Object $ mapWithKey formatText v |
| 70 | +formatMessage _ v = v |
| 71 | + |
| 72 | +formatText :: Key -> Value -> Value |
| 73 | +formatText "text" (String s) = String s' |
| 74 | + where |
| 75 | + l = Text.lines s |
| 76 | + s' | x:xs <- l = Text.unlines $ x : "<pre>" : xs ++ ["</pre>"] |
| 77 | + | [] <- l = s |
| 78 | +formatText _ v = v |
0 commit comments