module Fedora.Koji
       ( BuildID(..)
       , BuildInfo(..)
       , buildIDInfo
       , fedoraKojiHub
       , centosKojiHub
       , kojiBuildTags
       , kojiBuildTarget
       , kojiGetBuildID
       , kojiGetBuildState
       , kojiGetBuildTaskID
       , kojiGetCurrentRepo
       , kojiGetRepo
       , kojiGetTaskInfo
       , kojiGetTaskChildren
       , kojiGetTaskState
       , kojiGetUserID
       , kojiLatestBuild
       , kojiLatestBuildRepo
       , kojiListSideTags
       , kojiListTaskIDs
       , kojiUserBuildTasks
       , KojiBuild(..)
       , kojiListTaggedBuilds
       , PackageID(..)
       , TagID(..)
       , TaskID(..)
       , UserID(..)
       , displayID
       , getID
       , readID
       , readID'
       , TaskState(..)
       , getTaskState
       , openTaskStates
       , openTaskValues
       , readTaskState
       , BuildState(..)
       , readBuildState
       , Struct
       , lookupStruct
       , Value (..)
       , getInt
       , getString
       , maybeVal
       , RepoState(..)
       , readRepoState
       )
where

import Data.List
import Data.Maybe
import Network.XmlRpc.Internals

import Fedora.Koji.Internal

class ID a where
  getID :: a -> Int
  mkID :: Int -> a

displayID :: ID a => a -> String
displayID :: a -> String
displayID = Int -> String
forall a. Show a => a -> String
show (Int -> String) -> (a -> Int) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
forall a. ID a => a -> Int
getID

-- FIXME rename to structID ?
readID :: ID a => Struct -> Maybe a
readID :: Struct -> Maybe a
readID Struct
st = String -> Struct -> Maybe Value
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"id" Struct
st Maybe Value -> (Value -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Int -> a) -> Maybe Int -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> a
forall a. ID a => Int -> a
mkID (Maybe Int -> Maybe a) -> (Value -> Maybe Int) -> Value -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Maybe Int
getInt

readID' :: Struct -> Maybe Int
readID' :: Struct -> Maybe Int
readID' Struct
st = String -> Struct -> Maybe Value
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"id" Struct
st Maybe Value -> (Value -> Maybe Int) -> Maybe Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> Maybe Int
getInt

-- FIXME rename to valueInt ?
getInt :: Value -> Maybe Int
getInt :: Value -> Maybe Int
getInt (ValueInt Int
i) = Int -> Maybe Int
forall a. a -> Maybe a
Just Int
i
getInt Value
_ = Maybe Int
forall a. Maybe a
Nothing

getString :: Value -> Maybe String
getString :: Value -> Maybe String
getString (ValueString String
i) = String -> Maybe String
forall a. a -> Maybe a
Just String
i
getString Value
_ = Maybe String
forall a. Maybe a
Nothing

newtype TaskID = TaskId Int
  deriving Int -> TaskID -> ShowS
[TaskID] -> ShowS
TaskID -> String
(Int -> TaskID -> ShowS)
-> (TaskID -> String) -> ([TaskID] -> ShowS) -> Show TaskID
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TaskID] -> ShowS
$cshowList :: [TaskID] -> ShowS
show :: TaskID -> String
$cshow :: TaskID -> String
showsPrec :: Int -> TaskID -> ShowS
$cshowsPrec :: Int -> TaskID -> ShowS
Show

instance ID TaskID where
  getID :: TaskID -> Int
getID (TaskId Int
i) = Int
i
  mkID :: Int -> TaskID
mkID = Int -> TaskID
TaskId

newtype TagID = TagId Int
  deriving Int -> TagID -> ShowS
[TagID] -> ShowS
TagID -> String
(Int -> TagID -> ShowS)
-> (TagID -> String) -> ([TagID] -> ShowS) -> Show TagID
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TagID] -> ShowS
$cshowList :: [TagID] -> ShowS
show :: TagID -> String
$cshow :: TagID -> String
showsPrec :: Int -> TagID -> ShowS
$cshowsPrec :: Int -> TagID -> ShowS
Show

instance ID TagID where
  getID :: TagID -> Int
getID (TagId Int
i) = Int
i
  mkID :: Int -> TagID
mkID = Int -> TagID
TagId

