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

View File

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

View File

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

View File

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

View File

@ -17,7 +17,13 @@
<dd><a href="#{browsableUrl}">#{browsableUrl}</a> <dd><a href="#{browsableUrl}">#{browsableUrl}</a>
<dt>clone by <dt>clone by
<dd><code>git clone --single-branch #{publicSubmissionRepo} -b #{publicSubmissionBranch}</code> <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))} ^{resultTable (Entity (fsiSubmissionId submission) (fsiSubmission submission))}
<hr> <hr>

View File

@ -8,7 +8,7 @@ import qualified Data.Set as S
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import Test.Hspec import Test.Hspec
import Gonito.ExtractMetadata (extractMetadataFromRepoDir, GonitoMetadata(..), ExtractionOptions(..)) import Gonito.ExtractMetadata (extractMetadataFromRepoDir, GonitoMetadata(..), ExtractionOptions(..), Link(..))
spec :: Spec spec :: Spec
spec = do spec = do
@ -17,7 +17,8 @@ spec = do
extractMetadataFromRepoDir "test/fake-git-repos/simple/" def `shouldReturn` GonitoMetadata { extractMetadataFromRepoDir "test/fake-git-repos/simple/" def `shouldReturn` GonitoMetadata {
gonitoMetadataDescription = "Simple solution", gonitoMetadataDescription = "Simple solution",
gonitoMetadataTags = S.fromList ["foo", "simple-solution", "baz"], gonitoMetadataTags = S.fromList ["foo", "simple-solution", "baz"],
gonitoMetadataGeneralParams = M.empty gonitoMetadataGeneralParams = M.empty,
gonitoMetadataExternalLinks = []
} }
it "simple with some fields from the form" $ do it "simple with some fields from the form" $ do
extractMetadataFromRepoDir "test/fake-git-repos/simple/" def { extractMetadataFromRepoDir "test/fake-git-repos/simple/" def {
@ -26,7 +27,8 @@ spec = do
} `shouldReturn` GonitoMetadata { } `shouldReturn` GonitoMetadata {
gonitoMetadataDescription = "Other solution", gonitoMetadataDescription = "Other solution",
gonitoMetadataTags = S.fromList ["foo", "simple-solution", "baz", "other-tag"], gonitoMetadataTags = S.fromList ["foo", "simple-solution", "baz", "other-tag"],
gonitoMetadataGeneralParams = M.empty gonitoMetadataGeneralParams = M.empty,
gonitoMetadataExternalLinks = []
} }
it "with gonito.yaml" $ do it "with gonito.yaml" $ do
extractMetadataFromRepoDir "test/fake-git-repos/with-gonito-yaml/" def `shouldReturn` GonitoMetadata { extractMetadataFromRepoDir "test/fake-git-repos/with-gonito-yaml/" def `shouldReturn` GonitoMetadata {
@ -35,5 +37,9 @@ spec = do
gonitoMetadataGeneralParams = M.fromList [("level", "4"), gonitoMetadataGeneralParams = M.fromList [("level", "4"),
("altitude", "8900.3"), ("altitude", "8900.3"),
("q", "10.4"), ("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