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
|
2021-09-15 13:10:44 +02:00
|
|
|
import Handler.JWT
|
|
|
|
|
|
|
|
getMakePublicJsonR :: SubmissionId -> Handler Value
|
|
|
|
getMakePublicJsonR submissionId = do
|
|
|
|
Entity userId _ <- requireAuthPossiblyByToken
|
|
|
|
runViewProgressAsynchronously $ doMakePublic userId submissionId
|
2018-06-05 08:22:51 +02:00
|
|
|
|
2016-02-15 11:43:47 +01:00
|
|
|
getMakePublicR :: SubmissionId -> Handler TypedContent
|
2018-09-01 10:48:08 +02:00
|
|
|
getMakePublicR submissionId = do
|
|
|
|
userId <- requireAuthId
|
|
|
|
runViewProgress $ doMakePublic userId submissionId
|
2016-02-15 11:43:47 +01:00
|
|
|
|
2018-09-01 10:48:08 +02:00
|
|
|
doMakePublic :: UserId -> SubmissionId -> Channel -> Handler ()
|
|
|
|
doMakePublic userId submissionId chan = do
|
2019-12-14 10:56:07 +01:00
|
|
|
isOwner <- runDB $ checkWhetherGivenUserRepo userId submissionId
|
2016-02-15 11:43:47 +01:00
|
|
|
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
|
2020-09-05 14:22:12 +02:00
|
|
|
submissionRepoDir <- getRepoDirOrClone submissionRepoId chan
|
2018-06-06 13:08:38 +02:00
|
|
|
|
|
|
|
app <- getYesod
|
|
|
|
let scheme = appRepoScheme $ appSettings app
|
2019-12-07 22:48:58 +01:00
|
|
|
let repoHost = appRepoHost $ appSettings app
|
2018-06-06 13:08:38 +02:00
|
|
|
|
2019-12-07 22:48:58 +01:00
|
|
|
let targetRepoUrl = getPublicSubmissionUrl scheme repoHost (Just repo) $ challengeName challenge
|
2016-02-15 12:42:05 +01:00
|
|
|
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
|
2016-02-15 12:42:05 +01:00
|
|
|
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
|
2018-06-27 13:09:11 +02:00
|
|
|
(_, _) <- runProgram (Just repoDir) gitPath [
|
2016-02-15 11:43:47 +01:00
|
|
|
"push",
|
2019-12-07 23:17:12 +01:00
|
|
|
(T.unpack $ fixGitRepoUrl $ T.pack targetRepoUrl),
|
2016-02-15 11:43:47 +01:00
|
|
|
(T.unpack $ fromSHA1ToText commit) ++ ":refs/heads/" ++ targetBranchName] chan
|
|
|
|
return ()
|
|
|
|
|
|
|
|
checkWhetherUserRepo :: SubmissionId -> Handler Bool
|
|
|
|
checkWhetherUserRepo submissionId = do
|
2021-09-22 15:12:20 +02:00
|
|
|
Entity userId _ <- requireAuthPossiblyByToken
|
2019-12-14 10:56:07 +01:00
|
|
|
runDB $ checkWhetherGivenUserRepo userId submissionId
|