{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE NamedFieldPuns    #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
module GitHub.Tools.Settings
  ( syncSettings
  , validateSettings
  ) where

import           Control.Exception               (SomeException, try)
import           Control.Monad                   (forM_, unless, when)
import           Data.Aeson                      (FromJSON,
                                                  Value (Array, Null, Object, String),
                                                  encode, toJSON)
import qualified Data.Aeson.Key                  as AK
import qualified Data.Aeson.KeyMap               as KeyMap
import qualified Data.ByteString.Lazy.Char8      as LBS
import           Data.HashMap.Strict             (HashMap)
import qualified Data.HashMap.Strict             as HashMap
import           Data.List                       (isPrefixOf, nub, sortOn, (\\))
import           Data.Maybe                      (fromMaybe, isNothing,
                                                  mapMaybe)
import           Data.Text                       (Text)
import qualified Data.Text                       as Text
import qualified Data.Vector                     as V
import qualified GitHub
import           GitHub.Data.Request             (MediaType (..))
import qualified GitHub.Paths.Orgs               as Orgs
import qualified GitHub.Paths.Orgs.Teams         as Teams
import qualified GitHub.Paths.Orgs.Teams.Members as Members
import qualified GitHub.Paths.Repos              as Repos
import qualified GitHub.Paths.Repos.Branches     as Branches
import qualified GitHub.Paths.Repos.Labels       as Labels
import qualified GitHub.Paths.Repos.Rulesets     as Rulesets
import           GitHub.Tools.Requests           (mutate, mutate_, removeNulls,
                                                  request)
import           GitHub.Types.Base.User          (User (..))
import           GitHub.Types.Settings           (Label (Label, labelName),
                                                  OrgSettings (..),
                                                  RepoSettings (..),
                                                  Ruleset (..), Team (..),
                                                  TeamMembership (..))
import           Network.HTTP.Client             (Manager, newManager)
import           Network.HTTP.Client.TLS         (tlsManagerSettings)

delete :: Bool
delete :: Bool
delete = Bool
False

red :: String -> String
red :: String -> String
red String
s = String
"\ESC[31m" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
s String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"\ESC[0m"

green :: String -> String
green :: String -> String
green String
s = String
"\ESC[32m" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
s String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"\ESC[0m"

-- Fields we can't meaningfully diff (read-only, not echoed, or a different shape).
ignoredKeys :: [String]
ignoredKeys :: [String]
ignoredKeys = [String
"current_user_can_bypass", String
"automatic_copilot_code_review_enabled", String
"topics"]

diffV :: String -> Value -> Value -> [(String, Value, Value)]
diffV :: String -> Value -> Value -> [(String, Value, Value)]
diffV String
path (Object Object
cur) (Object Object
des) =
  ((Key, Value) -> [(String, Value, Value)])
-> [(Key, Value)] -> [(String, Value, Value)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Key, Value) -> [(String, Value, Value)]
step ([(Key, Value)] -> [(String, Value, Value)])
-> ([(Key, Value)] -> [(Key, Value)])
-> [(Key, Value)]
-> [(String, Value, Value)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Key, Value) -> Bool) -> [(Key, Value)] -> [(Key, Value)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ((Key, Value) -> Bool) -> (Key, Value) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
ignoredKeys) (String -> Bool)
-> ((Key, Value) -> String) -> (Key, Value) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> String
AK.toString (Key -> String) -> ((Key, Value) -> Key) -> (Key, Value) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Key, Value) -> Key
forall a b. (a, b) -> a
fst) ([(Key, Value)] -> [(String, Value, Value)])
-> [(Key, Value)] -> [(String, Value, Value)]
forall a b. (a -> b) -> a -> b
$ Object -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList Object
des
  where
    step :: (Key, Value) -> [(String, Value, Value)]
step (Key
k, Value
dv) =
      let p :: String
p  = if String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
path then Key -> String
AK.toString Key
k else String
path String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"." String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Key -> String
AK.toString Key
k
          cv :: Value
cv = Value -> Maybe Value -> Value
forall a. a -> Maybe a -> a
fromMaybe Value
Null (Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
k Object
cur)
      in String -> Value -> Value -> [(String, Value, Value)]
diffV String
p Value
cv Value
dv
diffV String
path (Array Array
cur) (Array Array
des)
  | Just [(Text, Value)]
dm <- Array -> Maybe [(Text, Value)]
byType Array
des, Just [(Text, Value)]
cm <- Array -> Maybe [(Text, Value)]
byType Array
cur =
      ((Text, Value) -> [(String, Value, Value)])
-> [(Text, Value)] -> [(String, Value, Value)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(Text
t, Value
dv) -> String -> Value -> Value -> [(String, Value, Value)]
diffV (Text -> String
at Text
t) (Value -> Maybe Value -> Value
forall a. a -> Maybe a -> a
fromMaybe Value
Null (Text -> [(Text, Value)] -> Maybe Value
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
t [(Text, Value)]
cm)) Value
dv) [(Text, Value)]
dm
        [(String, Value, Value)]
-> [(String, Value, Value)] -> [(String, Value, Value)]
forall a. [a] -> [a] -> [a]
++ [ (Text -> String
at Text
t, Value
cv, Value
Null) | (Text
t, Value
cv) <- [(Text, Value)]
cm, Maybe Value -> Bool
forall a. Maybe a -> Bool
isNothing (Text -> [(Text, Value)] -> Maybe Value
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
t [(Text, Value)]
dm) ]
  | Array -> Int
forall a. Vector a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Array
cur Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Array -> Int
forall a. Vector a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Array
des =
      [[(String, Value, Value)]] -> [(String, Value, Value)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[(String, Value, Value)]] -> [(String, Value, Value)])
-> [[(String, Value, Value)]] -> [(String, Value, Value)]
forall a b. (a -> b) -> a -> b
$ (Int -> Value -> Value -> [(String, Value, Value)])
-> [Int] -> [Value] -> [Value] -> [[(String, Value, Value)]]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 (\Int
i Value
c Value
d -> String -> Value -> Value -> [(String, Value, Value)]
diffV (String
path String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"[" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show (Int
i :: Int) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"]") Value
c Value
d)
               [Int
0 ..] (Array -> [Value]
forall a. Vector a -> [a]
V.toList Array
cur) (Array -> [Value]
forall a. Vector a -> [a]
V.toList Array
des)
  where
    at :: Text -> String
