{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE StrictData        #-}
{-# LANGUAGE TupleSections     #-}
-- | The workflow dependency lockfile, .github/workflows/actions.lock, as
-- `gh actions-lock` writes it: which actions each workflow's steps use, and
-- the commit each of those actions is pinned to.
--
-- A combined lock holds the workflows of several repositories, named by their
-- path in a workspace ("btox/.github/workflows/ci.yml"), so that every
-- repository's lock can be cut from it with the same commits.
module GitHub.Types.ActionsLock
  ( ActionsLock (..)
  , Dependency (..)
  , actionKey
  , actionRepo
  , stepActions
  , renderLock
  , renameWorkflows
  , repoWorkflowKey
  , repoLock
  , repoProblems
  , combinedProblems
  ) where

import           Control.Monad     (guard, unless)
import           Data.Aeson        (FromJSON (..), Value (..), withObject,
                                    (.!=), (.:), (.:?))
import qualified Data.Aeson.KeyMap as KeyMap
import           Data.Char         (isHexDigit)
import           Data.List         (sort)
import           Data.Map.Strict   (Map)
import qualified Data.Map.Strict   as Map
import           Data.Maybe        (mapMaybe)
import qualified Data.Set          as Set
import           Data.Text         (Text)
import qualified Data.Text         as Text
import qualified Data.Vector       as V

data Dependency = Dependency
  { Dependency -> Text
depRef     :: Text
  , Dependency -> Text
depCommit  :: Text
  , Dependency -> Int
depOwnerId :: Int
  , Dependency -> Int
depRepoId  :: Int
  , Dependency -> [Text]
depUses    :: [Text]
    -- ^ What a composite action uses in turn.
  } deriving (Dependency -> Dependency -> Bool
(Dependency -> Dependency -> Bool)
-> (Dependency -> Dependency -> Bool) -> Eq Dependency
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Dependency -> Dependency -> Bool
== :: Dependency -> Dependency -> Bool
$c/= :: Dependency -> Dependency -> Bool
/= :: Dependency -> Dependency -> Bool
Eq, Int -> Dependency -> ShowS
[Dependency] -> ShowS
Dependency -> FilePath
(Int -> Dependency -> ShowS)
-> (Dependency -> FilePath)
-> ([Dependency] -> ShowS)
-> Show Dependency
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Dependency -> ShowS
showsPrec :: Int -> Dependency -> ShowS
$cshow :: Dependency -> FilePath
show :: Dependency -> FilePath
$cshowList :: [Dependency] -> ShowS
showList :: [Dependency] -> ShowS
Show)

data ActionsLock = ActionsLock
  { ActionsLock -> Map Text [Text]
lockWorkflows    :: Map Text [Text]
  , ActionsLock -> Map Text Dependency
lockDependencies :: Map Text Dependency
  } deriving (ActionsLock -> ActionsLock -> Bool
(ActionsLock -> ActionsLock -> Bool)
-> (ActionsLock -> ActionsLock -> Bool) -> Eq ActionsLock
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ActionsLock -> ActionsLock -> Bool
== :: ActionsLock -> ActionsLock -> Bool
$c/= :: ActionsLock -> ActionsLock -> Bool
/= :: ActionsLock -> ActionsLock -> Bool
Eq, Int -> ActionsLock -> ShowS
[ActionsLock] -> ShowS
ActionsLock -> FilePath
(Int -> ActionsLock -> ShowS)
-> (ActionsLock -> FilePath)
-> ([ActionsLock] -> ShowS)
-> Show ActionsLock
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ActionsLock -> ShowS
showsPrec :: Int -> ActionsLock -> ShowS
$cshow :: ActionsLock -> FilePath
show :: ActionsLock -> FilePath
$cshowList :: [ActionsLock] -> ShowS
showList :: [ActionsLock] -> ShowS
Show)

lockVersion :: Text
lockVersion :: Text
lockVersion = Text
"v0.0.3"

instance FromJSON Dependency where
  parseJSON :: Value -> Parser Dependency
parseJSON = FilePath
-> (Object -> Parser Dependency) -> Value -> Parser Dependency
forall a. FilePath -> (Object -> Parser a) -> Value -> Parser a
withObject FilePath
"Dependency" ((Object -> Parser Dependency) -> Value -> Parser Dependency)
-> (Object -> Parser Dependency) -> Value -> Parser Dependency
forall a b. (a -> b) -> a -> b
$ \Object
o -> Text -> Text -> Int -> Int -> [Text] -> Dependency
Dependency
    (Text -> Text -> Int -> Int -> [Text] -> Dependency)
-> Parser Text
-> Parser (Text -> Int -> Int -> [Text] -> Dependency)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ref"
    Parser (Text -> Int -> Int -> [Text] -> Dependency)
-> Parser Text -> Parser (Int -> Int -> [Text] -> Dependency)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"commit"
    Parser (Int -> Int -> [Text] -> Dependency)
-> Parser Int -> Parser (Int -> [Text] -> Dependency)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"owner_id"
    Parser (Int -> [Text] -> Dependency)
-> Parser Int -> Parser ([Text] -> Dependency)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"repo_id"
    Parser ([Text] -> Dependency) -> Parser [Text] -> Parser Dependency
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe [Text])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"uses" Parser (Maybe [Text]) -> [Text] -> Parser [Text]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []

instance FromJSON ActionsLock where
  parseJSON :: Value -> Parser ActionsLock
parseJSON = FilePath
-> (Object -> Parser ActionsLock) -> Value -> Parser ActionsLock
forall a. FilePath -> (Object -> Parser a) -> Value -> Parser a
withObject FilePath
"ActionsLock" ((Object -> Parser ActionsLock) -> Value -> Parser ActionsLock)
-> (Object -> Parser ActionsLock) -> Value -> Parser ActionsLock
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
    Text
version <- Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"version"
    Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text
version Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
lockVersion) (Parser () -> Parser ())
-> (FilePath -> Parser ()) -> FilePath -> Parser ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Parser ()
forall a. FilePath -> Parser a
forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail (FilePath -> Parser ()) -> FilePath -> Parser ()
forall a b. (a -> b) -> a -> b
$
      FilePath
