feat: topics and list for org #1

Merged
zacheryprenovost merged 3 commits from topics-and-list-for-org into main 2023-11-27 22:00:56 +00:00
3 changed files with 37 additions and 2 deletions
Showing only changes of commit aea50e7603 - Show all commits

View File

@ -8,14 +8,17 @@ module Gitea.Repo where
import Prelude hiding ((/)) import Prelude hiding ((/))
import Control.Monad.Reader (ask) 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.Tuple.Nested ((/\))
import Data.URL (URL, (/)) import Data.URL (URL, (/), (?), (&))
import Effect.Aff.Class (class MonadAff) import Effect.Aff.Class (class MonadAff)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Gitea.Auth as Auth import Gitea.Auth as Auth
import Gitea.Error as Error import Gitea.Error as Error
import Gitea.Trans (GiteaT) 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 (Method(..), fetch) as HTTP
import HTTP.Request (json) as HTTP import HTTP.Request (json) as HTTP
import Node.Buffer (Buffer) 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 :: forall m. Monad m => Username -> RepoName -> GiteaT m URL
reposURL (Username owner) (RepoName repo) = (\{ baseURI } -> baseURI / "repos" / owner / repo) <$> ask 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 :: forall m. MonadAff m => Username -> RepoName -> GiteaT m (Record Repo)
get u r = do get u r = do
{ auth } <- ask { auth } <- ask
@ -34,6 +40,24 @@ get u r = do
rep <- HTTP.fetch $ HTTP.GET /\ url /\ Auth.headers auth rep <- HTTP.fetch $ HTTP.GET /\ url /\ Auth.headers auth
Error.tryGetRepJSON rep 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 update
:: forall m patch :: forall m patch
. MonadAff m . MonadAff m

View File

@ -6,6 +6,7 @@ import Control.Monad.Error.Class (class MonadError, class MonadThrow, throwError
import Control.Monad.Except (ExceptT, runExceptT) import Control.Monad.Except (ExceptT, runExceptT)
import Control.Monad.Morph (class MFunctor, class MMonad, embed, hoist) import Control.Monad.Morph (class MFunctor, class MMonad, embed, hoist)
import Control.Monad.Reader (class MonadAsk, class MonadReader, ReaderT(..), ask, runReaderT) 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 Control.Monad.Trans.Class (class MonadTrans, lift)
import Data.Either (Either) import Data.Either (Either)
import Data.Newtype (class Newtype, unwrap, wrap) 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 MonadAff m => MonadAff (GiteaT m)
derive newtype instance Monad m => MonadError Gitea.Error.Error (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 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 => Functor (GiteaT m)
derive newtype instance Monad m => Apply (GiteaT m) derive newtype instance Monad m => Apply (GiteaT m)

View File

@ -17,6 +17,14 @@ derive newtype instance Show RepoName
derive newtype instance WriteForeign RepoName derive newtype instance WriteForeign RepoName
derive newtype instance ReadForeign 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 newtype TagName = TagName String
derive instance Newtype TagName _ derive instance Newtype TagName _