gonito/Handler/MakePublic.hs

54 lines
1.9 KiB
Haskell
Raw Normal View History

2016-02-15 11:43:47 +01:00
module Handler.MakePublic where
import Import
import Handler.Shared
import PersistSHA1
import Data.Text as T
2018-06-05 08:22:51 +02:00
import Handler.Runner
2016-02-15 11:43:47 +01:00
getMakePublicR :: SubmissionId -> Handler TypedContent
getMakePublicR submissionId = runViewProgress $ doMakePublic submissionId
doMakePublic :: SubmissionId -> Channel -> Handler ()
doMakePublic submissionId chan = do
isOwner <- checkWhetherUserRepo submissionId
if not isOwner
then
err chan "Only the submitter can make a submission public!"
else do
msg chan "Making the submission public..."
runDB $ update submissionId [SubmissionIsPublic =. True]
submission <- runDB $ get404 submissionId
challenge <- runDB $ get404 $ submissionChallenge submission
2018-06-06 13:08:38 +02:00
repo <- runDB $ get404 $ challengePublicRepo challenge
2016-02-15 11:43:47 +01:00
let submissionRepoId = submissionRepo submission
submissionRepoDir <- getRepoDir submissionRepoId
2018-06-06 13:08:38 +02:00
app <- getYesod
let scheme = appRepoScheme $ appSettings app
let targetRepoUrl = getPublicSubmissionUrl scheme (Just repo) $ challengeName challenge
let targetBranchName = getPublicSubmissionBranch submissionId
msg chan $ "Start pushing from " ++ (T.pack submissionRepoDir) ++ " to repo " ++ targetRepoUrl ++ ", branch " ++ targetBranchName ++ " ..."
2016-02-15 11:43:47 +01:00
let commit = submissionCommit submission
pushRepo submissionRepoDir commit (T.unpack $ targetRepoUrl) (T.unpack $ targetBranchName) chan
2016-02-15 11:43:47 +01:00
return ()
pushRepo :: String -> SHA1 -> String -> String -> Channel -> Handler ()
pushRepo repoDir commit targetRepoUrl targetBranchName chan = do
(_, _) <- runProgram (Just repoDir) gitPath [
2016-02-15 11:43:47 +01:00
"push",
targetRepoUrl,
(T.unpack $ fromSHA1ToText commit) ++ ":refs/heads/" ++ targetBranchName] chan
return ()
checkWhetherUserRepo :: SubmissionId -> Handler Bool
checkWhetherUserRepo submissionId = do
submission <- runDB $ get404 submissionId
userId <- requireAuthId
return $ userId == submissionSubmitter submission