Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .release-notes/paginate-issue-comments-and-pull-request-files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Return issue comments and pull request files as paginated lists

`GetIssueComments`, `GetPullRequestFiles`, `Issue.get_comments()`, and `PullRequest.get_files()` made one request and returned that page as an array. GitHub returns these lists in pages, so an issue with more comments than fit on one page, or a pull request with more changed files, came back incomplete with no sign that more existed.

They now return a `PaginatedList`, like the library's other list operations. Read `results` for the current page and call `next_page()` to fetch the next one; `next_page()` returns `None` on the last page. Code that needs every comment or file must keep following `next_page()` until it returns `None`, as the `get-issue-comments` and `get-pull-request-files` examples do.

Before:

```pony
GetIssueComments(owner, repo, number, creds)
.next[None]({(r: IssueCommentsOrError) =>
match r
| let comments: Array[IssueComment] val =>
for c in comments.values() do env.out.print(c.body) end
end
})
```

After:

```pony
GetIssueComments(owner, repo, number, creds)
.next[None]({(r: (PaginatedList[IssueComment] | RequestError)) =>
match r
| let page: PaginatedList[IssueComment] =>
for c in page.results.values() do env.out.print(c.body) end
// page.next_page() returns a Promise for the next page, or None on the
// last page
end
})
```

`GetPullRequestFiles` and `PullRequest.get_files()` change the same way with `PaginatedList[PullRequestFile]`.

