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.Core
import GEval.OptionsParser import GEval.OptionsParser
import GEval.EvaluationScheme import GEval.EvaluationScheme
import GEval.Validation
import Gonito.ExtractMetadata (getLastCommitMessage) import Gonito.ExtractMetadata (getLastCommitMessage)
@ -95,7 +96,9 @@ doCreateChallenge name publicUrl publicBranch publicGitAnnexRemote privateUrl pr
repoSpecBranch = (repoBranch publicRepo), repoSpecBranch = (repoBranch publicRepo),
repoSpecGitAnnexRemote = (repoGitAnnexRemote publicRepo)}}) chan repoSpecGitAnnexRemote = (repoGitAnnexRemote publicRepo)}}) chan
case maybePrivateRepoId of 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 ()
Nothing -> return () Nothing -> return ()
@ -204,47 +207,51 @@ doChallengeUpdate challengeId
repoSpecGitAnnexRemote = privateGitAnnexRemote} repoSpecGitAnnexRemote = privateGitAnnexRemote}
chan chan
privateRepo <- runDB $ get404 $ privateRepoId isValidated <- validateChallenge privateRepoId chan
repoDir <- getRepoDir privateRepoId
(Just versionDescription) <- liftIO $ getLastCommitMessage repoDir
theNow <- liftIO getCurrentTime
let commit = (repoCurrentCommit privateRepo)
mAlreadyExistingVersion <- runDB $ getBy $ UniqueVersionByCommit commit when isValidated $
case mAlreadyExistingVersion of do
Just (Entity versionId _) -> do privateRepo <- runDB $ get404 $ privateRepoId
runDB $ update versionId [VersionDeadline =. newDeadline, repoDir <- getRepoDir privateRepoId
VersionMajor =. newMajor, (Just versionDescription) <- liftIO $ getLastCommitMessage repoDir
VersionMinor =. newMinor, theNow <- liftIO getCurrentTime
VersionPatch =. newPatch, let commit = (repoCurrentCommit privateRepo)
VersionDescription =. versionDescription,
VersionStamp =. theNow]
Nothing -> do mAlreadyExistingVersion <- runDB $ getBy $ UniqueVersionByCommit commit
_ <- runDB $ insert $ Version (Just challengeId) case mAlreadyExistingVersion of
commit Just (Entity versionId _) -> do
newDeadline runDB $ update versionId [VersionDeadline =. newDeadline,
newMajor VersionMajor =. newMajor,
newMinor VersionMinor =. newMinor,
newPatch VersionPatch =. newPatch,
versionDescription VersionDescription =. versionDescription,
theNow VersionStamp =. theNow]
return ()
(title, description, mImage) <- extractChallengeMetadata publicRepoId chan Nothing -> do
_ <- runDB $ insert $ Version (Just challengeId)
commit
newDeadline
newMajor
newMinor
newPatch
versionDescription
theNow
return ()
runDB $ update challengeId [ChallengePublicRepo =. publicRepoId, (title, description, mImage) <- extractChallengeMetadata publicRepoId chan
ChallengePrivateRepo =. privateRepoId,
ChallengeVersion =. commit,
ChallengeTitle =. title,
ChallengeDescription =. description,
ChallengeImage =. mImage]
updateTests challengeId chan runDB $ update challengeId [ChallengePublicRepo =. publicRepoId,
ChallengePrivateRepo =. privateRepoId,
ChallengeVersion =. commit,
ChallengeTitle =. title,
ChallengeDescription =. description,
ChallengeImage =. mImage]
updateTests challengeId chan
return ()
return () return ()
incrementVersion :: ChallengeUpdateType -> (Int, Int, Int) -> (Int, Int, Int) incrementVersion :: ChallengeUpdateType -> (Int, Int, Int) -> (Int, Int, Int)
incrementVersion MajorChange (major, _, _) = (major + 1, 0, 0) incrementVersion MajorChange (major, _, _) = (major + 1, 0, 0)
incrementVersion MinorChange (major, minor, _) = (major, minor + 1, 0) incrementVersion MinorChange (major, minor, _) = (major, minor + 1, 0)
@ -431,3 +438,29 @@ updateChallengeForm publicRepo privateRepo mDeadline = renderBootstrap3 Bootstra
<*> aopt dayField (bfs MsgChallengeDeadlineDay) (Just $ utctDay <$> mDeadline) <*> aopt dayField (bfs MsgChallengeDeadlineDay) (Just $ utctDay <$> mDeadline)
<*> aopt timeFieldTypeTime (fieldWithTooltip MsgChallengeDeadlineTime MsgChallengeDeadlineTooltip) <*> aopt timeFieldTypeTime (fieldWithTooltip MsgChallengeDeadlineTime MsgChallengeDeadlineTooltip)
(Just $ timeToTimeOfDay <$> utctDayTime <$> mDeadline) (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 , filemanip
, cryptohash , cryptohash
, markdown , markdown
, geval >= 1.21.1.0 && < 1.23 , geval >= 1.21.1.0 && < 1.24
, filepath , filepath
, yesod-table , yesod-table
, regex-tdfa , regex-tdfa