module Hadolint.Rule.DL3052 (rule) where

import qualified Data.Map as Map
import Data.Maybe
import qualified Data.Text as Text
import Hadolint.Rule
import Language.Docker.Syntax
import Network.URI as Uri


rule :: LabelSchema -> Rule args
rule :: LabelSchema -> Rule args
rule LabelSchema
labelschema = [Rule args] -> Rule args
forall a. Monoid a => [a] -> a
mconcat ([Rule args] -> Rule args) -> [Rule args] -> Rule args
forall a b. (a -> b) -> a -> b
$
  (LabelName -> Rule args) -> [LabelName] -> [Rule args]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LabelName -> Rule args
forall args. LabelName -> Rule args
labelIsNotUrlRule (LabelSchema -> [LabelName]
forall k a. Map k a -> [k]
Map.keys ((LabelType -> Bool) -> LabelSchema -> LabelSchema
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (LabelType -> LabelType -> Bool
forall a. Eq a => a -> a -> Bool
== LabelType
Url) LabelSchema
labelschema))
{-# INLINEABLE rule #-}

labelIsNotUrlRule :: LabelName -> Rule args
labelIsNotUrlRule :: LabelName -> Rule args
labelIsNotUrlRule LabelName
label = RuleCode
-> DLSeverity
-> LabelName
-> (Instruction args -> Bool)
-> Rule args
forall args.
RuleCode
-> DLSeverity
-> LabelName
-> (Instruction args -> Bool)
-> Rule args
simpleRule RuleCode
code DLSeverity
severity LabelName
message Instruction args -> Bool
forall args. Instruction args -> Bool
check
  where
    code :: RuleCode
code = RuleCode
"DL3052"
    severity :: DLSeverity
severity = DLSeverity
DLWarningC
    message :: LabelName
message = LabelName
"Label `" LabelName -> LabelName -> LabelName
forall a. Semigroup a => a -> a -> a
<> LabelName
label LabelName -> LabelName -> LabelName
forall a. Semigroup a => a -> a -> a
<> LabelName
"` is not a valid URL."
    check :: Instruction args -> Bool
check (Label Pairs
ls) = Pairs -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Pairs -> Bool) -> Pairs -> Bool
forall a b. (a -> b) -> a -> b
$ LabelName -> Pairs -> Pairs
getBadURLLabels LabelName
label Pairs
ls
    check Instruction args
_ = Bool
True

getBadURLLabels :: LabelName -> Pairs -> Pairs
getBadURLLabels :: LabelName -> Pairs -> Pairs
getBadURLLabels LabelName
lbl Pairs
prs = [(LabelName
l, LabelName
u) | (LabelName
l, LabelName
u) <- Pairs
prs, LabelName
l LabelName -> LabelName -> Bool
forall a. Eq a => a -> a -> Bool
== LabelName
lbl,
                                    (Maybe URI -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe URI -> Bool) -> (String -> Maybe URI) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe URI
Uri.parseURI) (LabelName -> String
Text.unpack LabelName
u)]