newtype UserID = UserId Int
  deriving Int -> UserID -> ShowS
[UserID] -> ShowS
UserID -> String
(Int -> UserID -> ShowS)
-> (UserID -> String) -> ([UserID] -> ShowS) -> Show UserID
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UserID] -> ShowS
$cshowList :: [UserID] -> ShowS
show :: UserID -> String
$cshow :: UserID -> String
showsPrec :: Int -> UserID -> ShowS
$cshowsPrec :: Int -> UserID -> ShowS
Show

instance ID UserID where
  getID :: UserID -> Int
getID (UserId Int
i) = Int
i
  mkID :: Int -> UserID
mkID = Int -> UserID
UserId

newtype BuildID = BuildId Int

instance ID BuildID where
  getID :: BuildID -> Int
getID (BuildId Int
i) = Int
i
  mkID :: Int -> BuildID
mkID = Int -> BuildID
BuildId

newtype PackageID = PackageId Int

instance ID PackageID where
  getID :: PackageID -> Int
getID (PackageId Int
i) = Int
i
  mkID :: Int -> PackageID
mkID = Int -> PackageID
PackageId

newtype BuildrootID = BuildrootId Int

instance ID BuildrootID where
  getID :: BuildrootID -> Int
getID (BuildrootId Int
i) = Int
i
  mkID :: Int -> BuildrootID
mkID = Int -> BuildrootID
BuildrootId

data BuildInfo = BuildInfoID Int | BuildInfoNVR String

buildInfo :: BuildInfo -> Info
buildInfo :: BuildInfo -> Info
buildInfo (BuildInfoID Int
bid) = Int -> Info
InfoID Int
bid
buildInfo (BuildInfoNVR String
nvr) = String -> Info
InfoString String
nvr

buildIDInfo :: BuildID -> BuildInfo
buildIDInfo :: BuildID -> BuildInfo
buildIDInfo (BuildId Int
bid) = Int -> BuildInfo
BuildInfoID Int
bid

fedoraKojiHub :: String
fedoraKojiHub :: String
fedoraKojiHub = String
"https://koji.fedoraproject.org/kojihub"

centosKojiHub :: String
centosKojiHub :: String
centosKojiHub = String
"https://koji.mbox.centos.org/kojihub"

kojiGetBuildID :: String -- ^ hub url
               -> String -- ^ NVR
               -> IO (Maybe BuildID)
kojiGetBuildID :: String -> String -> IO (Maybe BuildID)
kojiGetBuildID String
hubUrl String
nvr =
  (((Int -> BuildID) -> Maybe Int -> Maybe BuildID
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> BuildID
BuildId (Maybe Int -> Maybe BuildID)
-> (Struct -> Maybe Int) -> Struct -> Maybe BuildID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Struct -> Maybe Int
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"id") (Struct -> Maybe BuildID) -> Maybe Struct -> Maybe BuildID
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Maybe Struct -> Maybe BuildID)
-> IO (Maybe Struct) -> IO (Maybe BuildID)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Info -> IO (Maybe Struct)
getBuild String
hubUrl (String -> Info
InfoString String
nvr)

kojiGetBuildTaskID :: String -- ^ hub url
                   -> String -- ^ NVR
                   -> IO (Maybe TaskID)
kojiGetBuildTaskID :: String -> String -> IO (Maybe TaskID)
kojiGetBuildTaskID String
hubUrl String
nvr =
  (((Int -> TaskID) -> Maybe Int -> Maybe TaskID
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> TaskID
TaskId (Maybe Int -> Maybe TaskID)
-> (Struct -> Maybe Int) -> Struct -> Maybe TaskID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Struct -> Maybe Int
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"task_id") (Struct -> Maybe TaskID) -> Maybe Struct -> Maybe TaskID
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Maybe Struct -> Maybe TaskID)
-> IO (Maybe Struct) -> IO (Maybe TaskID)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Info -> IO (Maybe Struct)
getBuild String
hubUrl (String -> Info
InfoString String
nvr)

kojiListTaskIDs :: String
                -> Struct -- ^ opts
                -> Struct -- ^ qopts
                -> IO [TaskID]
kojiListTaskIDs :: String -> Struct -> Struct -> IO [TaskID]
kojiListTaskIDs String
hubUrl Struct
opts Struct
qopts =
  (Struct -> Maybe TaskID) -> [Struct] -> [TaskID]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Struct -> Maybe TaskID
