{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

module Fedora.Pagure.Client (
  OutputFormat(..),
  Packager(..),
  Forks(..),
  srcFedoraprojectOrg,
  defaultPrinter,
  yamlPrinter,
  listProjects,
  projectIssues,
  projectPRs,
  projectInfo,
  projectIssue,
  projectPR,
  repoBranches,
  users,
  userRepos,
  userInfo,
  username,
  userIssues,
  userRequestsFiled,
  userRequestsActionable,
  userActivityStats,
  userActivityDate,
  groups,
  groupInfo,
  groupProjects,
  gitUrl,
  contributors,
  )
where

import Control.Applicative (
#if !MIN_VERSION_base(4,8,0)
                            (<$>), (<*>),
#endif
                           )
import Control.Monad.Extra (unless, when, whenJust)
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.Aeson.Types
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.KeyMap as M
#else
import qualified Data.HashMap.Lazy as M
#endif
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.List.Extra (lower)
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Vector as V
import Data.Yaml (encode)
import Network.HTTP.Query ((+/+))
import SimpleCmd (error', (+-+))
import Fedora.Pagure

data OutputFormat = FormatDefault
                  | FormatJson
                  | FormatYaml

data Packager = Owner String
              | Committer String

data Forks = NoForks
           | IncludeForks
           | OnlyForks

-- | Fedora dist-git
srcFedoraprojectOrg :: String
srcFedoraprojectOrg :: [Char]
srcFedoraprojectOrg = [Char]
"src.fedoraproject.org"

-- | default print helper
defaultPrinter :: Bool -- ^ no hide empty
               -> OutputFormat
               -> (Object -> IO ()) -- ^ printer
               -> Object -- ^ JSON
               -> IO ()
defaultPrinter :: Bool -> OutputFormat -> (Object -> IO ()) -> Object -> IO ()
defaultPrinter Bool
_ OutputFormat
FormatDefault Object -> IO ()
pr = Object -> IO ()
pr
defaultPrinter Bool
nohide OutputFormat
fmt Object -> IO ()
_ = Bool -> OutputFormat -> Object -> IO ()
yamlPrinter Bool
nohide OutputFormat
fmt

-- | List projects
listProjects :: String -- ^ server
             -> Bool -- ^ count
             -> Bool -- ^ no hide
             -> OutputFormat
             -> Forks
             -> Maybe String -- ^ namespace
             -> Maybe Packager
             -> Maybe String -- ^ pattern
             -> IO ()
listProjects :: [Char]
-> Bool
-> Bool
-> OutputFormat
-> Forks
-> Maybe [Char]
-> Maybe Packager
-> Maybe [Char]
-> IO ()
listProjects [Char]
server Bool
count Bool
nohide OutputFormat
format Forks
forks Maybe [Char]
mnamespace Maybe Packager
mpackager Maybe [Char]
mpattern = do
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool
count Bool -> Bool -> Bool
|| Maybe Packager -> Bool
forall a. Maybe a -> Bool
isJust Maybe Packager
mpackager Bool -> Bool -> Bool
|| Maybe [Char] -> Bool
forall a. Maybe a -> Bool
isJust Maybe [Char]
mpattern) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
    [Char] -> IO ()
forall a. [Char] -> a
error' [Char]
"Please give a package pattern, --count, or --owner/--username"
  let path :: [Char]
path = [Char]
"projects"
      params :: [QueryItem]
params = [Char] -> [Char] -> [QueryItem]
makeKey [Char]
"short" [Char]
"1" [QueryItem] -> [QueryItem] -> [QueryItem]
forall a. [a] -> [a] -> [a]
++ [QueryItem]
fork [QueryItem] -> [QueryItem] -> [QueryItem]
forall a. [a] -> [a] -> [a]
++ [QueryItem]
packager [QueryItem] -> [QueryItem] -> [QueryItem]
forall a. [a] -> [a] -> [a]
++ [Char] -> Maybe [Char] -> [QueryItem]
maybeKey [Char]
"namespace" Maybe [Char]
mnamespace [QueryItem] -> [QueryItem] -> [QueryItem]
forall a. [a] -> [a] -> [a]
++ [Char] -> Maybe [Char] -> [QueryItem]
maybeKey [Char]
"pattern" Maybe [Char]
mpattern
  pages <- [Char]
-> Bool -> [Char] -> [QueryItem] -> ([Char], [Char]) -> IO [Object]
queryPagureCountPaged [Char]
server Bool
count [Char]
path [QueryItem]
params ([Char]
"pagination", [Char]
"page")
  let projects = (Object -> [Object]) -> [Object] -> [Object]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"projects") [Object]
pages
  mapM_ (defaultPrinter nohide format printProject) projects
  where
    -- (!orphan only works on pagure >=0.29)
    packager :: [QueryItem]
packager = case Maybe Packager
mpackager of
      Maybe Packager
