diff --git a/src/Gitea.Repo.purs b/src/Gitea.Repo.purs index 8a9c88e..82136e6 100644 --- a/src/Gitea.Repo.purs +++ b/src/Gitea.Repo.purs @@ -8,14 +8,17 @@ module Gitea.Repo where import Prelude hiding ((/)) import Control.Monad.Reader (ask) +import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM) +import Control.Plus (empty) +import Data.Array as Array import Data.Tuple.Nested ((/\)) -import Data.URL (URL, (/)) +import Data.URL (URL, (/), (?), (&)) import Effect.Aff.Class (class MonadAff) import Effect.Class (liftEffect) import Gitea.Auth as Auth import Gitea.Error as Error import Gitea.Trans (GiteaT) -import Gitea.Types (Repo, RepoName(..), Username(..), RepoUpdate) +import Gitea.Types (OrgName(..), Repo, RepoName(..), RepoUpdate, Username(..)) import HTTP (Method(..), fetch) as HTTP import HTTP.Request (json) as HTTP import Node.Buffer (Buffer) @@ -27,6 +30,9 @@ import Type.Row.Subset (class Subset) reposURL :: forall m. Monad m => Username -> RepoName -> GiteaT m URL reposURL (Username owner) (RepoName repo) = (\{ baseURI } -> baseURI / "repos" / owner / repo) <$> ask +orgReposURL :: forall m. Monad m => OrgName -> {page :: Int , limit :: Int} -> GiteaT m URL +orgReposURL (OrgName org) {page,limit} = (\{ baseURI } -> baseURI / "orgs" / org / "repos" ? "limit" /\ show limit & "page" /\ show page) <$> ask + get :: forall m. MonadAff m => Username -> RepoName -> GiteaT m (Record Repo) get u r = do { auth } <- ask @@ -34,6 +40,24 @@ get u r = do rep <- HTTP.fetch $ HTTP.GET /\ url /\ Auth.headers auth Error.tryGetRepJSON rep +listForOrg :: forall m. MonadAff m => MonadRec m => OrgName -> GiteaT m (Array (Record Repo)) +listForOrg o = let + limit = 10 + paginate (repos /\ pageN) = + do + newRepos <- getPage pageN + pure if Array.length newRepos < limit then + Done $ repos <> newRepos + else + Loop $ (repos <> newRepos) /\ (pageN + 1) + getPage n = do + { auth } <- ask + url <- orgReposURL o + rep <- HTTP.fetch $ HTTP.GET /\ url /\ Auth.headers auth + Error.tryGetRepJSON rep + in tailRecM paginate ([] /\ 1) + + update :: forall m patch . MonadAff m diff --git a/src/Gitea.Monad.purs b/src/Gitea.Trans.purs similarity index 94% rename from src/Gitea.Monad.purs rename to src/Gitea.Trans.purs index 59e1c29..857c06b 100644 --- a/src/Gitea.Monad.purs +++ b/src/Gitea.Trans.purs @@ -6,6 +6,7 @@ import Control.Monad.Error.Class (class MonadError, class MonadThrow, throwError import Control.Monad.Except (ExceptT, runExceptT) import Control.Monad.Morph (class MFunctor, class MMonad, embed, hoist) import Control.Monad.Reader (class MonadAsk, class MonadReader, ReaderT(..), ask, runReaderT) +import Control.Monad.Rec.Class (class MonadRec) import Control.Monad.Trans.Class (class MonadTrans, lift) import Data.Either (Either) import Data.Newtype (class Newtype, unwrap, wrap) @@ -39,6 +40,8 @@ derive newtype instance MonadEffect m => MonadEffect (GiteaT m) derive newtype instance MonadAff m => MonadAff (GiteaT m) derive newtype instance Monad m => MonadError Gitea.Error.Error (GiteaT m) derive newtype instance Monad m => MonadThrow Gitea.Error.Error (GiteaT m) +derive newtype instance MonadRec m => MonadRec (GiteaT m) + derive newtype instance Monad m => Functor (GiteaT m) derive newtype instance Monad m => Apply (GiteaT m) diff --git a/src/Gitea.Model.purs b/src/Gitea.Types.purs similarity index 97% rename from src/Gitea.Model.purs rename to src/Gitea.Types.purs index 337badf..a2ca0c9 100644 --- a/src/Gitea.Model.purs +++ b/src/Gitea.Types.purs @@ -17,6 +17,14 @@ derive newtype instance Show RepoName derive newtype instance WriteForeign RepoName derive newtype instance ReadForeign RepoName +newtype OrgName = OrgName String + +derive instance Newtype OrgName _ +derive newtype instance Eq OrgName +derive newtype instance Show OrgName +derive newtype instance WriteForeign OrgName +derive newtype instance ReadForeign OrgName + newtype TagName = TagName String derive instance Newtype TagName _