"actions.lock version " FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> FilePath
Text.unpack Text
version FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> FilePath
" isn't " FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> FilePath
Text.unpack Text
lockVersion
    Map Text [Text] -> Map Text Dependency -> ActionsLock
ActionsLock (Map Text [Text] -> Map Text Dependency -> ActionsLock)
-> Parser (Map Text [Text])
-> Parser (Map Text Dependency -> ActionsLock)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Map Text [Text])
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"workflows" Parser (Map Text Dependency -> ActionsLock)
-> Parser (Map Text Dependency) -> Parser ActionsLock
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe (Map Text Dependency))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"dependencies" Parser (Maybe (Map Text Dependency))
-> Map Text Dependency -> Parser (Map Text Dependency)
forall a. Parser (Maybe a) -> a -> Parser a
.!= Map Text Dependency
forall k a. Map k a
Map.empty

-- | The lock in the layout `gh actions-lock` writes, so either tool can
-- update a lock without changing anything else in it.
renderLock :: ActionsLock -> Text
renderLock :: ActionsLock -> Text
renderLock ActionsLock{Map Text [Text]
Map Text Dependency
lockWorkflows :: ActionsLock -> Map Text [Text]
lockDependencies :: ActionsLock -> Map Text Dependency
lockWorkflows :: Map Text [Text]
lockDependencies :: Map Text Dependency
..} = [Text] -> Text
Text.unlines ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
  [ Text
"# This file is machine-generated by `gh actions-lock`."
  , Text
"# Do not edit by hand; run `gh actions-lock` to update."
  , Text
"# Docs: https://gh.io/actions-lockfile"
  , Text
"version: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
lockVersion
  , Text
"workflows:"
  ] [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ ((Text, [Text]) -> [Text]) -> [(Text, [Text])] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Text, [Text]) -> [Text]
workflow (Map Text [Text] -> [(Text, [Text])]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text [Text]
lockWorkflows)
    [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (if Map Text Dependency -> Bool
forall k a. Map k a -> Bool
Map.null Map Text Dependency
lockDependencies then [] else Text
"dependencies:" Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: ((Text, Dependency) -> [Text]) -> [(Text, Dependency)] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Text, Dependency) -> [Text]
dependency (Map Text Dependency -> [(Text, Dependency)]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text Dependency
lockDependencies))
  where
    workflow :: (Text, [Text]) -> [Text]