forall a. ID a => Struct -> Maybe a
readID ([Struct] -> [TaskID]) -> IO [Struct] -> IO [TaskID]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Struct -> Struct -> IO [Struct]
listTasks String
hubUrl Struct
opts Struct
qopts

kojiUserBuildTasks :: String -> UserID -> Maybe String -> Maybe String -> IO [TaskID]
kojiUserBuildTasks :: String -> UserID -> Maybe String -> Maybe String -> IO [TaskID]
kojiUserBuildTasks String
hubUrl UserID
userid Maybe String
msource Maybe String
mtarget = do
  [Struct]
tasks <- String -> Struct -> Struct -> IO [Struct]
listTasks String
hubUrl [(String
"owner",Int -> Value
ValueInt (UserID -> Int
forall a. ID a => a -> Int
getID UserID
userid)),(String
"method",String -> Value
ValueString String
"build"),(String
"state",Value
openTaskValues)] []
  [TaskID] -> IO [TaskID]
forall (m :: * -> *) a. Monad m => a -> m a
return ([TaskID] -> IO [TaskID]) -> [TaskID] -> IO [TaskID]
forall a b. (a -> b) -> a -> b
$ (Int -> TaskID) -> [Int] -> [TaskID]
forall a b. (a -> b) -> [a] -> [b]
map Int -> TaskID
TaskId ([Int] -> [TaskID]) -> ([Struct] -> [Int]) -> [Struct] -> [TaskID]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Struct -> Maybe Int) -> [Struct] -> [Int]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (String -> Struct -> Maybe Int
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"id") ([Struct] -> [TaskID]) -> [Struct] -> [TaskID]
forall a b. (a -> b) -> a -> b
$ (Struct -> Bool) -> [Struct] -> [Struct]
forall a. (a -> Bool) -> [a] -> [a]
filter Struct -> Bool
isTheBuild [Struct]
tasks
  where
    isTheBuild :: Struct -> Bool
    isTheBuild :: Struct -> Bool
isTheBuild Struct
st =
      let mreq :: Maybe String
mreq = String -> Struct -> Maybe String
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"request" Struct
st in
        case Maybe String
mreq of
          Maybe String
Nothing -> Bool
False
          Just String
req ->
            Bool -> (String -> Bool) -> Maybe String -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` String
req) Maybe String
msource Bool -> Bool -> Bool
&&
            Bool -> (String -> Bool) -> Maybe String -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (\ String
target -> (String
"<value><string>" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
target String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"</string></value>") String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` String
req) Maybe String
mtarget

-- getTagID :: String -- ^ tag
--          -> IO TagID
-- getTagID tag =
--   TagId <$> koji "getTagID" tag

kojiGetUserID :: String -> String -> IO (Maybe UserID)
kojiGetUserID :: String -> String -> IO (Maybe UserID)
kojiGetUserID String
hubUrl String
name = do
  Maybe Struct
res <- String -> Info -> Bool -> IO (Maybe Struct)
getUser String
hubUrl (String -> Info
InfoString String
name) Bool
False
  Maybe UserID -> IO (Maybe UserID)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe UserID -> IO (Maybe UserID))
-> Maybe UserID -> IO (Maybe UserID)
forall a b. (a -> b) -> a -> b
$ Struct -> Maybe UserID
forall a. ID a => Struct -> Maybe a
readID (Struct -> Maybe UserID) -> Maybe Struct -> Maybe UserID
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe Struct
res

kojiBuildTags :: String -> BuildInfo -> IO [String]
kojiBuildTags :: String -> BuildInfo -> IO [String]
kojiBuildTags String
hubUrl BuildInfo
buildinfo = do
  [Struct]
lst <- String -> Maybe Info -> Maybe Info -> Bool -> IO [Struct]
listTags String
hubUrl (Info -> Maybe Info
forall a. a -> Maybe a
Just (BuildInfo -> Info
buildInfo BuildInfo
buildinfo)) Maybe Info
forall a. Maybe a
Nothing Bool
False
  [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> IO [String]) -> [String] -> IO [String]
forall a b. (a -> b) -> a -> b
$ (Struct -> Maybe String) -> [Struct] -> [String]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (String -> Struct -> Maybe String
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"name") [Struct]
lst

