Add external links

This commit is contained in:
Filip Gralinski 2018-11-12 20:41:46 +01:00
parent 0e8846b06c
commit 0a5f05604e
7 changed files with 61 additions and 15 deletions

View File

@ -6,7 +6,8 @@ module Gonito.ExtractMetadata (
ExtractionOptions(..),
parseCommitMessage,
getLastCommitMessage,
parseTags)
parseTags,
Link(..))
where
import Import
@ -34,7 +35,8 @@ data ExtractionOptions = ExtractionOptions {
extractionOptionsGeneralParams :: Maybe (M.Map Text Text),
extractionOptionsUnwantedParams :: Maybe [Text],
extractionOptionsParamFiles :: Maybe [String],
extractionOptionsMLRunPath :: Maybe FilePath
extractionOptionsMLRunPath :: Maybe FilePath,
extractionOptionsExternalLinks :: Maybe [Link]
}
instance FromJSON ExtractionOptions where
@ -45,6 +47,7 @@ instance FromJSON ExtractionOptions where
<*> v .:? "unwanted-params"
<*> v .:? "param-files"
<*> v .:? "mlrun-path"
<*> v .:? "links"
instance Default ExtractionOptions where
def = ExtractionOptions {
@ -53,13 +56,25 @@ instance Default ExtractionOptions where
extractionOptionsGeneralParams = Nothing,
extractionOptionsUnwantedParams = Nothing,
extractionOptionsParamFiles = Nothing,
extractionOptionsMLRunPath = Nothing
extractionOptionsMLRunPath = Nothing,
extractionOptionsExternalLinks = Nothing
}
data Link = Link {
linkTitle :: Maybe Text,
linkUrl :: Text }
deriving (Eq, Show)
instance FromJSON Link where
parseJSON = withObject "Link" $ \v -> Link
<$> v .:? "title"
<*> v .: "url"
data GonitoMetadata = GonitoMetadata {
gonitoMetadataDescription :: Text,
gonitoMetadataTags :: S.Set Text,
gonitoMetadataGeneralParams :: M.Map Text Text
gonitoMetadataGeneralParams :: M.Map Text Text,
gonitoMetadataExternalLinks :: [Link]
}
deriving (Eq, Show)
@ -84,7 +99,10 @@ combineExtractionOptions (Just otherOptions) options = ExtractionOptions {
extractionOptionsParamFiles = case extractionOptionsParamFiles options of
Nothing -> extractionOptionsParamFiles otherOptions
Just pfs -> Just pfs,
extractionOptionsMLRunPath = combineWithF extractionOptionsMLRunPath }
extractionOptionsMLRunPath = combineWithF extractionOptionsMLRunPath,
extractionOptionsExternalLinks = case extractionOptionsExternalLinks options of
Nothing -> extractionOptionsExternalLinks otherOptions
Just links -> Just (links ++ (fromMaybe [] $ extractionOptionsExternalLinks otherOptions)) }
where combineWithT fun = case fun options of
Nothing -> fun otherOptions
Just v -> Just v
@ -131,7 +149,8 @@ extractMetadataFromRepoDir repoDir formExtractionOptions = do
pure $ GonitoMetadata {
gonitoMetadataDescription = description,
gonitoMetadataTags = tagsParsed,
gonitoMetadataGeneralParams = params
gonitoMetadataGeneralParams = params,
gonitoMetadataExternalLinks = fromMaybe [] (extractionOptionsExternalLinks extractionOptions)
}

View File

@ -23,7 +23,8 @@ import Handler.MakePublic
import Gonito.ExtractMetadata (ExtractionOptions(..),
extractMetadataFromRepoDir,
GonitoMetadata(..),
parseTags)
parseTags,
Link(..))
import qualified Text.Read as TR
@ -257,6 +258,12 @@ doCreateSubmission userId challengeId mDescription mTags repoSpec chan = do
challengeId
(gonitoMetadataDescription gonitoMetadata)
chan
_ <- runDB $ mapM insert $ map (\l -> ExternalLink {
externalLinkSubmission = submissionId,
externalLinkTitle = linkTitle l,
externalLinkUrl = linkUrl l }) $ gonitoMetadataExternalLinks gonitoMetadata
_ <- getOuts chan submissionId (gonitoMetadataGeneralParams gonitoMetadata)
currentTagIds <- runDB $ selectList [SubmissionTagSubmission ==. submissionId] []

View File

