This is an automated email from the ASF dual-hosted git repository.

HyukjinKwon pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new 26e7b85f697c [SPARK-61331][INFRA] Read every page of the GitHub list 
responses in merge_spark_pr.py
26e7b85f697c is described below

commit 26e7b85f697cf1d67b6c0a864f420456d09f02ca
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Tue Nov 10 17:45:44 2026 +0900

    [SPARK-61331][INFRA] Read every page of the GitHub list responses in 
merge_spark_pr.py
    
    `dev/merge_spark_pr.py` reads three GitHub list endpoints with `get_json`, 
which returns only the first page of a list. GitHub puts 30 items on a page 
unless the request asks for more.
    
    This PR adds `get_json_pages`, which asks for 100 items per page 
(`GITHUB_PAGE_SIZE`, the most GitHub allows) and follows the `rel="next"` URL 
of each page's `Link` header until a page has none, as `gh api --paginate` 
does. It is used for:
    
    - the PR's events (`/issues/<n>/events`), where `find_merge_commit` looks 
for the merge commit;
    - the PR's commits (`/pulls/<n>/commits`), which give the co-authors and, 
when the profile has none, the author's email;
    - the repository's branches (`/branches`), which give the release branches 
to pick into.
    
    The request part of `get_json` moves into `open_github_url`, so that 
`get_json_pages` can read each response's `Link` header. `main()` now reads the 
events only for a closed PR, and the author's profile and the commits only when 
it merges, since nothing else uses them. An open PR's events and a backported 
PR's commits are no longer paged through only to be dropped.
    
    On a merged PR with more than 30 events, the `closed` event that carries 
the merge commit is not on the first page. The script then finds no merge 
commit and treats the PR as unmerged. Instead of offering a backport and the 
JIRA update, it stops at "Pull request N is not mergeable in its current form. 
Continue? (experts only!)". This happened with #60129, which has 38 events, 
with the `closed` event 31st.
    
    The same limit drops the commits after the 30th, so their authors get no 
`Co-authored-by` trailer. It also caps the branches: apache/spark has 31 now, 
so `master` is already off the first page, and a couple more release branches 
would push a release branch off it too, so the script would stop offering it as 
a pick target.
    
    No. It changes only the committers' merge script.
    
    - New doctests for `get_json_pages`, with a fake that sends GitHub-like 
`Link` headers: three pages, the middle one shorter than the first, are all 
read in order, and none of the last page's links back is followed; a one-page 
list without a `Link` header costs one request; and a URL that already has a 
query string gets the page size appended to it.
    - The `main_titles` doctest, the one that drives `main()`, now serves 
`get_json_pages` from its fake. Each run checks the release branches that 
`main()` hands to `default_pick_branch`. It also checks that a merge reads no 
events, and that a backport reads neither the commits nor the author's profile. 
Breaking any of these on purpose makes the doctest fail. All doctests of the 
script pass, 432 of them (426 before this change):
      ```
      python3 -c 'import doctest, sys; sys.path.insert(0, "dev"); import 
merge_spark_pr; print(doctest.testmod(merge_spark_pr))'
      ```
    - Against GitHub, with the page size set to 10 to force several pages, 
`get_json_pages` read as many items as `gh api --paginate`: 38 events of 
#60129, 59 events and 32 commits of #59424, and 31 branches.
    - Manually:
      - `dev/merge_spark_pr.py --dry-run 60129` now prints "Pull request 60129 
has already been merged, assuming you want to backport", finds the merge commit 
`bb0d4b792e99` and reports "Already backported to: branch-4.x". Without this 
change, it stopped at "Pull request 60129 is not mergeable in its current 
form.".
      - `--dry-run 60273`, this PR while it is open, reads through to the JIRA 
check.
    - `ruff check` and `ruff format --check` with ruff 0.14.8.
    
    Generated-by: Claude Code (Claude Opus 5.5)
    
    This pull request and its description were written by Isaac.
    
    Closes #60273 from HyukjinKwon/merge-script-paging.
    
    Authored-by: Hyukjin Kwon <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
    
    (cherry picked from commit a8bae200d3e327ca22c04e0d6391445761615d67)
    Signed-off-by: Hyukjin Kwon <[email protected]>
    
    
    skip-checks: true
---
 dev/merge_spark_pr.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 68 insertions(+), 5 deletions(-)

diff --git a/dev/merge_spark_pr.py b/dev/merge_spark_pr.py
index bc793d91e8ff..15347c8b6761 100755
--- a/dev/merge_spark_pr.py
+++ b/dev/merge_spark_pr.py
@@ -68,6 +68,9 @@ GITHUB_OAUTH_KEY = os.environ.get("GITHUB_OAUTH_KEY")
 
 GITHUB_BASE = "https://github.com/apache/spark/pull";
 GITHUB_API_BASE = "https://api.github.com/repos/apache/spark";
+# Items per page that get_json_pages asks GitHub list endpoints for: the most 
GitHub allows.
+# Without it, GitHub returns 30.
+GITHUB_PAGE_SIZE = 100
 JIRA_BASE = "https://issues.apache.org/jira/browse";
 JIRA_API_BASE = "https://issues.apache.org/jira";
 # Prefix added to temporary branches
@@ -82,12 +85,13 @@ def bold_input(prompt) -> str:
     return input("\033[1m%s\033[0m" % prompt)
 
 
