8000 Fix query object state mutation during pagination by babakks · Pull Request #11244 · cli/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix query object state mutation during pagination #11244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2025

Conversation

babakks
Copy link
Member
@babakks babakks commented Jul 7, 2025

Fixes #11228

This PR fixes the issue with mutating original query object during pagination API calls. Test cases were added to assert the expected behaviour.

Verifying A/C

Given I have a query that contains a whitespace character, and my query will result in several pages of pagination
When I run the gh search command (for any subcommand)
Then my query should complete without error

I found a simple use case to verify the expected behaviour. It's looking for the abundant Lorem Ipsum phrase with a grouping of words that forces gh to quote them:

go run ./cmd/gh search prs --limit 200 "Lorem ipsum" "dolor sit" "amet, consectetur" "adipiscing elit," "sed do" "eiusmod tempor" "incididunt ut" "labore et" "dolore magna" "aliqua. Ut" "enim ad" "minim veniam," "quis nostrud" "exercitation ullamco" "laboris nisi" "ut aliquip" "ex ea"
go run ./cmd/gh search issues --limit 200 "Lorem ipsum" "dolor sit" "amet, consectetur" "adipiscing elit," "sed do" "eiusmod tempor" "incididunt ut" "labore et" "dolore magna" "aliqua. Ut" "enim ad" "minim veniam," "quis nostrud" "exercitation ullamco" "laboris nisi" "ut aliquip" "ex ea"
go run ./cmd/gh search commits --limit 200 "Lorem ipsum" "dolor sit" "amet, consectetur" "adipiscing elit," "sed do" "eiusmod tempor" "incididunt ut" "labore et" "dolore magna" "aliqua. Ut" "enim ad" "minim veniam," "quis nostrud" "exercitation ullamco" "laboris nisi" "ut aliquip" "ex ea"

This works fine with the changes in this PR, but if we run it with the latest gh (or on the trunk branch) we'll get the following error:

Invalid search query "\"\\\"Lorem ipsum\\\"\" \"\\\"dolor sit\\\"\" \"\\\"amet, consectetur\\\"\" \"\\\"adipiscing elit,\\\"\" \"\\\"sed do\\\"\" \"\\\"eiusmod tempor\\\"\" \"\\\"incididunt ut\\\"\" \"\\\"labore et\\\"\" \"\\\"dolore magna\\\"\" \"\\\"aliqua. Ut\\\"\" \"\\\"enim ad\\\"\" \"\\\"minim veniam,\\\"\" \"\\\"quis nostrud\\\"\" \"\\\"exercitation ullamco\\\"\" \"\\\"laboris nisi\\\"\" \"\\\"ut aliquip\\\"\" \"\\\"ex ea\\\"\" type:pr".
The search is longer than 256 characters.

babakks added 3 commits July 7, 2025 15:42
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
@Copilot Copilot AI review requested due to automatic review settings July 7, 2025 17:44
@babakks babakks requested a review from a team as a code owner July 7, 2025 17:44
@babakks babakks linked an issue Jul 7, 2025 that may be closed by this pull request
@babakks babakks requested a review from andyfeller July 7, 2025 17:44
@babakks babakks temporarily deployed to cli-automation July 7, 2025 17:44 — with GitHub Actions Inactive
Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes mutation of the original query slice during pagination and adds tests to ensure quoted multi-word keywords are handled correctly across search endpoints and URL generation.

  • Refactor formatKeywords to return a new slice instead of mutating the input
  • Add pagination test cases for multi-word queries in code, commits, repositories, and issues
  • Add a URL-encoding test for quoted multi-word keywords

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/search/query.go Changed formatKeywords to allocate a new result slice rather than mutating the input
pkg/search/searcher_test.go Added tests for paginating with quoted multi-word queries and URL encoding

Copy link
Member
@andyfeller andyfeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Comment on lines 151 to +161
func formatKeywords(ks []string) []string {
result := make([]string, len(ks))
for i, k := range ks {
before, after, found := strings.Cut(k, ":")
if !found {
ks[i] = quote(k)
result[i] = quote(k)
} else {
ks[i] = fmt.Sprintf("%s:%s", before, quote(after))
result[i] = fmt.Sprintf("%s:%s", before, quote(after))
}
}
return ks
return result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: just to clarify, this function had a side effect of modifying Query.Keywords field which is used else where. Refactoring this logic to not modify the input avoids the redundant formatting issue.

@andyfeller andyfeller merged commit 28c3424 into trunk Jul 7, 2025
19 checks passed
@andyfeller andyfeller deleted the babakks/fix-query-mutation-during-pagination branch July 7, 2025 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gh search does not properly handle multi-word search query terms
2 participants
0