@ -18,7 +18,8 @@ data FullSubmissionInfo = FullSubmissionInfo {
fsiChallenge :: Challenge,
fsiChallengeRepo :: Repo,
fsiScheme :: RepoScheme,
fsiTags :: [(Entity Tag, Entity SubmissionTag)] }
fsiTags :: [(Entity Tag, Entity SubmissionTag)],
fsiExternalLinks :: [Entity ExternalLink] }
getFullInfo :: Entity Submission -> Handler FullSubmissionInfo
getFullInfo (Entity submissionId submission) = do
@ -29,6 +30,8 @@ getFullInfo (Entity submissionId submission) = do
tags <- runDB $ getTags submissionId
links <- runDB $ selectList [ExternalLinkSubmission ==. submissionId] [Asc ExternalLinkTitle]
app <- getYesod
let scheme = appRepoScheme $ appSettings app
@ -40,7 +43,8 @@ getFullInfo (Entity submissionId submission) = do
fsiChallenge = challenge,
fsiChallengeRepo = challengeRepo,
fsiScheme = scheme,
fsiTags = tags }
fsiTags = tags,
fsiExternalLinks = links }
getTags submissionId = do
sts <- selectList [SubmissionTagSubmission ==. submissionId] []

View File

@ -68,6 +68,10 @@ Parameter
name Text
value Text
UniqueParameterName variant name
ExternalLink
submission SubmissionId
title Text Maybe
url Text
Fork
source SubmissionId
target SubmissionId

View File

@ -17,7 +17,13 @@
<dd><a href="#{browsableUrl}">#{browsableUrl}</a>
<dt>clone by
<dd><code>git clone --single-branch #{publicSubmissionRepo} -b #{publicSubmissionBranch}</code>
$if not (null (fsiExternalLinks submission))
<dt>see also
<dd>
$forall (Entity _ externalLink) <- fsiExternalLinks submission
<a href="#{externalLinkUrl externalLink}">
#{fromMaybe (externalLinkUrl externalLink) (externalLinkTitle externalLink)}
<br>
^{resultTable (Entity (fsiSubmissionId submission) (fsiSubmission submission))}
<hr>

View File

@ -8,7 +8,7 @@ import qualified Data.Set as S
import qualified Data.Map.Strict as M
import Test.Hspec
import Gonito.ExtractMetadata (extractMetadataFromRepoDir, GonitoMetadata(..), ExtractionOptions(..))
import Gonito.ExtractMetadata (extractMetadataFromRepoDir, GonitoMetadata(..), ExtractionOptions(..), Link(..))
spec :: Spec
spec = do
@ -17,7 +17,8 @@ spec = do
extractMetadataFromRepoDir "test/fake-git-repos/simple/" def `shouldReturn` GonitoMetadata {
gonitoMetadataDescription = "Simple solution",
gonitoMetadataTags = S.fromList ["foo", "simple-solution", "baz"],
gonitoMetadataGeneralParams = M.empty
gonitoMetadataGeneralParams = M.empty,
gonitoMetadataExternalLinks = []
}
it "simple with some fields from the form" $ do
extractMetadataFromRepoDir "test/fake-git-repos/simple/" def {
@ -26,7 +27,8 @@ spec = do
} `shouldReturn` GonitoMetadata {
gonitoMetadataDescription = "Other solution",
gonitoMetadataTags = S.fromList ["foo", "simple-solution", "baz", "other-tag"],
gonitoMetadataGeneralParams = M.empty
gonitoMetadataGeneralParams = M.empty,
gonitoMetadataExternalLinks = []
}
it "with gonito.yaml" $ do
extractMetadataFromRepoDir "test/fake-git-repos/with-gonito-yaml/" def `shouldReturn` GonitoMetadata {
@ -35,5 +37,9 @@ spec = do
gonitoMetadataGeneralParams = M.fromList [("level", "4"),
("altitude", "8900.3"),
("q", "10.4"),
("style", "bold")]
("style", "bold")],
gonitoMetadataExternalLinks = [
Link (Just "gitlab") "https://about.gitlab.com/",
Link (Just "Polish Wikipedia") "https://pl.wikipedia.org/wiki/Wikipedia:Strona_g%C5%82%C3%B3wna",
Link Nothing "https://tvtropes.org/" ]
}

@ -1 +1 @@
Subproject commit 262f063bfbdb6417fbc4589ea6dfb60be773e6b1
Subproject commit f30144141f64f5bdee9d423d628cf8aeb5f5e466