-def get_json(url):
+def open_github_url(url):
+    """Open the GitHub API `url`, with GITHUB_OAUTH_KEY if set, and exit on an 
HTTP error."""
     try:
         request = Request(url)
         if GITHUB_OAUTH_KEY:
             request.add_header("Authorization", "token %s" % GITHUB_OAUTH_KEY)
-        return json.load(urlopen(request))
+        return urlopen(request)
     except HTTPError as e:
         if "X-RateLimit-Remaining" in e.headers and 
e.headers["X-RateLimit-Remaining"] == "0":
             print_error(
@@ -106,6 +110,65 @@ def get_json(url):
         sys.exit(-1)
 
 
+def get_json(url):
+    return json.load(open_github_url(url))
+
+
+def get_json_pages(url):
+    """Return every item of the GitHub list endpoint at `url`, reading all of 
its pages.
+
+    `get_json` reads only the first page, and GitHub fills it with 30 items 
unless asked for
+    more. On a PR with a long history, that misses the `closed` event that 
records the merge
+    commit, so a merged PR looks unmerged (#60129, with 38 events). It also 
drops the commits
+    after the 30th, so their authors get no Co-authored-by, and the release 
branches after
+    the 30th, so they cannot be picked into. Each page asks for 
GITHUB_PAGE_SIZE items, and
+    reading follows the `rel="next"` URL of each page's `Link` header until a 
page has none,
+    as `gh api --paginate` does, so a page with fewer items does not end the 
read early.
+
+    >>> import io
+    >>> from unittest.mock import patch
+    >>> base = "https://api.github.com/repos/apache/spark";
+    >>> def read(path, *pages):
+    ...     # Serve `pages` in order, with Link headers like the ones GitHub 
sends.
+    ...     requested = []
+    ...     def links(*rels):
+    ...         return ", ".join('<%s/p?page=%d>; rel="%s"' % (base, n, rel) 
for rel, n in rels)
+    ...     def open_page(page_url):
+    ...         requested.append(page_url[len(base) :])
+    ...         n = len(requested)
+    ...         page = io.BytesIO(json.dumps(pages[n - 1]).encode())
+    ...         page.headers = {}
+    ...         if n < len(pages):
+    ...             page.headers["Link"] = links(("next", n + 1), ("last", 
len(pages)))
+    ...         elif n > 1:
+    ...             page.headers["Link"] = links(("prev", n - 1), ("first", 1))
+    ...         return page
+    ...     with patch.dict(get_json_pages.__globals__, 
open_github_url=open_page):
+    ...         return get_json_pages(base + path), requested
+
+    Every page is read in order, including the page after one with fewer 
items, and the last
+    page's links back to earlier pages are not followed:
+
+    >>> read("/issues/1/events", ["a", "b"], ["c"], ["d", "e"])
+    (['a', 'b', 'c', 'd', 'e'], ['/issues/1/events?per_page=100', '/p?page=2', 
'/p?page=3'])
+
+    A list that fits on one page comes without a Link header and costs one 
request, and a URL
+    that already has a query string gets the page size appended to it:
+
+    >>> read("/branches?protected=false", ["master", "branch-4.x"])
+    (['master', 'branch-4.x'], ['/branches?protected=false&per_page=100'])
+    """
+    separator = "&" if "?" in url else "?"
+    url = "%s%sper_page=%d" % (url, separator, GITHUB_PAGE_SIZE)
+    items = []
+    while url is not None:
+        response = open_github_url(url)
+        items.extend(json.load(response))
+        next_link = re.search(r'<([^>]*)>;\s*rel="next"', 
response.headers.get("Link") or "")
+        url = next_link.group(1) if next_link else None
+    return items
+
+
 def close_pr(pr_num):
     url = "%s/pulls/%s" % (GITHUB_API_BASE, pr_num)
     data = json.dumps({"state": "closed"}).encode("utf-8")
@@ -617,7 +680,7 @@ def main():
     os.chdir(SPARK_HOME)
     original_head = get_current_ref()
 
-    branches = get_json("%s/branches" % GITHUB_API_BASE)
+    branches = get_json_pages("%s/branches" % GITHUB_API_BASE)
     branch_names = list(filter(lambda x: x.startswith("branch-"), [x["name"] 
for x in branches]))
     # Assumes branch names can be sorted lexicographically
     branch_names = sorted(branch_names, reverse=True)
@@ -629,7 +692,7 @@ def main():
         pr_num = sys.argv[1]
         print("Start to merge pull request #%s" % (pr_num))
     pr = get_json("%s/pulls/%s" % (GITHUB_API_BASE, pr_num))
-    pr_events = get_json("%s/issues/%s/events" % (GITHUB_API_BASE, pr_num))
+    pr_events = get_json_pages("%s/issues/%s/events" % (GITHUB_API_BASE, 
pr_num))
 
     url = pr["url"]
 
@@ -682,7 +745,7 @@ def main():
     pr_author_info = get_json("https://api.github.com/users/%s"; % user_login)
     pr_author_name = pr_author_info.get("name") or user_login
     pr_author_email = pr_author_info.get("email")
-    pr_commits = get_json("%s/pulls/%s/commits" % (GITHUB_API_BASE, pr_num))
+    pr_commits = get_json_pages("%s/pulls/%s/commits" % (GITHUB_API_BASE, 
pr_num))
     if not pr_author_email:
         # If the GitHub profile has no public email, find the author's email 
from the
         # PR's git commits. Only use it if GitHub links the commit to the PR 
author's


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to