Validate challenge when add or update

This commit is contained in:
Filip Gralinski 2019-12-13 22:14:00 +01:00
parent 5bd6e34c6b
commit f5c480205e
2 changed files with 68 additions and 35 deletions

View File

@ -11,6 +11,7 @@ import Handler.Extract
import GEval.Core
import GEval.OptionsParser
import GEval.EvaluationScheme
import GEval.Validation
import Gonito.ExtractMetadata (getLastCommitMessage)
@ -95,7 +96,9 @@ doCreateChallenge name publicUrl publicBranch publicGitAnnexRemote privateUrl pr
repoSpecBranch = (repoBranch publicRepo),
repoSpecGitAnnexRemote = (repoGitAnnexRemote publicRepo)}}) chan
case maybePrivateRepoId of
Just privateRepoId -> addChallenge name publicRepoId privateRepoId mDeadline chan
Just privateRepoId -> do
isValidated <- validateChallenge privateRepoId chan
when isValidated $ addChallenge name publicRepoId privateRepoId mDeadline chan
Nothing -> return ()
Nothing -> return ()
@ -204,6 +207,10 @@ doChallengeUpdate challengeId
repoSpecGitAnnexRemote = privateGitAnnexRemote}
chan
isValidated <- validateChallenge privateRepoId chan
when isValidated $
do
privateRepo <- runDB $ get404 $ privateRepoId
repoDir <- getRepoDir privateRepoId
(Just versionDescription) <- liftIO $ getLastCommitMessage repoDir
@ -243,7 +250,7 @@ doChallengeUpdate challengeId
updateTests challengeId chan
return ()
return ()
incrementVersion :: ChallengeUpdateType -> (Int, Int, Int) -> (Int, Int, Int)
incrementVersion MajorChange (major, _, _) = (major + 1, 0, 0)
@ -431,3 +438,29 @@ updateChallengeForm publicRepo privateRepo mDeadline = renderBootstrap3 Bootstra
<*> aopt dayField (bfs MsgChallengeDeadlineDay) (Just $ utctDay <$> mDeadline)
<*> aopt timeFieldTypeTime (fieldWithTooltip MsgChallengeDeadlineTime MsgChallengeDeadlineTooltip)
(Just $ timeToTimeOfDay <$> utctDayTime <$> mDeadline)
-- Validate whether a challenge is correct.
-- Contrary to `GEval.Validate.validationChallenge` do not
-- throw an exception (just return `False`)
validateChallenge :: RepoId -- ID of the private repository
-> Channel
-> Handler Bool -- returns false if not validated
validateChallenge repoId chan = do
msg chan "Validating the challenge..."
repoDir <- getRepoDir repoId
optionsParsingResult <- liftIO $ getOptions [
"--expected-directory", repoDir]
case optionsParsingResult of
Left _ -> do
err chan "Cannot read metric"
return False
Right opts -> do
result <- liftIO (try $ validationChallenge repoDir (geoSpec opts) :: IO (Either SomeException ()))
case result of
Left ex -> do
err chan (T.pack $ "Invalid challenge!!! " ++ (show ex))
return False
Right _ -> return True

View File

@ -127,7 +127,7 @@ library
, filemanip
, cryptohash
, markdown
, geval >= 1.21.1.0 && < 1.23
, geval >= 1.21.1.0 && < 1.24
, filepath
, yesod-table
, regex-tdfa