1- {-# LANGUAGE OverloadedStrings #-}
2- {-# LANGUAGE TemplateHaskell #-}
1+ {-# LANGUAGE DataKinds #-}
2+ {-# LANGUAGE FlexibleInstances #-}
3+ {-# LANGUAGE MultiParamTypeClasses #-}
4+ {-# LANGUAGE OverloadedStrings #-}
5+ {-# LANGUAGE TemplateHaskell #-}
36module GitHub.PullRequestsSpec where
47
5- import qualified GitHub
8+ import qualified GitHub as GH
69
710import Prelude ()
811import Prelude.Compat
912
10- import Data.Aeson (eitherDecodeStrict )
11- import Data.ByteString (ByteString )
12- import Data.Either.Compat (isRight )
13- import Data.FileEmbed (embedFile )
14- import Data.Foldable (for_ )
15- import Data.String (fromString )
16- import qualified Data.Vector as V
13+ import Data.Aeson
14+ (FromJSON (.. ), eitherDecodeStrict , withObject , (.:) )
15+ import Data.ByteString (ByteString )
1716import qualified Data.ByteString.Lazy.Char8 as LBS8
18- import System.Environment (lookupEnv )
17+ import Data.Either.Compat (isRight )
18+ import Data.FileEmbed (embedFile )
19+ import Data.Foldable (for_ )
20+ import Data.String (fromString )
21+ import Data.Tagged (Tagged (.. ))
22+ import Data.Text (Text )
23+ import qualified Data.Vector as V
24+ import System.Environment (lookupEnv )
1925import Test.Hspec
2026 (Spec , describe , it , pendingWith , shouldBe , shouldSatisfy )
2127
2228fromRightS :: Show a => Either a b -> b
2329fromRightS (Right b) = b
2430fromRightS (Left a) = error $ " Expected a Right and got a Left" ++ show a
2531
26- withAuth :: (GitHub . Auth -> IO () ) -> IO ()
32+ withAuth :: (GH . Auth -> IO () ) -> IO ()
2733withAuth action = do
2834 mtoken <- lookupEnv " GITHUB_TOKEN"
2935 case mtoken of
3036 Nothing -> pendingWith " no GITHUB_TOKEN"
31- Just token -> action (GitHub . OAuth $ fromString token)
37+ Just token -> action (GH . OAuth $ fromString token)
3238
3339spec :: Spec
3440spec = do
3541 describe " pullRequestsForR" $ do
3642 it " works" $ withAuth $ \ auth -> for_ repos $ \ (owner, repo) -> do
37- cs <- GitHub . executeRequest auth $
38- GitHub . pullRequestsForR owner repo opts GitHub . FetchAll
43+ cs <- GH . executeRequest auth $
44+ GH . pullRequestsForR owner repo opts GH . FetchAll
3945 cs `shouldSatisfy` isRight
4046
4147 describe " pullRequestPatchR" $
4248 it " works" $ withAuth $ \ auth -> do
43- Right patch <- GitHub . executeRequest auth $
44- GitHub . pullRequestPatchR " phadej" " github" (GitHub . IssueNumber 349 )
49+ Right patch <- GH . executeRequest auth $
50+ GH . pullRequestPatchR " phadej" " github" (GH . IssueNumber 349 )
4551 head (LBS8. lines patch) `shouldBe` " From c0e4ad33811be82e1f72ee76116345c681703103 Mon Sep 17 00:00:00 2001"
4652
4753 describe " decoding pull request payloads" $ do
4854 it " decodes a pull request 'opened' payload" $ do
49- V. length (GitHub . simplePullRequestRequestedReviewers simplePullRequestOpened)
55+ V. length (GH . simplePullRequestRequestedReviewers simplePullRequestOpened)
5056 `shouldBe` 0
5157
52- V. length (GitHub . pullRequestRequestedReviewers pullRequestOpened)
58+ V. length (GH . pullRequestRequestedReviewers pullRequestOpened)
5359 `shouldBe` 0
5460
5561 it " decodes a pull request 'review_requested' payload" $ do
56- V. length (GitHub . simplePullRequestRequestedReviewers simplePullRequestReviewRequested)
62+ V. length (GH . simplePullRequestRequestedReviewers simplePullRequestReviewRequested)
5763 `shouldBe` 1
5864
59- V. length (GitHub . pullRequestRequestedReviewers pullRequestReviewRequested)
65+ V. length (GH . pullRequestRequestedReviewers pullRequestReviewRequested)
6066 `shouldBe` 1
6167
6268 describe " checking if a pull request is merged" $ do
6369 it " works" $ withAuth $ \ auth -> do
64- b <- GitHub . executeRequest auth $ GitHub . isPullRequestMergedR " phadej" " github" (GitHub . IssueNumber 14 )
70+ b <- GH . executeRequest auth $ GH . isPullRequestMergedR " phadej" " github" (GH . IssueNumber 14 )
6571 b `shouldSatisfy` isRight
6672 fromRightS b `shouldBe` True
6773
74+ describe " Draft Pull Request" $ do
75+ it " works" $ withAuth $ \ auth -> do
76+ cs <- GH. executeRequest auth $
77+ draftPullRequestsForR " phadej" " github" opts GH. FetchAll
78+
79+ cs `shouldSatisfy` isRight
80+
6881 where
6982 repos =
7083 [ (" thoughtbot" , " paperclip" )
7184 , (" phadej" , " github" )
7285 ]
73- opts = GitHub . stateClosed
86+ opts = GH . stateClosed
7487
75- simplePullRequestOpened :: GitHub . SimplePullRequest
88+ simplePullRequestOpened :: GH . SimplePullRequest
7689 simplePullRequestOpened =
7790 fromRightS (eitherDecodeStrict prOpenedPayload)
7891
79- pullRequestOpened :: GitHub . PullRequest
92+ pullRequestOpened :: GH . PullRequest
8093 pullRequestOpened =
8194 fromRightS (eitherDecodeStrict prOpenedPayload)
8295
83- simplePullRequestReviewRequested :: GitHub . SimplePullRequest
96+ simplePullRequestReviewRequested :: GH . SimplePullRequest
8497 simplePullRequestReviewRequested =
8598 fromRightS (eitherDecodeStrict prReviewRequestedPayload)
8699
87- pullRequestReviewRequested :: GitHub . PullRequest
100+ pullRequestReviewRequested :: GH . PullRequest
88101 pullRequestReviewRequested =
89102 fromRightS (eitherDecodeStrict prReviewRequestedPayload)
90103
@@ -93,3 +106,41 @@ spec = do
93106
94107 prReviewRequestedPayload :: ByteString
95108 prReviewRequestedPayload = $ (embedFile " fixtures/pull-request-review-requested.json" )
109+
110+ -------------------------------------------------------------------------------
111+ -- Draft Pull Requests
112+ -------------------------------------------------------------------------------
113+
114+ draftPullRequestsForR
115+ :: GH. Name GH. Owner
116+ -> GH. Name GH. Repo
117+ -> GH. PullRequestMod
118+ -> GH. FetchCount
119+ -> GH. GenRequest ('GH.MtPreview ShadowCat ) k (V. Vector DraftPR )
120+ draftPullRequestsForR user repo opts = GH. PagedQuery
121+ [" repos" , GH. toPathPart user, GH. toPathPart repo, " pulls" ]
122+ (GH. prModToQueryString opts)
123+
124+ data DraftPR = DraftPR
125+ { dprId :: ! (GH. Id GH. PullRequest )
126+ , dprNumber :: ! GH. IssueNumber
127+ , dprTitle :: ! Text
128+ , dprDraft :: ! Bool
129+ }
130+ deriving (Show )
131+
132+ instance FromJSON DraftPR where
133+ parseJSON = withObject " DraftPR" $ \ obj -> DraftPR
134+ <$> obj .: " id"
135+ <*> obj .: " number"
136+ <*> obj .: " title"
137+ <*> obj .: " draft"
138+
139+ -- | @application/vnd.github.shadow-cat-preview+json@ <https://developer.github.com/v3/previews/#draft-pull-requests>
140+ data ShadowCat
141+
142+ instance GH. PreviewAccept ShadowCat where
143+ previewContentType = Tagged " application/vnd.github.shadow-cat-preview+json"
144+
145+ instance FromJSON a => GH. PreviewParseResponse ShadowCat a where
146+ previewParseResponse _ res = Tagged (GH. parseResponseJSON res)
0 commit comments