data BuildState = BuildBuilding | BuildComplete | BuildDeleted | BuildFailed | BuildCanceled
  deriving (BuildState -> BuildState -> Bool
(BuildState -> BuildState -> Bool)
-> (BuildState -> BuildState -> Bool) -> Eq BuildState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BuildState -> BuildState -> Bool
$c/= :: BuildState -> BuildState -> Bool
== :: BuildState -> BuildState -> Bool
$c== :: BuildState -> BuildState -> Bool
Eq, Int -> BuildState
BuildState -> Int
BuildState -> [BuildState]
BuildState -> BuildState
BuildState -> BuildState -> [BuildState]
BuildState -> BuildState -> BuildState -> [BuildState]
(BuildState -> BuildState)
-> (BuildState -> BuildState)
-> (Int -> BuildState)
-> (BuildState -> Int)
-> (BuildState -> [BuildState])
-> (BuildState -> BuildState -> [BuildState])
-> (BuildState -> BuildState -> [BuildState])
-> (BuildState -> BuildState -> BuildState -> [BuildState])
-> Enum BuildState
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: BuildState -> BuildState -> BuildState -> [BuildState]
$cenumFromThenTo :: BuildState -> BuildState -> BuildState -> [BuildState]
enumFromTo :: BuildState -> BuildState -> [BuildState]
$cenumFromTo :: BuildState -> BuildState -> [BuildState]
enumFromThen :: BuildState -> BuildState -> [BuildState]
$cenumFromThen :: BuildState -> BuildState -> [BuildState]
enumFrom :: BuildState -> [BuildState]
$cenumFrom :: BuildState -> [BuildState]
fromEnum :: BuildState -> Int
$cfromEnum :: BuildState -> Int
toEnum :: Int -> BuildState
$ctoEnum :: Int -> BuildState
pred :: BuildState -> BuildState
$cpred :: BuildState -> BuildState
succ :: BuildState -> BuildState
$csucc :: BuildState -> BuildState
Enum, Int -> BuildState -> ShowS
[BuildState] -> ShowS
BuildState -> String
(Int -> BuildState -> ShowS)
-> (BuildState -> String)
-> ([BuildState] -> ShowS)
-> Show BuildState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BuildState] -> ShowS
$cshowList :: [BuildState] -> ShowS
show :: BuildState -> String
$cshow :: BuildState -> String
showsPrec :: Int -> BuildState -> ShowS
$cshowsPrec :: Int -> BuildState -> ShowS
Show)

readBuildState :: Value -> BuildState
readBuildState :: Value -> BuildState
readBuildState (ValueInt Int
i) | Int
i Int -> [Int] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (BuildState -> Int) -> [BuildState] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map BuildState -> Int
forall a. Enum a => a -> Int
fromEnum (BuildState -> [BuildState]
forall a. Enum a => a -> [a]
enumFrom BuildState
BuildBuilding) = Int -> BuildState
forall a. Enum a => Int -> a
toEnum Int
i
readBuildState Value
_ = String -> BuildState
forall a. HasCallStack => String -> a
error String
"invalid build state"

kojiGetBuildState :: String -> BuildInfo -> IO (Maybe BuildState)
kojiGetBuildState :: String -> BuildInfo -> IO (Maybe BuildState)
kojiGetBuildState String
hubUrl BuildInfo
buildinfo =
  (((Value -> BuildState) -> Maybe Value -> Maybe BuildState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value -> BuildState
readBuildState (Maybe Value -> Maybe BuildState)
-> (Struct -> Maybe Value) -> Struct -> Maybe BuildState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Struct -> Maybe Value
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"state") (Struct -> Maybe BuildState) -> Maybe Struct -> Maybe BuildState
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Maybe Struct -> Maybe BuildState)
-> IO (Maybe Struct) -> IO (Maybe BuildState)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
  String -> Info -> IO (Maybe Struct)
getBuild String
hubUrl (BuildInfo -> Info
buildInfo BuildInfo
buildinfo)

