Return sorted PatchId list from the get*Patch functions

This commit is contained in:
fr33domlover 2020-05-26 08:41:02 +00:00
parent e68a659221
commit ad8c0ce8b4
2 changed files with 20 additions and 18 deletions

View file

@ -100,9 +100,9 @@ getSharerPatchesR =
getSharerPatchR getSharerPatchR
:: ShrIdent -> KeyHashid TicketAuthorLocal -> Handler TypedContent :: ShrIdent -> KeyHashid TicketAuthorLocal -> Handler TypedContent
getSharerPatchR shr talkhid = do getSharerPatchR shr talkhid = do
(ticket, repo, massignee, ptids') <- runDB $ do (ticket, ptids, repo, massignee) <- runDB $ do
(_, _, Entity tid t, tp) <- getSharerPatch404 shr talkhid (_, _, Entity tid t, tp, ptids) <- getSharerPatch404 shr talkhid
(,,,) t (,,,) t ptids
<$> bitraverse <$> bitraverse
(\ (_, Entity _ trl) -> do (\ (_, Entity _ trl) -> do
r <- getJust $ ticketRepoLocalRepo trl r <- getJust $ ticketRepoLocalRepo trl
@ -125,11 +125,6 @@ getSharerPatchR shr talkhid = do
p <- getJust pidAssignee p <- getJust pidAssignee
getJust $ personIdent p 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 hLocal <- getsYesod siteInstanceHost
encodeRouteLocal <- getEncodeRouteLocal encodeRouteLocal <- getEncodeRouteLocal
encodeRouteHome <- getEncodeRouteHome encodeRouteHome <- getEncodeRouteHome
@ -197,7 +192,7 @@ getSharerPatchDiscussionR
:: ShrIdent -> KeyHashid TicketAuthorLocal -> Handler TypedContent :: ShrIdent -> KeyHashid TicketAuthorLocal -> Handler TypedContent
getSharerPatchDiscussionR shr talkhid = getSharerPatchDiscussionR shr talkhid =
getRepliesCollection (SharerPatchDiscussionR shr talkhid) $ do getRepliesCollection (SharerPatchDiscussionR shr talkhid) $ do
(_, Entity _ lt, _, _) <- getSharerPatch404 shr talkhid (_, Entity _ lt, _, _, _) <- getSharerPatch404 shr talkhid
return $ localTicketDiscuss lt return $ localTicketDiscuss lt
getSharerPatchDeps getSharerPatchDeps
@ -210,7 +205,7 @@ getSharerPatchDeps forward shr talkhid =
if forward then SharerPatchDepsR else SharerPatchReverseDepsR if forward then SharerPatchDepsR else SharerPatchReverseDepsR
in route shr talkhid in route shr talkhid
getTicketId404 = do getTicketId404 = do
(_, _, Entity tid _, _) <- getSharerPatch404 shr talkhid (_, _, Entity tid _, _, _) <- getSharerPatch404 shr talkhid
return tid return tid
getSharerPatchDepsR getSharerPatchDepsR
@ -227,7 +222,7 @@ getSharerPatchFollowersR shr talkhid = getFollowersCollection here getFsid
where where
here = SharerPatchFollowersR shr talkhid here = SharerPatchFollowersR shr talkhid
getFsid = do getFsid = do
(_, Entity _ lt, _, _) <- getSharerPatch404 shr talkhid (_, Entity _ lt, _, _, _) <- getSharerPatch404 shr talkhid
return $ localTicketFollowers lt return $ localTicketFollowers lt
getSharerPatchEventsR getSharerPatchEventsR
@ -245,7 +240,7 @@ getSharerPatchVersionR
-> Handler TypedContent -> Handler TypedContent
getSharerPatchVersionR shr talkhid ptkhid = do getSharerPatchVersionR shr talkhid ptkhid = do
(vcs, patch) <- runDB $ do (vcs, patch) <- runDB $ do
(_, _, Entity tid _, repo) <- getSharerPatch404 shr talkhid (_, _, Entity tid _, repo, _) <- getSharerPatch404 shr talkhid
(,) <$> case repo of (,) <$> case repo of
Left (_, Entity _ trl) -> Left (_, Entity _ trl) ->
repoVcs <$> getJust (ticketRepoLocalRepo trl) repoVcs <$> getJust (ticketRepoLocalRepo trl)

View file

@ -24,6 +24,7 @@ where
import Control.Monad import Control.Monad
import Control.Monad.Trans.Class import Control.Monad.Trans.Class
import Control.Monad.Trans.Maybe import Control.Monad.Trans.Maybe
import Data.List.NonEmpty (NonEmpty, nonEmpty)
import Data.Maybe import Data.Maybe
import Data.Traversable import Data.Traversable
import Database.Persist import Database.Persist
@ -53,6 +54,7 @@ getSharerPatch
( Entity TicketProjectRemote ( Entity TicketProjectRemote
, Maybe (Entity TicketProjectRemoteAccept) , Maybe (Entity TicketProjectRemoteAccept)
) )
, NonEmpty PatchId
) )
) )
getSharerPatch shr talid = runMaybeT $ do getSharerPatch shr talid = runMaybeT $ do
@ -65,8 +67,9 @@ getSharerPatch shr talid = runMaybeT $ do
lt <- lift $ getJust ltid lt <- lift $ getJust ltid
let tid = localTicketTicket lt let tid = localTicketTicket lt
t <- lift $ getJust tid t <- lift $ getJust tid
npatches <- lift $ count [PatchTicket ==. tid] ptids <-
guard $ npatches >= 1 MaybeT $
nonEmpty <$> selectKeysList [PatchTicket ==. tid] [Desc PatchId]
repo <- repo <-
requireEitherAlt requireEitherAlt
(do mtcl <- lift $ getBy $ UniqueTicketContextLocal tid (do mtcl <- lift $ getBy $ UniqueTicketContextLocal tid
@ -85,7 +88,7 @@ getSharerPatch shr talid = runMaybeT $ do
) )
"MR doesn't have context" "MR doesn't have context"
"MR has both local and remote 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 getSharerPatch404
:: ShrIdent :: ShrIdent
@ -101,6 +104,7 @@ getSharerPatch404
( Entity TicketProjectRemote ( Entity TicketProjectRemote
, Maybe (Entity TicketProjectRemoteAccept) , Maybe (Entity TicketProjectRemoteAccept)
) )
, NonEmpty PatchId
) )
getSharerPatch404 shr talkhid = do getSharerPatch404 shr talkhid = do
talid <- decodeKeyHashid404 talkhid talid <- decodeKeyHashid404 talkhid
@ -124,6 +128,7 @@ getRepoPatch
, Either , Either
(Entity TicketAuthorLocal, Entity TicketUnderProject) (Entity TicketAuthorLocal, Entity TicketUnderProject)
(Entity TicketAuthorRemote) (Entity TicketAuthorRemote)
, NonEmpty PatchId
) )
) )
getRepoPatch shr rp ltid = runMaybeT $ do getRepoPatch shr rp ltid = runMaybeT $ do
@ -135,8 +140,9 @@ getRepoPatch shr rp ltid = runMaybeT $ do
etcl@(Entity tclid _) <- MaybeT $ getBy $ UniqueTicketContextLocal tid etcl@(Entity tclid _) <- MaybeT $ getBy $ UniqueTicketContextLocal tid
etrl@(Entity _ trl) <- MaybeT $ getBy $ UniqueTicketRepoLocal tclid etrl@(Entity _ trl) <- MaybeT $ getBy $ UniqueTicketRepoLocal tclid
guard $ ticketRepoLocalRepo trl == rid guard $ ticketRepoLocalRepo trl == rid
npatches <- lift $ count [PatchTicket ==. tid] ptids <-
guard $ npatches >= 1 MaybeT $
nonEmpty <$> selectKeysList [PatchTicket ==. tid] [Desc PatchId]
author <- author <-
requireEitherAlt requireEitherAlt
(do mtal <- lift $ getBy $ UniqueTicketAuthorLocal ltid (do mtal <- lift $ getBy $ UniqueTicketAuthorLocal ltid
@ -150,7 +156,7 @@ getRepoPatch shr rp ltid = runMaybeT $ do
(lift $ getBy $ UniqueTicketAuthorRemote tclid) (lift $ getBy $ UniqueTicketAuthorRemote tclid)
"MR doesn't have author" "MR doesn't have author"
"MR has both local and remote 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 getRepoPatch404
:: ShrIdent :: ShrIdent
@ -166,6 +172,7 @@ getRepoPatch404
, Either , Either
(Entity TicketAuthorLocal, Entity TicketUnderProject) (Entity TicketAuthorLocal, Entity TicketUnderProject)
(Entity TicketAuthorRemote) (Entity TicketAuthorRemote)
, NonEmpty PatchId
) )
getRepoPatch404 shr rp ltkhid = do getRepoPatch404 shr rp ltkhid = do
ltid <- decodeKeyHashid404 ltkhid ltid <- decodeKeyHashid404 ltkhid