1
0
Fork 0
mirror of https://code.sup39.dev/repos/Wqawg synced 2025-01-09 13:46:47 +09:00
vervis/src/Vervis/Handler/Repo.hs

312 lines
11 KiB
Haskell
Raw Normal View History

2016-02-27 14:41:36 +09:00
{- This file is part of Vervis.
-
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
-
- Copying is an act of love. Please copy, reuse and share.
-
- The author(s) have dedicated all copyright and related and neighboring
- rights to this software to the public domain worldwide. This software is
- distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along
- with this software. If not, see
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
module Vervis.Handler.Repo
( getReposR
, postReposR
, getRepoNewR
, getRepoR
2016-05-14 04:23:56 +09:00
, deleteRepoR
, postRepoR
, getRepoSourceR
, getRepoHeadChangesR
, getRepoChangesR
2016-05-29 23:13:25 +09:00
, getRepoDevsR
, postRepoDevsR
, getRepoDevNewR
, getRepoDevR
, deleteRepoDevR
, postRepoDevR
2016-05-13 19:58:42 +09:00
, getDarcsDownloadR
2016-02-27 14:41:36 +09:00
)
where
import Prelude
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Logger (logWarn)
import Data.Git.Graph
import Data.Git.Harder
import Data.Git.Named (RefName (..))
2016-03-03 17:15:54 +09:00
import Data.Git.Ref (toHex)
import Data.Git.Repository
import Data.Git.Storage (withRepo)
import Data.Git.Storage.Object (Object (..))
import Data.Git.Types (Blob (..), Commit (..), Person (..), entName)
import Data.Graph.Inductive.Graph (noNodes)
import Data.Graph.Inductive.Query.Topsort
import Data.List (inits)
import Data.Text (Text, unpack)
2016-03-03 17:15:54 +09:00
import Data.Text.Encoding (decodeUtf8With)
import Data.Text.Encoding.Error (lenientDecode)
2016-05-14 04:23:56 +09:00
import Database.Esqueleto hiding (delete, (%))
import Database.Persist (delete)
2016-03-03 17:15:54 +09:00
import Data.Hourglass (timeConvert)
2016-05-14 04:23:56 +09:00
import Formatting (sformat, stext, (%))
import System.Directory
2016-03-03 17:15:54 +09:00
import System.Hourglass (dateCurrent)
import Text.Blaze.Html (Html)
import Yesod.Auth (requireAuth)
import Yesod.Core (defaultLayout, setMessage)
import Yesod.Core.Handler (lookupPostParam, redirect, notFound)
import Yesod.Form.Functions (runFormPost)
import Yesod.Form.Types (FormResult (..))
import Yesod.Persist.Core (runDB, getBy404)
2016-03-03 17:15:54 +09:00
import qualified Data.CaseInsensitive as CI (foldedCase)
import qualified Data.DList as D
import qualified Data.Set as S (member)
import qualified Data.Text.Lazy.Encoding as L (decodeUtf8With)
2016-03-03 17:15:54 +09:00
import Data.ByteString.Char8.Local (takeLine)
import Data.Git.Local
import Text.FilePath.Local (breakExt)
2016-02-27 14:41:36 +09:00
import Vervis.Form.Repo
2016-03-03 17:15:54 +09:00
import Vervis.Foundation
import Vervis.Handler.Repo.Darcs
import Vervis.Handler.Repo.Git
2016-03-03 17:15:54 +09:00
import Vervis.Path
import Vervis.MediaType (chooseMediaType)
2016-03-03 17:15:54 +09:00
import Vervis.Model
import Vervis.Model.Ident
import Vervis.Model.Repo
import Vervis.Paginate
import Vervis.Readme
import Vervis.Render
2016-03-03 17:15:54 +09:00
import Vervis.Settings
import Vervis.SourceTree
2016-04-12 23:44:43 +09:00
import Vervis.Style
import Vervis.Widget.Repo
import Vervis.Widget.Sharer
2016-02-27 14:41:36 +09:00
2016-05-08 23:28:03 +09:00
import qualified Darcs.Local.Repository as D (createRepo)
import qualified Data.ByteString.Lazy as BL (ByteString)
import qualified Data.Git.Local as G (createRepo)
2016-05-08 23:28:03 +09:00
import qualified Vervis.Darcs as D (readSourceView, readChangesView)
import qualified Vervis.Formatting as F
import qualified Vervis.Git as G (readSourceView, readChangesView, listRefs)
getReposR :: ShrIdent -> Handler Html
getReposR user = do
repos <- runDB $ select $ from $ \ (sharer, repo) -> do
2016-02-27 14:41:36 +09:00
where_ $
sharer ^. SharerIdent ==. val user &&.
sharer ^. SharerId ==. repo ^. RepoSharer
2016-02-27 14:41:36 +09:00
orderBy [asc $ repo ^. RepoIdent]
return $ repo ^. RepoIdent
defaultLayout $(widgetFile "repo/list")
2016-02-27 14:41:36 +09:00
postReposR :: ShrIdent -> Handler Html
postReposR user = do
Entity pid person <- requireAuth
Entity sid _sharer <- runDB $ getBy404 $ UniqueSharer user
((result, widget), enctype) <- runFormPost $ newRepoForm pid sid Nothing
2016-02-27 14:41:36 +09:00
case result of
FormSuccess nrp -> do
parent <- askSharerDir user
liftIO $ do
createDirectoryIfMissing True parent
let repoName =
unpack $ CI.foldedCase $ unRpIdent $ nrpIdent nrp
case nrpVcs nrp of
VCSDarcs -> D.createRepo parent repoName
VCSGit -> G.createRepo parent repoName
runDB $ do
let repo = Repo
{ repoIdent = nrpIdent nrp
, repoSharer = sid
, repoVcs = nrpVcs nrp
, repoProject = nrpProj nrp
, repoDesc = nrpDesc nrp
, repoMainBranch = "master"
}
rid <- insert repo
let collab = RepoCollab
{ repoCollabRepo = rid
, repoCollabPerson = pid
, repoCollabRole = nrpRole nrp
}
insert_ collab
setMessage "Repo added."
redirect $ ReposR user
2016-02-27 14:41:36 +09:00
FormMissing -> do
setMessage "Field(s) missing"
defaultLayout $(widgetFile "repo/new")
FormFailure _l -> do
setMessage "Repo creation failed, see errors below"
defaultLayout $(widgetFile "repo/new")
2016-02-27 14:41:36 +09:00
getRepoNewR :: ShrIdent -> Handler Html
getRepoNewR user = do
Entity pid person <- requireAuth
Entity sid _sharer <- runDB $ getBy404 $ UniqueSharer user
((_result, widget), enctype) <- runFormPost $ newRepoForm pid sid Nothing
defaultLayout $(widgetFile "repo/new")
2016-02-27 14:41:36 +09:00
selectRepo :: ShrIdent -> RpIdent -> AppDB Repo
selectRepo shar repo = do
Entity sid _s <- getBy404 $ UniqueSharer shar
Entity _rid r <- getBy404 $ UniqueRepo repo sid
return r
getRepoR :: ShrIdent -> RpIdent -> Handler Html
getRepoR shar repo = do
repository <- runDB $ selectRepo shar repo
case repoVcs repository of
VCSDarcs -> getDarcsRepoSource repository shar repo []
VCSGit ->
getGitRepoSource
repository shar repo (repoMainBranch repository) []
deleteRepoR :: ShrIdent -> RpIdent -> Handler Html
2016-05-14 04:23:56 +09:00
deleteRepoR shar repo = do
runDB $ do
Entity sid _s <- getBy404 $ UniqueSharer shar
2016-05-14 04:23:56 +09:00
Entity rid _r <- getBy404 $ UniqueRepo repo sid
delete rid
path <- askRepoDir shar repo
exists <- liftIO $ doesDirectoryExist path
if exists
then liftIO $ removeDirectoryRecursive path
else
$logWarn $ sformat
( "Deleted repo " % F.sharer % "/" % F.repo
2016-05-14 04:23:56 +09:00
% " from DB but repo dir doesn't exist"
)
shar repo
setMessage "Repo deleted."
redirect HomeR
postRepoR :: ShrIdent -> RpIdent -> Handler Html
2016-05-14 04:23:56 +09:00
postRepoR shar repo = do
mmethod <- lookupPostParam "_method"
case mmethod of
Just "DELETE" -> deleteRepoR shar repo
_ -> notFound
getRepoSourceR :: ShrIdent -> RpIdent -> [Text] -> Handler Html
getRepoSourceR shar repo refdir = do
repository <- runDB $ selectRepo shar repo
case repoVcs repository of
VCSDarcs -> getDarcsRepoSource repository shar repo refdir
VCSGit -> case refdir of
[] -> notFound
(ref:dir) -> getGitRepoSource repository shar repo ref dir
getRepoHeadChangesR :: ShrIdent -> RpIdent -> Handler Html
getRepoHeadChangesR user repo = do
repository <- runDB $ selectRepo user repo
case repoVcs repository of
VCSDarcs -> getDarcsRepoHeadChanges user repo
VCSGit -> getGitRepoHeadChanges repository user repo
getRepoChangesR :: ShrIdent -> RpIdent -> Text -> Handler Html
getRepoChangesR shar repo ref = do
repository <- runDB $ selectRepo shar repo
case repoVcs repository of
VCSDarcs -> getDarcsRepoChanges shar repo ref
VCSGit -> getGitRepoChanges shar repo ref
2016-05-29 23:13:25 +09:00
getRepoDevsR :: ShrIdent -> RpIdent -> Handler Html
getRepoDevsR shr rp = do
devs <- runDB $ do
rid <- do
Entity s _ <- getBy404 $ UniqueSharer shr
Entity r _ <- getBy404 $ UniqueRepo rp s
return r
select $ from $ \ (collab, person, sharer, role) -> do
where_ $
collab ^. RepoCollabRepo ==. val rid &&.
collab ^. RepoCollabPerson ==. person ^. PersonId &&.
person ^. PersonIdent ==. sharer ^. SharerId &&.
collab ^. RepoCollabRole ==. role ^. RepoRoleId
return (sharer, role ^. RepoRoleIdent)
defaultLayout $(widgetFile "repo/collab/list")
2016-05-29 23:13:25 +09:00
postRepoDevsR :: ShrIdent -> RpIdent -> Handler Html
postRepoDevsR shr rp = do
(pid, rid) <- runDB $ do
Entity s _ <- getBy404 $ UniqueSharer shr
Entity p _ <- getBy404 $ UniquePersonIdent s
Entity r _ <- getBy404 $ UniqueRepo rp s
return (p, r)
((result, widget), enctype) <- runFormPost $ newRepoCollabForm pid rid
case result of
FormSuccess nc -> do
runDB $ do
let collab = RepoCollab
{ repoCollabRepo = rid
, repoCollabPerson = ncPerson nc
, repoCollabRole = ncRole nc
}
insert_ collab
setMessage "Collaborator added."
redirect $ RepoDevsR shr rp
FormMissing -> do
setMessage "Field(s) missing"
defaultLayout $(widgetFile "repo/collab/new")
FormFailure _l -> do
setMessage "Operation failed, see errors below"
defaultLayout $(widgetFile "repo/collab/new")
2016-05-29 23:13:25 +09:00
getRepoDevNewR :: ShrIdent -> RpIdent -> Handler Html
getRepoDevNewR shr rp = do
(pid, rid) <- runDB $ do
Entity s _ <- getBy404 $ UniqueSharer shr
Entity p _ <- getBy404 $ UniquePersonIdent s
Entity r _ <- getBy404 $ UniqueRepo rp s
return (p, r)
((_result, widget), enctype) <- runFormPost $ newRepoCollabForm pid rid
defaultLayout $(widgetFile "repo/collab/new")
2016-05-29 23:13:25 +09:00
getRepoDevR :: ShrIdent -> RpIdent -> ShrIdent -> Handler Html
getRepoDevR shr rp dev = do
rl <- runDB $ do
rid <- do
Entity s _ <- getBy404 $ UniqueSharer shr
Entity r _ <- getBy404 $ UniqueRepo rp s
return r
pid <- do
Entity s _ <- getBy404 $ UniqueSharer dev
Entity p _ <- getBy404 $ UniquePersonIdent s
return p
Entity _cid collab <- getBy404 $ UniqueRepoCollab rid pid
role <- getJust $ repoCollabRole collab
return $ repoRoleIdent role
defaultLayout $(widgetFile "repo/collab/one")
2016-05-29 23:13:25 +09:00
deleteRepoDevR :: ShrIdent -> RpIdent -> ShrIdent -> Handler Html
2016-05-30 23:32:20 +09:00
deleteRepoDevR shr rp dev = do
runDB $ do
rid <- do
Entity s _ <- getBy404 $ UniqueSharer shr
Entity r _ <- getBy404 $ UniqueRepo rp s
return r
pid <- do
Entity s _ <- getBy404 $ UniqueSharer dev
Entity p _ <- getBy404 $ UniquePersonIdent s
return p
Entity cid _collab <- getBy404 $ UniqueRepoCollab rid pid
2016-05-30 23:32:20 +09:00
delete cid
setMessage "Collaborator removed."
redirect $ RepoDevsR shr rp
2016-05-29 23:13:25 +09:00
postRepoDevR :: ShrIdent -> RpIdent -> ShrIdent -> Handler Html
2016-05-30 23:32:20 +09:00
postRepoDevR shr rp dev = do
mmethod <- lookupPostParam "_method"
case mmethod of
Just "DELETE" -> deleteRepoDevR shr rp dev
_ -> notFound