data TaskState = TaskFree | TaskOpen | TaskClosed | TaskCanceled | TaskAssigned | TaskFailed
  deriving (TaskState -> TaskState -> Bool
(TaskState -> TaskState -> Bool)
-> (TaskState -> TaskState -> Bool) -> Eq TaskState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TaskState -> TaskState -> Bool
$c/= :: TaskState -> TaskState -> Bool
== :: TaskState -> TaskState -> Bool
$c== :: TaskState -> TaskState -> Bool
Eq, Int -> TaskState
TaskState -> Int
TaskState -> [TaskState]
TaskState -> TaskState
TaskState -> TaskState -> [TaskState]
TaskState -> TaskState -> TaskState -> [TaskState]
(TaskState -> TaskState)
-> (TaskState -> TaskState)
-> (Int -> TaskState)
-> (TaskState -> Int)
-> (TaskState -> [TaskState])
-> (TaskState -> TaskState -> [TaskState])
-> (TaskState -> TaskState -> [TaskState])
-> (TaskState -> TaskState -> TaskState -> [TaskState])
-> Enum TaskState
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: TaskState -> TaskState -> TaskState -> [TaskState]
$cenumFromThenTo :: TaskState -> TaskState -> TaskState -> [TaskState]
enumFromTo :: TaskState -> TaskState -> [TaskState]
$cenumFromTo :: TaskState -> TaskState -> [TaskState]
enumFromThen :: TaskState -> TaskState -> [TaskState]
$cenumFromThen :: TaskState -> TaskState -> [TaskState]
enumFrom :: TaskState -> [TaskState]
$cenumFrom :: TaskState -> [TaskState]
fromEnum :: TaskState -> Int
$cfromEnum :: TaskState -> Int
toEnum :: Int -> TaskState
$ctoEnum :: Int -> TaskState
pred :: TaskState -> TaskState
$cpred :: TaskState -> TaskState
succ :: TaskState -> TaskState
$csucc :: TaskState -> TaskState
Enum, Int -> TaskState -> ShowS
[TaskState] -> ShowS
TaskState -> String
(Int -> TaskState -> ShowS)
-> (TaskState -> String)
-> ([TaskState] -> ShowS)
-> Show TaskState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TaskState] -> ShowS
$cshowList :: [TaskState] -> ShowS
show :: TaskState -> String
$cshow :: TaskState -> String
showsPrec :: Int -> TaskState -> ShowS
$cshowsPrec :: Int -> TaskState -> ShowS
Show)

openTaskStates :: [TaskState]
openTaskStates :: [TaskState]
openTaskStates = [TaskState
TaskFree, TaskState
TaskOpen, TaskState
TaskAssigned]

openTaskValues :: Value
openTaskValues :: Value
openTaskValues = [Value] -> Value
ValueArray ([Value] -> Value) -> [Value] -> Value
forall a b. (a -> b) -> a -> b
$ (TaskState -> Value) -> [TaskState] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map TaskState -> Value
taskStateToValue [TaskState]
openTaskStates
  where
    taskStateToValue :: TaskState -> Value
    taskStateToValue :: TaskState -> Value
taskStateToValue = Int -> Value
ValueInt (Int -> Value) -> (TaskState -> Int) -> TaskState -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TaskState -> Int
forall a. Enum a => a -> Int
fromEnum

readTaskState :: Value -> TaskState
readTaskState :: Value -> TaskState
readTaskState (ValueInt Int
i) | Int
i Int -> [Int] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (TaskState -> Int) -> [TaskState] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map TaskState -> Int
forall a. Enum a => a -> Int
fromEnum (TaskState -> [TaskState]
forall a. Enum a => a -> [a]
enumFrom TaskState
TaskFree) = Int -> TaskState
forall a. Enum a => Int -> a
toEnum Int
i
readTaskState Value
_ = String -> TaskState
forall a. HasCallStack => String -> a
error String
"invalid task state"

getTaskState :: Struct -> Maybe TaskState
getTaskState :: Struct -> Maybe TaskState
getTaskState Struct
st = Value -> TaskState
readTaskState (Value -> TaskState) -> Maybe Value -> Maybe TaskState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Struct -> Maybe Value
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"state" Struct
st

kojiGetTaskState :: String -> TaskID -> IO (Maybe TaskState)
kojiGetTaskState :: String -> TaskID -> IO (Maybe TaskState)
kojiGetTaskState String
hubUrl TaskID
tid = do
  Maybe Struct
mti <- String -> Int -> Bool -> IO (Maybe Struct)
getTaskInfo String
hubUrl (TaskID -> Int
forall a. ID a => a -> Int
getID TaskID
tid) Bool
False
  Maybe TaskState -> IO (Maybe TaskState)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe TaskState -> IO (Maybe TaskState))