workflow (Text
path, [])   = [Int -> Text -> Text
indent Int
1 (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
quote Text
path Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
": []"]
    workflow (Text
path, [Text]
keys) = Int -> Text -> Text
indent Int
1 (Text -> Text
quote Text
path Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":") Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: Int -> [Text] -> [Text]
list Int
2 [Text]
keys
    dependency :: (Text, Dependency) -> [Text]
dependency (Text
key, Dependency{Int
[Text]
Text
depRef :: Dependency -> Text
depCommit :: Dependency -> Text
depOwnerId :: Dependency -> Int
depRepoId :: Dependency -> Int
depUses :: Dependency -> [Text]
depRef :: Text
depCommit :: Text
depOwnerId :: Int
depRepoId :: Int
depUses :: [Text]
..}) =
      Int -> Text -> Text
indent Int
1 (Text -> Text
quote Text
key Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":")
      Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> Text -> Text
indent Int
2)
        [ Text
"ref: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
depRef
        , Text
"commit: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
depCommit
        , Text
"owner_id: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
Text.pack (Int -> FilePath
forall a. Show a => a -> FilePath
show Int
depOwnerId)
        , Text
"repo_id: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
Text.pack (Int -> FilePath
forall a. Show a => a -> FilePath
show Int
depRepoId)
        ]
      [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (if [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
depUses then [] else Int -> Text -> Text
indent Int
2 Text
"uses:" Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: Int -> [Text] -> [Text]
list Int
3 [Text]
depUses)
    list :: Int -> [Text] -> [Text]
list Int
n = (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> Text -> Text
indent Int
n (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
"- " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
quote)
    indent :: Int -> Text -> Text
indent Int
n = (Int -> Text -> Text
Text.replicate (Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n) Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>)
    quote :: Text -> Text
quote Text
t = Text
"'" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
Text.replace Text
"'" Text
"''" Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'"

-- | Split a step's `uses:` into the action's path and its ref.
splitUses :: Text -> Maybe (Text, Text)
splitUses :: Text -> Maybe (Text, Text)
splitUses Text
uses = case HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
Text.breakOnEnd Text
"@" Text
uses of
  (Text
"", Text
_)       -> Maybe (Text, Text)
forall a. Maybe a
Nothing
  (Text
pathAt, Text
ref) -> (Text, Text) -> Maybe (Text, Text)
forall a. a -> Maybe a
Just (Int -> Text -> Text
Text.dropEnd Int
1 Text
pathAt, Text
ref)

-- | The repository an action lives in, lowercased ("google/clusterfuzzlite"
-- for "google/clusterfuzzlite/actions/run_fuzzers").
actionRepo :: Text -> Text
actionRepo :: Text -> Text
actionRepo = Text -> Text
Text.toLower (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> Text
Text.intercalate Text
"/" ([Text] -> Text) -> (Text -> [Text]) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Text] -> [Text]
forall a. Int -> [a] -> [a]
take Int
2 ([Text] -> [Text]) -> (Text -> [Text]) -> Text -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
Text.splitOn Text
"/"

-- | How actions.lock names the action a step uses: its repository and ref.
-- Local actions and containers aren't locked.
actionKey :: Text -> Maybe Text
actionKey :: Text -> Maybe Text
actionKey Text
uses
  | (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Text -> Text -> Bool
`Text.isPrefixOf` Text
uses) [Text
"./", Text
"$/", Text
"docker://"] = Maybe Text
forall a. Maybe a
Nothing
  | Bool
otherwise = (\(Text
path, Text
ref) -> Text -> Text
actionRepo Text
path Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"@" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ref) ((Text, Text) -> Text) -> Maybe (Text, Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe (Text, Text)
splitUses Text
uses

-- | The actions a workflow's steps use. Reusable workflows (jobs.<id>.uses)
-- aren't locked.
stepActions :: Value -> [Text]
stepActions :: Value -> [Text]
stepActions (Object Object
workflow) = Set Text -> [Text]
forall a. Set a -> [a]
Set.toList (Set Text -> [Text]) -> ([Text] -> Set Text) -> [Text] -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$
  [ Text
key
  | Just (Object Object
jobs) <- [Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"jobs" Object
workflow]
  , Object Object
job <- Object -> [Value]
forall v. KeyMap v -> [v]
KeyMap.elems Object
jobs
  , Just (Array Array
steps) <- [Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"steps" Object
job]
  , Object Object
step <- Array -> [Value]
forall a. Vector a -> [a]
V.toList Array
steps
  , Just (String Text
uses) <- [Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"uses" Object
step]
  , Just Text
key <- [Text -> Maybe Text
actionKey Text
uses]
  ]
stepActions Value
_ = []

-- | A workflow's name in a combined lock, given its repository's path in the
-- workspace ("." for the workspace itself) and its file name.
repoWorkflowKey :: FilePath -> FilePath -> Text
repoWorkflowKey :: FilePath -> FilePath -> Text
repoWorkflowKey FilePath
repo FilePath
file = FilePath -> Text
repoPrefix FilePath
repo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".github/workflows/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
Text.pack FilePath
file

repoPrefix :: FilePath -> Text
repoPrefix :: FilePath -> Text
repoPrefix FilePath
"."  = Text
""
repoPrefix FilePath
repo = FilePath -> Text
Text.pack FilePath
repo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/"

renameWorkflows :: (Text -> Maybe Text) -> ActionsLock -> ActionsLock
renameWorkflows :: (Text -> Maybe Text) -> ActionsLock -> ActionsLock
renameWorkflows Text -> Maybe Text
rename ActionsLock
lock = ActionsLock
lock
  { lockWorkflows = Map.fromList . mapMaybe (\(Text
k, [Text]
v) -> (, [Text]
v) (Text -> (Text, [Text])) -> Maybe Text -> Maybe (Text, [Text])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe Text
rename Text
k) . Map.toList $ lockWorkflows lock }

-- | Drop the dependencies no workflow needs.
prune :: ActionsLock -> ActionsLock
prune :: ActionsLock -> ActionsLock
prune lock :: ActionsLock
lock@ActionsLock{Map Text [Text]
Map Text Dependency
lockWorkflows :: ActionsLock -> Map Text [Text]
lockDependencies :: ActionsLock -> Map Text Dependency
lockWorkflows :: Map Text [Text]
lockDependencies :: Map Text Dependency
..} = ActionsLock
lock { lockDependencies = Map.restrictKeys lockDependencies needed }
  where
    needed :: Set Text
needed = Set Text -> [Text] -> Set Text
go Set Text
forall a. Set a
Set.empty ([Text] -> Set Text)
-> ([[Text]] -> [Text]) -> [[Text]] -> Set Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Text]] -> [Text]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Text]] -> Set Text) -> [[Text]] -> Set Text
forall a b. (a -> b) -> a -> b
$ Map Text [Text] -> [[Text]]
forall k a. Map k a -> [a]
Map.elems Map Text [Text]
lockWorkflows
    go :: Set Text -> [Text] -> Set Text
