{-# LANGUAGE OverloadedStrings #-}

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

import qualified Data.ByteString.Lazy as BSL
import Data.Either
import qualified Data.Text as T
import GitLab.Types
import GitLab.WebRequests.GitLabWebCalls
import Network.HTTP.Client

-- | returns all jobs ran on a project.
jobs ::
  -- | the project
  Project ->
  GitLab [Job]
jobs :: Project -> GitLab [Job]
jobs Project
project = do
  Either (Response ByteString) [Job]
result <- Int -> GitLab (Either (Response ByteString) [Job])
jobs' (Project -> Int
project_id Project
project)
  [Job] -> GitLab [Job]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Job] -> Either (Response ByteString) [Job] -> [Job]
forall b a. b -> Either a b -> b
fromRight ([Char] -> [Job]
forall a. HasCallStack => [Char] -> a
error [Char]
"jobs error") Either (Response ByteString) [Job]
result)

-- | returns all jobs ran on a project given its project ID.
jobs' ::
  -- | the project ID
  Int ->
  GitLab (Either (Response BSL.ByteString) [Job])
jobs' :: Int -> GitLab (Either (Response ByteString) [Job])
jobs' Int
projectId =
  Text
-> [GitLabParam] -> GitLab (Either (Response ByteString) [Job])
forall a.
FromJSON a =>
Text -> [GitLabParam] -> GitLab (Either (Response ByteString) [a])
gitlabGetMany Text
addr []
  where
    addr :: Text
addr =
      Text
"/projects/"
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show Int
projectId)
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/jobs"