From d95170129afb86a41d9c700f6b4e27bef933c0da Mon Sep 17 00:00:00 2001 From: chanyang721 Date: Mon, 16 Nov 2026 00:56:07 +0900 Subject: [PATCH 1/2] fix: complete pull request content paths from the PR head Path completion for repo://{owner}/{repo}/refs/pull/{prNumber}/head/contents{/path*} ignored prNumber and listed the default branch tree, so files added in the pull request were never offered. Use refs/pull/{prNumber}/head as the tree ref when no branch, sha or tag is resolved, and reject a non-numeric prNumber before calling the API. --- pkg/github/repository_resource_completions.go | 7 ++ .../repository_resource_completions_test.go | 80 +++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/pkg/github/repository_resource_completions.go b/pkg/github/repository_resource_completions.go index 54c6e9c364..ffa45d3f3b 100644 --- a/pkg/github/repository_resource_completions.go +++ b/pkg/github/repository_resource_completions.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "strconv" "strings" "github.com/google/go-github/v92/github" @@ -249,6 +250,12 @@ func completePath(ctx context.Context, client *github.Client, resolved map[strin if refVal == "" { refVal = resolved["tag"] } + if refVal == "" && resolved["prNumber"] != "" { + if _, err := strconv.Atoi(resolved["prNumber"]); err != nil { + return nil, fmt.Errorf("invalid pull request number: %w", err) + } + refVal = "refs/pull/" + resolved["prNumber"] + "/head" + } if refVal == "" { refVal = "HEAD" } diff --git a/pkg/github/repository_resource_completions_test.go b/pkg/github/repository_resource_completions_test.go index c7936efd98..10d87016d4 100644 --- a/pkg/github/repository_resource_completions_test.go +++ b/pkg/github/repository_resource_completions_test.go @@ -5,6 +5,9 @@ import ( "encoding/json" "errors" "fmt" + "maps" + "net/http" + "strings" "testing" "github.com/google/go-github/v92/github" @@ -268,6 +271,83 @@ func TestCompletePath_RefSelection(t *testing.T) { assert.Nil(t, result) } +func TestCompletePath_RequestsTreeForResolvedRef(t *testing.T) { + tests := []struct { + name string + resolved map[string]string + expectedTree string + }{ + { + name: "branch", + resolved: map[string]string{"branch": "main"}, + expectedTree: "main", + }, + { + name: "sha", + resolved: map[string]string{"sha": "abc123"}, + expectedTree: "abc123", + }, + { + name: "tag", + resolved: map[string]string{"tag": "v1.0.0"}, + expectedTree: "v1.0.0", + }, + { + name: "pull request head", + resolved: map[string]string{"prNumber": "42"}, + expectedTree: "refs/pull/42/head", + }, + { + name: "default branch", + resolved: map[string]string{}, + expectedTree: "HEAD", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var requestedTrees []string + client := mustNewGHClient(t, NewMockedHTTPClient(func(handlers map[string]http.HandlerFunc) { + handlers[""] = func(w http.ResponseWriter, r *http.Request) { + tree, ok := strings.CutPrefix(r.URL.Path, "/repos/owner/repo/git/trees/") + if !ok { + w.WriteHeader(http.StatusNotFound) + return + } + requestedTrees = append(requestedTrees, tree) + mockResponse(t, http.StatusOK, &github.Tree{ + Entries: []*github.TreeEntry{{Path: github.Ptr("README.md"), Type: github.Ptr("blob")}}, + })(w, r) + } + })) + + resolved := map[string]string{"owner": "owner", "repo": "repo"} + maps.Copy(resolved, tt.resolved) + + values, err := completePath(t.Context(), client, resolved, "") + require.NoError(t, err) + assert.Equal(t, []string{tt.expectedTree}, requestedTrees) + assert.Equal(t, []string{"README.md"}, values) + }) + } +} + +func TestCompletePath_InvalidPRNumber(t *testing.T) { + client := mustNewGHClient(t, NewMockedHTTPClient(func(handlers map[string]http.HandlerFunc) { + handlers[""] = func(_ http.ResponseWriter, r *http.Request) { + t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path) + } + })) + + values, err := completePath(t.Context(), client, map[string]string{ + "owner": "owner", + "repo": "repo", + "prNumber": "42/../../main", + }, "") + require.Error(t, err) + assert.Nil(t, values) +} + func TestRepositoryResourceArgumentResolvers_Existence(t *testing.T) { // Test that all expected resolvers are present expectedResolvers := []string{ From 57c06b39c4c68cbfb1079e91e66c45adbbf916ef Mon Sep 17 00:00:00 2001 From: chanyang721 Date: Mon, 16 Nov 2026 01:02:30 +0900 Subject: [PATCH 2/2] test: use new() for tree entry pointers in completion test Match the repository convention and clear the govet inline finding on github.Ptr. --- pkg/github/repository_resource_completions_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/github/repository_resource_completions_test.go b/pkg/github/repository_resource_completions_test.go index 10d87016d4..d44afd6b89 100644 --- a/pkg/github/repository_resource_completions_test.go +++ b/pkg/github/repository_resource_completions_test.go @@ -316,7 +316,7 @@ func TestCompletePath_RequestsTreeForResolvedRef(t *testing.T) { } requestedTrees = append(requestedTrees, tree) mockResponse(t, http.StatusOK, &github.Tree{ - Entries: []*github.TreeEntry{{Path: github.Ptr("README.md"), Type: github.Ptr("blob")}}, + Entries: []*github.TreeEntry{{Path: new("README.md"), Type: new("blob")}}, })(w, r) } }))