These single-page array types and converters are removed: `IssueComments`, `IssueCommentsOrError`, `IssueCommentsJSONConverter`, `PullRequestFiles`, `PullRequestFilesOrError`, and `PullRequestFilesJSONConverter`. `PullRequestFileJSONConverter` is new and converts a single file object, the way `IssueCommentJSONConverter` converts a single comment. Code that used a removed array converter with its own request can call `GetIssueComments.by_url` or `GetPullRequestFiles.by_url` instead, or build a `PaginatedListJSONConverter` from `IssueCommentJSONConverter` or `PullRequestFileJSONConverter`.
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Each example has its own `Makefile`. Build with `make ssl=3.0.x` (or the SSL ver
| `get-issue-oo` | OO | Same as `get-issue`, chaining through `Repository.get_issue()` |
| `get-issues` | Functional | List issues in a repository with sort, direction, and filter options |
| `get-issues-oo` | OO | Same as `get-issues`, chaining through `Repository.get_issues()` |
| `get-issue-comments` | Functional | List comments on an issue |
| `get-issue-comments` | Functional | List comments on an issue with pagination |
| `get-issue-comments-oo` | OO | Same as `get-issue-comments`, chaining through `Issue.get_comments()` |
| `create-issue-comment` | Functional | Create a comment on an issue |
| `create-issue-comment-oo` | OO | Same as `create-issue-comment`, chaining through `Issue.create_comment()` |
Expand All @@ -47,7 +47,7 @@ Each example has its own `Makefile`. Build with `make ssl=3.0.x` (or the SSL ver
|---------|-------|-------------|
| `get-pull-request` | Functional | Fetch a pull request by number |
| `get-pull-request-oo` | OO | Same as `get-pull-request`, chaining through `Repository.get_pull_request()` |
| `get-pull-request-files` | Functional | List files changed in a pull request |
| `get-pull-request-files` | Functional | List files changed in a pull request with pagination |
| `get-pull-request-files-oo` | OO | Same as `get-pull-request-files`, chaining through `PullRequest.get_files()` |

## Commits
Expand Down
2 changes: 1 addition & 1 deletion examples/get-issue-comments-oo/get_issue_comments_oo.pony
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Demonstrates fetching comments on a GitHub issue using the object
convenience method.
convenience method, following each page of results.
"""
18 changes: 12 additions & 6 deletions examples/get-issue-comments-oo/main.pony
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ actor Main
GitHub(creds).get_repo(owner, repo)
.flatten_next[IssueOrError](
RetrieveIssue~apply(issue))
.flatten_next[IssueCommentsOrError](
.flatten_next[(PaginatedList[IssueComment] | RequestError)](
RetrieveComments~apply())
.next[None](
PrintIssueComments~apply(env.out))
Expand Down Expand Up @@ -98,13 +98,13 @@ primitive RetrieveComments
"""
fun apply(
i: IssueOrError)
: Promise[IssueCommentsOrError]
: Promise[(PaginatedList[IssueComment] | RequestError)]
=>
match \exhaustive\ i
| let issue: Issue =>
issue.get_comments()
| let e: RequestError =>
Promise[IssueCommentsOrError] .> apply(e)
Promise[(PaginatedList[IssueComment] | RequestError)] .> apply(e)
end

primitive PrintIssueComments
Expand All @@ -113,16 +113,22 @@ primitive PrintIssueComments
"""
fun apply(
out: OutStream,
r: IssueCommentsOrError)
r: (PaginatedList[IssueComment] | RequestError))
=>
match \exhaustive\ r
| let comments: Array[IssueComment] val =>
for c in comments.values() do
| let list: PaginatedList[IssueComment] =>
for c in list.results.values() do
out.print("Comment ==>")
out.print(c.body)
out.print(c.html_url)
out.print(c.issue_url)
end
match list.next_page()
| let promise: Promise[(PaginatedList[IssueComment] | RequestError)] =>
promise.next[None](PrintIssueComments~apply(out))
else
out.print("------- No more comments")
end
| let e: RequestError =>
out.print("Unable to retrieve issue comments")
out.print(e.status.string())
Expand Down
2 changes: 1 addition & 1 deletion examples/get-issue-comments/get_issue_comments.pony
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Demonstrates fetching comments on a GitHub issue using the
GetIssueComments operation.
GetIssueComments operation, following each page of results.
"""
13 changes: 10 additions & 3 deletions examples/get-issue-comments/main.pony
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use "../../github_rest_api/request"
use "cli"
use ct = "constrained_types"
use "net"
use "promises"

actor Main
new create(env: Env) =>
Expand Down Expand Up @@ -78,16 +79,22 @@ primitive PrintIssueComments
"""
fun apply(
out: OutStream,
r: IssueCommentsOrError)
r: (PaginatedList[IssueComment] | RequestError))
=>
match \exhaustive\ r
| let comments: Array[IssueComment] val =>
for c in comments.values() do
| let list: PaginatedList[IssueComment] =>
for c in list.results.values() do
out.print("Comment ==>")
out.print(c.body)
out.print(c.html_url)
out.print(c.issue_url)
end
match list.next_page()
| let promise: Promise[(PaginatedList[IssueComment] | RequestError)] =>
promise.next[None](PrintIssueComments~apply(out))
else
out.print("------- No more comments")
end
| let e: RequestError =>
out.print("Unable to retrieve issue comments")
out.print(e.status.string())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Demonstrates fetching files changed in a pull request using the
object convenience method.
object convenience method, following each page of results.
"""
18 changes: 12 additions & 6 deletions examples/get-pull-request-files-oo/main.pony
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ actor Main
GitHub(creds).get_repo(owner, repo)
.flatten_next[PullRequestOrError](
RetrievePullRequest~apply(pr))
.flatten_next[PullRequestFilesOrError](
.flatten_next[(PaginatedList[PullRequestFile] | RequestError)](
RetrievePullRequestFiles~apply())
.next[None](
PrintPullRequestFiles~apply(env.out))
Expand Down Expand Up @@ -98,13 +98,13 @@ primitive RetrievePullRequestFiles
"""
fun apply(
p: PullRequestOrError)
: Promise[PullRequestFilesOrError]
: Promise[(PaginatedList[PullRequestFile] | RequestError)]
=>
match \exhaustive\ p
| let pr: PullRequest =>
pr.get_files()
| let e: RequestError =>
Promise[PullRequestFilesOrError] .> apply(e)
Promise[(PaginatedList[PullRequestFile] | RequestError)] .> apply(e)
end

primitive PrintPullRequestFiles
Expand All @@ -113,13 +113,19 @@ primitive PrintPullRequestFiles
"""
fun apply(
out: OutStream,
r: PullRequestFilesOrError)
r: (PaginatedList[PullRequestFile] | RequestError))
=>
match \exhaustive\ r
| let files: Array[PullRequestFile] val =>
for f in files.values() do
| let list: PaginatedList[PullRequestFile] =>
for f in list.results.values() do
out.print(f.filename)
end
match list.next_page()
| let promise: Promise[(PaginatedList[PullRequestFile] | RequestError)] =>
promise.next[None](PrintPullRequestFiles~apply(out))
else
out.print("------- No more files")
end
| let e: RequestError =>
out.print(
"Unable to retrieve pull request files")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Demonstrates fetching files changed in a pull request using the
GetPullRequestFiles operation.
GetPullRequestFiles operation, following each page of results.
"""
13 changes: 10 additions & 3 deletions examples/get-pull-request-files/main.pony
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use "../../github_rest_api/request"
use "cli"
use ct = "constrained_types"
use "net"
use "promises"

actor Main
new create(env: Env) =>
Expand Down Expand Up @@ -78,13 +79,19 @@ primitive PrintPullRequestFiles
"""
fun apply(
out: OutStream,
r: PullRequestFilesOrError)
r: (PaginatedList[PullRequestFile] | RequestError))
=>
match \exhaustive\ r
| let files: Array[PullRequestFile] val =>
for f in files.values() do
| let list: PaginatedList[PullRequestFile] =>
for f in list.results.values() do
out.print(f.filename)
end
match list.next_page()
| let promise: Promise[(PaginatedList[PullRequestFile] | RequestError)] =>
promise.next[None](PrintPullRequestFiles~apply(out))
else
out.print("------- No more files")
end
| let e: RequestError =>
out.print(
"Unable to retrieve pull request files")
Expand Down
3 changes: 3 additions & 0 deletions github_rest_api/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ actor \nodoc\ Main is TestList
test(_TestLicenseJSONConverterMissingField)
test(_TestCommitFileJSONConverterPreservesValues)
test(_TestCommitFileJSONConverterMissingField)
test(_TestPullRequestFileJSONConverterPreservesValues)
test(_TestPullRequestFileJSONConverterMissingField)
test(_TestGistChangeStatusJSONConverterPreservesValues)
test(_TestGistChangeStatusJSONConverterMissingField)
test(_TestLabelJSONConverterPreservesValues)
Expand Down Expand Up @@ -115,6 +117,7 @@ actor \nodoc\ Main is TestList
test(_TestRepositoryGetIssuesSendsQuery)
test(_TestRepositoryGetIssuesDefaultQuery)
_OperationRequestLineTests.tests(test)
_TestPaginatedCommentsFilesTests.tests(test)
test(_TestJSONRequesterSendsGitHubHeaders)
test(_TestNoContentRequesterSendsGitHubHeaders)
test(_TestCheckRequesterSendsGitHubHeaders)
Expand Down
26 changes: 26 additions & 0 deletions github_rest_api/_test_json_converters.pony
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,32 @@ class \nodoc\ _TestCommitFileJSONConverterPreservesValues is UnitTest
end
})?

