From 967da232d384cb1bf814fa5417914c3548e22c93 Mon Sep 17 00:00:00 2001 From: martoon Date: Fri, 26 Jun 2020 10:48:12 +0300 Subject: [PATCH 1/2] Disable progress bar in CI automatically Problem: we have two major use cases: 1. Using xrefcheck in CI, in this case it is annoying for devops to always pass mostly always reasonable `--no-progress` option. 2. Using it locally, here we want progress bar to be enabled by default so that user could run `xrefcheck` without any options most of the time. Solution: Detect whether are we running in CI and use the respective default for whether progress bar should be displayed. Detecting whether we are within CI is possible using `CI` env variable, at least Github actions, Gitlab CI and Buildkite pass this env. `--no-progress` option still can be passed if user wants to suppress progress bar in local run. The opposite `--progress` seems to also make sense, because in case if high timeouts are set, user probably wants to track progress somehow to make sure that CI didn't hang. So we add the latter, and these two options can be used to override the default behaviour defined by `CI` env var. --- CHANGES.md | 4 ++++ exec/Main.hs | 8 ++++++-- src/Xrefcheck/CLI.hs | 17 ++++++++++++----- src/Xrefcheck/System.hs | 11 +++++++++++ 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 19da2d65..f5f54039 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,10 @@ Unreleased ========== +* [#44](https://github.com/serokell/xrefcheck/pull/44) + + Decide whether to show progress bar by default depending on `CI` env variable. + + Added `--progress` option. + 0.1.1.2 ======= diff --git a/exec/Main.hs b/exec/Main.hs index a07c4488..e2fc90aa 100644 --- a/exec/Main.hs +++ b/exec/Main.hs @@ -16,6 +16,7 @@ import Xrefcheck.Config import Xrefcheck.Progress import Xrefcheck.Scan import Xrefcheck.Scanners +import Xrefcheck.System import Xrefcheck.Verify formats :: FormatsSupport @@ -39,14 +40,17 @@ defaultAction Options{..} = do Just configPath -> do readConfig configPath - repoInfo <- allowRewrite oShowProgressBar $ \rw -> do + withinCI <- askWithinCI + let showProgressBar = oShowProgressBar ?: not withinCI + + repoInfo <- allowRewrite showProgressBar $ \rw -> do let fullConfig = addTraversalOptions (cTraversal config) oTraversalOptions gatherRepoInfo rw formats fullConfig root when oVerbose $ fmtLn $ "=== Repository data ===\n\n" <> indentF 2 (build repoInfo) - verifyRes <- allowRewrite oShowProgressBar $ \rw -> + verifyRes <- allowRewrite showProgressBar $ \rw -> verifyRepo rw (cVerification config) oMode root repoInfo case verifyErrors verifyRes of Nothing -> diff --git a/src/Xrefcheck/CLI.hs b/src/Xrefcheck/CLI.hs index f5d402c7..d2a37999 100644 --- a/src/Xrefcheck/CLI.hs +++ b/src/Xrefcheck/CLI.hs @@ -18,7 +18,7 @@ module Xrefcheck.CLI ) where import Data.Version (showVersion) -import Options.Applicative (Parser, ReadM, command, eitherReader, execParser, fullDesc, help, +import Options.Applicative (Parser, ReadM, command, eitherReader, execParser, flag', fullDesc, help, helper, hsubparser, info, infoOption, long, metavar, option, progDesc, short, strOption, switch, value) import Paths_xrefcheck (version) @@ -50,7 +50,7 @@ data Options = Options , oRoot :: FilePath , oMode :: VerifyMode , oVerbose :: Bool - , oShowProgressBar :: Bool + , oShowProgressBar :: Maybe Bool , oTraversalOptions :: TraversalOptions } @@ -99,9 +99,16 @@ optionsParser = do short 'v' <> long "verbose" <> help "Report repository scan and verification details." - oShowProgressBar <- fmap not . switch $ - long "no-progress" <> - help "Do not display progress bar during verification." + oShowProgressBar <- asum + [ flag' (Just True) $ + long "progress" <> + help "Display progress bar during verification. \ + \This is enabled by default unless `CI` env var is set to true." + , flag' (Just False) $ + long "no-progress" <> + help "Do not display progress bar during verification." + , pure Nothing + ] oTraversalOptions <- traversalOptionsParser return Options{..} diff --git a/src/Xrefcheck/System.hs b/src/Xrefcheck/System.hs index 5a82ca67..fb9cfc8c 100644 --- a/src/Xrefcheck/System.hs +++ b/src/Xrefcheck/System.hs @@ -5,13 +5,16 @@ module Xrefcheck.System ( readingSystem + , askWithinCI , RelGlobPattern (..) , bindGlobPattern ) where import Data.Aeson (FromJSON (..), withText) +import qualified Data.Char as C import GHC.IO.Unsafe (unsafePerformIO) import System.Directory (canonicalizePath) +import System.Environment (lookupEnv) import System.FilePath (()) import qualified System.FilePath.Glob as Glob @@ -20,6 +23,14 @@ import qualified System.FilePath.Glob as Glob readingSystem :: IO a -> a readingSystem = unsafePerformIO +-- | Heuristics to check whether we are running within CI. +-- Check the respective env variable which is usually set in all CIs. +askWithinCI :: IO Bool +askWithinCI = lookupEnv "CI" <&> \case + Just "1" -> True + Just (map C.toLower -> "true") -> True + _ -> False + -- | Glob pattern relative to repository root. newtype RelGlobPattern = RelGlobPattern FilePath From 0d6e61905f68bc708962c1f50bbde3a5edb51dfe Mon Sep 17 00:00:00 2001 From: martoon Date: Fri, 26 Jun 2020 15:41:52 +0300 Subject: [PATCH 2/2] Make a release --- CHANGES.md | 3 +++ package.yaml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index f5f54039..4febe146 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,9 @@ Unreleased ========== +0.1.2 +======= + * [#44](https://github.com/serokell/xrefcheck/pull/44) + Decide whether to show progress bar by default depending on `CI` env variable. + Added `--progress` option. diff --git a/package.yaml b/package.yaml index 3a56a515..d46dbc24 100644 --- a/package.yaml +++ b/package.yaml @@ -5,7 +5,7 @@ spec-version: 0.31.0 name: xrefcheck -version: 0.1.1.2 +version: 0.1.2 github: serokell/xrefcheck license: MPL-2.0 license-file: LICENSE