at Text
t = String
path String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"[" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
t String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"]"
    byType :: Array -> Maybe [(Text, Value)]
byType = (Value -> Maybe (Text, Value)) -> [Value] -> Maybe [(Text, Value)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Value -> Maybe (Text, Value)
typed ([Value] -> Maybe [(Text, Value)])
-> (Array -> [Value]) -> Array -> Maybe [(Text, Value)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array -> [Value]
forall a. Vector a -> [a]
V.toList
    typed :: Value -> Maybe (Text, Value)
typed (Object Object
o) | Just (String Text
t) <- Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"type" Object
o = (Text, Value) -> Maybe (Text, Value)
forall a. a -> Maybe a
Just (Text
t, Object -> Value
Object Object
o)
    typed Value
_ = Maybe (Text, Value)
forall a. Maybe a
Nothing
diffV String
path Value
cur Value
des
  | Value
cur Value -> Value -> Bool
forall a. Eq a => a -> a -> Bool
== Value
des = []
  | Bool
otherwise  = [(String
path, Value
cur, Value
des)]

-- | The current value of something on GitHub, or Null if we can't get it.
fetchOrNull :: GitHub.Auth -> Manager -> GitHub.Request 'GitHub.RO Value -> IO Value
fetchOrNull :: Auth -> Manager -> Request 'RO Value -> IO Value
fetchOrNull Auth
auth Manager
mgr Request 'RO Value
req = do
  Either SomeException Value
res <- IO Value -> IO (Either SomeException Value)
forall e a. Exception e => IO a -> IO (Either e a)
try (Maybe Auth -> Manager -> Request 'RO Value -> IO Value
forall a.
FromJSON a =>
Maybe Auth -> Manager -> Request 'RO a -> IO a
request (Auth -> Maybe Auth
forall a. a -> Maybe a
Just Auth
auth) Manager
mgr Request 'RO Value
req)
  Value -> IO Value
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Value -> IO Value) -> Value -> IO Value
forall a b. (a -> b) -> a -> b
$ (SomeException -> Value)
-> (Value -> Value) -> Either SomeException Value -> Value
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Value -> SomeException -> Value
forall a b. a -> b -> a
const Value
Null) Value -> Value
forall a. a -> a
id (Either SomeException Value
res :: Either SomeException Value)

printDiff :: String -> [(String, Value, Value)] -> IO ()
printDiff :: String -> [(String, Value, Value)] -> IO ()
printDiff String
header [(String, Value, Value)]
diffs =
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([(String, Value, Value)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(String, Value, Value)]
diffs) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
    String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
header String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
":"
    [(String, Value, Value)]
