From 4e3523e3db1483b5642414cf14ba9fea515cfb92 Mon Sep 17 00:00:00 2001 From: Filip Gralinski Date: Mon, 15 Feb 2016 12:42:05 +0100 Subject: [PATCH] only public submissions are listed when queried --- Handler/MakePublic.hs | 11 ++++------- Handler/Query.hs | 18 ++++++++++++++---- Handler/Shared.hs | 24 +++++++++++++++++++++++- templates/query-form.hamlet | 2 ++ templates/query-result.hamlet | 4 +++- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/Handler/MakePublic.hs b/Handler/MakePublic.hs index 4e9907f..d08507e 100644 --- a/Handler/MakePublic.hs +++ b/Handler/MakePublic.hs @@ -6,9 +6,6 @@ import Handler.Shared import PersistSHA1 -import Text.Printf -import Database.Persist.Sql - import Data.Text as T getMakePublicR :: SubmissionId -> Handler TypedContent @@ -23,15 +20,15 @@ doMakePublic submissionId chan = do 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 targetRepoUrl = getPublicSubmissionUrl $ challengeName challenge + let targetBranchName = getPublicSubmissionBranch submissionId + msg chan $ "Start pushing from " ++ (T.pack submissionRepoDir) ++ " to repo " ++ targetRepoUrl ++ ", branch " ++ targetBranchName ++ " ..." let commit = submissionCommit submission - pushRepo submissionRepoDir commit targetRepoUrl targetBranchName chan + pushRepo submissionRepoDir commit (T.unpack $ targetRepoUrl) (T.unpack $ targetBranchName) chan return () diff --git a/Handler/Query.hs b/Handler/Query.hs index b676700..f7c1bb1 100644 --- a/Handler/Query.hs +++ b/Handler/Query.hs @@ -3,6 +3,7 @@ module Handler.Query where import Import import Handler.Tables (formatSubmitter) +import Handler.Shared import PersistSHA1 import Database.Persist.Sql @@ -12,22 +13,27 @@ import Yesod.Form.Bootstrap3 (BootstrapFormLayout (..), renderBootstrap3, withSmallInput) data FullSubmissionInfo = FullSubmissionInfo { + fsiSubmissionId :: SubmissionId, fsiSubmission :: Submission, fsiUser :: User, - fsiRepo :: Repo } + fsiRepo :: Repo, + fsiChallenge :: Challenge } getFullInfo :: Entity Submission -> Handler FullSubmissionInfo getFullInfo (Entity submissionId submission) = do repo <- runDB $ get404 $ submissionRepo submission user <- runDB $ get404 $ submissionSubmitter submission + challenge <- runDB $ get404 $ submissionChallenge submission return $ FullSubmissionInfo { + fsiSubmissionId = submissionId, fsiSubmission = submission, fsiUser = user, - fsiRepo = repo } + fsiRepo = repo, + fsiChallenge = challenge } findSubmissions :: Text -> Handler [FullSubmissionInfo] findSubmissions sha1Prefix = do - submissions <- runDB $ rawSql "SELECT ?? FROM submission WHERE cast(commit as text) like ?" [PersistText $ "\\\\x" ++ sha1Prefix ++ "%"] + submissions <- runDB $ rawSql "SELECT ?? FROM submission WHERE is_public AND cast(commit as text) like ?" [PersistText $ "\\\\x" ++ sha1Prefix ++ "%"] mapM getFullInfo submissions getQueryFormR :: Handler Html @@ -62,9 +68,13 @@ processQuery query = do setTitle "query results" $(widgetFile "query-results") -queryResult submission = $(widgetFile "query-result") +queryResult submission = do + $(widgetFile "query-result") where commitSha1AsText = fromSHA1ToText $ submissionCommit $ fsiSubmission submission submitter = formatSubmitter $ fsiUser submission + publicSubmissionBranch = getPublicSubmissionBranch $ fsiSubmissionId submission + publicSubmissionRepo = getReadOnlySubmissionUrl $ challengeName $ fsiChallenge submission + browsableUrl = browsableGitRepoBranch (challengeName $ fsiChallenge submission) publicSubmissionBranch stamp = T.pack $ show $ submissionStamp $ fsiSubmission submission queryForm :: Form Text diff --git a/Handler/Shared.hs b/Handler/Shared.hs index 79a3e2b..650d494 100644 --- a/Handler/Shared.hs +++ b/Handler/Shared.hs @@ -27,6 +27,9 @@ import PersistSHA1 import qualified Data.ByteString as BS +import Text.Printf +import Database.Persist.Sql + atom = Control.Concurrent.STM.atomically type Channel = TChan (Maybe Text) @@ -42,8 +45,27 @@ gitPath = "/usr/bin/git" browsableGitSite :: Text browsableGitSite = "http://gonito.net/gitlist/" +serverAddress :: Text +serverAddress = "gonito.net" + gitServer :: Text -gitServer = "ssh://gitolite@gonito.net/" +gitServer = "ssh://gitolite@" ++ serverAddress ++ "/" + +gitReadOnlyServer :: Text +gitReadOnlyServer = "git://" ++ serverAddress ++ "/" + + +getPublicSubmissionBranch :: SubmissionId -> Text +getPublicSubmissionBranch = T.pack . (printf "submission-%05d") . fromSqlKey + +getPublicSubmissionUrl :: Text -> Text +getPublicSubmissionUrl bareRepoName = gitServer ++ bareRepoName + +getReadOnlySubmissionUrl :: Text -> Text +getReadOnlySubmissionUrl bareRepoName = gitReadOnlyServer ++ bareRepoName + +browsableGitRepoBranch :: Text -> Text -> Text +browsableGitRepoBranch bareRepoName branch = (browsableGitRepo bareRepoName) ++ "/" ++ branch ++ "/" browsableGitRepo :: Text -> Text browsableGitRepo bareRepoName diff --git a/templates/query-form.hamlet b/templates/query-form.hamlet index dff2564..23df07b 100644 --- a/templates/query-form.hamlet +++ b/templates/query-form.hamlet @@ -1,5 +1,7 @@

Search for submission by Git commit SHA1 hashes: +

(Only public submissions will be returned.) +

^{formWidget} diff --git a/templates/query-result.hamlet b/templates/query-result.hamlet index df91230..098c9be 100644 --- a/templates/query-result.hamlet +++ b/templates/query-result.hamlet @@ -2,5 +2,7 @@
#{submissionDescription $ fsiSubmission submission}
#{submitter}
#{stamp} -
#{repoUrl $ fsiRepo submission} / #{repoBranch $ fsiRepo submission} +
taken from: #{repoUrl $ fsiRepo submission} / branch #{repoBranch $ fsiRepo submission} +
publicly available at: #{publicSubmissionRepo} / branch #{publicSubmissionBranch} +
browsable at: #{browsableUrl}