go Set Text
seen [] = Set Text
seen
    go Set Text
seen (Text
k:[Text]
ks)
      | Text
k Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set Text
seen = Set Text -> [Text] -> Set Text
go Set Text
seen [Text]
ks
      | Bool
otherwise = Set Text -> [Text] -> Set Text
go (Text -> Set Text -> Set Text
forall a. Ord a => a -> Set a -> Set a
Set.insert Text
k Set Text
seen) ([Text] -> (Dependency -> [Text]) -> Maybe Dependency -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Dependency -> [Text]
depUses (Text -> Map Text Dependency -> Maybe Dependency
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
k Map Text Dependency
lockDependencies) [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text]
ks)

-- | A repository's lock, cut from a combined lock.
repoLock :: FilePath -> ActionsLock -> ActionsLock
repoLock :: FilePath -> ActionsLock -> ActionsLock
repoLock FilePath
repo = ActionsLock -> ActionsLock
prune (ActionsLock -> ActionsLock)
-> (ActionsLock -> ActionsLock) -> ActionsLock -> ActionsLock
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Maybe Text) -> ActionsLock -> ActionsLock
renameWorkflows Text -> Maybe Text
own
  where
    -- The workspace's own workflows have no prefix, so check what's left.
    own :: Text -> Maybe Text
own Text
key = do
      Text
path <- Text -> Text -> Maybe Text
Text.stripPrefix (FilePath -> Text
repoPrefix FilePath
repo) Text
key
      Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ Text
