From 923b1fe8f0836a41c45ec324b389559b07e21a4e Mon Sep 17 00:00:00 2001 From: Filip Gralinski Date: Mon, 15 Feb 2016 11:43:47 +0100 Subject: [PATCH] add "make it public" feature --- Application.hs | 1 + Handler/MakePublic.hs | 50 +++++++++++++++++++++++++++++++++++++++++++ Handler/Shared.hs | 3 +++ config/routes | 1 + gonito.cabal | 1 + 5 files changed, 56 insertions(+) create mode 100644 Handler/MakePublic.hs diff --git a/Application.hs b/Application.hs index 29bbfdb..8dbabf7 100644 --- a/Application.hs +++ b/Application.hs @@ -41,6 +41,7 @@ import Handler.Graph import Handler.Home import Handler.CreateChallenge import Handler.ListChallenges +import Handler.MakePublic import Handler.Query import Handler.ShowChallenge import Handler.Shared diff --git a/Handler/MakePublic.hs b/Handler/MakePublic.hs new file mode 100644 index 0000000..4e9907f --- /dev/null +++ b/Handler/MakePublic.hs @@ -0,0 +1,50 @@ +module Handler.MakePublic where + +import Import + +import Handler.Shared + +import PersistSHA1 + +import Text.Printf +import Database.Persist.Sql + +import Data.Text as T + +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] + let targetBranchName = printf "submission-%05d" $ fromSqlKey submissionId + submission <- runDB $ get404 submissionId + challenge <- runDB $ get404 $ submissionChallenge submission + let submissionRepoId = submissionRepo submission + submissionRepoDir <- getRepoDir submissionRepoId + let targetRepoUrl = T.unpack $ gitServer ++ challengeName challenge + msg chan $ T.pack $ "Start pushing from " ++ submissionRepoDir ++ " to repo " ++ targetRepoUrl ++ ", branch " ++ targetBranchName ++ " ..." + let commit = submissionCommit submission + pushRepo submissionRepoDir commit targetRepoUrl targetBranchName chan + return () + + +pushRepo :: String -> SHA1 -> String -> String -> Channel -> Handler () +pushRepo repoDir commit targetRepoUrl targetBranchName chan = do + (exitCode, _) <- runProgram (Just repoDir) gitPath [ + "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 diff --git a/Handler/Shared.hs b/Handler/Shared.hs index e5722d6..79a3e2b 100644 --- a/Handler/Shared.hs +++ b/Handler/Shared.hs @@ -42,6 +42,9 @@ gitPath = "/usr/bin/git" browsableGitSite :: Text browsableGitSite = "http://gonito.net/gitlist/" +gitServer :: Text +gitServer = "ssh://gitolite@gonito.net/" + browsableGitRepo :: Text -> Text browsableGitRepo bareRepoName | ".git" `isSuffixOf` bareRepoName = browsableGitSite ++ bareRepoName diff --git a/config/routes b/config/routes index 3a4998d..a5d1275 100644 --- a/config/routes +++ b/config/routes @@ -22,5 +22,6 @@ /q QueryFormR GET POST /q/#Text QueryResultsR GET +/make-public/#SubmissionId MakePublicR GET /account YourAccountR GET POST diff --git a/gonito.cabal b/gonito.cabal index 340e458..5ea58ff 100644 --- a/gonito.cabal +++ b/gonito.cabal @@ -35,6 +35,7 @@ library Handler.Graph Handler.Home Handler.ListChallenges + Handler.MakePublic Handler.Shared Handler.ShowChallenge Handler.Extract