-> ((String, Value, Value) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String, Value, Value)]
diffs (((String, Value, Value) -> IO ()) -> IO ())
-> ((String, Value, Value) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(String
p, Value
cur, Value
des) -> do
      String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
p String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
":"
      String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> String
red   (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"    - " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value -> String
render Value
cur
      String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> String
green (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"    + " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value -> String
render Value
des
  where
    render :: Value -> String
render Value
Null = String
"(absent)"
    render Value
v    = ByteString -> String
LBS.unpack (Value -> ByteString
forall a. ToJSON a => a -> ByteString
encode Value
v)

report
  :: FromJSON a
  => Bool -> GitHub.Auth -> Manager -> String
  -> GitHub.Request 'GitHub.RW a -> IO ()
report :: forall a.
FromJSON a =>
Bool -> Auth -> Manager -> String -> Request 'RW a -> IO ()
report Bool
dry Auth
auth Manager
mgr String
what Request 'RW a
req =
  if Bool
dry
    then String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"~ would update " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
what
    else Auth -> Manager -> Request 'RW a -> IO a
forall a. FromJSON a => Auth -> Manager -> Request 'RW a -> IO a
mutate Auth
auth Manager
mgr Request 'RW a
req IO a -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

report_
  :: Bool -> GitHub.Auth -> Manager -> String
  -> GitHub.GenRequest 'MtUnit 'GitHub.RW () -> IO ()
report_ :: Bool
-> Auth -> Manager -> String -> GenRequest 'MtUnit 'RW () -> IO ()
report_ Bool
dry Auth
auth Manager
mgr String
what GenRequest 'MtUnit 'RW ()
req =
  if Bool
dry
    then String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"~ would delete " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
what
    else Auth -> Manager -> GenRequest 'MtUnit 'RW () -> IO ()
mutate_ Auth
auth Manager
mgr GenRequest 'MtUnit 'RW ()
req

getRulesetId :: V.Vector Ruleset -> Text -> Maybe Int
getRulesetId :: Vector Ruleset -> Text -> Maybe Int
getRulesetId Vector Ruleset
rulesets Text
name = case (Ruleset -> Bool) -> Vector Ruleset -> Maybe Ruleset
forall a. (a -> Bool) -> Vector a -> Maybe a
V.find ((Text -> Maybe Text
forall a. a -> Maybe a
Just Text
name Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
==) (Maybe Text -> Bool) -> (Ruleset -> Maybe Text) -> Ruleset -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ruleset -> Maybe Text
rulesetName) Vector Ruleset
rulesets of
  Just Ruleset{rulesetId :: Ruleset -> Maybe Int
rulesetId = Just Int
rId} -> Int -> Maybe Int
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
rId
  Maybe Ruleset
_                                  -> Maybe Int
forall a. Maybe a
Nothing

getTeamId :: V.Vector Team -> Text -> Maybe Int
getTeamId :: Vector Team -> Text -> Maybe Int
getTeamId Vector Team
teams Text
name = case (Team -> Bool) -> Vector Team -> Maybe Team
forall a. (a -> Bool) -> Vector a -> Maybe a
V.find ((Text -> Maybe Text
forall a. a -> Maybe a
Just Text
name Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
==) (Maybe Text -> Bool) -> (Team -> Maybe Text) -> Team -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Team -> Maybe Text
teamName) Vector Team
teams of
  Just Team{teamId :: Team -> Maybe Int
teamId = Just Int
tId} -> Int -> Maybe Int
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
tId
  Maybe Team
_                            -> Maybe Int
forall a. Maybe a
Nothing

syncSettings
  :: GitHub.Auth
  -> OrgSettings
  -> HashMap Text RepoSettings
  -> Text
  -> Bool
  -> IO ()
syncSettings :: Auth
-> OrgSettings
-> HashMap Text RepoSettings
-> Text
-> Bool
-> IO ()
syncSettings Auth
auth OrgSettings
org HashMap Text RepoSettings
repos Text
repoFilter Bool
dry = do
  -- Initialise HTTP manager so we can benefit from keep-alive connections.
  Manager
mgr <- ManagerSettings -> IO Manager
newManager ManagerSettings
tlsManagerSettings
  let orgLogin :: Text
orgLogin = OrgSettings -> Text
orgSettingsLogin OrgSettings
org

  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text
repoFilter Text -> Text -> Bool
`Text.isPrefixOf` Text
orgLogin) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
    Bool -> Auth -> Manager -> OrgSettings -> IO ()
syncOrgSettings Bool
dry Auth
auth Manager
mgr OrgSettings
org

  [(Text, RepoSettings)] -> ((Text, RepoSettings) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (((Text, RepoSettings) -> Text)
-> [(Text, RepoSettings)] -> [(Text, RepoSettings)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (Text, RepoSettings) -> Text
forall a b. (a, b) -> a
fst ([(Text, RepoSettings)] -> [(Text, RepoSettings)])
-> (HashMap Text RepoSettings -> [(Text, RepoSettings)])
-> HashMap Text RepoSettings
-> [(Text, RepoSettings)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, RepoSettings)] -> [(Text, RepoSettings)]
forall {b}. [(Text, b)] -> [(Text, b)]
filterRepos ([(Text, RepoSettings)] -> [(Text, RepoSettings)])
-> (HashMap Text RepoSettings -> [(Text, RepoSettings)])
-> HashMap Text RepoSettings
-> [(Text, RepoSettings)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap Text RepoSettings -> [(Text, RepoSettings)]
forall a. HashMap Text a -> [(Text, a)]
each (HashMap Text RepoSettings -> [(Text, RepoSettings)])
-> HashMap Text RepoSettings -> [(Text, RepoSettings)]
forall a b. (a -> b) -> a -> b
$ HashMap Text RepoSettings
repos) (((Text, RepoSettings) -> IO ()) -> IO ())
-> ((Text, RepoSettings) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Text
repo, RepoSettings
repoSettings) -> do
    Bool -> Auth -> Manager -> Text -> Text -> RepoSettings -> IO ()
syncRepoSettings Bool
dry Auth
auth Manager
mgr Text
orgLogin Text
repo RepoSettings
repoSettings
  where
    filterRepos :: [(Text, b)] -> [(Text, b)]
filterRepos = ((Text, b) -> Bool) -> [(Text, b)] -> [(Text, b)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Text
repoFilter Text -> Text -> Bool
`Text.isPrefixOf`) (Text -> Bool) -> ((Text, b) -> Text) -> (Text, b) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, b) -> Text
forall a b. (a, b) -> a
fst)


syncOrgSettings :: Bool -> GitHub.Auth -> Manager -> OrgSettings -> IO ()
syncOrgSettings :: Bool -> Auth -> Manager -> OrgSettings -> IO ()
syncOrgSettings Bool
dry Auth
auth Manager
mgr OrgSettings{Value
Text
HashMap Text Team
orgSettingsLogin :: OrgSettings -> Text
orgSettingsEditOrg :: Value
orgSettingsLogin :: Text
orgSettingsTeams :: HashMap Text Team
orgSettingsTeams :: OrgSettings -> HashMap Text Team
orgSettingsEditOrg :: OrgSettings -> Value
..} = do
  if Bool
dry
    then do
      Value
current <- Auth -> Manager -> Request 'RO Value -> IO Value
fetchOrNull Auth
auth Manager
mgr (Text -> Request 'RO Value
Orgs.getOrgR Text
orgSettingsLogin)
      String -> [(String, Value, Value)] -> IO ()
printDiff (String
"org " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
orgSettingsLogin) (String -> Value -> Value -> [(String, Value, Value)]
diffV String
"" Value
current (Value -> Value
forall a. ToJSON a => a -> Value
removeNulls Value
orgSettingsEditOrg))
    else Bool -> Auth -> Manager -> String -> Request 'RW Value -> IO ()
forall a.
FromJSON a =>
Bool -> Auth -> Manager -> String -> Request 'RW a -> IO ()
report Bool
dry Auth
auth Manager
mgr (String
"org " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
orgSettingsLogin)
           (Text -> Value -> Request 'RW Value
Orgs.editOrgR Text
orgSettingsLogin Value
orgSettingsEditOrg)
  Bool -> Auth -> Manager -> Text -> HashMap Text Team -> IO ()
syncOrgTeams Bool
dry Auth
auth Manager
mgr Text
orgSettingsLogin HashMap Text Team
orgSettingsTeams


syncOrgTeams :: Bool -> GitHub.Auth -> Manager -> Text -> HashMap Text Team -> IO ()
syncOrgTeams :: Bool -> Auth -> Manager -> Text -> HashMap Text Team -> IO ()
syncOrgTeams Bool
dry Auth
auth Manager
mgr Text
orgLogin HashMap Text Team
orgTeams = do
  Vector Team
teams <- Maybe Auth
-> Manager -> Request 'RO (Vector Team) -> IO (Vector Team)
forall a.
FromJSON a =>
Maybe Auth -> Manager -> Request 'RO a -> IO a
request (Auth -> Maybe Auth
forall a. a -> Maybe a
Just Auth
auth) Manager
mgr (Text -> Request 'RO (Vector Team)
Teams.getTeamsR Text
orgLogin)
  [(Text, Team)] -> ((Text, Team) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (HashMap Text Team -> [(Text, Team)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList HashMap Text Team
orgTeams) (((Text, Team) -> IO ()) -> IO ())
-> ((Text, Team) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Text
name, Team
team) -> do
    let namedTeam :: Team
namedTeam = Team
team
          { teamName = Just name
          , teamParentTeamId = teamParent team >>= teamName >>= getTeamId teams
          , teamMembers = Nothing  -- Synced later.
          }
    case Vector Team -> Text -> Maybe Int
getTeamId Vector Team
teams Text
name of
      Just{} | Bool
dry -> do
        Value
current <- Auth -> Manager -> Request 'RO Value -> IO Value
fetchOrNull Auth
auth Manager
mgr (Text -> Text -> Request 'RO Value
Teams.getTeamR Text
orgLogin Text
name)
        -- GitHub shows the parent, not parent_team_id, so the parent is compared by name.
        String -> [(String, Value, Value)] -> IO ()
printDiff (String
"team " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
name)
          (String -> Value -> Value -> [(String, Value, Value)]
diffV String
"" Value
current (Value -> Value
forall a. ToJSON a => a -> Value
removeNulls (Value -> Value) -> (Team -> Value) -> Team -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Team -> Value
forall a. ToJSON a => a -> Value
toJSON (Team -> Value) -> Team -> Value
forall a b. (a -> b) -> a -> b
$ Team
namedTeam{teamParentTeamId = Nothing}))
      Just{}  -> Bool -> Auth -> Manager -> String -> Request 'RW Value -> IO ()
forall a.
FromJSON a =>
Bool -> Auth -> Manager -> String -> Request 'RW a -> IO ()
report Bool
dry Auth
auth Manager
mgr (String
"team " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
name)
                   (Text -> Text -> Team -> Request 'RW Value
Teams.updateTeamR Text
orgLogin Text
name Team
namedTeam)
      Maybe Int
Nothing -> Bool -> Auth -> Manager -> String -> Request 'RW Value -> IO ()
forall a.
FromJSON a =>
Bool -> Auth -> Manager -> String -> Request 'RW a -> IO ()
report Bool
dry Auth
auth Manager
mgr (String
"new team " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
name)
                   (Text -> Team -> Request 'RW Value
Teams.createTeamR Text
orgLogin Team
namedTeam)
    Maybe (HashMap Text Text) -> (HashMap Text Text -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Team -> Maybe (HashMap Text Text)
teamMembers Team
team) ((HashMap Text Text -> IO ()) -> IO ())
-> (HashMap Text Text -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Bool
-> Auth -> Manager -> Text -> Text -> HashMap Text Text -> IO ()
syncTeamMembers Bool
dry Auth
auth Manager
mgr Text
orgLogin Text
name


syncTeamMembers :: Bool -> GitHub.Auth -> Manager -> Text -> Text -> HashMap Text Text -> IO ()
syncTeamMembers :: Bool
-> Auth -> Manager -> Text -> Text -> HashMap Text Text -> IO ()
syncTeamMembers Bool
dry Auth
auth Manager
mgr Text
orgLogin Text
team HashMap Text Text
teamMembers = do
  Vector User
currentMembers <- Maybe Auth
-> Manager -> Request 'RO (Vector User) -> IO (Vector User)
forall a.
FromJSON a =>
Maybe Auth -> Manager -> Request 'RO a -> IO a
request (Auth -> Maybe Auth
forall a. a -> Maybe a
Just Auth
auth) Manager
mgr (Text -> Text -> Request 'RO (Vector User)
Members.getMembersR Text
orgLogin Text
team)
  -- Remove members that are not in the settings.
  [User] -> (User -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Vector User -> [User]
forall a. Vector a -> [a]
V.toList Vector User
currentMembers) ((User -> IO ()) -> IO ()) -> (User -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \User{Text
userLogin :: Text
userLogin :: User -> Text
userLogin} ->
    case Text -> HashMap Text Text -> Maybe Text
forall k v. Hashable k => k -> HashMap k v -> Maybe v
HashMap.lookup Text
userLogin HashMap Text Text
teamMembers of
      Maybe Text
Nothing ->
        String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"Removing team member " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
userLogin String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" from " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
team
        -- mutate_ auth mgr (Members.deleteMemberR orgLogin team userLogin)
      Just{} -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  -- Add members that are in the settings but not in the team.
  [(Text, Text)] -> ((Text, Text) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (HashMap Text Text -> [(Text, Text)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList HashMap Text Text
teamMembers) (((Text, Text) -> IO ()) -> IO ())
-> ((Text, Text) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Text
login, Text
role) ->
    case (User -> Bool) -> Vector User -> Maybe User
forall a. (a -> Bool) -> Vector a -> Maybe a
V.find ((Text
login Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
==) (Text -> Bool) -> (User -> Text) -> User -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. User -> Text
userLogin) Vector User
currentMembers of
      Maybe User
Nothing ->
        Bool
-> Auth -> Manager -> String -> Request 'RW TeamMembership -> IO ()
forall a.
FromJSON a =>
Bool -> Auth -> Manager -> String -> Request 'RW a -> IO ()
report Bool
dry Auth
auth Manager
mgr (String
"add " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
role String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
login String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" to team " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
team)
          (Text
-> Text -> Text -> TeamMembership -> Request 'RW TeamMembership
Members.addMemberR Text
orgLogin Text
team Text
login (Text -> TeamMembership
TeamMembership Text
role))
      Just{} -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  -- Update roles of existing members if necessary.
  [(Text, Text)] -> ((Text, Text) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (HashMap Text Text -> [(Text, Text)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList HashMap Text Text
teamMembers) (((Text, Text) -> IO ()) -> IO ())
-> ((Text, Text) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Text
login, Text
role) ->
    case (User -> Bool) -> Vector User -> Maybe User
forall a. (a -> Bool) -> Vector a -> Maybe a
V.find ((Text
login Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
==) (Text -> Bool) -> (User -> Text) -> User -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. User -> Text
userLogin) Vector User
currentMembers of
      Maybe User
Nothing -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      Just{}  -> do
        TeamMembership
currentMembership <- Maybe Auth
-> Manager -> Request 'RO TeamMembership -> IO TeamMembership
forall a.
FromJSON a =>
Maybe Auth -> Manager -> Request 'RO a -> IO a
request (Auth -> Maybe Auth
forall a. a -> Maybe a
Just Auth
auth) Manager
mgr (Text -> Text -> Text -> Request 'RO TeamMembership
Members.getMembershipR Text
orgLogin Text
team Text
login)
        Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TeamMembership -> Text
teamMembershipRole TeamMembership
currentMembership Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
role) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
          Bool
-> Auth -> Manager -> String -> Request 'RW TeamMembership -> IO ()
forall a.
FromJSON a =>
Bool -> Auth -> Manager -> String -> Request 'RW a -> IO ()
report Bool
dry Auth
auth Manager
mgr (String
"role of " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
login String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" in team " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
team String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" to " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
role)
            (Text
-> Text -> Text -> TeamMembership -> Request 'RW TeamMembership
Members.addMemberR Text
orgLogin Text
team Text
login (Text -> TeamMembership
TeamMembership Text
role))


syncRepoSettings :: Bool -> GitHub.Auth -> Manager -> Text -> Text -> RepoSettings -> IO ()
syncRepoSettings :: Bool -> Auth -> Manager -> Text -> Text -> RepoSettings -> IO ()
syncRepoSettings Bool
dry Auth
auth Manager
mgr Text
orgLogin Text
repo RepoSettings{Maybe (HashMap Text Value)
Maybe (HashMap Text Ruleset)
Value
HashMap Text Label
repoSettingsEditRepo :: Value
repoSettingsBranches :: Maybe (HashMap Text Value)
repoSettingsRulesets :: Maybe (HashMap Text Ruleset)
repoSettingsLabels :: HashMap Text Label
repoSettingsLabels :: RepoSettings -> HashMap Text Label
repoSettingsRulesets :: RepoSettings -> Maybe (HashMap Text Ruleset)
repoSettingsBranches :: RepoSettings -> Maybe (HashMap Text Value)
repoSettingsEditRepo :: RepoSettings -> Value
..} = do
  if Bool
dry
    then do
      Value
current <- Auth -> Manager -> Request 'RO Value -> IO Value
fetchOrNull Auth
auth Manager
mgr (Text -> Text -> Request 'RO Value
Repos.getRepoR Text
orgLogin Text
repo)
      String -> [(String, Value, Value)] -> IO ()
printDiff (String
"repo " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
repo) (String -> Value -> Value -> [(String, Value, Value)]
diffV String
"" Value
current (Value -> Value
forall a. ToJSON a => a -> Value
removeNulls Value
repoSettingsEditRepo))
    else Bool -> Auth -> Manager -> String -> Request 'RW Value -> IO ()
forall a.
FromJSON a =>
Bool -> Auth -> Manager -> String -> Request 'RW a -> IO ()
report Bool
dry Auth
auth Manager
mgr (String
"repo " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
repo)
           (Text -> Text -> Value -> Request 'RW Value
Repos.editRepoR Text
orgLogin Text
repo Value
repoSettingsEditRepo)
  Bool
-> Auth -> Manager -> Text -> Text -> HashMap Text Label -> IO ()
syncLabels Bool
dry Auth
auth Manager
mgr Text
orgLogin Text
repo HashMap Text Label
repoSettingsLabels
  [(Text, Value)] -> ((Text, Value) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([(Text, Value)]
-> (HashMap Text Value -> [(Text, Value)])
-> Maybe (HashMap Text Value)
-> [(Text, Value)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] HashMap Text Value -> [(Text, Value)]
forall a. HashMap Text a -> [(Text, a)]
each Maybe (HashMap Text Value)
repoSettingsBranches) (((Text, Value) -> IO ()) -> IO ())
-> ((Text, Value) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Text
branch, Value
update) ->
    if Bool
dry
      then Auth -> Manager -> Text -> Text -> Text -> Value -> IO ()
diffBranchProtection Auth
auth Manager
mgr Text
orgLogin Text
repo Text
branch Value
update
      else Auth -> Manager -> Request 'RW Value -> IO Value
forall a. FromJSON a => Auth -> Manager -> Request 'RW a -> IO a
mutate Auth
auth Manager
mgr (Text -> Text -> Text -> Value -> Request 'RW Value
Branches.addProtectionR Text
orgLogin Text
repo Text
branch Value
update) IO Value -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  Bool
-> Auth
-> Manager
-> Text
-> Text
-> Maybe (HashMap Text Ruleset)
-> IO ()
syncRepoRulesets Bool
dry Auth
auth Manager
mgr Text
orgLogin Text
repo Maybe (HashMap Text Ruleset)
repoSettingsRulesets


diffBranchProtection :: GitHub.Auth -> Manager -> Text -> Text -> Text -> Value -> IO ()
diffBranchProtection :: Auth -> Manager -> Text -> Text -> Text -> Value -> IO ()
diffBranchProtection Auth
auth Manager
mgr Text
orgLogin Text
repo Text
branch Value
desired = do
  Either SomeException Value
res <- IO Value -> IO (Either SomeException Value)
forall e a. Exception e => IO a -> IO (Either e a)
try (Maybe Auth -> Manager -> Request 'RO Value -> IO Value
forall a.
FromJSON a =>
Maybe Auth -> Manager -> Request 'RO a -> IO a
request (Auth -> Maybe Auth
forall a. a -> Maybe a
Just Auth
auth) Manager
mgr (Text -> Text -> Text -> Request 'RO Value
Branches.getProtectionR Text
orgLogin Text
repo Text
branch))
  Value
current <- case Either SomeException Value
res :: Either SomeException Value of
    Left SomeException
e  -> String -> IO ()
putStrLn (String
"  (could not fetch current protection: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> SomeException -> String
forall a. Show a => a -> String
show SomeException
e String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
")") IO () -> IO Value -> IO Value
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Value -> IO Value
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Value
Null
    Right Value
v -> Value -> IO Value
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Value
v
  let cur :: [Text]
cur     = Value -> [Text]
contexts Value
current
      des :: [Text]
des     = Value -> [Text]
contexts Value
desired
      removed :: [Text]
removed = [Text]
cur [Text] -> [Text] -> [Text]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Text]
des
      added :: [Text]
added   = [Text]
des [Text] -> [Text] -> [Text]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Text]
cur
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
removed Bool -> Bool -> Bool
&& [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
added) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
    String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"branch protection " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
repo String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
branch String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
":"
    [Text] -> (Text -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Text]
removed ((Text -> IO ()) -> IO ()) -> (Text -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Text
c -> String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> String
red   (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"  - " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
c
    [Text] -> (Text -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Text]
added   ((Text -> IO ()) -> IO ()) -> (Text -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Text
c -> String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> String
green (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"  + " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
c
  where
    contexts :: Value -> [Text]
contexts (Object Object
o)
      | Just (Object Object
r) <- Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"required_status_checks" Object
o
      = [Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ Object -> [Text]
fromContexts Object
r [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> Object -> [Text]
fromChecks Object
r
    contexts Value
_ = []
    fromContexts :: Object -> [Text]
fromContexts Object
r = case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"contexts" Object
r of
      Just (Array Array
a) -> [Text
t | String Text
t <- Array -> [Value]
forall a. Vector a -> [a]
V.toList Array
a]
      Maybe Value
_              -> []
    fromChecks :: Object -> [Text]
fromChecks Object
r = case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"checks" Object
r of
      Just (Array Array
a) -> [Text
t | Object Object
c <- Array -> [Value]
forall a. Vector a -> [a]
V.toList Array
a, Just (String Text
t) <- [Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"context" Object
c]]
      Maybe Value
_              -> []


syncRepoRulesets :: Bool -> GitHub.Auth -> Manager -> Text -> Text -> Maybe (HashMap Text Ruleset) -> IO ()
syncRepoRulesets :: Bool
-> Auth
-> Manager
-> Text
-> Text
-> Maybe (HashMap Text Ruleset)
-> IO ()
syncRepoRulesets Bool
dry Auth
auth Manager
mgr Text
orgLogin Text
repo Maybe (HashMap Text Ruleset)
repoRulesets = do
  Vector Ruleset
rulesets <- Maybe Auth
-> Manager -> Request 'RO (Vector Ruleset) -> IO (Vector Ruleset)
forall a.
FromJSON a =>
Maybe Auth -> Manager -> Request 'RO a -> IO a
request (Auth -> Maybe Auth
forall a. a -> Maybe a
Just Auth
auth) Manager
mgr (Text -> Text -> Request 'RO (Vector Ruleset)
Rulesets.getRulesetsR Text
orgLogin Text
repo)
  [(Text, Ruleset)] -> ((Text, Ruleset) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([(Text, Ruleset)]
-> (HashMap Text Ruleset -> [(Text, Ruleset)])
-> Maybe (HashMap Text Ruleset)
-> [(Text, Ruleset)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] HashMap Text Ruleset -> [(Text, Ruleset)]
forall a. HashMap Text a -> [(Text, a)]
each Maybe (HashMap Text Ruleset)
repoRulesets) (((Text, Ruleset) -> IO ()) -> IO ())
-> ((Text, Ruleset) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Text
name, Ruleset
ruleset) -> do
    let namedRuleset :: Ruleset
namedRuleset = Ruleset
ruleset{rulesetName = Just name}
        hdr :: String
hdr = String
"ruleset " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
repo String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
name
    case Vector Ruleset -> Text -> Maybe Int
getRulesetId Vector Ruleset
rulesets Text
name of
      Just Int
rId
        | Bool
dry -> do
            Either SomeException Value
res <- IO Value -> IO (Either SomeException Value)
forall e a. Exception e => IO a -> IO (Either e a)
try (Maybe Auth -> Manager -> Request 'RO Value -> IO Value
forall a.
FromJSON a =>
Maybe Auth -> Manager -> Request 'RO a -> IO a
request (Auth -> Maybe Auth
forall a. a -> Maybe a
Just Auth
auth) Manager
mgr (Text -> Text -> Int -> Request 'RO Value
Rulesets.getRulesetR Text
orgLogin Text
repo Int
rId))
            let current :: Value
current = (SomeException -> Value)
-> (Value -> Value) -> Either SomeException Value -> Value
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Value -> SomeException -> Value
forall a b. a -> b -> a
const Value
Null) Value -> Value
forall a. a -> a
id (Either SomeException Value
res :: Either SomeException Value)
            String -> [(String, Value, Value)] -> IO ()
printDiff String
hdr (String -> Value -> Value -> [(String, Value, Value)]
diffV String
"" Value
current (Ruleset -> Value
forall a. ToJSON a => a -> Value
removeNulls Ruleset
namedRuleset))
        | Bool
otherwise -> Auth -> Manager -> Request 'RW Value -> IO Value
forall a. FromJSON a => Auth -> Manager -> Request 'RW a -> IO a
mutate Auth
auth Manager
mgr (Text -> Text -> Int -> Ruleset -> Request 'RW Value
Rulesets.updateRulesetR Text
orgLogin Text
repo Int
rId Ruleset
namedRuleset) IO Value -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      Maybe Int
Nothing
        | Bool
dry       -> String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> String
green (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"+ would create " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
hdr
        | Bool
otherwise -> Auth -> Manager -> Request 'RW Value -> IO Value
forall a. FromJSON a => Auth -> Manager -> Request 'RW a -> IO a
mutate Auth
auth Manager
mgr (Text -> Text -> Ruleset -> Request 'RW Value
Rulesets.addRulesetR Text
orgLogin Text
repo Ruleset
namedRuleset) IO Value -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()


syncLabels :: Bool -> GitHub.Auth -> Manager -> Text -> Text -> HashMap Text Label -> IO ()
syncLabels :: Bool
-> Auth -> Manager -> Text -> Text -> HashMap Text Label -> IO ()
syncLabels Bool
dry Auth
auth Manager
mgr Text
orgLogin Text
repo HashMap Text Label
labels = do
  let newLabels :: [(Text, Label)]
newLabels = [(Text, Label)] -> [(Text, Label)]
forall a. Eq a => [a] -> [a]
nub ([(Text, Label)] -> [(Text, Label)])
-> (HashMap Text Label -> [(Text, Label)])
-> HashMap Text Label
-> [(Text, Label)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Text, Label) -> (Text, Label))
-> [(Text, Label)] -> [(Text, Label)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Text
name, Label
label) -> (Text
name, Label
label{labelName = Just name})) ([(Text, Label)] -> [(Text, Label)])
-> (HashMap Text Label -> [(Text, Label)])
-> HashMap Text Label
-> [(Text, Label)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap Text Label -> [(Text, Label)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList (HashMap Text Label -> [(Text, Label)])
-> HashMap Text Label -> [(Text, Label)]
forall a b. (a -> b) -> a -> b
$ HashMap Text Label
labels
  [(Text, Label)]
oldLabels <- [(Text, Label)] -> [(Text, Label)]
forall a. Eq a => [a] -> [a]
nub ([(Text, Label)] -> [(Text, Label)])
-> (Vector Label -> [(Text, Label)])
-> Vector Label
-> [(Text, Label)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Label -> (Text, Label)) -> [Label] -> [(Text, Label)]
forall a b. (a -> b) -> [a] -> [b]
map (\label :: Label
label@Label{Maybe Text
labelName :: Label -> Maybe Text
labelName :: Maybe Text
labelName} -> (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" Maybe Text
labelName, Label
label)) ([Label] -> [(Text, Label)])
-> (Vector Label -> [Label]) -> Vector Label -> [(Text, Label)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Label -> [Label]
forall a. Vector a -> [a]
V.toList
    (Vector Label -> [(Text, Label)])
-> IO (Vector Label) -> IO [(Text, Label)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Auth
-> Manager -> Request 'RO (Vector Label) -> IO (Vector Label)
forall a.
FromJSON a =>
Maybe Auth -> Manager -> Request 'RO a -> IO a
request (Auth -> Maybe Auth
forall a. a -> Maybe a
Just Auth
auth) Manager
mgr (Text -> Text -> Request 'RO (Vector Label)
Labels.getLabelsR Text
orgLogin Text
repo)
  let changes :: [(Text, Label)]
changes = [(Text, Label)]
newLabels [(Text, Label)] -> [(Text, Label)] -> [(Text, Label)]
forall a. Eq a => [a] -> [a] -> [a]
\\ [(Text, Label)]
oldLabels
      isUpdate :: Text -> Bool
isUpdate Text
lblName = ((Text, Label) -> Bool) -> [(Text, Label)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Text
lblName Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
==) (Text -> Bool) -> ((Text, Label) -> Text) -> (Text, Label) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Label) -> Text
forall a b. (a, b) -> a
fst) [(Text, Label)]
oldLabels
  if Bool
dry
    then Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([(Text, Label)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Text, Label)]
changes) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"labels " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
repo String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
":"
      [(Text, Label)] -> ((Text, Label) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Text, Label)]
changes (((Text, Label) -> IO ()) -> IO ())
-> ((Text, Label) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Text
lblName, Label
_) ->
        String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> String
green (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> (if Text -> Bool
isUpdate Text
lblName then String
"~ " else String
"+ ") String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
lblName
    else do
      [(Text, Label)] -> ((Text, Label) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([(Text, Label)]
oldLabels [(Text, Label)] -> [(Text, Label)] -> [(Text, Label)]
forall a. Eq a => [a] -> [a] -> [a]
\\ [(Text, Label)]
newLabels) (((Text, Label) -> IO ()) -> IO ())
-> ((Text, Label) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Text
lblName, Label
lbl) ->
        if Bool
delete
          then Bool
-> Auth -> Manager -> String -> GenRequest 'MtUnit 'RW () -> IO ()
report_ Bool
dry Auth
auth Manager
mgr (String
"label " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
repo String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
lblName)
                 (Text -> Text -> Text -> GenRequest 'MtUnit 'RW ()
Labels.deleteLabelR Text
orgLogin Text
repo Text
lblName)
          else String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"NOT deleting old label: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Label -> String
forall a. Show a => a -> String
show Label
lbl
      [(Text, Label)] -> ((Text, Label) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Text, Label)]
changes (((Text, Label) -> IO ()) -> IO ())
-> ((Text, Label) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Text
lblName, Label
lbl) ->
        if Text -> Bool
isUpdate Text
lblName
          then Bool -> Auth -> Manager -> String -> Request 'RW Label -> IO ()
forall a.
FromJSON a =>
Bool -> Auth -> Manager -> String -> Request 'RW a -> IO ()
report Bool
dry Auth
auth Manager
mgr (String
"label " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
repo String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
lblName)
                 (Text -> Text -> Text -> Label -> Request 'RW Label
Labels.updateLabelR Text
orgLogin Text
repo Text
lblName Label
lbl)
          else Bool -> Auth -> Manager -> String -> Request 'RW Label -> IO ()
forall a.
FromJSON a =>
Bool -> Auth -> Manager -> String -> Request 'RW a -> IO ()
report Bool
dry Auth
auth Manager
mgr (String
"new label " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
repo String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
lblName)
                 (Text -> Text -> Label -> Request 'RW Label
Labels.createLabelR Text
orgLogin Text
repo Label
lbl)


validateSettings :: MonadFail m => HashMap Text RepoSettings -> m ()
validateSettings :: forall (m :: * -> *).
MonadFail m =>
HashMap Text RepoSettings -> m ()
validateSettings HashMap Text RepoSettings
repos = do
  HashMap Text Value
commonBranches <- case Text -> HashMap Text RepoSettings -> Maybe RepoSettings
forall k v. Hashable k => k -> HashMap k v -> Maybe v
HashMap.lookup Text
"_common" HashMap Text RepoSettings
repos Maybe RepoSettings
-> (RepoSettings -> Maybe (HashMap Text Value))
-> Maybe (HashMap Text Value)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= RepoSettings -> Maybe (HashMap Text Value)
repoSettingsBranches of
    Maybe (HashMap Text Value)
Nothing -> String -> m (HashMap Text Value)
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"no _common section found"
    Just HashMap Text Value
ok -> HashMap Text Value -> m (HashMap Text Value)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return HashMap Text Value
ok
  [Text]
commonContexts <- case Text -> HashMap Text Value -> Maybe Value
forall k v. Hashable k => k -> HashMap k v -> Maybe v
HashMap.lookup Text
"master" HashMap Text Value
commonBranches of
    Maybe Value
Nothing -> String -> m [Text]
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"no \"master\" branch in _common section found"
    Just Value
ok -> Text -> Text -> Value -> m [Text]
forall {m :: * -> *}.
MonadFail m =>
Text -> Text -> Value -> m [Text]
getContexts Text
"_common" Text
"master" (Value -> m [Text]) -> m Value -> m [Text]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Text -> Text -> Value -> m Value
forall {m :: * -> *}.
MonadFail m =>
Text -> Text -> Value -> m Value
getRequiredStatusChecks Text
"_common" Text
"master" Value
ok
  -- Check that each repo's branch protection contexts start with the common ones.
  [(Text, RepoSettings)] -> ((Text, RepoSettings) -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([(Text, RepoSettings)] -> [(Text, RepoSettings)]
forall {b}. [(Text, b)] -> [(Text, b)]
filterRepos ([(Text, RepoSettings)] -> [(Text, RepoSettings)])
-> (HashMap Text RepoSettings -> [(Text, RepoSettings)])
-> HashMap Text RepoSettings
-> [(Text, RepoSettings)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap Text RepoSettings -> [(Text, RepoSettings)]
forall a. HashMap Text a -> [(Text, a)]
each (HashMap Text RepoSettings -> [(Text, RepoSettings)])
-> HashMap Text RepoSettings -> [(Text, RepoSettings)]
forall a b. (a -> b) -> a -> b
$ HashMap Text RepoSettings
repos) (((Text, RepoSettings) -> m ()) -> m ())
-> ((Text, RepoSettings) -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \(Text
repo, RepoSettings{Maybe (HashMap Text Value)
Maybe (HashMap Text Ruleset)
Value
HashMap Text Label
repoSettingsLabels :: RepoSettings -> HashMap Text Label
repoSettingsRulesets :: RepoSettings -> Maybe (HashMap Text Ruleset)
repoSettingsBranches :: RepoSettings -> Maybe (HashMap Text Value)
repoSettingsEditRepo :: RepoSettings -> Value
repoSettingsEditRepo :: Value
repoSettingsBranches :: Maybe (HashMap Text Value)
repoSettingsRulesets :: Maybe (HashMap Text Ruleset)
repoSettingsLabels :: HashMap Text Label
..}) ->
    [(Text, Value)] -> ((Text, Value) -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([(Text, Value)]
-> (HashMap Text Value -> [(Text, Value)])
-> Maybe (HashMap Text Value)
-> [(Text, Value)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] HashMap Text Value -> [(Text, Value)]
forall a. HashMap Text a -> [(Text, a)]
each Maybe (HashMap Text Value)
repoSettingsBranches) (((Text, Value) -> m ()) -> m ())
-> ((Text, Value) -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \(Text
branch, Value
update) -> do
      [Text]
contexts <- Text -> Text -> Value -> m [Text]
forall {m :: * -> *}.
MonadFail m =>
Text -> Text -> Value -> m [Text]
getContexts Text
repo Text
branch (Value -> m [Text]) -> m Value -> m [Text]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Text -> Text -> Value -> m Value
forall {m :: * -> *}.
MonadFail m =>
Text -> Text -> Value -> m Value
getRequiredStatusChecks Text
repo Text
branch Value
update
      let ctx :: Text
ctx = Text
repo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".branches." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
branch Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".required_status_checks.contexts"
      Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Text]
commonContexts [Text] -> [Text] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` [Text]
contexts) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
        String -> m ()
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m ()) -> (Text -> String) -> Text -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ Text
ctx Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" should start with " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack ([Text] -> String
forall a. Show a => a -> String
show [Text]
commonContexts)
      let dups :: [Text]
dups = [Text]
contexts [Text] -> [Text] -> [Text]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub [Text]
contexts
      Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
dups) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
        String -> m ()
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m ()) -> (Text -> String) -> Text -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ Text
ctx Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" has duplicates: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack ([Text] -> String
forall a. Show a => a -> String
show [Text]
dups)

  where
    filterRepos :: [(Text, b)] -> [(Text, b)]
filterRepos = ((Text, b) -> Bool) -> [(Text, b)] -> [(Text, b)]
forall a. (a -> Bool) -> [a] -> [a]
filter (((Text, b) -> Bool) -> [(Text, b)] -> [(Text, b)])
-> ((Text, b) -> Bool) -> [(Text, b)] -> [(Text, b)]
forall a b. (a -> b) -> a -> b
$ (Text
"experimental" Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/=) (Text -> Bool) -> ((Text, b) -> Text) -> (Text, b) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, b) -> Text
forall a b. (a, b) -> a
fst
    getRequiredStatusChecks :: Text -> Text -> Value -> m Value
getRequiredStatusChecks Text
repo Text
branch (Object Object
mems) =
      case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"required_status_checks" Object
mems of
        Maybe Value
Nothing -> String -> m Value
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m Value) -> (Text -> String) -> Text -> m Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack (Text -> m Value) -> Text -> m Value
forall a b. (a -> b) -> a -> b
$ Text
repo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".branches." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
branch Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" should contain required_status_checks"
        Just Value
ok -> Value -> m Value
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Value
ok
    getRequiredStatusChecks Text
repo Text
branch Value
_ =
      String -> m Value
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m Value) -> (Text -> String) -> Text -> m Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack (Text -> m Value) -> Text -> m Value
forall a b. (a -> b) -> a -> b
$ Text
repo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".branches." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
branch Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" should be an object"

    getContexts :: Text -> Text -> Value -> m [Text]
getContexts Text
repo Text
branch (Object Object
mems) =
      case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"contexts" Object
mems of
        Just (Array Array
arr) -> [Text] -> m [Text]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Text] -> m [Text]) -> [Text] -> m [Text]
forall a b. (a -> b) -> a -> b
$ (Value -> Maybe Text) -> [Value] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Value -> Maybe Text
toString ([Value] -> [Text]) -> [Value] -> [Text]
forall a b. (a -> b) -> a -> b
$ Array -> [Value]
forall a. Vector a -> [a]
V.toList Array
arr
        Just Value
_ -> String -> m [Text]
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m [Text]) -> (Text -> String) -> Text -> m [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack (Text -> m [Text]) -> Text -> m [Text]
forall a b. (a -> b) -> a -> b
$ Text
repo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".branches." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
branch Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".required_status_checks.contexts should be an array"
        Maybe Value
Nothing -> String -> m [Text]
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m [Text]) -> (Text -> String) -> Text -> m [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack (Text -> m [Text]) -> Text -> m [Text]
forall a b. (a -> b) -> a -> b
$ Text
repo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".branches." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
branch Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".required_status_checks should contain contexts"
    getContexts Text
repo Text
branch Value
_ =
      String -> m [Text]
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m [Text]) -> (Text -> String) -> Text -> m [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack (Text -> m [Text]) -> Text -> m [Text]
forall a b. (a -> b) -> a -> b
$ Text
repo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".branches." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
branch Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".required_status_checks should be an object"

    toString :: Value -> Maybe Text
toString (String Text
str) = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
str
    toString Value
_            = Maybe Text
forall a. Maybe a
Nothing

each :: HashMap Text a -> [(Text, a)]
each :: forall a. HashMap Text a -> [(Text, a)]
each = HashMap Text a -> [(Text, a)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList (HashMap Text a -> [(Text, a)])
-> (HashMap Text a -> HashMap Text a)
-> HashMap Text a
-> [(Text, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> a -> Bool) -> HashMap Text a -> HashMap Text a
forall k v. (k -> v -> Bool) -> HashMap k v -> HashMap k v
HashMap.filterWithKey (\Text
k a
_ -> Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text
"_" Text -> Text -> Bool
`Text.isPrefixOf` Text
k)