Skip to content

Commit c629019

Browse files
authored
Code cleanup and documentation. (#23)
1 parent 5bdbad7 commit c629019

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

src/AutomationDetails.hs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,22 @@ import Data.Aeson.KeyMap hiding (lookup, map)
2121
import Data.Maybe (fromMaybe)
2222
import Data.Text hiding (singleton)
2323

24-
-- See https://github.com/github/codeql-action/blob/v2/lib/upload-lib.js
2524
-- See https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/sarif-support-for-code-scanning#runautomationdetails-object
2625

2726
add :: [(String, String)] -> Maybe String -> Value -> Value
28-
add env category (Object v) = Object $ mapWithKey (addToRuns details) v
27+
add env category (Object v) = Object $ mapWithKey addToRuns v
2928
where
3029
details = fromMaybe "" category <> "/" <> runId
3130
runId = fromMaybe "" (lookup "GITHUB_RUN_ID" env)
32-
add _ _ v = v
3331

34-
addToRuns :: String -> Key -> Value -> Value
35-
addToRuns details "runs" (Array v) =
36-
Array $ fmap (addToRun details) v
37-
addToRuns _ _ v = v
32+
addToRuns "runs" (Array u) = Array $ fmap addToRun u
33+
addToRuns _ u = u
3834

39-
addToRun :: String -> Value -> Value
40-
addToRun details (Object v) = Object $ addDetails details v
41-
addToRun _ v = v
35+
addToRun (Object u) = Object $ addDetails u
36+
addToRun u = u
4237

43-
addDetails :: String -> Object -> Object
44-
addDetails details =
45-
insert
46-
"automationDetails"
47-
(Object $ singleton "id" (String $ pack details))
38+
addDetails =
39+
insert
40+
"automationDetails"
41+
(Object $ singleton "id" (String $ pack details))
42+
add _ _ v = v

src/Fingerprint.hs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ limitations under the License.
2222
--
2323
-- SARIF uses partial fingerprints in results to aid in an attempt
2424
-- to track the "same" issues despite changes. This fills partial
25-
-- fingerprints in results which do not already have them,
25+
-- fingerprints in result objects which do not already have them,
2626
-- while keeping everything else the same in SARIF output.
2727
module Fingerprint (fill) where
2828

@@ -53,10 +53,9 @@ fillResults _ v = v
5353
fillResult :: Value -> Value
5454
fillResult o@(Object v)
5555
| member "partialFingerprint" v = o
56-
| otherwise = Object $ insert "partialFingerprints" fpValue v
56+
| otherwise = Object $ insert "partialFingerprints" fp v
5757
where
58-
fp = toPartialFingerprint $ toCodeIssue v
59-
fpValue = Object $ singleton "LogicalCodeIssue/v1" $ String fp
58+
fp = toPartialFingerprint v
6059
fillResult v = v
6160

6261
data CodeIssue = CodeIssue
@@ -96,9 +95,14 @@ qualifiedName (Object v)
9695
| otherwise = Nothing
9796
qualifiedName _ = Nothing
9897

99-
toPartialFingerprint :: CodeIssue -> Text
100-
toPartialFingerprint CodeIssue {ruleId, level, locations} =
101-
encodeTextList [ruleId, level, encodeTextList . map Just <$> locations]
98+
toPartialFingerprint :: Object -> Value
99+
toPartialFingerprint v =
100+
Object $ singleton propertyName $ String encodedResult
101+
where
102+
CodeIssue {ruleId, level, locations} = toCodeIssue v
103+
encodedLocations = encodeTextList . map Just <$> locations
104+
encodedResult = encodeTextList [ruleId, level, encodedLocations]
105+
propertyName = "LogicalCodeIssue/v1"
102106

103107
encodeTextList :: [Maybe Text] -> Text
104108
encodeTextList = encodeBase64 . Text.concat . map encodeItem

src/Scan.hs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ import Prelude hiding (lookup, putStr)
4848
-- In particular, this is used to pass on the category and access token
4949
-- which would have been passed in as arguments to the program
5050
-- from the argument parsing stage to the API call to GitHub.
51-
data Context = Context {category :: Maybe String, gitHubToken :: Maybe String}
51+
data Context = Context
52+
{ category :: Maybe String,
53+
gitHubToken :: Maybe String
54+
}
5255

5356
main :: [String] -> IO ()
5457
main args = case Arguments.validate args of
@@ -57,10 +60,12 @@ main args = case Arguments.validate args of
5760

5861
invoke :: [String] -> IO ()
5962
invoke args = do
60-
let (executable, flags, cat, tok) = Arguments.translate args
61-
(exitCode, out, err) <- readCreateProcessWithExitCode (proc executable flags) ""
63+
let (executable, flags, category, token) = Arguments.translate args
64+
(exitCode, out, err) <-
65+
readCreateProcessWithExitCode (proc executable flags) ""
66+
let context = Context {category = category, gitHubToken = token}
6267
case exitCode of
63-
ExitSuccess -> annotate Context {category = cat, gitHubToken = tok} (fromString out)
68+
ExitSuccess -> annotate context $ fromString out
6469
_ -> putStrLn err >> exitWith exitCode
6570

6671
annotate :: Context -> ByteString -> IO ()
@@ -70,7 +75,7 @@ annotate context output = do
7075
let annotated' = Fingerprint.fill <$> annotated
7176
case annotated' of
7277
Nothing -> die $ "invalid encoding\n" <> show output <> "\n"
73-
Just output' -> send context (encode output')
78+
Just output' -> send context $ encode output'
7479
where
7580
value = decode output :: Maybe Value
7681

@@ -81,8 +86,9 @@ send context output = do
8186
let endpoint' = toCall env output
8287
case endpoint' of
8388
Just endpoint -> call settings endpoint
84-
_ -> die ("missing environment variables\n" <> show env)
89+
_ -> die "not all necessary environment variables available"
8590

8691
call :: GitHubSettings -> GHEndpoint -> IO ()
87-
call settings endpoint = do
88-
putStrLn . unlines . toOutputs =<< runGitHubT settings (queryGitHub endpoint)
92+
call settings endpoint =
93+
putStrLn . unlines . toOutputs
94+
=<< runGitHubT settings (queryGitHub endpoint)

src/Upload.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ toCall :: [(String, String)] -> ByteString -> Maybe GHEndpoint
2929
toCall env sarifLog
3030
| Just repo <- repo',
3131
Just commitSha <- commitSha',
32-
Just ref <- ref' =
32+
Just ref <- ref',
33+
Just workspace <- workspace' =
3334
Just
3435
GHEndpoint
3536
{ method = POST,
@@ -39,6 +40,7 @@ toCall env sarifLog
3940
[ "commit_sha" := commitSha,
4041
"ref" := ref,
4142
"sarif" := encodedSarif,
43+
"checkout_uri" := "file://" <> workspace,
4244
"tool_name" := ("HLint" :: Text),
4345
"validate" := True
4446
]
@@ -50,6 +52,7 @@ toCall env sarifLog
5052
repo' = lookup "GITHUB_REPOSITORY" env
5153
commitSha' = lookup "GITHUB_SHA" env
5254
ref' = lookup "GITHUB_REF" env
55+
workspace' = lookup "GITHUB_WORKSPACE" env
5356
encodedSarif = encodeBase64 $ compress sarifLog
5457

5558
toSettings :: Maybe String -> GitHubSettings

0 commit comments

Comments
 (0)