".github/workflows/" Text -> Text -> Bool
`Text.isPrefixOf` Text
path
      Text -> Maybe Text
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return Text
path

-- | What's wrong with a repository's actions.lock (Nothing if it has none),
-- given what its workflows use and the combined lock it should be cut from.
repoProblems :: ActionsLock -> FilePath -> Maybe ActionsLock -> Map Text [Text] -> [Text]
repoProblems :: ActionsLock
-> FilePath -> Maybe ActionsLock -> Map Text [Text] -> [Text]
repoProblems ActionsLock
combined FilePath
repo Maybe ActionsLock
lock Map Text [Text]
used =
  [ Text
path Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" uses " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe [Text] -> Text
showKeys (Text -> Map Text [Text] -> Maybe [Text]
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
path Map Text [Text]
used) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", but the combined lock has "
      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe [Text] -> Text
showKeys (Text -> Map Text [Text] -> Maybe [Text]
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
path (ActionsLock -> Map Text [Text]
lockWorkflows ActionsLock
expected))
  | Text
path <- Map Text [Text] -> Map Text [Text] -> [Text]
forall k v. (Ord k, Eq v) => Map k v -> Map k v -> [k]
differences Map Text [Text]
used (ActionsLock -> Map Text [Text]
lockWorkflows ActionsLock
expected) ]
  [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ case Maybe ActionsLock
lock of
    Maybe ActionsLock
Nothing -> [Text
"There is no .github/workflows/actions.lock."]
    Just ActionsLock
actual ->
      [ Text
path Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
": actions.lock has " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe [Text] -> Text
showKeys (Text -> Map Text [Text] -> Maybe [Text]
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
path (ActionsLock -> Map Text [Text]
lockWorkflows ActionsLock
actual))
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", the combined lock has " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe [Text] -> Text
showKeys (Text -> Map Text [Text] -> Maybe [Text]
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
path (ActionsLock -> Map Text [Text]
lockWorkflows ActionsLock
expected))
      | Text
path <- Map Text [Text] -> Map Text [Text] -> [Text]
forall k v. (Ord k, Eq v) => Map k v -> Map k v -> [k]
differences (ActionsLock -> Map Text [Text]
lockWorkflows ActionsLock
actual) (ActionsLock -> Map Text [Text]
lockWorkflows ActionsLock
expected) ]
      [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [ Text
key Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
": actions.lock has " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe Dependency -> Text
showDep (Text -> Map Text Dependency -> Maybe Dependency
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
key (ActionsLock -> Map Text Dependency
lockDependencies ActionsLock
actual))
             Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", the combined lock has " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe Dependency -> Text
showDep (Text -> Map Text Dependency -> Maybe Dependency
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
key (ActionsLock -> Map Text Dependency
lockDependencies ActionsLock
expected))
         | Text
key <- Map Text Dependency -> Map Text Dependency -> [Text]
forall k v. (Ord k, Eq v) => Map k v -> Map k v -> [k]
differences (ActionsLock -> Map Text Dependency
lockDependencies ActionsLock
actual) (ActionsLock -> Map Text Dependency
lockDependencies ActionsLock
expected) ]
  where
    expected :: ActionsLock
expected = FilePath -> ActionsLock -> ActionsLock
repoLock FilePath
repo ActionsLock
combined
    showKeys :: Maybe [Text] -> Text
showKeys = Text -> ([Text] -> Text) -> Maybe [Text] -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"nothing" (Text -> [Text] -> Text
Text.intercalate Text
", ")
    showDep :: Maybe Dependency -> Text
showDep = Text -> (Dependency -> Text) -> Maybe Dependency -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"nothing" Dependency -> Text
depCommit

-- | What's wrong with a combined lock: every action must be pinned, and each
-- action's repository used at one ref in all workflows.
combinedProblems :: ActionsLock -> [Text]
combinedProblems :: ActionsLock -> [Text]
combinedProblems ActionsLock{Map Text [Text]
Map Text Dependency
lockWorkflows :: ActionsLock -> Map Text [Text]
lockDependencies :: ActionsLock -> Map Text Dependency
lockWorkflows :: Map Text [Text]
lockDependencies :: Map Text Dependency
..} =
  [ Text
key Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" has no pinned commit"
  | Text
key <- Set Text -> [Text]
forall a. Set a -> [a]
Set.toList (Set Text -> [Text]) -> ([Text] -> Set Text) -> [Text] -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [[Text]] -> [Text]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Map Text [Text] -> [[Text]]
forall k a. Map k a -> [a]
Map.elems Map Text [Text]
lockWorkflows) [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (Dependency -> [Text]) -> [Dependency] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Dependency -> [Text]
depUses (Map Text Dependency -> [Dependency]
forall k a. Map k a -> [a]
Map.elems Map Text Dependency
lockDependencies)
  , Bool -> (Dependency -> Bool) -> Maybe Dependency -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (Bool -> Bool
not (Bool -> Bool) -> (Dependency -> Bool) -> Dependency -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
isPinned (Text -> Bool) -> (Dependency -> Text) -> Dependency -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dependency -> Text
depCommit) (Text -> Map Text Dependency -> Maybe Dependency
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
key Map Text Dependency
lockDependencies) ]
  [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [ Text
repo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is used at " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
Text.intercalate Text
" and " (((Text, [Text]) -> Text) -> [(Text, [Text])] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text, [Text]) -> Text
showRef (Map Text [Text] -> [(Text, [Text])]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text [Text]
refs))
     | (Text
repo, Map Text [Text]
refs) <- Map Text (Map Text [Text]) -> [(Text, Map Text [Text])]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text (Map Text [Text])
refsByRepo, Map Text [Text] -> Int
forall k a. Map k a -> Int
Map.size Map Text [Text]
refs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 ]
  where
    refsByRepo :: Map Text (Map Text [Text])
refsByRepo = (Map Text [Text] -> Map Text [Text] -> Map Text [Text])
-> [(Text, Map Text [Text])] -> Map Text (Map Text [Text])
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith (([Text] -> [Text] -> [Text])
-> Map Text [Text] -> Map Text [Text] -> Map Text [Text]
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
(++))
      [ (Text -> Text
actionRepo Text
path, Text -> [Text] -> Map Text [Text]
forall k a. k -> a -> Map k a
Map.singleton Text
ref [Text
workflow])
      | (Text
workflow, [Text]
keys) <- Map Text [Text] -> [(Text, [Text])]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text [Text]
lockWorkflows, Just (Text
path, Text
ref) <- (Text -> Maybe (Text, Text)) -> [Text] -> [Maybe (Text, Text)]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Maybe (Text, Text)
splitUses [Text]
keys ]
    showRef :: (Text, [Text]) -> Text
showRef (Text
ref, [Text]
workflows)
      | [Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
workflows Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
3 = Text
ref Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
Text.pack (Int -> FilePath
forall a. Show a => a -> FilePath
show ([Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
workflows)) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" workflows)"
      | Bool
otherwise = Text
ref Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
Text.intercalate Text
", " ([Text] -> [Text]
forall a. Ord a => [a] -> [a]
sort [Text]
workflows) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"

-- | Whether a lockfile commit is a full SHA-1, written "sha1-<40 hex digits>".
isPinned :: Text -> Bool
isPinned :: Text -> Bool
isPinned Text
commit = case Text -> Text -> Maybe Text
Text.stripPrefix Text
"sha1-" Text
commit of
  Just Text
sha -> Text -> Int
Text.length Text
sha Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
40 Bool -> Bool -> Bool
&& (Char -> Bool) -> Text -> Bool
Text.all Char -> Bool
isHexDigit Text
sha
  Maybe Text
Nothing  -> Bool
False

differences :: (Ord k, Eq v) => Map k v -> Map k v -> [k]
differences :: forall k v. (Ord k, Eq v) => Map k v -> Map k v -> [k]
differences Map k v
a Map k v
b =
  [ k
k | k
k <- Set k -> [k]
forall a. Set a -> [a]
Set.toList (Map k v -> Set k
forall k a. Map k a -> Set k
Map.keysSet Map k v
a Set k -> Set k -> Set k
forall a. Semigroup a => a -> a -> a
<> Map k v -> Set k
forall k a. Map k a -> Set k
Map.keysSet Map k v
b), k -> Map k v -> Maybe v
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
k Map k v
a Maybe v -> Maybe v -> Bool
forall a. Eq a => a -> a -> Bool
/= k -> Map k v -> Maybe v
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
k Map k v
b ]