From ad8c0ce8b4e79287f42f65fff1c27850a926df85 Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Tue, 26 May 2020 08:41:02 +0000 Subject: [PATCH] Return sorted PatchId list from the get*Patch functions --- src/Vervis/Handler/Patch.hs | 19 +++++++------------ src/Vervis/Patch.hs | 19 +++++++++++++------ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/Vervis/Handler/Patch.hs b/src/Vervis/Handler/Patch.hs index 3e26b8b..719283a 100644 --- a/src/Vervis/Handler/Patch.hs +++ b/src/Vervis/Handler/Patch.hs @@ -100,9 +100,9 @@ getSharerPatchesR = getSharerPatchR :: ShrIdent -> KeyHashid TicketAuthorLocal -> Handler TypedContent getSharerPatchR shr talkhid = do - (ticket, repo, massignee, ptids') <- runDB $ do - (_, _, Entity tid t, tp) <- getSharerPatch404 shr talkhid - (,,,) t + (ticket, ptids, repo, massignee) <- runDB $ do + (_, _, Entity tid t, tp, ptids) <- getSharerPatch404 shr talkhid + (,,,) t ptids <$> bitraverse (\ (_, Entity _ trl) -> do r <- getJust $ ticketRepoLocalRepo trl @@ -125,11 +125,6 @@ getSharerPatchR shr talkhid = do p <- getJust pidAssignee getJust $ personIdent p ) - <*> selectKeysList [PatchTicket ==. tid] [Desc PatchId] - let ptids = - case NE.nonEmpty ptids' of - Nothing -> error "No patches found!" - Just ne -> ne hLocal <- getsYesod siteInstanceHost encodeRouteLocal <- getEncodeRouteLocal encodeRouteHome <- getEncodeRouteHome @@ -197,7 +192,7 @@ getSharerPatchDiscussionR :: ShrIdent -> KeyHashid TicketAuthorLocal -> Handler TypedContent getSharerPatchDiscussionR shr talkhid = getRepliesCollection (SharerPatchDiscussionR shr talkhid) $ do - (_, Entity _ lt, _, _) <- getSharerPatch404 shr talkhid + (_, Entity _ lt, _, _, _) <- getSharerPatch404 shr talkhid return $ localTicketDiscuss lt getSharerPatchDeps @@ -210,7 +205,7 @@ getSharerPatchDeps forward shr talkhid = if forward then SharerPatchDepsR else SharerPatchReverseDepsR in route shr talkhid getTicketId404 = do - (_, _, Entity tid _, _) <- getSharerPatch404 shr talkhid + (_, _, Entity tid _, _, _) <- getSharerPatch404 shr talkhid return tid getSharerPatchDepsR @@ -227,7 +222,7 @@ getSharerPatchFollowersR shr talkhid = getFollowersCollection here getFsid where here = SharerPatchFollowersR shr talkhid getFsid = do - (_, Entity _ lt, _, _) <- getSharerPatch404 shr talkhid + (_, Entity _ lt, _, _, _) <- getSharerPatch404 shr talkhid return $ localTicketFollowers lt getSharerPatchEventsR @@ -245,7 +240,7 @@ getSharerPatchVersionR -> Handler TypedContent getSharerPatchVersionR shr talkhid ptkhid = do (vcs, patch) <- runDB $ do - (_, _, Entity tid _, repo) <- getSharerPatch404 shr talkhid + (_, _, Entity tid _, repo, _) <- getSharerPatch404 shr talkhid (,) <$> case repo of Left (_, Entity _ trl) -> repoVcs <$> getJust (ticketRepoLocalRepo trl) diff --git a/src/Vervis/Patch.hs b/src/Vervis/Patch.hs index 5cc8bd5..cdb0703 100644 --- a/src/Vervis/Patch.hs +++ b/src/Vervis/Patch.hs @@ -24,6 +24,7 @@ where import Control.Monad import Control.Monad.Trans.Class import Control.Monad.Trans.Maybe +import Data.List.NonEmpty (NonEmpty, nonEmpty) import Data.Maybe import Data.Traversable import Database.Persist @@ -53,6 +54,7 @@ getSharerPatch ( Entity TicketProjectRemote , Maybe (Entity TicketProjectRemoteAccept) ) + , NonEmpty PatchId ) ) getSharerPatch shr talid = runMaybeT $ do @@ -65,8 +67,9 @@ getSharerPatch shr talid = runMaybeT $ do lt <- lift $ getJust ltid let tid = localTicketTicket lt t <- lift $ getJust tid - npatches <- lift $ count [PatchTicket ==. tid] - guard $ npatches >= 1 + ptids <- + MaybeT $ + nonEmpty <$> selectKeysList [PatchTicket ==. tid] [Desc PatchId] repo <- requireEitherAlt (do mtcl <- lift $ getBy $ UniqueTicketContextLocal tid @@ -85,7 +88,7 @@ getSharerPatch shr talid = runMaybeT $ do ) "MR doesn't have context" "MR has both local and remote context" - return (Entity talid tal, Entity ltid lt, Entity tid t, repo) + return (Entity talid tal, Entity ltid lt, Entity tid t, repo, ptids) getSharerPatch404 :: ShrIdent @@ -101,6 +104,7 @@ getSharerPatch404 ( Entity TicketProjectRemote , Maybe (Entity TicketProjectRemoteAccept) ) + , NonEmpty PatchId ) getSharerPatch404 shr talkhid = do talid <- decodeKeyHashid404 talkhid @@ -124,6 +128,7 @@ getRepoPatch , Either (Entity TicketAuthorLocal, Entity TicketUnderProject) (Entity TicketAuthorRemote) + , NonEmpty PatchId ) ) getRepoPatch shr rp ltid = runMaybeT $ do @@ -135,8 +140,9 @@ getRepoPatch shr rp ltid = runMaybeT $ do etcl@(Entity tclid _) <- MaybeT $ getBy $ UniqueTicketContextLocal tid etrl@(Entity _ trl) <- MaybeT $ getBy $ UniqueTicketRepoLocal tclid guard $ ticketRepoLocalRepo trl == rid - npatches <- lift $ count [PatchTicket ==. tid] - guard $ npatches >= 1 + ptids <- + MaybeT $ + nonEmpty <$> selectKeysList [PatchTicket ==. tid] [Desc PatchId] author <- requireEitherAlt (do mtal <- lift $ getBy $ UniqueTicketAuthorLocal ltid @@ -150,7 +156,7 @@ getRepoPatch shr rp ltid = runMaybeT $ do (lift $ getBy $ UniqueTicketAuthorRemote tclid) "MR doesn't have author" "MR has both local and remote author" - return (es, er, Entity tid t, Entity ltid lt, etcl, etrl, author) + return (es, er, Entity tid t, Entity ltid lt, etcl, etrl, author, ptids) getRepoPatch404 :: ShrIdent @@ -166,6 +172,7 @@ getRepoPatch404 , Either (Entity TicketAuthorLocal, Entity TicketUnderProject) (Entity TicketAuthorRemote) + , NonEmpty PatchId ) getRepoPatch404 shr rp ltkhid = do ltid <- decodeKeyHashid404 ltkhid