class \nodoc\ _TestPullRequestFileJSONConverterPreservesValues is UnitTest
fun name(): String => "pull-request-file-json-converter/preserves-values"

fun ref apply(h: TestHelper) ? =>
let creds = _TestDummyCredentials(h)
h.for_all[String](
recover val Generators.ascii_printable(1, 20) end)(
{(base, h)(creds) =>
let filename_val: String val = "filename_" + base.clone()
let json = JSONNav(JSONObject.update("filename", filename_val))
try
let file = PullRequestFileJSONConverter(json, creds)?
h.assert_eq[String](filename_val, file.filename)
else
h.fail("converter raised an error")
end
})?

class \nodoc\ _TestPullRequestFileJSONConverterMissingField is UnitTest
fun name(): String => "pull-request-file-json-converter/missing-field"

fun ref apply(h: TestHelper) =>
let base = JSONObject.update("filename", "filename_test")
let required: Array[String] val = ["filename"]
_TestRequiredFields(h, PullRequestFileJSONConverter, base, required)

class \nodoc\ _TestCommitFileJSONConverterMissingField is UnitTest
fun name(): String => "commit-file-json-converter/missing-field"

Expand Down
8 changes: 4 additions & 4 deletions github_rest_api/_test_operation_request_lines.pony
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ primitive \nodoc\ _OperationRequestLineTests
=>
CreateIssueComment._apply_at(base_url, "o", "r", 7, "c", creds)
} val))
test(_TestOperationRequestLine[IssueComments](
test(_TestOperationRequestLine[PaginatedList[IssueComment]](
"operation-request-line/get-issue-comments",
"48225",
"GET /repos/o/r/issues/7/comments HTTP/1.1",
{(base_url: String, creds: req.Credentials)
: Promise[IssueCommentsOrError]
: Promise[(PaginatedList[IssueComment] | req.RequestError)]
=>
GetIssueComments._apply_at(base_url, "o", "r", 7, creds)
} val))
Expand Down Expand Up @@ -285,12 +285,12 @@ primitive \nodoc\ _OperationRequestLineTests
=>
GetPullRequest._apply_at(base_url, "o", "r", 7, creds)
} val))
test(_TestOperationRequestLine[PullRequestFiles](
test(_TestOperationRequestLine[PaginatedList[PullRequestFile]](
"operation-request-line/get-pull-request-files",
"48229",
"GET /repos/o/r/pulls/7/files HTTP/1.1",
{(base_url: String, creds: req.Credentials)
: Promise[PullRequestFilesOrError]
: Promise[(PaginatedList[PullRequestFile] | req.RequestError)]
=>
GetPullRequestFiles._apply_at(base_url, "o", "r", 7, creds)
} val))
Expand Down
Loading
Loading