Skip to content

Commit 3606288

Browse files
committed
Test wrapping parts of text message in <pre>.
1 parent cbeda52 commit 3606288

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ outputs:
3838

3939
runs:
4040
using: docker
41-
image: docker://ghcr.io/haskell-actions/hlint-scan:v0.4.1
41+
image: Dockerfile # docker://ghcr.io/haskell-actions/hlint-scan:main
4242
args:
4343
- binary=${{ inputs.binary }}
4444
- path=${{ inputs.path }}

src/Format.hs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
-- Maintainer: [email protected]
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

src/Scan.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import Data.Maybe (isJust)
4141
import Data.String
4242
import FilePath qualified
4343
import Fingerprint qualified
44+
import Format (formatMessages)
4445
import GitHub.REST
4546
import System.Environment (getEnvironment)
4647
import System.Exit (ExitCode (ExitSuccess), die, exitWith)
@@ -103,7 +104,7 @@ annotate :: Context -> ByteString -> IO ()
103104
annotate context output = do
104105
env <- getEnvironment
105106
let annotated = AutomationDetails.add env (category context) <$> value
106-
let annotated' = FilePath.normalize . Fingerprint.fill <$> annotated
107+
let annotated' = formatMessages . FilePath.normalize . Fingerprint.fill <$> annotated
107108

108109
when (runnerDebug context) $ do
109110
putStrLn "rewritten output:"

0 commit comments

Comments
 (0)