88{-# LANGUAGE PolyKinds #-}
99{-# LANGUAGE RecordWildCards #-}
1010{-# LANGUAGE ScopedTypeVariables #-}
11- {-# LANGUAGE TupleSections #-}
1211
1312module Branches
1413 ( showBranchesForGroup ,
@@ -22,7 +21,7 @@ import qualified Data.Text as T (intercalate)
2221import Data.Time hiding (getCurrentTime )
2322import Effects
2423import Gitlab.Branch
25- import Gitlab.Client.MTL ( UpdateError )
24+ import Gitlab.Client.Queue. MTL
2625import Gitlab.Project
2726import Relude
2827
@@ -36,28 +35,31 @@ showBranchesForGroup = do
3635 write " ✔ - the branch is merged"
3736 write " ✗ - the branch is stale (older than 90 days)"
3837 write " ⚬ - the branch is protected"
39- getProjectsForGroup SkipArchivedProjects >>= \ case
38+ results <- processProjectsForGroupQueued SkipArchivedProjects (fmap Right . processProject)
39+ case results of
4040 Left err -> write $ show err
41- Right projects -> do
42- results <- traverse (getBranchesForProject >=> printResult) projects
43- writeSummary results
44-
45- getBranchesForProject :: Project -> App (Project , Either UpdateError [Branch ])
46- getBranchesForProject p = (p,) <$> getBranches (projectId p)
47-
48- printResult :: (Project , Either UpdateError [Branch ]) -> App (Project , Either UpdateError [Branch ])
49- printResult input@ (project, Left err) = do
50- write $ " === " <> show (projectName project)
51- write $ " something went wrong: " <> show err
52- pure input
53- printResult input@ (project, Right branches) = do
41+ Right res -> do
42+ writeSummary res
43+
44+ processProject :: Project -> App (ProcessResult (Project , [Branch ]))
45+ processProject project = do
46+ getBranches (projectId project) >>= \ case
47+ Left err -> pure $ PrintLines $ " === " <> show (projectName project) :| [" something went wrong: " <> show err]
48+ Right branches -> do
49+ maybeTxts <- printResult (project, branches)
50+ pure
51+ $ case maybeTxts of
52+ Nothing -> Result (project, branches)
53+ Just txts -> PrintLinesWithResult txts (project, branches)
54+
55+ printResult :: (Project , [Branch ]) -> App (Maybe (NonEmpty Text ))
56+ printResult (project, branches) = do
5457 let branchesWithoutDefaultBranch = sortOn (commitCommittedDate . branchCommit) $ filter (not . branchDefault) branches
55- unless (null branchesWithoutDefaultBranch) $ do
56- write " "
57- write $ formatWith [bold] (" === " <> show (projectName project))
58- now <- getCurrentTime
59- traverse_ (\ b -> write $ " " <> prettyPrintBranch now b) branchesWithoutDefaultBranch
60- pure input
58+ now <- getCurrentTime
59+ pure
60+ $ if null branchesWithoutDefaultBranch
61+ then Nothing
62+ else Just $ " " :| (formatWith [bold] (" === " <> show (projectName project)) : ((\ b -> " " <> prettyPrintBranch now b) <$> branchesWithoutDefaultBranch))
6163
6264prettyPrintBranch :: UTCTime -> Branch -> Text
6365prettyPrintBranch now Branch {.. } =
@@ -94,13 +96,13 @@ type MergedBranchesCount = Sum Int
9496
9597type Summary = (ProjectCount , BranchesCount , StaleBranchesCount , MergedBranchesCount )
9698
97- writeSummary :: [(Project , Either UpdateError [Branch ])] -> App ()
99+ writeSummary :: [(Project , [Branch ])] -> App ()
98100writeSummary results = do
99101 now <- getCurrentTime
100102 write " "
101103 write . showSummary $ summary now results
102104
103- summary :: UTCTime -> [(Project , Either UpdateError [Branch ])] -> Summary
105+ summary :: UTCTime -> [(Project , [Branch ])] -> Summary
104106summary now = foldMap (count now)
105107
106108showSummary :: Summary -> Text
@@ -121,9 +123,8 @@ showSummary (projects, branches, stale, merged) =
121123 isAre (Sum 1 ) = " is"
122124 isAre _ = " are"
123125
124- count :: UTCTime -> (Project , Either UpdateError [Branch ]) -> Summary
125- count _ (_, Left _) = mempty
126- count now (_, Right branches) = (hasBranches, notDefaultCount, stale, merged)
126+ count :: UTCTime -> (Project , [Branch ]) -> Summary
127+ count now (_, branches) = (hasBranches, notDefaultCount, stale, merged)
127128 where
128129 notDefault = filter (not . branchDefault) branches
129130 hasBranches = Sum $ if notDefaultCount /= 0 then 1 else 0
0 commit comments