{-# LANGUAGE OverloadedStrings #-}

-- |
-- Module      : Pipelines
-- Description : Queries about project pipelines
-- Copyright   : (c) Rob Stewart, Heriot-Watt University, 2019
-- License     : BSD3
-- Maintainer  : robstewart57@gmail.com
-- Stability   : stable
module GitLab.API.Pipelines 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 the pipelines for a project.
pipelines ::
  -- | the project
  Project ->
  GitLab [Pipeline]
pipelines :: Project -> GitLab [Pipeline]
pipelines Project
p = do
  Either (Response ByteString) [Pipeline]
result <- Int -> GitLab (Either (Response ByteString) [Pipeline])
pipelines' (Project -> Int
project_id Project
p)
  [Pipeline] -> GitLab [Pipeline]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Pipeline] -> Either (Response ByteString) [Pipeline] -> [Pipeline]
forall b a. b -> Either a b -> b
fromRight ([Char] -> [Pipeline]
forall a. HasCallStack => [Char] -> a
error [Char]
"pipelines error") Either (Response ByteString) [Pipeline]
result)

-- | returns the pipelines for a project given its project ID.
pipelines' ::
  -- | the project ID
  Int ->
  GitLab (Either (Response BSL.ByteString) [Pipeline])
pipelines' :: Int -> GitLab (Either (Response ByteString) [Pipeline])
pipelines' Int
projectId =
  Text
-> [GitLabParam]
-> GitLab (Either (Response ByteString) [Pipeline])
forall a.
FromJSON a =>
Text -> [GitLabParam] -> GitLab (Either (Response ByteString) [a])
gitlabGetMany
    Text
addr
    [(ByteString
"sort", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"desc")] -- most recent first
  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
"/pipelines"

-- | get a pipeline’s test report.  Since GitLab 13.0.
pipelineTestReport ::
  -- | the project
  Project ->
  -- | the pipeline
  Pipeline ->
  GitLab TestReport
pipelineTestReport :: Project -> Pipeline -> GitLab TestReport
pipelineTestReport Project
proj Pipeline
pipeline = do
  Either (Response ByteString) (Maybe TestReport)
result <- Int
-> Int -> GitLab (Either (Response ByteString) (Maybe TestReport))
pipelineTestReport' (Project -> Int
project_id Project
proj) (Pipeline -> Int
pipeline_id Pipeline
pipeline)
  case Maybe TestReport
-> Either (Response ByteString) (Maybe TestReport)
-> Maybe TestReport
forall b a. b -> Either a b -> b
fromRight ([Char] -> Maybe TestReport
forall a. HasCallStack => [Char] -> a
error [Char]
"pipelineTestReport error") Either (Response ByteString) (Maybe TestReport)
result of
    Maybe TestReport
Nothing -> [Char] -> GitLab TestReport
forall a. HasCallStack => [Char] -> a
error [Char]
"pipelineTestReport error"
    Just TestReport
testReport -> TestReport -> GitLab TestReport
forall (m :: * -> *) a. Monad m => a -> m a
return TestReport
testReport

-- | get a pipeline’s test report. Since GitLab 13.0.
pipelineTestReport' ::
  -- | the project ID
  Int ->
  -- | the pipeline ID
  Int ->
  GitLab (Either (Response BSL.ByteString) (Maybe TestReport))
pipelineTestReport' :: Int
-> Int -> GitLab (Either (Response ByteString) (Maybe TestReport))
pipelineTestReport' Int
projId Int
pipelineId = do
  let urlPath :: Text
urlPath =
        [Char] -> Text
T.pack
          ( [Char]
"/projects/"
              [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> Int -> [Char]
forall a. Show a => a -> [Char]
show Int
projId
              [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"/pipelines/"
              [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> Int -> [Char]
forall a. Show a => a -> [Char]
show Int
pipelineId
              [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"/test_report"
          )
  Text
-> [GitLabParam]
-> GitLab (Either (Response ByteString) (Maybe TestReport))
forall a.
FromJSON a =>
Text
-> [GitLabParam] -> GitLab (Either (Response ByteString) (Maybe a))
gitlabGetOne Text
urlPath []