2018-03-31 06:07:46 +03:00
|
|
|
{-# LANGUAGE ExtendedDefaultRules #-}
|
2019-01-18 11:20:47 +03:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2018-04-04 12:24:55 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-03-31 06:07:46 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
|
2018-04-04 12:24:55 +03:00
|
|
|
|
2018-12-24 02:02:54 +03:00
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Control.Applicative ((<**>))
|
2018-03-31 06:07:46 +03:00
|
|
|
import qualified Data.Text as T
|
2018-07-11 05:30:34 +03:00
|
|
|
import qualified Data.Text.IO as T
|
2018-09-06 16:47:09 +03:00
|
|
|
import DeleteMerged (deleteDone)
|
2020-06-13 07:34:18 +03:00
|
|
|
import Git
|
2023-03-03 07:30:36 +03:00
|
|
|
import qualified GitHub as GH
|
2019-10-07 02:17:08 +03:00
|
|
|
import NVD (withVulnDB)
|
2019-09-08 02:48:10 +03:00
|
|
|
import qualified Nix
|
|
|
|
import qualified Options.Applicative as O
|
2020-01-20 02:13:28 +03:00
|
|
|
import OurPrelude
|
|
|
|
import qualified Repology
|
|
|
|
import System.IO (BufferMode (..), hSetBuffering, stderr, stdout)
|
2020-02-09 02:24:37 +03:00
|
|
|
import qualified System.Posix.Env as P
|
2023-01-05 07:29:13 +03:00
|
|
|
import Update (cveAll, cveReport, sourceGithubAll, updatePackage)
|
2020-06-07 05:11:23 +03:00
|
|
|
import Utils (Options (..), UpdateEnv (..), getGithubToken, getGithubUser)
|
2018-04-04 02:03:46 +03:00
|
|
|
|
2018-03-31 06:07:46 +03:00
|
|
|
default (T.Text)
|
|
|
|
|
2020-07-21 15:36:23 +03:00
|
|
|
data UpdateOptions = UpdateOptions
|
|
|
|
{ pr :: Bool,
|
|
|
|
cve :: Bool,
|
|
|
|
nixpkgsReview :: Bool,
|
2021-08-27 22:15:26 +03:00
|
|
|
outpaths :: Bool,
|
|
|
|
attrpathOpt :: Bool
|
2020-07-21 15:36:23 +03:00
|
|
|
}
|
2019-01-18 11:20:47 +03:00
|
|
|
|
2019-09-08 02:48:10 +03:00
|
|
|
data Command
|
2023-01-05 07:25:21 +03:00
|
|
|
= Update UpdateOptions Text
|
2022-07-07 18:01:40 +03:00
|
|
|
| UpdateBatch UpdateOptions Text
|
2020-06-13 07:34:18 +03:00
|
|
|
| DeleteDone Bool
|
2019-09-08 02:48:10 +03:00
|
|
|
| Version
|
2019-09-08 02:53:01 +03:00
|
|
|
| UpdateVulnDB
|
2019-10-07 02:17:08 +03:00
|
|
|
| CheckAllVulnerable
|
2019-10-13 00:37:34 +03:00
|
|
|
| SourceGithub
|
2020-01-20 02:13:28 +03:00
|
|
|
| FetchRepology
|
2019-10-07 02:17:08 +03:00
|
|
|
| CheckVulnerable Text Text Text
|
2018-04-04 02:03:46 +03:00
|
|
|
|
2020-02-09 20:13:46 +03:00
|
|
|
updateOptionsParser :: O.Parser UpdateOptions
|
2019-09-08 02:48:10 +03:00
|
|
|
updateOptionsParser =
|
2020-02-09 20:13:46 +03:00
|
|
|
UpdateOptions
|
2020-04-06 03:23:12 +03:00
|
|
|
<$> O.flag False True (O.long "pr" <> O.help "Make a pull request using Hub.")
|
2020-04-07 06:39:27 +03:00
|
|
|
<*> O.flag False True (O.long "cve" <> O.help "Make a CVE vulnerability report.")
|
2020-04-13 02:36:59 +03:00
|
|
|
<*> O.flag False True (O.long "nixpkgs-review" <> O.help "Runs nixpkgs-review on update commit rev")
|
2020-02-22 09:18:14 +03:00
|
|
|
<*> O.flag False True (O.long "outpaths" <> O.help "Calculate outpaths to determine the branch to target")
|
2021-08-27 22:15:26 +03:00
|
|
|
<*> O.flag False True (O.long "attrpath" <> O.help "UPDATE_INFO uses the exact attrpath.")
|
2019-01-18 11:20:47 +03:00
|
|
|
|
2020-04-06 03:23:12 +03:00
|
|
|
updateParser :: O.Parser Command
|
|
|
|
updateParser =
|
|
|
|
Update
|
|
|
|
<$> updateOptionsParser
|
|
|
|
<*> O.strArgument (O.metavar "UPDATE_INFO" <> O.help "update string of the form: 'pkg oldVer newVer update-page'\n\n example: 'tflint 0.15.0 0.15.1 repology.org'")
|
|
|
|
|
2022-07-28 06:24:10 +03:00
|
|
|
updateBatchParser :: O.Parser Command
|
|
|
|
updateBatchParser =
|
|
|
|
UpdateBatch
|
|
|
|
<$> updateOptionsParser
|
|
|
|
<*> O.strArgument (O.metavar "UPDATE_INFO" <> O.help "update string of the form: 'pkg oldVer newVer update-page'\n\n example: 'tflint 0.15.0 0.15.1 repology.org'")
|
|
|
|
|
2020-06-13 07:34:18 +03:00
|
|
|
deleteDoneParser :: O.Parser Command
|
|
|
|
deleteDoneParser =
|
|
|
|
DeleteDone
|
|
|
|
<$> O.flag False True (O.long "delete" <> O.help "Actually delete the done branches. Otherwise just prints the branches to delete.")
|
|
|
|
|
2019-09-08 02:48:10 +03:00
|
|
|
commandParser :: O.Parser Command
|
|
|
|
commandParser =
|
|
|
|
O.hsubparser
|
2024-04-03 01:01:16 +03:00
|
|
|
( O.command
|
|
|
|
"update"
|
|
|
|
(O.info (updateParser) (O.progDesc "Update one package"))
|
2022-07-07 18:01:40 +03:00
|
|
|
<> O.command
|
|
|
|
"update-batch"
|
2022-07-28 06:24:10 +03:00
|
|
|
(O.info (updateBatchParser) (O.progDesc "Update one package in batch mode."))
|
2020-01-20 02:13:28 +03:00
|
|
|
<> O.command
|
|
|
|
"delete-done"
|
|
|
|
( O.info
|
2020-06-13 07:34:18 +03:00
|
|
|
deleteDoneParser
|
2020-01-20 02:13:28 +03:00
|
|
|
(O.progDesc "Deletes branches from PRs that were merged or closed")
|
|
|
|
)
|
|
|
|
<> O.command
|
|
|
|
"version"
|
|
|
|
( O.info
|
|
|
|
(pure Version)
|
|
|
|
( O.progDesc
|
|
|
|
"Displays version information for nixpkgs-update and dependencies"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
<> O.command
|
|
|
|
"update-vulnerability-db"
|
|
|
|
( O.info
|
|
|
|
(pure UpdateVulnDB)
|
|
|
|
(O.progDesc "Updates the vulnerability database")
|
|
|
|
)
|
|
|
|
<> O.command
|
|
|
|
"check-vulnerable"
|
|
|
|
(O.info checkVulnerable (O.progDesc "checks if something is vulnerable"))
|
|
|
|
<> O.command
|
|
|
|
"check-all-vulnerable"
|
|
|
|
( O.info
|
|
|
|
(pure CheckAllVulnerable)
|
|
|
|
(O.progDesc "checks all packages to update for vulnerabilities")
|
|
|
|
)
|
|
|
|
<> O.command
|
|
|
|
"source-github"
|
|
|
|
(O.info (pure SourceGithub) (O.progDesc "looks for updates on GitHub"))
|
|
|
|
<> O.command
|
|
|
|
"fetch-repology"
|
|
|
|
(O.info (pure FetchRepology) (O.progDesc "fetches update from Repology and prints them to stdout"))
|
|
|
|
)
|
2019-10-07 00:16:35 +03:00
|
|
|
|
|
|
|
checkVulnerable :: O.Parser Command
|
|
|
|
checkVulnerable =
|
2024-04-03 01:01:16 +03:00
|
|
|
CheckVulnerable
|
|
|
|
<$> O.strArgument (O.metavar "PRODUCT_ID")
|
2020-01-20 02:13:28 +03:00
|
|
|
<*> O.strArgument (O.metavar "OLD_VERSION")
|
|
|
|
<*> O.strArgument (O.metavar "NEW_VERSION")
|
2019-09-08 02:48:10 +03:00
|
|
|
|
|
|
|
programInfo :: O.ParserInfo Command
|
2018-04-06 18:17:22 +03:00
|
|
|
programInfo =
|
2019-09-08 02:48:10 +03:00
|
|
|
O.info
|
|
|
|
(commandParser <**> O.helper)
|
2020-01-20 02:13:28 +03:00
|
|
|
( O.fullDesc
|
|
|
|
<> O.progDesc "Update packages in the Nixpkgs repository"
|
|
|
|
<> O.header "nixpkgs-update"
|
|
|
|
)
|
2018-03-31 06:07:46 +03:00
|
|
|
|
|
|
|
main :: IO ()
|
2018-07-11 05:30:34 +03:00
|
|
|
main = do
|
2020-01-13 18:52:39 +03:00
|
|
|
hSetBuffering stdout LineBuffering
|
|
|
|
hSetBuffering stderr LineBuffering
|
2019-09-08 02:48:10 +03:00
|
|
|
command <- O.execParser programInfo
|
2020-06-07 05:11:23 +03:00
|
|
|
ghUser <- getGithubUser
|
2023-03-03 07:30:36 +03:00
|
|
|
token <- fromMaybe "" <$> getGithubToken
|
2020-06-07 05:11:23 +03:00
|
|
|
P.setEnv "GITHUB_TOKEN" (T.unpack token) True
|
2020-10-11 22:35:18 +03:00
|
|
|
P.setEnv "GITHUB_API_TOKEN" (T.unpack token) True
|
2020-06-07 05:11:23 +03:00
|
|
|
P.setEnv "PAGER" "" True
|
2019-09-08 02:48:10 +03:00
|
|
|
case command of
|
2020-06-13 07:34:18 +03:00
|
|
|
DeleteDone delete -> do
|
2023-03-03 07:30:36 +03:00
|
|
|
setupNixpkgs $ GH.untagName ghUser
|
2020-06-13 07:34:18 +03:00
|
|
|
deleteDone delete token ghUser
|
2021-11-13 22:58:24 +03:00
|
|
|
Update UpdateOptions {pr, cve, nixpkgsReview, outpaths, attrpathOpt} update -> do
|
2023-03-03 07:30:36 +03:00
|
|
|
setupNixpkgs $ GH.untagName ghUser
|
2021-11-13 22:58:24 +03:00
|
|
|
updatePackage (Options pr False ghUser token cve nixpkgsReview outpaths attrpathOpt) update
|
2022-07-07 18:01:40 +03:00
|
|
|
UpdateBatch UpdateOptions {pr, cve, nixpkgsReview, outpaths, attrpathOpt} update -> do
|
2023-03-03 07:30:36 +03:00
|
|
|
setupNixpkgs $ GH.untagName ghUser
|
2022-07-07 18:01:40 +03:00
|
|
|
updatePackage (Options pr True ghUser token cve nixpkgsReview outpaths attrpathOpt) update
|
2019-09-08 02:48:10 +03:00
|
|
|
Version -> do
|
2019-09-26 16:56:49 +03:00
|
|
|
v <- runExceptT Nix.version
|
2019-09-08 02:48:10 +03:00
|
|
|
case v of
|
|
|
|
Left t -> T.putStrLn ("error:" <> t)
|
|
|
|
Right t -> T.putStrLn t
|
2019-10-01 13:49:24 +03:00
|
|
|
UpdateVulnDB -> withVulnDB $ \_conn -> pure ()
|
2019-10-07 02:17:08 +03:00
|
|
|
CheckAllVulnerable -> do
|
2023-03-03 07:30:36 +03:00
|
|
|
setupNixpkgs $ GH.untagName ghUser
|
2019-10-07 02:17:08 +03:00
|
|
|
updates <- T.readFile "packages-to-update.txt"
|
2020-04-07 06:39:27 +03:00
|
|
|
cveAll undefined updates
|
2019-10-07 02:17:08 +03:00
|
|
|
CheckVulnerable productID oldVersion newVersion -> do
|
2023-03-03 07:30:36 +03:00
|
|
|
setupNixpkgs $ GH.untagName ghUser
|
2019-10-07 02:17:08 +03:00
|
|
|
report <-
|
|
|
|
cveReport
|
2021-08-27 22:15:26 +03:00
|
|
|
(UpdateEnv productID oldVersion newVersion Nothing (Options False False ghUser token False False False False))
|
2019-10-07 02:17:08 +03:00
|
|
|
T.putStrLn report
|
2019-10-13 00:37:34 +03:00
|
|
|
SourceGithub -> do
|
|
|
|
updates <- T.readFile "packages-to-update.txt"
|
2023-03-03 07:30:36 +03:00
|
|
|
setupNixpkgs $ GH.untagName ghUser
|
2021-08-27 22:15:26 +03:00
|
|
|
sourceGithubAll (Options False False ghUser token False False False False) updates
|
2020-01-20 02:13:28 +03:00
|
|
|
FetchRepology -> Repology.fetch
|