-> Maybe TaskState -> IO (Maybe TaskState)
forall a b. (a -> b) -> a -> b
$ case Maybe Struct
mti of
             Maybe Struct
Nothing -> Maybe TaskState
forall a. Maybe a
Nothing
             Just Struct
ti -> Value -> TaskState
readTaskState (Value -> TaskState) -> Maybe Value -> Maybe TaskState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Struct -> Maybe Value
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"state" Struct
ti

kojiGetTaskInfo :: String
                -> TaskID
                -> IO (Maybe Struct)
kojiGetTaskInfo :: String -> TaskID -> IO (Maybe Struct)
kojiGetTaskInfo String
hubUrl TaskID
tid = String -> Int -> Bool -> IO (Maybe Struct)
getTaskInfo String
hubUrl (TaskID -> Int
forall a. ID a => a -> Int
getID TaskID
tid) Bool
True
  -- res <- kojiCall "getTaskInfo" [show taskid]
  -- let state = res ^? key "state" % _Integer <&> (toEnum . fromInteger)
  --     arch = res ^? key "arch" % _String
  -- return $ TaskInfo arch state

kojiGetTaskChildren :: String -> TaskID -> Bool -> IO [Struct]
kojiGetTaskChildren :: String -> TaskID -> Bool -> IO [Struct]
kojiGetTaskChildren String
hubUrl TaskID
tid =
  String -> Int -> Bool -> IO [Struct]
getTaskChildren String
hubUrl (TaskID -> Int
forall a. ID a => a -> Int
getID TaskID
tid)

kojiLatestBuild :: String -- ^ hub
                -> String -- ^ tag
                -> String -- ^ pkg
                -> IO (Maybe Struct)
kojiLatestBuild :: String -> String -> String -> IO (Maybe Struct)
kojiLatestBuild String
hubUrl String
tag String
pkg =
  [Struct] -> Maybe Struct
forall a. [a] -> Maybe a
listToMaybe ([Struct] -> Maybe Struct) -> IO [Struct] -> IO (Maybe Struct)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String
-> Info -> Maybe Int -> Maybe String -> Maybe String -> IO [Struct]
getLatestBuilds String
hubUrl (String -> Info
InfoString String
tag) Maybe Int
forall a. Maybe a
Nothing (String -> Maybe String
forall a. a -> Maybe a
Just String
pkg) Maybe String
forall a. Maybe a
Nothing

kojiLatestBuildRepo :: String -- ^ hub
                    -> String -- ^ tag
                    -> Int    -- ^ event
                    -> String -- ^ pkg
                    -> IO (Maybe Struct)
kojiLatestBuildRepo :: String -> String -> Int -> String -> IO (Maybe Struct)
kojiLatestBuildRepo String
hubUrl String
tag Int
event String
pkg =
  [Struct] -> Maybe Struct
forall a. [a] -> Maybe a
listToMaybe ([Struct] -> Maybe Struct) -> IO [Struct] -> IO (Maybe Struct)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String
-> Info -> Maybe Int -> Maybe String -> Maybe String -> IO [Struct]
getLatestBuilds String
hubUrl (String -> Info
InfoString String
tag) (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
event) (String -> Maybe String
forall a. a -> Maybe a
Just String
pkg) Maybe String
forall a. Maybe a
Nothing

data KojiBuild
  = KojiBuild
      { KojiBuild -> Int
kbBuildId :: Int
      , KojiBuild -> Int
kbPackageId :: Int
      , KojiBuild -> String
kbOwnerName :: String
      , KojiBuild -> String
kbNvr :: String
      }
  deriving (Int -> KojiBuild -> ShowS
[KojiBuild] -> ShowS
KojiBuild -> String
(Int -> KojiBuild -> ShowS)
-> (KojiBuild -> String)
-> ([KojiBuild] -> ShowS)
-> Show KojiBuild
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [KojiBuild] -> ShowS
$cshowList :: [KojiBuild] -> ShowS
show :: KojiBuild -> String
$cshow :: KojiBuild -> String
showsPrec :: Int -> KojiBuild -> ShowS
$cshowsPrec :: Int -> KojiBuild -> ShowS
Show)

