Skip to content

Commit

Permalink
tools: add next-id command
Browse files Browse the repository at this point in the history
  • Loading branch information
frasertweedale authored and blackheaven committed Aug 2, 2024
1 parent 59bea00 commit 2105eb1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions EXAMPLE_ADVISORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
```toml

[advisory]
# Submit PRs with HSEC-0000-0000, or run `hsec-tools next-id` to
# print the next available ID.
id = "HSEC-0000-0000"
cwe = []

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ to remove the explanatory comments for each field.
[advisory]
# Identifier for the advisory (mandatory). Will be assigned a "HSEC-YYYY-NNNN"
# identifier e.g. HSEC-2022-0001. Please use "HSEC-0000-0000" in PRs.
# Or run `hsec-tools next-id` to print the next available ID.
id = "HSEC-0000-0000"

# Publication date of the advisory as an RFC 3339 date.
Expand Down
24 changes: 24 additions & 0 deletions code/hsec-tools/app/Command/NextID.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{-# LANGUAGE LambdaCase #-}

module Command.NextID where

import Control.Monad (unless)
import Data.Maybe (fromMaybe)
import System.Exit (die)

import Security.Advisories.Git (getRepoRoot)
import Security.Advisories.Core.HsecId (printHsecId, getNextHsecId)
import Security.Advisories.Filesystem (isSecurityAdvisoriesRepo, getGreatestId)

runNextIDCommand :: Maybe FilePath -> IO ()
runNextIDCommand mPath = do
let
path = fromMaybe "." mPath
repoPath <- getRepoRoot path >>= \case
Left _ -> die "Not a git repo"
Right a -> pure a
isRepo <- isSecurityAdvisoriesRepo repoPath
unless isRepo $
die "Not a security-advisories repo"

getGreatestId repoPath >>= getNextHsecId >>= putStrLn . printHsecId
10 changes: 9 additions & 1 deletion code/hsec-tools/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Main where

import qualified Command.Reserve
import Control.Monad (forM_, join, void, when)
import Control.Monad.Trans.Except (runExceptT, ExceptT (ExceptT), withExceptT, throwE)
import Control.Monad.IO.Class (liftIO)
Expand All @@ -29,6 +28,9 @@ import System.FilePath (takeBaseName)
import System.IO (hPrint, hPutStrLn, stderr)
import Validation (Validation (..))

import qualified Command.Reserve
import qualified Command.NextID

main :: IO ()
main =
join $
Expand All @@ -43,6 +45,7 @@ cliOpts = info (commandsParser <**> helper) (fullDesc <> header "Haskell Advisor
commandsParser =
hsubparser
( command "check" (info commandCheck (progDesc "Syntax check a single advisory"))
<> command "next-id" (info commandNextID (progDesc "Print the next available HSEC ID"))
<> command "reserve" (info commandReserve (progDesc "Reserve an HSEC ID"))
<> command "osv" (info commandOsv (progDesc "Convert a single advisory to OSV"))
<> command "render" (info commandRender (progDesc "Render a single advisory as HTML"))
Expand Down Expand Up @@ -76,6 +79,11 @@ commandReserve =
<> help "Commit the reservation file"
)

commandNextID :: Parser (IO ())
commandNextID =
Command.NextID.runNextIDCommand
<$> optional (argument str (metavar "REPO"))

commandCheck :: Parser (IO ())
commandCheck =
withAdvisory go
Expand Down
1 change: 1 addition & 0 deletions code/hsec-tools/hsec-tools.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ library
executable hsec-tools
main-is: Main.hs
other-modules: Command.Reserve
, Command.NextID

-- Modules included in this executable, other than Main.
-- other-modules:
Expand Down

0 comments on commit 2105eb1

Please sign in to comment.