Nothing -> Bool -> [Char] -> [Char] -> [QueryItem]
boolKey ([Char]
server [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
srcFedoraprojectOrg) [Char]
"owner" [Char]
"!orphan"
      Just (Owner [Char]
o) -> [Char] -> [Char] -> [QueryItem]
makeKey [Char]
"owner" [Char]
o
      Just (Committer [Char]
c) -> [Char] -> [Char] -> [QueryItem]
makeKey [Char]
"username" [Char]
c

    fork :: [QueryItem]
fork = case Forks
forks of
      Forks
NoForks -> [Char] -> [Char] -> [QueryItem]
makeKey [Char]
"fork" [Char]
"0"
      Forks
IncludeForks -> []
      Forks
OnlyForks -> [Char] -> [Char] -> [QueryItem]
makeKey [Char]
"fork" [Char]
"1"

    printProject :: Object -> IO ()
    printProject :: Object -> IO ()
printProject Object
project = do
      let key' :: Text
key' = if Maybe [Char] -> Bool
forall a. Maybe a -> Bool
isJust Maybe [Char]
mnamespace then Text
"name" else Text
"fullname"
      Maybe Text -> (Text -> IO ()) -> IO ()
forall (m :: * -> *) a.
Applicative m =>
Maybe a -> (a -> m ()) -> m ()
whenJust (Text -> Object -> Maybe Text
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey Text
key' Object
project) Text -> IO ()
T.putStrLn

-- FIXME duplicates subset of listProjects
-- | Variant of `pagureUserRepos` or `pagureUserForks`
userRepos :: String -- ^ server
          -> Bool -- ^ count
          -> Bool -- ^ forks
          -> String -- ^ user
          -> IO ()
userRepos :: [Char] -> Bool -> Bool -> [Char] -> IO ()
userRepos [Char]
server Bool
count Bool
forks [Char]
user =
  if Bool
count then do
    let path :: [Char]
path = [Char]
"user" [Char] -> [Char] -> [Char]
+/+ [Char]
user
    mcnt <- [Char] -> [Char] -> [QueryItem] -> [Char] -> IO (Maybe Integer)
queryPagureCount [Char]
server [Char]
path [] ([Char] -> IO (Maybe Integer)) -> [Char] -> IO (Maybe Integer)
forall a b. (a -> b) -> a -> b
$
            if Bool
forks then [Char]
"forks_pagination" else [Char]
"repos_pagination"
    print $
      fromMaybe
      (error' ("number of" +-+ (if forks then "forks" else "repos") +-+ "could not be determined"))
      mcnt
    else do
    repos <- (if Bool
forks then [Char] -> [Char] -> IO [Text]
pagureUserForks else [Char] -> [Char] -> IO [Text]
pagureUserRepos) [Char]
server [Char]
user
    mapM_ T.putStrLn repos

boolKey :: Bool -> String -> String -> Query
boolKey :: Bool -> [Char] -> [Char] -> [QueryItem]
boolKey Bool
False [Char]
_ [Char]
_ = []
boolKey Bool
True [Char]
k [Char]
val = [Char] -> [Char] -> [QueryItem]
makeKey [Char]
k [Char]
val

-- FIXME limit max number of issues
-- | List project issues
--
-- Please use allstatus with care since it can list of a lot issues
projectIssues :: String -- ^ server
              -> Bool -- ^ count
              -> Bool -- ^ no hide
              -> OutputFormat
              -> Bool -- ^ all status
              -> Maybe String -- ^ author
              -> Maybe String -- ^ since
              -> Maybe String -- ^ pattern
              -> String -- ^ repo
              -> IO ()
projectIssues :: [Char]
-> Bool
-> Bool
-> OutputFormat
-> Bool
-> Maybe [Char]
-> Maybe [Char]
-> Maybe [Char]
-> [Char]
-> IO ()
projectIssues [Char]
server Bool
count Bool
nohide OutputFormat
format Bool
allstatus Maybe [Char]
mauthor Maybe [Char]
msince Maybe [Char]
mpat [Char]
repo = do
  let path :: [Char]
path = [Char]
repo [Char] -> [Char] -> [Char]
+/+ [Char]
"issues"
      params :: [QueryItem]
params = Bool -> [Char] -> [Char] -> [QueryItem]
boolKey Bool
allstatus [Char]
"status" [Char]
"all" [QueryItem] -> [QueryItem] -> [QueryItem]
forall a. [a] -> [a] -> [a]
++
               [Char] -> Maybe [Char] -> [QueryItem]
maybeKey [Char]
"author" Maybe [Char]
mauthor [QueryItem] -> [QueryItem] -> [QueryItem]
forall a. [a] -> [a] -> [a]
++ [Char] -> Maybe [Char] -> [QueryItem]
maybeKey [Char]
"since" Maybe [Char]
msince
  pages <- [Char]
-> Bool -> [Char] -> [QueryItem] -> ([Char], [Char]) -> IO [Object]
queryPagureCountPaged [Char]
server Bool
count [Char]
path [QueryItem]
params ([Char]
"pagination", [Char]
"page")
  mapM_ (defaultPrinter nohide format printIssues) pages
  where
    printIssues :: Object -> IO ()
    printIssues :: Object -> IO ()
printIssues Object
result = do
      let issues :: [Object]
issues = Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"issues" Object
result :: [Object]
      (Object -> IO ()) -> [Object] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Object -> IO ()
printIssue [Object]
issues

    printIssue :: Object -> IO ()
    printIssue :: Object -> IO ()
printIssue Object
issue = do
      case Object -> Maybe (Integer, Text, Text)
parseIssue Object
issue of
        Maybe (Integer, Text, Text)
Nothing -> [Char] -> IO ()
putStrLn [Char]
"parsing issue failed"
        Just (Integer
id',Text
title,Text
status) ->
          Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe [Char] -> Bool
forall a. Maybe a -> Bool
isNothing Maybe [Char]
mpat Bool -> Bool -> Bool
|| [Char] -> Text
T.pack (Maybe [Char] -> [Char]
forall a. HasCallStack => Maybe a -> a
fromJust Maybe [Char]
mpat) Text -> Text -> Bool
`T.isInfixOf` Text
title) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
          [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"https://" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
server [Char] -> [Char] -> [Char]
+/+ [Char]
repo [Char] -> [Char] -> [Char]
+/+ [Char]
"issue" [Char] -> [Char] -> [Char]
+/+ Integer -> [Char]
forall a. Show a => a -> [Char]
show Integer
id' [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ (if Bool
allstatus then [Char]
" (" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
status [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
")" else [Char]
"") [Char] -> [Char] -> [Char]
+-+ [Char]
":" [Char] -> [Char] -> [Char]
+-+ Text -> [Char]
T.unpack Text
title

    parseIssue :: Object -> Maybe (Integer, Text, Text)
    parseIssue :: Object -> Maybe (Integer, Text, Text)
parseIssue =
      (Object -> Parser (Integer, Text, Text))
-> Object -> Maybe (Integer, Text, Text)
forall a b. (a -> Parser b) -> a -> Maybe b
parseMaybe ((Object -> Parser (Integer, Text, Text))
 -> Object -> Maybe (Integer, Text, Text))
-> (Object -> Parser (Integer, Text, Text))
-> Object
-> Maybe (Integer, Text, Text)
forall a b. (a -> b) -> a -> b
$ \Object
obj -> do
        id' <- Object
obj Object -> Key -> Parser Integer
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
        title <- obj .: "title"
        status <- obj .: "status"
        return (id',title,status)

-- FIXME limit max number of issues
-- | List pull-requests
--
-- Please use allstatus with care since it can list of a lot of requests
projectPRs :: String -- ^ server
           -> Bool -- ^ count
           -> Bool -- ^ no hide
           -> OutputFormat
           -> Bool -- ^ all status
           -> Maybe String -- ^ author
           -> Maybe String -- ^ since
           -> Maybe String -- ^ pattern
           -> String -- ^ repo
           -> IO ()
projectPRs :: [Char]
-> Bool
-> Bool
-> OutputFormat
-> Bool
-> Maybe [Char]
-> Maybe [Char]
-> Maybe [Char]
-> [Char]
-> IO ()
projectPRs [Char]
server Bool
count Bool
nohide OutputFormat
format Bool
allstatus Maybe [Char]
mauthor Maybe [Char]
msince Maybe [Char]
mpat [Char]
repo = do
  let namespace :: [Char]
namespace =
        if [Char]
server [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
srcFedoraprojectOrg Bool -> Bool -> Bool
&& Char
'/' Char -> [Char] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Char]
repo
        then [Char]
"rpms/" else [Char]
""
      project :: [Char]
project = [Char]
namespace [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
repo
      path :: [Char]
path = [Char]
project [Char] -> [Char] -> [Char]
+/+ [Char]
"pull-requests"
      params :: [QueryItem]
params = [[Char] -> [Char] -> QueryItem
makeItem [Char]
"status" [Char]
"all" | Bool
allstatus] [QueryItem] -> [QueryItem] -> [QueryItem]
forall a. [a] -> [a] -> [a]
++
               [Char] -> Maybe [Char] -> [QueryItem]
maybeKey [Char]
"author" Maybe [Char]
mauthor [QueryItem] -> [QueryItem] -> [QueryItem]
forall a. [a] -> [a] -> [a]
++ [Char] -> Maybe [Char] -> [QueryItem]
maybeKey [Char]
"since" Maybe [Char]
msince
  pages <- [Char]
-> Bool -> [Char] -> [QueryItem] -> ([Char], [Char]) -> IO [Object]
queryPagureCountPaged [Char]
server Bool
count [Char]
path [QueryItem]
params ([Char]
"pagination", [Char]
"page")
  mapM_ (defaultPrinter nohide format (printPRs project)) pages
  where
    printPRs :: String -> Object -> IO ()
    printPRs :: [Char] -> Object -> IO ()
printPRs [Char]
project Object
result = do
      let prs :: [Object]
prs = Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"requests" Object
result :: [Object]
      (Object -> IO ()) -> [Object] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ([Char] -> Object -> IO ()
printPR [Char]
project) [Object]
prs

    printPR :: String -> Object -> IO ()
    printPR :: [Char] -> Object -> IO ()
printPR [Char]
project Object
pr = do
      case Object -> Maybe (Integer, Text, Text)
parsePR Object
pr of
        Maybe (Integer, Text, Text)
Nothing -> [Char] -> IO ()
putStrLn [Char]
"parsing issue failed"
        Just (Integer
id',Text
title,Text
status) ->
          Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe [Char] -> Bool
forall a. Maybe a -> Bool
isNothing Maybe [Char]
mpat Bool -> Bool -> Bool
|| [Char] -> Text
T.pack (Maybe [Char] -> [Char]
forall a. HasCallStack => Maybe a -> a
fromJust Maybe [Char]
mpat) Text -> Text -> Bool
`T.isInfixOf` Text
title) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
          [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"https://" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
server [Char] -> [Char] -> [Char]
+/+ [Char]
project [Char] -> [Char] -> [Char]
+/+ [Char]
"pull-request" [Char] -> [Char] -> [Char]
+/+ Integer -> [Char]
forall a. Show a => a -> [Char]
show Integer
id' [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ (if Bool
allstatus then [Char]
" (" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
lower (Text -> [Char]
T.unpack Text
status) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
")" else [Char]
"") [Char] -> [Char] -> [Char]
+-+ [Char]
":" [Char] -> [Char] -> [Char]
+-+ Text -> [Char]
T.unpack Text
title

    parsePR :: Object -> Maybe (Integer, Text, Text)
    parsePR :: Object -> Maybe (Integer, Text, Text)
parsePR =
      (Object -> Parser (Integer, Text, Text))
-> Object -> Maybe (Integer, Text, Text)
forall a b. (a -> Parser b) -> a -> Maybe b
parseMaybe ((Object -> Parser (Integer, Text, Text))
 -> Object -> Maybe (Integer, Text, Text))
-> (Object -> Parser (Integer, Text, Text))
-> Object
-> Maybe (Integer, Text, Text)
forall a b. (a -> b) -> a -> b
$ \Object
obj -> do
        id' <- Object
obj Object -> Key -> Parser Integer
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
        title <- obj .: "title"
        status <- obj .: "status"
        return (id',title,status)

-- | List repo git branches
repoBranches :: String -- ^ server
             -> Bool -- ^ no hide
             -> OutputFormat
             -> String -- ^ repo
             -> IO ()
repoBranches :: [Char] -> Bool -> OutputFormat -> [Char] -> IO ()
repoBranches [Char]
server Bool
nohide OutputFormat
format [Char]
repo = do
  let namespace :: [Char]
namespace =
        if [Char]
server [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
srcFedoraprojectOrg Bool -> Bool -> Bool
&& Char
'/' Char -> [Char] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Char]
repo
        then [Char]
"rpms/" else [Char]
""
      path :: [Char]
path = [Char]
namespace [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
repo [Char] -> [Char] -> [Char]
+/+ [Char]
"git/branches"
  eres <- [Char] -> [Char] -> [QueryItem] -> IO (Either [Char] Object)
queryPagureSingle [Char]
server [Char]
path []
  either error' (defaultPrinter nohide format (printKeyList "branches")) eres

-- | List users matching pattern
users :: String -- ^ server
      -> Bool -- ^ no hide
      -> OutputFormat
      -> String -- ^ pattern
      -> IO ()
users :: [Char] -> Bool -> OutputFormat -> [Char] -> IO ()
users [Char]
server Bool
nohide OutputFormat
format [Char]
pat = do
  let path :: [Char]
path = [Char]
"users"
      params :: [QueryItem]
params = [Char] -> [Char] -> [QueryItem]
makeKey [Char]
"pattern" [Char]
pat
  res <- [Char] -> [Char] -> [QueryItem] -> IO Object
queryPagure [Char]
server [Char]
path [QueryItem]
params
  defaultPrinter nohide format (printKeyList "users") res

-- | Get full name of user
username :: String -- ^ server
         -> Bool -- ^ no hide
         -> OutputFormat
         -> String -- ^ user
         -> IO ()
username :: [Char] -> Bool -> OutputFormat -> [Char] -> IO ()
username [Char]
server Bool
nohide OutputFormat
format [Char]
user = do
  let path :: [Char]
path = [Char]
"user" [Char] -> [Char] -> [Char]
+/+ [Char]
user
  eres <- [Char] -> [Char] -> [QueryItem] -> IO (Either [Char] Object)
queryPagureSingle [Char]
server [Char]
path ([QueryItem] -> IO (Either [Char] Object))
-> [QueryItem] -> IO (Either [Char] Object)
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char] -> [QueryItem]
makeKey [Char]
"per_page" [Char]
"1"
  case eres of
    Left [Char]
err -> [Char] -> IO ()
forall a. [Char] -> a
error' [Char]
err
    Right Object
res -> Bool -> OutputFormat -> (Object -> IO ()) -> Object -> IO ()
defaultPrinter Bool
nohide OutputFormat
format Object -> IO ()
printName Object
res
  where
    printName :: Object -> IO ()
printName Object
res =
      case Text -> Object -> Maybe Object
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey Text
"user" Object
res Maybe Object -> (Object -> Maybe Text) -> Maybe Text
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Object -> Maybe Text
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey Text
"fullname" of
        Maybe Text
Nothing -> [Char] -> IO ()
forall a. [Char] -> a
error' [Char]
"User fullname not found"
        Just Text
fn -> Text -> IO ()
T.putStrLn Text
fn

-- | List groups with optional pattern
groups :: String -- ^ server
       -> Bool -- ^ count
       -> Bool -- ^ no hide
       -> OutputFormat
       -> Maybe String -- ^ pattern
       -> IO ()
groups :: [Char] -> Bool -> Bool -> OutputFormat -> Maybe [Char] -> IO ()
groups [Char]
server Bool
count Bool
nohide OutputFormat
format Maybe [Char]
mpat = do
  let path :: [Char]
path = [Char]
"groups"
      params :: [QueryItem]
params = [Char] -> Maybe [Char] -> [QueryItem]
maybeKey [Char]
"pattern" Maybe [Char]
mpat
  pages <- [Char]
-> Bool -> [Char] -> [QueryItem] -> ([Char], [Char]) -> IO [Object]
queryPagureCountPaged [Char]
server Bool
count [Char]
path [QueryItem]
params ([Char]
"pagination", [Char]
"page")
  mapM_ (defaultPrinter nohide format (printKeyList "groups")) pages

printKeyList :: String -> Object -> IO ()
printKeyList :: [Char] -> Object -> IO ()
printKeyList [Char]
key' Object
res =
  (Text -> IO ()) -> [Text] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Text -> IO ()
T.putStrLn (Text -> Object -> [Text]
forall a. FromJSON a => Text -> Object -> a
lookupKey' ([Char] -> Text
T.pack [Char]
key') Object
res :: [Text])

-- | Print git url of project
gitUrl :: String -- ^ server
       -> Bool -- ^ no hide
       -> OutputFormat
       -> String -- ^ repo
       -> IO ()
gitUrl :: [Char] -> Bool -> OutputFormat -> [Char] -> IO ()
gitUrl [Char]
server Bool
nohide OutputFormat
format [Char]
repo = do
  let namespace :: [Char]
namespace =
        if [Char]
server [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
srcFedoraprojectOrg Bool -> Bool -> Bool
&& Char
'/' Char -> [Char] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Char]
repo
        then [Char]
"rpms/" else [Char]
""
      path :: [Char]
path = [Char]
namespace [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
repo [Char] -> [Char] -> [Char]
+/+ [Char]
"git/urls"
  res <- [Char] -> [Char] -> [QueryItem] -> IO Object
queryPagure [Char]
server [Char]
path []
  defaultPrinter nohide format printURLs res
  where
    printURLs :: Object -> IO ()
printURLs Object
result =
      (Text -> IO ()) -> [Text] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Text -> IO ()
T.putStrLn ([Text] -> IO ()) -> [Text] -> IO ()
forall a b. (a -> b) -> a -> b
$
      KeyMap Text -> [Text]
forall v. KeyMap v -> [v]
M.elems (KeyMap Text -> [Text]) -> KeyMap Text -> [Text]
forall a b. (a -> b) -> a -> b
$ [Char] -> Object -> KeyMap Text
localLookupKey [Char]
"urls" Object
result

    localLookupKey :: String -> Object ->
#if MIN_VERSION_aeson(2,0,0)
                      M.KeyMap Text
#else
                      M.HashMap Text Text
#endif
    localLookupKey :: [Char] -> Object -> KeyMap Text
localLookupKey = Text -> Object -> KeyMap Text
forall a. FromJSON a => Text -> Object -> a
lookupKey' (Text -> Object -> KeyMap Text)
-> ([Char] -> Text) -> [Char] -> Object -> KeyMap Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
T.pack

-- | List contributors of project
contributors :: String -- ^ server
             -> Bool -- ^ no hide
             -> OutputFormat
             -> String -- ^ repo
             -> IO ()
contributors :: [Char] -> Bool -> OutputFormat -> [Char] -> IO ()
contributors [Char]
server Bool
nohide OutputFormat
format [Char]
repo = do
  let namespace :: [Char]
namespace =
        if [Char]
server [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
srcFedoraprojectOrg Bool -> Bool -> Bool
&& Char
'/' Char -> [Char] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Char]
repo
        then [Char]
"rpms/" else [Char]
""
      path :: [Char]
path = [Char]
namespace [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
repo [Char] -> [Char] -> [Char]
+/+ [Char]
"contributors"
  res <- [Char] -> [Char] -> [QueryItem] -> IO Object
queryPagure [Char]
server [Char]
path []
  yamlPrinter nohide format res

-- | Yaml printer
yamlPrinter :: Bool -- ^ no hide
            -> OutputFormat
            -> Object
            -> IO ()
yamlPrinter :: Bool -> OutputFormat -> Object -> IO ()
yamlPrinter Bool
nohide OutputFormat
format =
  case OutputFormat
format of
    OutputFormat
FormatDefault -> Bool -> OutputFormat -> Object -> IO ()
yamlPrinter Bool
nohide OutputFormat
FormatYaml
    OutputFormat
FormatJson -> ByteString -> IO ()
BL.putStrLn (ByteString -> IO ()) -> (Object -> ByteString) -> Object -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Object -> ByteString
forall a. ToJSON a => a -> ByteString
encodePretty (Object -> ByteString)
-> (Object -> Object) -> Object -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Object -> Object
hideEmpty
    OutputFormat
FormatYaml -> ByteString -> IO ()
B.putStrLn (ByteString -> IO ()) -> (Object -> ByteString) -> Object -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Object -> ByteString
forall a. ToJSON a => a -> ByteString
encode (Object -> ByteString)
-> (Object -> Object) -> Object -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Object -> Object
hideEmpty
  where
    hideEmpty :: Object -> Object
hideEmpty = if Bool
nohide then Object -> Object
forall a. a -> a
id else Object -> Object
cleanObject

    cleanObject :: Object -> Object
    cleanObject :: Object -> Object
cleanObject = (Value -> Maybe Value) -> Object -> Object
forall a b. (a -> Maybe b) -> KeyMap a -> KeyMap b
M.mapMaybe Value -> Maybe Value
go
      where
        go :: Value -> Maybe Value
        -- drop empty lists
        go :: Value -> Maybe Value
go (Array Array
v) | Array -> Bool
forall a. Vector a -> Bool
V.null Array
v = Maybe Value
forall a. Maybe a
Nothing
                     | Bool
otherwise = Value -> Maybe Value
forall a. a -> Maybe a
Just (Value -> Maybe Value) -> Value -> Maybe Value
forall a b. (a -> b) -> a -> b
$ Array -> Value
Array ((Value -> Value) -> Array -> Array
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((Value -> Maybe Value) -> Value -> Value
wrap Value -> Maybe Value
go) Array
v)
                     where
                       wrap :: (Value -> Maybe Value) -> Value -> Value
wrap Value -> Maybe Value
f (Object Object
o) = Object -> Value
Object ((Value -> Maybe Value) -> Object -> Object
forall a b. (a -> Maybe b) -> KeyMap a -> KeyMap b
M.mapMaybe Value -> Maybe Value
f Object
o)
                       wrap Value -> Maybe Value
f (Array Array
a)  = Array -> Value
Array ((Value -> Value) -> Array -> Array
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((Value -> Maybe Value) -> Value -> Value
wrap Value -> Maybe Value
f) Array
a)
                       wrap Value -> Maybe Value
_ Value
x          = Value
x
        -- drop empty objects
        go (Object Object
o) =
            let cleaned :: Object
cleaned = (Value -> Maybe Value) -> Object -> Object
forall a b. (a -> Maybe b) -> KeyMap a -> KeyMap b
M.mapMaybe Value -> Maybe Value
go Object
o
            in if Object -> Bool
forall v. KeyMap v -> Bool
M.null Object
cleaned then Maybe Value
forall a. Maybe a
Nothing else Value -> Maybe Value
forall a. a -> Maybe a
Just (Object -> Value
Object Object
cleaned)
        go Value
other = Value -> Maybe Value
forall a. a -> Maybe a
Just Value
other

-- | Show project info
projectInfo :: String -- ^ server
            -> Bool -- ^ no hide
            -> OutputFormat
            -> String -- ^ repo
            -> IO ()
projectInfo :: [Char] -> Bool -> OutputFormat -> [Char] -> IO ()
projectInfo [Char]
server Bool
nohide OutputFormat
format [Char]
repo = do
  let namespace :: [Char]
namespace =
        if [Char]
server [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
srcFedoraprojectOrg Bool -> Bool -> Bool
&& Char
'/' Char -> [Char] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Char]
repo
        then [Char]
"rpms/" else [Char]
""
      path :: [Char]
path = [Char]
namespace [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
repo
  eval <- [Char] -> [Char] -> IO (Either [Char] Object)
pagureProjectInfo [Char]
server [Char]
path
  either error' (yamlPrinter nohide format) eval

-- | Display project issue
projectIssue :: String -- ^ server
             -> Bool -- ^ no hide
             -> OutputFormat
             -> String -- ^ repo
             -> Int -- ^ issue
             -> IO ()
projectIssue :: [Char] -> Bool -> OutputFormat -> [Char] -> Int -> IO ()
projectIssue [Char]
server Bool
nohide OutputFormat
format [Char]
repo Int
issue = do
  eval <- [Char] -> [Char] -> Int -> IO (Either [Char] Object)
pagureProjectIssueInfo [Char]
server [Char]
repo Int
issue
  either error' (yamlPrinter nohide format) eval

-- | Display project pull request
projectPR :: String -- ^ server
          -> Bool -- ^ no hide
          -> OutputFormat
          -> String -- ^ repo
          -> Int -- ^ PR number
          -> IO ()
projectPR :: [Char] -> Bool -> OutputFormat -> [Char] -> Int -> IO ()
projectPR [Char]
server Bool
nohide OutputFormat
format [Char]
repo Int
issue = do
  let namespace :: [Char]
namespace =
        if [Char]
server [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
srcFedoraprojectOrg Bool -> Bool -> Bool
&& Char
'/' Char -> [Char] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Char]
repo
        then [Char]
"rpms/" else [Char]
""
      project :: [Char]
project = [Char]
namespace [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
repo
  eval <- [Char] -> [Char] -> Int -> IO (Either [Char] Object)
pagureProjectPRInfo [Char]
server [Char]
project Int
issue
  either error' (yamlPrinter nohide format) eval

-- | Display user info
userInfo :: String -- ^ server
         -> Bool -- ^ no hide
         -> OutputFormat
         -> String -- ^ user
         -> IO ()
userInfo :: [Char] -> Bool -> OutputFormat -> [Char] -> IO ()
userInfo [Char]
server Bool
nohide OutputFormat
format [Char]
user = do
  eval <- [Char] -> [Char] -> [QueryItem] -> IO (Either [Char] Object)
pagureUserInfo [Char]
server [Char]
user []
  either error' (yamlPrinter nohide format) eval

-- | Show user activity stats
userActivityStats :: String -- ^ server
                  -> OutputFormat
                  -> String -- ^ user
                  -> IO ()
userActivityStats :: [Char] -> OutputFormat -> [Char] -> IO ()
userActivityStats [Char]
server OutputFormat
format [Char]
user = do
  eval <- [Char] -> Bool -> [Char] -> IO (Either [Char] Object)
pagureUserActivityStats [Char]
server Bool
False [Char]
user
  either error' (yamlPrinter False format) eval

-- | Show user activity on date
userActivityDate :: String -- ^ server
                 -> OutputFormat
                 -> Bool -- ^ summary
                 -> String -- ^ user
                 -> String -- ^ date
                 -> IO ()
userActivityDate :: [Char] -> OutputFormat -> Bool -> [Char] -> [Char] -> IO ()
userActivityDate [Char]
server OutputFormat
format Bool
summary [Char]
user [Char]
date = do
  eval <- [Char] -> Bool -> [Char] -> [Char] -> IO (Either [Char] Object)
pagureUserActivityDate [Char]
server Bool
summary [Char]
user [Char]
date
  either error' (yamlPrinter False format) eval

-- | List user issues
userIssues :: String -- ^ server
           -> Bool -- ^ count
           -> OutputFormat
           -> String -- ^ user
           -> IO ()
userIssues :: [Char] -> Bool -> OutputFormat -> [Char] -> IO ()
userIssues [Char]
server Bool
count OutputFormat
format [Char]
user = do
  issues <- [Char] -> Bool -> [Char] -> IO [Object]
pagureUserIssues [Char]
server Bool
count [Char]
user
  mapM_ (yamlPrinter False format) issues

-- | List pull requests filed by user
userRequestsFiled :: String -- ^ server
                  -> Bool -- ^ count
                  -> OutputFormat
                  -> Maybe String -- ^ pattern
                  -> Maybe String -- ^ status
                  -> String -- ^ user
                  -> IO ()
userRequestsFiled :: [Char]
-> Bool
-> OutputFormat
-> Maybe [Char]
-> Maybe [Char]
-> [Char]
-> IO ()
userRequestsFiled [Char]
server Bool
count OutputFormat
format Maybe [Char]
mpat Maybe [Char]
mstatus [Char]
user = do
  result <- [Char] -> Bool -> [Char] -> [Char] -> IO [Object]
pagureUserRequestsFiled [Char]
server Bool
count ([Char] -> Maybe [Char] -> [Char]
forall a. a -> Maybe a -> a
fromMaybe [Char]
"Open" Maybe [Char]
mstatus) [Char]
user
  let requests = (Object -> [Object]) -> [Object] -> [Object]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"requests") [Object]
result
  mapM_ (defaultPrinter False format printPR) requests
  where
    printPR :: Object -> IO ()
    printPR :: Object -> IO ()
printPR Object
pr = do
      case Object -> Maybe (Integer, Text, Text)
parsePR Object
pr of
        Maybe (Integer, Text, Text)
Nothing -> [Char] -> IO ()
putStrLn [Char]
"parsing PR failed"
        Just (Integer
id',Text
title,Text
repo) ->
          Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe [Char] -> Bool
forall a. Maybe a -> Bool
isNothing Maybe [Char]
mpat Bool -> Bool -> Bool
|| [Char] -> Text
T.pack (Maybe [Char] -> [Char]
forall a. HasCallStack => Maybe a -> a
fromJust Maybe [Char]
mpat) Text -> Text -> Bool
`T.isInfixOf` Text
title) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
          [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"https://" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
server [Char] -> [Char] -> [Char]
+/+ Text -> [Char]
T.unpack Text
repo [Char] -> [Char] -> [Char]
+/+ [Char]
"pull-request" [Char] -> [Char] -> [Char]
+/+ Integer -> [Char]
forall a. Show a => a -> [Char]
show Integer
id' [Char] -> [Char] -> [Char]
+-+ [Char] -> [Char]
forall a. Show a => a -> [Char]
show (Text -> [Char]
T.unpack Text
title)

    parsePR :: Object -> Maybe (Integer, Text, Text)
    parsePR :: Object -> Maybe (Integer, Text, Text)
parsePR =
      (Object -> Parser (Integer, Text, Text))
-> Object -> Maybe (Integer, Text, Text)
forall a b. (a -> Parser b) -> a -> Maybe b
parseMaybe ((Object -> Parser (Integer, Text, Text))
 -> Object -> Maybe (Integer, Text, Text))
-> (Object -> Parser (Integer, Text, Text))
-> Object
-> Maybe (Integer, Text, Text)
forall a b. (a -> b) -> a -> b
$ \Object
obj -> do
        id' <- Object
obj Object -> Key -> Parser Integer
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
        title <- obj .: "title"
        proj <- obj .: "project"
        repo <- proj .: "fullname"
        return (id',title,repo)

-- | Show actionable pull requests for user
userRequestsActionable :: String -- ^ server
                       -> Bool -- ^ count
                       -> OutputFormat
                       -> Maybe String -- ^ pattern
                       -> Maybe String -- ^ status
                       -> String -- ^ user
                       -> IO ()
userRequestsActionable :: [Char]
-> Bool
-> OutputFormat
-> Maybe [Char]
-> Maybe [Char]
-> [Char]
-> IO ()
userRequestsActionable [Char]
server Bool
count OutputFormat
format Maybe [Char]
mpat Maybe [Char]
mstatus [Char]
user = do
  result <- [Char] -> Bool -> [Char] -> [Char] -> IO [Object]
pagureUserRequestsActionable [Char]
server Bool
count ([Char] -> Maybe [Char] -> [Char]
forall a. a -> Maybe a -> a
fromMaybe [Char]
"Open" Maybe [Char]
mstatus) [Char]
user
  let requests = (Object -> [Object]) -> [Object] -> [Object]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"requests") [Object]
result
  mapM_ (defaultPrinter False format printPR) requests
  where
    printPR :: Object -> IO ()
    printPR :: Object -> IO ()
printPR Object
pr = do
      case Object -> Maybe (Integer, Text, Text)
parsePR Object
pr of
        Maybe (Integer, Text, Text)
Nothing -> [Char] -> IO ()
putStrLn [Char]
"parsing PR failed"
        Just (Integer
id',Text
title,Text
repo) ->
          Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe [Char] -> Bool
forall a. Maybe a -> Bool
isNothing Maybe [Char]
mpat Bool -> Bool -> Bool
|| [Char] -> Text
T.pack (Maybe [Char] -> [Char]
forall a. HasCallStack => Maybe a -> a
fromJust Maybe [Char]
mpat) Text -> Text -> Bool
`T.isInfixOf` Text
title) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
          [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"https://" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
server [Char] -> [Char] -> [Char]
+/+ Text -> [Char]
T.unpack Text
repo [Char] -> [Char] -> [Char]
+/+ [Char]
"pull-request" [Char] -> [Char] -> [Char]
+/+ Integer -> [Char]
forall a. Show a => a -> [Char]
show Integer
id' [Char] -> [Char] -> [Char]
+-+ [Char] -> [Char]
forall a. Show a => a -> [Char]
show (Text -> [Char]
T.unpack Text
title)

    parsePR :: Object -> Maybe (Integer, Text, Text)
    parsePR :: Object -> Maybe (Integer, Text, Text)
parsePR =
      (Object -> Parser (Integer, Text, Text))
-> Object -> Maybe (Integer, Text, Text)
forall a b. (a -> Parser b) -> a -> Maybe b
parseMaybe ((Object -> Parser (Integer, Text, Text))
 -> Object -> Maybe (Integer, Text, Text))
-> (Object -> Parser (Integer, Text, Text))
-> Object
-> Maybe (Integer, Text, Text)
forall a b. (a -> b) -> a -> b
$ \Object
obj -> do
        id' <- Object
obj Object -> Key -> Parser Integer
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
        title <- obj .: "title"
        proj <- obj .: "project"
        repo <- proj .: "fullname"
        return (id',title,repo)

-- | Show group info
groupInfo :: String -- ^ server
          -> Bool -- ^ projects
          -> Bool -- ^ no hide
          -> OutputFormat
          -> String -- ^ group
          -> IO ()
groupInfo :: [Char] -> Bool -> Bool -> OutputFormat -> [Char] -> IO ()
groupInfo [Char]
server Bool
projects Bool
nohide OutputFormat
format [Char]
group = do
  let params :: [QueryItem]
params = Bool -> [Char] -> [Char] -> [QueryItem]
boolKey Bool
projects [Char]
"projects" [Char]
"1"
  eval <- [Char] -> [Char] -> [QueryItem] -> IO (Either [Char] Object)
pagureGroupInfo [Char]
server [Char]
group [QueryItem]
params
  either error' (yamlPrinter nohide format) eval

-- FIXME support acl parameter (admin, commit or ticket)
-- | List group projects
groupProjects :: String -- ^ server
              -> Bool -- ^ count
              -> String -- ^ group
              -> IO ()
groupProjects :: [Char] -> Bool -> [Char] -> IO ()
groupProjects [Char]
server Bool
count [Char]
group =
  [Char] -> Bool -> [Char] -> IO [Text]
pagureGroupRepos [Char]
server Bool
count [Char]
group IO [Text] -> ([Text] -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
  (Text -> IO ()) -> [Text] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Text -> IO ()
T.putStrLn