Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions pkg/github/repository_resource_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 22 additions & 9 deletions pkg/github/repository_resource_completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand All @@ -315,19 +321,26 @@ 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)
}
}))

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)
})
}
}
Expand Down