kojiListTaggedBuilds :: String -> Bool -> String -> IO [KojiBuild]
kojiListTaggedBuilds :: String -> Bool -> String -> IO [KojiBuild]
kojiListTaggedBuilds String
hubUrl Bool
latest String
tag =
  (Struct -> Maybe KojiBuild) -> [Struct] -> [KojiBuild]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Struct -> Maybe KojiBuild
readKojiBuild ([Struct] -> [KojiBuild]) -> IO [Struct] -> IO [KojiBuild]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String
-> String
-> Maybe Int
-> Bool
-> Maybe String
-> Bool
-> Maybe String
-> Maybe String
-> Maybe String
-> IO [Struct]
listTagged String
hubUrl String
tag Maybe Int
forall a. Maybe a
Nothing Bool
False Maybe String
forall a. Maybe a
Nothing Bool
latest Maybe String
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing
  where
    readKojiBuild :: Struct -> Maybe KojiBuild
    readKojiBuild :: Struct -> Maybe KojiBuild
readKojiBuild Struct
values = do
      Int
buildId <- String -> Struct -> Maybe Int
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"build_id" Struct
values
      Int
packageId <- String -> Struct -> Maybe Int
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"package_id" Struct
values
      String
owner <- String -> Struct -> Maybe String
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"owner_name" Struct
values
      String
nvr <- String -> Struct -> Maybe String
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"nvr" Struct
values
      KojiBuild -> Maybe KojiBuild
forall (m :: * -> *) a. Monad m => a -> m a
return (KojiBuild -> Maybe KojiBuild) -> KojiBuild -> Maybe KojiBuild
forall a b. (a -> b) -> a -> b
$ Int -> Int -> String -> String -> KojiBuild
KojiBuild Int
buildId Int
packageId String
owner String
nvr

kojiBuildTarget :: String -- ^ hubUrl
                -> String -- ^ target
                -> IO (Maybe (String, String)) -- ^ (build-tag,dest-tag)
kojiBuildTarget :: String -> String -> IO (Maybe (String, String))
kojiBuildTarget String
hub String
target = do
  Maybe Struct
mres <- Value -> Maybe Struct
maybeStruct (Value -> Maybe Struct) -> IO Value -> IO (Maybe Struct)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> IO Value
getBuildTarget String
hub String
target
  case Maybe Struct
mres of
    Maybe Struct
Nothing -> Maybe (String, String) -> IO (Maybe (String, String))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (String, String)
forall a. Maybe a
Nothing
    Just Struct
res -> Maybe (String, String) -> IO (Maybe (String, String))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (String, String) -> IO (Maybe (String, String)))
-> Maybe (String, String) -> IO (Maybe (String, String))
forall a b. (a -> b) -> a -> b
$ Struct -> Maybe (String, String)
forall a b. (XmlRpcType a, XmlRpcType b) => Struct -> Maybe (a, b)
readTarget Struct
res
  where
  readTarget :: Struct -> Maybe (a, b)
readTarget Struct
res = do
    a
buildtag <- String -> Struct -> Maybe a
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"build_tag_name" Struct
res
    b
desttag <- String -> Struct -> Maybe b
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"dest_tag_name" Struct
res
    (a, b) -> Maybe (a, b)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
buildtag, b
desttag)

-- | List sidetags (preferably for user and/or basetag)
kojiListSideTags :: String -- ^ hubUrl
                 -> Maybe String -- ^ basetag
                 -> Maybe String -- ^ user
                 -> IO [String] -- ^ list of sidetags
