Skip to content

Commit 0c31bbd

Browse files
authored
Merge pull request #350 from phadej/cherry-picks
Cherry-picks from TomMD's PRs.
2 parents c2968f3 + 1180ebe commit 0c31bbd

File tree

8 files changed

+126
-15
lines changed

8 files changed

+126
-15
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,24 @@
22

33
## Changes for 0.21
44

5+
- Refactor `Request` type.
6+
[#349](https://github.com/phadej/github/pull/349)
57
- Allow `http-client-0.6`
8+
[#344](https://github.com/phadej/github/pull/344)
69
- Change to use `cryptohash-sha1` (`cryptohash` was used before)
10+
- Add Create milestone endponts
11+
[#337](https://github.com/phadej/github/pull/337)
12+
- Make fileBlobUrl and fileRawUrl are optional
13+
[#339](https://github.com/phadej/github/issues/339)
14+
[#340](https://github.com/phadej/github/pull/340)
15+
- Add organizationsR to request user organizations
16+
[#345](https://github.com/phadej/github/pull/345)
17+
- Add updateMilestoneR, deleteMilestoneR
18+
[#338](https://github.com/phadej/github/pull/338)
19+
- Allow multiple assignees in NewIssue and EditIssue
20+
[#336](https://github.com/phadej/github/pull/336)
21+
- Add `pullRequestPatchR` and `pullRequestDiffR`
22+
[#325](https://github.com/phadej/github/pull/325)
723

824
## Changes for 0.20
925

spec/GitHub/PullRequestsSpec.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Data.FileEmbed (embedFile)
1414
import Data.Foldable (for_)
1515
import Data.String (fromString)
1616
import qualified Data.Vector as V
17+
import qualified Data.ByteString.Lazy.Char8 as LBS8
1718
import System.Environment (lookupEnv)
1819
import Test.Hspec
1920
(Spec, describe, it, pendingWith, shouldBe, shouldSatisfy)
@@ -37,6 +38,12 @@ spec = do
3738
GitHub.pullRequestsForR owner repo opts GitHub.FetchAll
3839
cs `shouldSatisfy` isRight
3940

41+
describe "pullRequestPatchR" $
42+
it "works" $ withAuth $ \auth -> do
43+
Right patch <- GitHub.executeRequest auth $
44+
GitHub.pullRequestPatchR "phadej" "github" (GitHub.IssueNumber 349)
45+
head (LBS8.lines patch) `shouldBe` "From c0e4ad33811be82e1f72ee76116345c681703103 Mon Sep 17 00:00:00 2001"
46+
4047
describe "decoding pull request payloads" $ do
4148
it "decodes a pull request 'opened' payload" $ do
4249
V.length (GitHub.simplePullRequestRequestedReviewers simplePullRequestOpened)
@@ -62,7 +69,6 @@ spec = do
6269
repos =
6370
[ ("thoughtbot", "paperclip")
6471
, ("phadej", "github")
65-
, ("haskell", "cabal")
6672
]
6773
opts = GitHub.stateClosed
6874

src/GitHub.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ module GitHub (
193193
-- | See <https://developer.github.com/v3/pulls/>
194194
pullRequestsForR,
195195
pullRequestR,
196+
pullRequestPatchR,
197+
pullRequestDiffR,
196198
createPullRequestR,
197199
updatePullRequestR,
198200
pullRequestCommitsR,
@@ -206,11 +208,11 @@ module GitHub (
206208
-- Missing endpoints:
207209
--
208210
-- * List comments in a repository
209-
-- * Create a comment
210211
-- * Edit a comment
211212
-- * Delete a comment
212213
pullRequestCommentsR,
213214
pullRequestCommentR,
215+
createPullCommentR,
214216

215217
-- ** Pull request reviews
216218
-- | See <https://developer.github.com/v3/pulls/reviews/>

src/GitHub/Data/Comments.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,22 @@ instance Binary EditComment
6464

6565
instance ToJSON EditComment where
6666
toJSON (EditComment b) = object [ "body" .= b ]
67+
68+
data NewPullComment = NewPullComment
69+
{ newPullCommentCommit :: !Text
70+
, newPullCommentPath :: !Text
71+
, newPullCommentPosition :: !Int
72+
, newPullCommentBody :: !Text
73+
}
74+
deriving (Show, Data, Typeable, Eq, Ord, Generic)
75+
76+
instance NFData NewPullComment where rnf = genericRnf
77+
instance Binary NewPullComment
78+
79+
instance ToJSON NewPullComment where
80+
toJSON (NewPullComment c path pos b) =
81+
object [ "body" .= b
82+
, "commit_id" .= c
83+
, "path" .= path
84+
, "position" .= pos
85+
]

src/GitHub/Data/PullRequests.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ data SimplePullRequest = SimplePullRequest
3232
, simplePullRequestUser :: !SimpleUser
3333
, simplePullRequestPatchUrl :: !URL
3434
, simplePullRequestState :: !IssueState
35-
, simplePullRequestNumber :: !Int
35+
, simplePullRequestNumber :: !IssueNumber
3636
, simplePullRequestHtmlUrl :: !URL
3737
, simplePullRequestUpdatedAt :: !UTCTime
3838
, simplePullRequestBody :: !(Maybe Text)
@@ -57,7 +57,7 @@ data PullRequest = PullRequest
5757
, pullRequestUser :: !SimpleUser
5858
, pullRequestPatchUrl :: !URL
5959
, pullRequestState :: !IssueState
60-
, pullRequestNumber :: !Int
60+
, pullRequestNumber :: !IssueNumber
6161
, pullRequestHtmlUrl :: !URL
6262
, pullRequestUpdatedAt :: !UTCTime
6363
, pullRequestBody :: !(Maybe Text)

src/GitHub/Endpoints/PullRequests.hs

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ module GitHub.Endpoints.PullRequests (
1212
pullRequest',
1313
pullRequest,
1414
pullRequestR,
15+
pullRequestDiff',
16+
pullRequestDiff,
17+
pullRequestDiffR,
18+
pullRequestPatch',
19+
pullRequestPatch,
20+
pullRequestPatchR,
1521
createPullRequest,
1622
createPullRequestR,
1723
updatePullRequest,
@@ -33,6 +39,7 @@ import GitHub.Data
3339
import GitHub.Internal.Prelude
3440
import GitHub.Request
3541
import Prelude ()
42+
import Data.ByteString.Lazy (ByteString)
3643

3744
-- | All open pull requests for the repo, by owner and repo name.
3845
--
@@ -60,25 +67,60 @@ pullRequestsForR user repo opts = pagedQuery
6067
["repos", toPathPart user, toPathPart repo, "pulls"]
6168
(prModToQueryString opts)
6269

70+
-- | Obtain the diff of a pull request
71+
-- See <https://developer.github.com/v3/pulls/#get-a-single-pull-request>
72+
pullRequestDiff' :: Maybe Auth -> Name Owner -> Name Repo -> IssueNumber -> IO (Either Error ByteString)
73+
pullRequestDiff' auth user repo prid =
74+
executeRequestMaybe auth $ pullRequestDiffR user repo prid
75+
76+
-- | Obtain the diff of a pull request
77+
-- See <https://developer.github.com/v3/pulls/#get-a-single-pull-request>
78+
pullRequestDiff :: Name Owner -> Name Repo -> IssueNumber -> IO (Either Error ByteString)
79+
pullRequestDiff = pullRequestDiff' Nothing
80+
81+
-- | Query a single pull request to obtain the diff
82+
-- See <https://developer.github.com/v3/pulls/#get-a-single-pull-request>
83+
pullRequestDiffR :: Name Owner -> Name Repo -> IssueNumber -> GenRequest 'MtDiff rw ByteString
84+
pullRequestDiffR user repo prid =
85+
Query ["repos", toPathPart user, toPathPart repo, "pulls", toPathPart prid] []
86+
87+
-- | Obtain the patch of a pull request
88+
--
89+
-- See <https://developer.github.com/v3/pulls/#get-a-single-pull-request>
90+
pullRequestPatch' :: Maybe Auth -> Name Owner -> Name Repo -> IssueNumber -> IO (Either Error ByteString)
91+
pullRequestPatch' auth user repo prid =
92+
executeRequestMaybe auth $ pullRequestPatchR user repo prid
93+
94+
-- | Obtain the patch of a pull request
95+
-- See <https://developer.github.com/v3/pulls/#get-a-single-pull-request>
96+
pullRequestPatch :: Name Owner -> Name Repo -> IssueNumber -> IO (Either Error ByteString)
97+
pullRequestPatch = pullRequestPatch' Nothing
98+
99+
-- | Query a single pull request to obtain the patch
100+
-- See <https://developer.github.com/v3/pulls/#get-a-single-pull-request>
101+
pullRequestPatchR :: Name Owner -> Name Repo -> IssueNumber -> GenRequest 'MtPatch rw ByteString
102+
pullRequestPatchR user repo prid =
103+
Query ["repos", toPathPart user, toPathPart repo, "pulls", toPathPart prid] []
104+
63105
-- | A detailed pull request, which has much more information. This takes the
64106
-- repo owner and name along with the number assigned to the pull request.
65107
-- With authentification.
66108
--
67109
-- > pullRequest' (Just ("github-username", "github-password")) "thoughtbot" "paperclip" 562
68-
pullRequest' :: Maybe Auth -> Name Owner -> Name Repo -> Id PullRequest -> IO (Either Error PullRequest)
110+
pullRequest' :: Maybe Auth -> Name Owner -> Name Repo -> IssueNumber -> IO (Either Error PullRequest)
69111
pullRequest' auth user repo prid =
70112
executeRequestMaybe auth $ pullRequestR user repo prid
71113

72114
-- | A detailed pull request, which has much more information. This takes the
73115
-- repo owner and name along with the number assigned to the pull request.
74116
--
75117
-- > pullRequest "thoughtbot" "paperclip" 562
76-
pullRequest :: Name Owner -> Name Repo -> Id PullRequest -> IO (Either Error PullRequest)
118+
pullRequest :: Name Owner -> Name Repo -> IssueNumber -> IO (Either Error PullRequest)
77119
pullRequest = pullRequest' Nothing
78120

79121
-- | Query a single pull request.
80122
-- See <https://developer.github.com/v3/pulls/#get-a-single-pull-request>
81-
pullRequestR :: Name Owner -> Name Repo -> Id PullRequest -> Request k PullRequest
123+
pullRequestR :: Name Owner -> Name Repo -> IssueNumber -> Request k PullRequest
82124
pullRequestR user repo prid =
83125
query ["repos", toPathPart user, toPathPart repo, "pulls", toPathPart prid] []
84126

@@ -100,15 +142,15 @@ createPullRequestR user repo cpr =
100142
command Post ["repos", toPathPart user, toPathPart repo, "pulls"] (encode cpr)
101143

102144
-- | Update a pull request
103-
updatePullRequest :: Auth -> Name Owner -> Name Repo -> Id PullRequest -> EditPullRequest -> IO (Either Error PullRequest)
145+
updatePullRequest :: Auth -> Name Owner -> Name Repo -> IssueNumber -> EditPullRequest -> IO (Either Error PullRequest)
104146
updatePullRequest auth user repo prid epr =
105147
executeRequest auth $ updatePullRequestR user repo prid epr
106148

107149
-- | Update a pull request.
108150
-- See <https://developer.github.com/v3/pulls/#update-a-pull-request>
109151
updatePullRequestR :: Name Owner
110152
-> Name Repo
111-
-> Id PullRequest
153+
-> IssueNumber
112154
-> EditPullRequest
113155
-> Request 'RW PullRequest
114156
updatePullRequestR user repo prid epr =
@@ -119,20 +161,20 @@ updatePullRequestR user repo prid epr =
119161
-- With authentification.
120162
--
121163
-- > pullRequestCommits' (Just ("github-username", "github-password")) "thoughtbot" "paperclip" 688
122-
pullRequestCommits' :: Maybe Auth -> Name Owner -> Name Repo -> Id PullRequest -> IO (Either Error (Vector Commit))
164+
pullRequestCommits' :: Maybe Auth -> Name Owner -> Name Repo -> IssueNumber -> IO (Either Error (Vector Commit))
123165
pullRequestCommits' auth user repo prid =
124166
executeRequestMaybe auth $ pullRequestCommitsR user repo prid FetchAll
125167

126168
-- | All the commits on a pull request, given the repo owner, repo name, and
127169
-- the number of the pull request.
128170
--
129171
-- > pullRequestCommits "thoughtbot" "paperclip" 688
130-
pullRequestCommitsIO :: Name Owner -> Name Repo -> Id PullRequest -> IO (Either Error (Vector Commit))
172+
pullRequestCommitsIO :: Name Owner -> Name Repo -> IssueNumber -> IO (Either Error (Vector Commit))
131173
pullRequestCommitsIO = pullRequestCommits' Nothing
132174

133175
-- | List commits on a pull request.
134176
-- See <https://developer.github.com/v3/pulls/#list-commits-on-a-pull-request>
135-
pullRequestCommitsR :: Name Owner -> Name Repo -> Id PullRequest -> FetchCount -> Request k (Vector Commit)
177+
pullRequestCommitsR :: Name Owner -> Name Repo -> IssueNumber -> FetchCount -> Request k (Vector Commit)
136178
pullRequestCommitsR user repo prid =
137179
pagedQuery ["repos", toPathPart user, toPathPart repo, "pulls", toPathPart prid, "commits"] []
138180

@@ -141,20 +183,20 @@ pullRequestCommitsR user repo prid =
141183
-- With authentification.
142184
--
143185
-- > pullRequestFiles' (Just ("github-username", "github-password")) "thoughtbot" "paperclip" 688
144-
pullRequestFiles' :: Maybe Auth -> Name Owner -> Name Repo -> Id PullRequest -> IO (Either Error (Vector File))
186+
pullRequestFiles' :: Maybe Auth -> Name Owner -> Name Repo -> IssueNumber -> IO (Either Error (Vector File))
145187
pullRequestFiles' auth user repo prid =
146188
executeRequestMaybe auth $ pullRequestFilesR user repo prid FetchAll
147189

148190
-- | The individual files that a pull request patches. Takes the repo owner and
149191
-- name, plus the number assigned to the pull request.
150192
--
151193
-- > pullRequestFiles "thoughtbot" "paperclip" 688
152-
pullRequestFiles :: Name Owner -> Name Repo -> Id PullRequest -> IO (Either Error (Vector File))
194+
pullRequestFiles :: Name Owner -> Name Repo -> IssueNumber -> IO (Either Error (Vector File))
153195
pullRequestFiles = pullRequestFiles' Nothing
154196

155197
-- | List pull requests files.
156198
-- See <https://developer.github.com/v3/pulls/#list-pull-requests-files>
157-
pullRequestFilesR :: Name Owner -> Name Repo -> Id PullRequest -> FetchCount -> Request k (Vector File)
199+
pullRequestFilesR :: Name Owner -> Name Repo -> IssueNumber -> FetchCount -> Request k (Vector File)
158200
pullRequestFilesR user repo prid =
159201
pagedQuery ["repos", toPathPart user, toPathPart repo, "pulls", toPathPart prid, "files"] []
160202

src/GitHub/Endpoints/PullRequests/Comments.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module GitHub.Endpoints.PullRequests.Comments (
1010
pullRequestCommentsR,
1111
pullRequestComment,
1212
pullRequestCommentR,
13+
createPullComment,
14+
createPullCommentR,
1315
module GitHub.Data,
1416
) where
1517

@@ -43,3 +45,21 @@ pullRequestComment user repo cid =
4345
pullRequestCommentR :: Name Owner -> Name Repo -> Id Comment -> Request k Comment
4446
pullRequestCommentR user repo cid =
4547
query ["repos", toPathPart user, toPathPart repo, "pulls", "comments", toPathPart cid] []
48+
49+
-- | Create a new comment.
50+
--
51+
-- > createPullComment (User (user, password)) user repo issue commit path position
52+
-- > "some words"
53+
createPullComment :: Auth -> Name Owner -> Name Repo -> IssueNumber -> Text -> Text -> Int -> Text
54+
-> IO (Either Error Comment)
55+
createPullComment auth user repo iss commit path position body =
56+
executeRequest auth $ createPullCommentR user repo iss commit path position body
57+
58+
-- | Create a comment.
59+
--
60+
-- See <https://developer.github.com/v3/pulls/comments/#create-a-comment>
61+
createPullCommentR :: Name Owner -> Name Repo -> IssueNumber -> Text -> Text -> Int -> Text -> Request 'RW Comment
62+
createPullCommentR user repo iss commit path position body =
63+
command Post parts (encode $ NewPullComment commit path position body)
64+
where
65+
parts = ["repos", toPathPart user, toPathPart repo, "pulls", toPathPart iss, "comments"]

src/GitHub/Endpoints/Repos.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module GitHub.Endpoints.Repos (
3838
createRepoR,
3939
createOrganizationRepo',
4040
createOrganizationRepoR,
41+
forkExistingRepo',
4142
forkExistingRepoR,
4243

4344
-- ** Edit
@@ -167,6 +168,11 @@ createRepoR :: NewRepo -> Request 'RW Repo
167168
createRepoR nrepo =
168169
command Post ["user", "repos"] (encode nrepo)
169170

171+
-- | Fork an existing repository.
172+
forkExistingRepo' :: Auth -> Name Owner -> Name Repo -> Maybe (Name Owner) -> IO (Either Error Repo)
173+
forkExistingRepo' auth owner repo morg =
174+
executeRequest auth $ forkExistingRepoR owner repo morg
175+
170176
-- | Fork an existing repository.
171177
-- See <https://developer.github.com/v3/repos/forks/#create-a-fork>
172178
-- TODO: The third paramater (an optional Organisation) is not used yet.

0 commit comments

Comments
 (0)