From 03780aa63ccf2a577734d1932c965306f57946c6 Mon Sep 17 00:00:00 2001 From: Zain Qureshi <43629888+ZainnQureshii@users.noreply.github.com> Date: Sat, 28 Nov 2026 18:58:08 +0100 Subject: [PATCH] fix: use selected refs for repository path completion --- pkg/github/repository_resource_completions.go | 18 +++++------ .../repository_resource_completions_test.go | 31 +++++++++++++------ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/pkg/github/repository_resource_completions.go b/pkg/github/repository_resource_completions.go index ffa45d3f3b..ab7b4c93c1 100644 --- a/pkg/github/repository_resource_completions.go +++ b/pkg/github/repository_resource_completions.go @@ -243,22 +243,20 @@ func completePath(ctx context.Context, client *github.Client, resolved map[strin if owner == "" || repo == "" { return nil, errors.New("owner or repo not specified") } - refVal := resolved["branch"] - if refVal == "" { + refVal := "HEAD" + switch { + case resolved["branch"] != "": + refVal = "refs/heads/" + resolved["branch"] + case resolved["sha"] != "": refVal = resolved["sha"] - } - if refVal == "" { - refVal = resolved["tag"] - } - if refVal == "" && resolved["prNumber"] != "" { + case resolved["tag"] != "": + refVal = "refs/tags/" + resolved["tag"] + case 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" - } // Determine the prefix to complete (directory path or file path) prefix := argValue diff --git a/pkg/github/repository_resource_completions_test.go b/pkg/github/repository_resource_completions_test.go index d44afd6b89..8776cb248b 100644 --- a/pkg/github/repository_resource_completions_test.go +++ b/pkg/github/repository_resource_completions_test.go @@ -276,31 +276,37 @@ func TestCompletePath_RequestsTreeForResolvedRef(t *testing.T) { name string resolved map[string]string expectedTree string + expectedPath string }{ { - name: "branch", - resolved: map[string]string{"branch": "main"}, - expectedTree: "main", + name: "branch sharing a name with a tag", + resolved: map[string]string{"branch": "0.8.1"}, + expectedTree: "refs/heads/0.8.1", + expectedPath: "contrib/LICENSE", }, { name: "sha", resolved: map[string]string{"sha": "abc123"}, expectedTree: "abc123", + expectedPath: "contrib/README.md", }, { - name: "tag", - resolved: map[string]string{"tag": "v1.0.0"}, - expectedTree: "v1.0.0", + name: "tag sharing a name with a branch", + resolved: map[string]string{"tag": "0.8.1"}, + expectedTree: "refs/tags/0.8.1", + expectedPath: "contrib/hadoop-consumer/", }, { name: "pull request head", resolved: map[string]string{"prNumber": "42"}, expectedTree: "refs/pull/42/head", + expectedPath: "contrib/README.md", }, { name: "default branch", resolved: map[string]string{}, expectedTree: "HEAD", + expectedPath: "contrib/README.md", }, } @@ -315,8 +321,15 @@ func TestCompletePath_RequestsTreeForResolvedRef(t *testing.T) { return } requestedTrees = append(requestedTrees, tree) + path := "contrib/README.md" + switch tree { + case "0.8.1", "refs/heads/0.8.1": + path = "contrib/LICENSE" + case "refs/tags/0.8.1": + path = "contrib/hadoop-consumer/LICENSE" + } mockResponse(t, http.StatusOK, &github.Tree{ - Entries: []*github.TreeEntry{{Path: new("README.md"), Type: new("blob")}}, + Entries: []*github.TreeEntry{{Path: new(path), Type: new("blob")}}, })(w, r) } })) @@ -324,10 +337,10 @@ func TestCompletePath_RequestsTreeForResolvedRef(t *testing.T) { resolved := map[string]string{"owner": "owner", "repo": "repo"} maps.Copy(resolved, tt.resolved) - values, err := completePath(t.Context(), client, resolved, "") + values, err := completePath(t.Context(), client, resolved, "contrib/") require.NoError(t, err) assert.Equal(t, []string{tt.expectedTree}, requestedTrees) - assert.Equal(t, []string{"README.md"}, values) + assert.Equal(t, []string{tt.expectedPath}, values) }) } }