kojiListSideTags :: String -> Maybe String -> Maybe String -> IO [String]
kojiListSideTags String
hub Maybe String
mbasetag Maybe String
muser =
  (Struct -> Maybe String) -> [Struct] -> [String]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (String -> Struct -> Maybe String
forall a. XmlRpcType a => String -> Struct -> Maybe a
lookupStruct String
"name") ([Struct] -> [String]) -> (Value -> [Struct]) -> Value -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> [Struct]
structArray (Value -> [String]) -> IO Value -> IO [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Maybe Info -> Maybe Info -> IO Value
listSideTags String
hub (String -> Info
InfoString (String -> Info) -> Maybe String -> Maybe Info
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe String
mbasetag) (String -> Info
InfoString (String -> Info) -> Maybe String -> Maybe Info
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe String
muser)

data RepoState = RepoInit | RepoReady | RepoExpired | RepoDeleted | RepoProblem
  deriving (RepoState -> RepoState -> Bool
(RepoState -> RepoState -> Bool)
-> (RepoState -> RepoState -> Bool) -> Eq RepoState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RepoState -> RepoState -> Bool
$c/= :: RepoState -> RepoState -> Bool
== :: RepoState -> RepoState -> Bool
$c== :: RepoState -> RepoState -> Bool
Eq, Int -> RepoState
RepoState -> Int
RepoState -> [RepoState]
RepoState -> RepoState
RepoState -> RepoState -> [RepoState]
RepoState -> RepoState -> RepoState -> [RepoState]
(RepoState -> RepoState)
-> (RepoState -> RepoState)
-> (Int -> RepoState)
-> (RepoState -> Int)
-> (RepoState -> [RepoState])
-> (RepoState -> RepoState -> [RepoState])
-> (RepoState -> RepoState -> [RepoState])
-> (RepoState -> RepoState -> RepoState -> [RepoState])
-> Enum RepoState
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: RepoState -> RepoState -> RepoState -> [RepoState]
$cenumFromThenTo :: RepoState -> RepoState -> RepoState -> [RepoState]
enumFromTo :: RepoState -> RepoState -> [RepoState]
$cenumFromTo :: RepoState -> RepoState -> [RepoState]
enumFromThen :: RepoState -> RepoState -> [RepoState]
$cenumFromThen :: RepoState -> RepoState -> [RepoState]
enumFrom :: RepoState -> [RepoState]
$cenumFrom :: RepoState -> [RepoState]
fromEnum :: RepoState -> Int
$cfromEnum :: RepoState -> Int
toEnum :: Int -> RepoState
$ctoEnum :: Int -> RepoState
pred :: RepoState -> RepoState
$cpred :: RepoState -> RepoState
succ :: RepoState -> RepoState
$csucc :: RepoState -> RepoState
Enum, Int -> RepoState -> ShowS
[RepoState] -> ShowS
RepoState -> String
(Int -> RepoState -> ShowS)
-> (RepoState -> String)
-> ([RepoState] -> ShowS)
-> Show RepoState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RepoState] -> ShowS
$cshowList :: [RepoState] -> ShowS
show :: RepoState -> String
$cshow :: RepoState -> String
showsPrec :: Int -> RepoState -> ShowS
$cshowsPrec :: Int -> RepoState -> ShowS
Show)

readRepoState :: Value -> RepoState
readRepoState :: Value -> RepoState
readRepoState (ValueInt Int
i) | Int
i Int -> [Int] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (RepoState -> Int) -> [RepoState] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map RepoState -> Int
forall a. Enum a => a -> Int
fromEnum (RepoState -> [RepoState]
forall a. Enum a => a -> [a]
enumFrom RepoState
RepoInit) = Int -> RepoState
forall a. Enum a => Int -> a
toEnum Int
i
readRepoState Value
_ = String -> RepoState
forall a. HasCallStack => String -> a
error String
"invalid repo state"

-- getRepoState :: Struct -> Maybe RepoState
-- getRepoState st = readRepoState <$> lookup "state" st

kojiGetRepo :: String -> String -> Maybe RepoState -> Maybe Int
            -> IO (Maybe Struct)
kojiGetRepo :: String
-> String -> Maybe RepoState -> Maybe Int -> IO (Maybe Struct)
kojiGetRepo String
hub String
tag Maybe RepoState
mstate Maybe Int
mevent =
  Value -> Maybe Struct
maybeStruct (Value -> Maybe Struct) -> IO Value -> IO (Maybe Struct)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Maybe Int -> Maybe Int -> Bool -> IO Value
getRepo String
hub String
tag (RepoState -> Int
forall a. Enum a => a -> Int
fromEnum (RepoState -> Int) -> Maybe RepoState -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe RepoState
mstate) Maybe Int
mevent Bool
False

kojiGetCurrentRepo :: String -> String -> IO (Maybe Struct)
kojiGetCurrentRepo :: String -> String -> IO (Maybe Struct)
kojiGetCurrentRepo String
hub String
tag =
  Value -> Maybe Struct
maybeStruct (Value -> Maybe Struct) -> IO Value -> IO (Maybe Struct)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Maybe Int -> Maybe Int -> Bool -> IO Value
getRepo String
hub String
tag Maybe Int
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Bool
False