{-# LANGUAGE OverloadedStrings #-}

-- |
-- Module      : Branches
-- Description : Queries about repository branches
-- Copyright   : (c) Rob Stewart, Heriot-Watt University, 2019
-- License     : BSD3
-- Maintainer  : robstewart57@gmail.com
-- Stability   : stable
module GitLab.API.Branches where

import Control.Monad.IO.Unlift
import Data.Either
import qualified Data.Text as T
import GitLab.Types
import GitLab.WebRequests.GitLabWebCalls
import Network.HTTP.Types.Status

-- | Get a list of repository branches from a project, sorted by name
-- alphabetically.
branches :: (MonadIO m) => Project -> GitLab m [Branch]
branches project = do
  result <- branches' (project_id project)
  return (fromRight (error "branches error") result)

-- | Get a list of repository branches from a project given its
-- project ID, sorted by name alphabetically.
branches' :: (MonadIO m) => Int -> GitLab m (Either Status [Branch])
branches' projectId =
  gitlab addr
  where
    addr =
      "/projects/"
        <> T.pack (show projectId)
        <> "/repository"
        <> "/branches"