From 5ccdf9ba1d44fa69f49b1d5858b8c6be88d33302 Mon Sep 17 00:00:00 2001 From: "Sean T. Allen" Date: Fri, 4 Dec 2026 03:00:00 -0500 Subject: [PATCH] Return issue comments and pull request files as paginated lists GetIssueComments and GetPullRequestFiles made one request and returned that page as an array, dropping GitHub's Link header. An issue with more comments than fit on a page, or a pull request with more changed files, came back incomplete with no way to fetch the rest or tell that more existed. Both operations, and Issue.get_comments() and PullRequest.get_files(), now return a PaginatedList like every other list operation. This removes the public single-page array types and converters. Closes #221 --- ...e-issue-comments-and-pull-request-files.md | 35 ++++ examples/README.md | 4 +- .../get_issue_comments_oo.pony | 2 +- examples/get-issue-comments-oo/main.pony | 18 +- .../get_issue_comments.pony | 2 +- examples/get-issue-comments/main.pony | 13 +- .../get_pull_request_files_oo.pony | 2 +- examples/get-pull-request-files-oo/main.pony | 18 +- .../get_pull_request_files.pony | 2 +- examples/get-pull-request-files/main.pony | 13 +- github_rest_api/_test.pony | 3 + github_rest_api/_test_json_converters.pony | 26 +++ .../_test_operation_request_lines.pony | 8 +- .../_test_paginated_comments_files.pony | 174 ++++++++++++++++++ github_rest_api/issue.pony | 6 +- github_rest_api/issue_comment.pony | 54 ++---- github_rest_api/pull_request.pony | 6 +- github_rest_api/pull_request_file.pony | 56 ++---- 18 files changed, 334 insertions(+), 108 deletions(-) create mode 100644 .release-notes/paginate-issue-comments-and-pull-request-files.md create mode 100644 github_rest_api/_test_paginated_comments_files.pony diff --git a/.release-notes/paginate-issue-comments-and-pull-request-files.md b/.release-notes/paginate-issue-comments-and-pull-request-files.md new file mode 100644 index 0000000..b4da52e --- /dev/null +++ b/.release-notes/paginate-issue-comments-and-pull-request-files.md @@ -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`. diff --git a/examples/README.md b/examples/README.md index 6f14fe5..eda79ab 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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()` | @@ -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 diff --git a/examples/get-issue-comments-oo/get_issue_comments_oo.pony b/examples/get-issue-comments-oo/get_issue_comments_oo.pony index b255a2f..42e5ded 100644 --- a/examples/get-issue-comments-oo/get_issue_comments_oo.pony +++ b/examples/get-issue-comments-oo/get_issue_comments_oo.pony @@ -1,4 +1,4 @@ """ Demonstrates fetching comments on a GitHub issue using the object -convenience method. +convenience method, following each page of results. """ diff --git a/examples/get-issue-comments-oo/main.pony b/examples/get-issue-comments-oo/main.pony index 36ce8b6..8bd8262 100644 --- a/examples/get-issue-comments-oo/main.pony +++ b/examples/get-issue-comments-oo/main.pony @@ -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)) @@ -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 @@ -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()) diff --git a/examples/get-issue-comments/get_issue_comments.pony b/examples/get-issue-comments/get_issue_comments.pony index 376a2f1..385fdfd 100644 --- a/examples/get-issue-comments/get_issue_comments.pony +++ b/examples/get-issue-comments/get_issue_comments.pony @@ -1,4 +1,4 @@ """ Demonstrates fetching comments on a GitHub issue using the -GetIssueComments operation. +GetIssueComments operation, following each page of results. """ diff --git a/examples/get-issue-comments/main.pony b/examples/get-issue-comments/main.pony index 7735fbc..e14ee83 100644 --- a/examples/get-issue-comments/main.pony +++ b/examples/get-issue-comments/main.pony @@ -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) => @@ -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()) diff --git a/examples/get-pull-request-files-oo/get_pull_request_files_oo.pony b/examples/get-pull-request-files-oo/get_pull_request_files_oo.pony index f049cc9..e765564 100644 --- a/examples/get-pull-request-files-oo/get_pull_request_files_oo.pony +++ b/examples/get-pull-request-files-oo/get_pull_request_files_oo.pony @@ -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. """ diff --git a/examples/get-pull-request-files-oo/main.pony b/examples/get-pull-request-files-oo/main.pony index ee1a12d..59bd6fe 100644 --- a/examples/get-pull-request-files-oo/main.pony +++ b/examples/get-pull-request-files-oo/main.pony @@ -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)) @@ -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 @@ -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") diff --git a/examples/get-pull-request-files/get_pull_request_files.pony b/examples/get-pull-request-files/get_pull_request_files.pony index e2a4040..1f530c8 100644 --- a/examples/get-pull-request-files/get_pull_request_files.pony +++ b/examples/get-pull-request-files/get_pull_request_files.pony @@ -1,4 +1,4 @@ """ Demonstrates fetching files changed in a pull request using the -GetPullRequestFiles operation. +GetPullRequestFiles operation, following each page of results. """ diff --git a/examples/get-pull-request-files/main.pony b/examples/get-pull-request-files/main.pony index dbeb580..84d73cc 100644 --- a/examples/get-pull-request-files/main.pony +++ b/examples/get-pull-request-files/main.pony @@ -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) => @@ -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") diff --git a/github_rest_api/_test.pony b/github_rest_api/_test.pony index 7f403fa..efd62ad 100644 --- a/github_rest_api/_test.pony +++ b/github_rest_api/_test.pony @@ -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) @@ -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) diff --git a/github_rest_api/_test_json_converters.pony b/github_rest_api/_test_json_converters.pony index 4d45bda..dbadb18 100644 --- a/github_rest_api/_test_json_converters.pony +++ b/github_rest_api/_test_json_converters.pony @@ -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" diff --git a/github_rest_api/_test_operation_request_lines.pony b/github_rest_api/_test_operation_request_lines.pony index dc3a373..6e745f1 100644 --- a/github_rest_api/_test_operation_request_lines.pony +++ b/github_rest_api/_test_operation_request_lines.pony @@ -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)) @@ -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)) diff --git a/github_rest_api/_test_paginated_comments_files.pony b/github_rest_api/_test_paginated_comments_files.pony new file mode 100644 index 0000000..fab98ab --- /dev/null +++ b/github_rest_api/_test_paginated_comments_files.pony @@ -0,0 +1,174 @@ +use "json" +use "net" +use "pony_test" +use "promises" +use req = "request" + +class \nodoc\ iso _TestFollowsNextPage[A: Any val] is UnitTest + """ + Calls an operation against a mock server whose first page links to a second + page with `rel="next"`. Passes when the first page holds `first`, following + its `next_page()` gives a page that holds `second`, and that page has no next + page. + """ + let _name: String + let _port: String + let _page1_body: String + let _page2_body: String + let _first: String + let _second: String + let _value: {(A): String} val + let _call: {(String, req.Credentials) + : Promise[(PaginatedList[A] | req.RequestError)]} val + + new iso create( + name': String, + port: String, + page1_body: String, + page2_body: String, + first: String, + second: String, + value: {(A): String} val, + call: {(String, req.Credentials) + : Promise[(PaginatedList[A] | req.RequestError)]} val) + => + _name = name' + _port = port + _page1_body = page1_body + _page2_body = page2_body + _first = first + _second = second + _value = value + _call = call + + fun name(): String => + _name + + fun ref apply(h: TestHelper) ? => + let sslctx = _TestSSLContext(h)? + let creds = + req.Credentials(TCPConnectAuth(h.env.root) where ssl_ctx' = sslctx) + let base_url = _TestURL(_TestHost(), _port, "") + let page2_url = _TestURL(_TestHost(), _port, "/page2") + let page1_body = _page1_body + let page2_body = _page2_body + let first = _first + let second = _second + let value = _value + let call = _call + + let responder: _Responder = + {(request: String)(page1_body, page2_body, page2_url): String => + if request.at("GET /page2 ") then + "HTTP/1.1 200 OK\r\n" + + "Content-Length: " + page2_body.size().string() + "\r\n" + + "\r\n" + + page2_body + else + "HTTP/1.1 200 OK\r\n" + + "Link: <" + page2_url + ">; rel=\"next\"\r\n" + + "Content-Length: " + page1_body.size().string() + "\r\n" + + "\r\n" + + page1_body + end + } val + + h.long_test(10_000_000_000) + let listener = + _MockHTTPListener( + h, + _port, + sslctx, + responder, + {()(h, creds, base_url, first, second, value, call) => + call(base_url, creds).next[None]( + {(result: (PaginatedList[A] | req.RequestError))( + h, first, second, value) => + match \exhaustive\ result + | let page1: PaginatedList[A] => + _TestCheckPage[A](h, page1, first, value) + match \exhaustive\ page1.next_page() + | let next: Promise[(PaginatedList[A] | req.RequestError)] => + next.next[None]( + {(result2: (PaginatedList[A] | req.RequestError))( + h, second, value) => + match \exhaustive\ result2 + | let page2: PaginatedList[A] => + _TestCheckPage[A](h, page2, second, value) + h.assert_true( + page2.next_page() is None, + "page 2 should have no next page") + h.complete(true) + | let e: req.RequestError => + h.fail("page 2 error: " + e.message) + h.complete(false) + end + }) + | None => + h.fail("page 1 should have a next page") + h.complete(false) + end + | let e: req.RequestError => + h.fail("page 1 error: " + e.message) + h.complete(false) + end + }) + } val) + h.dispose_when_done(listener) + +primitive \nodoc\ _TestCheckPage[A: Any val] + fun apply( + h: TestHelper, + page: PaginatedList[A], + expected: String, + value: {(A): String} val) + => + try + h.assert_eq[USize](1, page.results.size()) + h.assert_eq[String](expected, value(page.results(0)?)) + else + h.fail("page should hold one result") + end + +primitive \nodoc\ _TestPaginatedCommentsFilesTests + fun tests(test: PonyTest) => + test(_TestFollowsNextPage[IssueComment]( + "issue-comments/follows-next-page", + "48239", + _TestIssueCommentJSON("first"), + _TestIssueCommentJSON("second"), + "body_first", + "body_second", + {(c: IssueComment): String => c.body } val, + {(base_url: String, creds: req.Credentials) + : Promise[(PaginatedList[IssueComment] | req.RequestError)] + => + GetIssueComments._apply_at(base_url, "o", "r", 7, creds) + } val)) + test(_TestFollowsNextPage[PullRequestFile]( + "pull-request-files/follows-next-page", + "48240", + """[{"filename":"first.pony"}]""", + """[{"filename":"second.pony"}]""", + "first.pony", + "second.pony", + {(f: PullRequestFile): String => f.filename } val, + {(base_url: String, creds: req.Credentials) + : Promise[(PaginatedList[PullRequestFile] | req.RequestError)] + => + GetPullRequestFiles._apply_at(base_url, "o", "r", 7, creds) + } val)) + +primitive \nodoc\ _TestIssueCommentJSON + """ + A JSON array holding one issue comment whose field values end in `b`. + """ + fun apply(b: String): String => + JSONArray + .push( + JSONObject + .update("body", "body_" + b) + .update("url", "url_" + b) + .update("html_url", "html_" + b) + .update("issue_url", "issue_" + b)) + .print() diff --git a/github_rest_api/issue.pony b/github_rest_api/issue.pony index 4f78bc1..7d88508 100644 --- a/github_rest_api/issue.pony +++ b/github_rest_api/issue.pony @@ -163,9 +163,11 @@ class val Issue CreateIssueComment.by_url( comments_url, comment, _creds) - fun get_comments(): Promise[IssueCommentsOrError] => + fun get_comments() + : Promise[(PaginatedList[IssueComment] | req.RequestError)] + => """ - Fetches all comments on this issue. + Fetches the comments on this issue as a paginated list. """ GetIssueComments.by_url(comments_url, _creds) diff --git a/github_rest_api/issue_comment.pony b/github_rest_api/issue_comment.pony index 972c3a4..bd69d30 100644 --- a/github_rest_api/issue_comment.pony +++ b/github_rest_api/issue_comment.pony @@ -4,10 +4,6 @@ use req = "request" use ut = "uri/template" type IssueCommentOrError is (IssueComment | req.RequestError) -type IssueComments is Array[IssueComment] val - -type IssueCommentsOrError is - (IssueComments | req.RequestError) class val IssueComment """ @@ -86,13 +82,14 @@ primitive CreateIssueComment primitive GetIssueComments """ - Fetches all comments on an issue. + Fetches the comments on an issue as a paginated list. """ fun apply( owner: String, repo: String, number: I64, - creds: req.Credentials): Promise[IssueCommentsOrError] + creds: req.Credentials) + : Promise[(PaginatedList[IssueComment] | req.RequestError)] => _apply_at("https://api.github.com", owner, repo, number, creds) @@ -101,7 +98,8 @@ primitive GetIssueComments owner: String, repo: String, number: I64, - creds: req.Credentials): Promise[IssueCommentsOrError] + creds: req.Credentials) + : Promise[(PaginatedList[IssueComment] | req.RequestError)] => let u = IssueCommentsURL._at(base_url, owner, repo, number) @@ -109,7 +107,7 @@ primitive GetIssueComments | let u': String => by_url(u', creds) | let e: ut.URITemplateParseError => - Promise[IssueCommentsOrError] .> apply( + Promise[(PaginatedList[IssueComment] | req.RequestError)] .> apply( req.RequestError( where message' = e.message)) end @@ -117,19 +115,18 @@ primitive GetIssueComments fun by_url( url: String, creds: req.Credentials - ): Promise[IssueCommentsOrError] + ): Promise[(PaginatedList[IssueComment] | req.RequestError)] => """ - Fetches issue comments from a URL. + Fetches issue comments from a URL as a paginated list. """ - let p = Promise[IssueCommentsOrError] - let r = - req.ResultReceiver[IssueComments]( - creds, - p, - IssueCommentsJSONConverter) + let plc = + PaginatedListJSONConverter[IssueComment]( + creds, IssueCommentJSONConverter) + let p = Promise[(PaginatedList[IssueComment] | req.RequestError)] + let r = PaginatedResultReceiver[IssueComment](creds, p, plc) - req.JSONRequester.get(creds, url, r) + req.LinkedJSONRequester(creds, url, r) p primitive IssueCommentsURL @@ -183,26 +180,3 @@ primitive IssueCommentJSONConverter is let issue_url = json("issue_url").as_string()? IssueComment(creds, body, url, html_url, issue_url) - -primitive IssueCommentsJSONConverter is - req.JSONConverter[Array[IssueComment] val] - """ - Converts a JSON array of issue comment objects into an Array - of IssueComment. - """ - fun apply( - json: JSONNav, - creds: req.Credentials): Array[IssueComment] val ? - => - """ - Builds an IssueComment array from JSON. - """ - let comments = recover trn Array[IssueComment] end - - for i in json.as_array()?.values() do - let comment = - IssueCommentJSONConverter(JSONNav(i), creds)? - comments.push(comment) - end - - consume comments diff --git a/github_rest_api/pull_request.pony b/github_rest_api/pull_request.pony index c7e4bdf..fad71a9 100644 --- a/github_rest_api/pull_request.pony +++ b/github_rest_api/pull_request.pony @@ -46,9 +46,11 @@ class val PullRequest comments_url = comments_url' files_url = url + "/files" - fun get_files(): Promise[PullRequestFilesOrError] => + fun get_files() + : Promise[(PaginatedList[PullRequestFile] | req.RequestError)] + => """ - Fetches the files changed in this pull request. + Fetches the files changed in this pull request as a paginated list. """ GetPullRequestFiles.by_url(files_url, _creds) diff --git a/github_rest_api/pull_request_file.pony b/github_rest_api/pull_request_file.pony index 9c082c6..4feed50 100644 --- a/github_rest_api/pull_request_file.pony +++ b/github_rest_api/pull_request_file.pony @@ -3,11 +3,6 @@ use "promises" use req = "request" use ut = "uri/template" -type PullRequestFiles is Array[PullRequestFile] val - -type PullRequestFilesOrError is - (PullRequestFiles | req.RequestError) - class val PullRequestFile """ A file changed in a pull request. Currently only captures the filename. @@ -21,13 +16,15 @@ class val PullRequestFile primitive GetPullRequestFiles """ - Fetches the list of files changed in a pull request. + Fetches the files changed in a pull request as a paginated list. GitHub + returns at most 3000 files for a pull request, even across pages. """ fun apply( owner: String, repo: String, number: I64, - creds: req.Credentials): Promise[PullRequestFilesOrError] + creds: req.Credentials) + : Promise[(PaginatedList[PullRequestFile] | req.RequestError)] => _apply_at("https://api.github.com", owner, repo, number, creds) @@ -36,7 +33,8 @@ primitive GetPullRequestFiles owner: String, repo: String, number: I64, - creds: req.Credentials): Promise[PullRequestFilesOrError] + creds: req.Credentials) + : Promise[(PaginatedList[PullRequestFile] | req.RequestError)] => match \exhaustive\ ut.URITemplateParse( base_url + "/repos" + @@ -49,7 +47,7 @@ primitive GetPullRequestFiles let u: String val = tpl.expand(vars) by_url(u, creds) | let e: ut.URITemplateParseError => - Promise[PullRequestFilesOrError] .> apply( + Promise[(PaginatedList[PullRequestFile] | req.RequestError)] .> apply( req.RequestError( where message' = e.message)) end @@ -57,41 +55,27 @@ primitive GetPullRequestFiles fun by_url( url: String, creds: req.Credentials - ): Promise[PullRequestFilesOrError] + ): Promise[(PaginatedList[PullRequestFile] | req.RequestError)] => """ - Fetches pull request files from a URL. + Fetches pull request files from a URL as a paginated list. """ - let p = Promise[PullRequestFilesOrError] - let r = - req.ResultReceiver[PullRequestFiles]( - creds, - p, - PullRequestFilesJSONConverter) + let plc = + PaginatedListJSONConverter[PullRequestFile]( + creds, PullRequestFileJSONConverter) + let p = Promise[(PaginatedList[PullRequestFile] | req.RequestError)] + let r = PaginatedResultReceiver[PullRequestFile](creds, p, plc) - req.JSONRequester.get(creds, url, r) + req.LinkedJSONRequester(creds, url, r) p -primitive PullRequestFilesJSONConverter is - req.JSONConverter[Array[PullRequestFile] val] +primitive PullRequestFileJSONConverter is req.JSONConverter[PullRequestFile] """ - Converts a JSON array of pull request file objects into an Array of + Converts a JSON object from the pull request files API into a PullRequestFile. """ - fun apply( - json: JSONNav, - creds: req.Credentials): Array[PullRequestFile] val ? - => + fun apply(json: JSONNav, creds: req.Credentials): PullRequestFile ? => """ - Builds a PullRequestFile array from JSON. + Builds a PullRequestFile from its JSON representation. """ - let files = recover trn Array[PullRequestFile] end - - for i in json.as_array()?.values() do - let json_i = JSONNav(i) - let filename = json_i("filename").as_string()? - let file = PullRequestFile(creds, filename) - files.push(file) - end - - consume files + PullRequestFile(creds, json("filename").as_string()?)