Fix PR and Issue list

Separate the GQL and JSON decoded types that we use for queries.
This commit is contained in:
William Martin 2023-11-27 16:59:28 +01:00
parent 7aeccdb7a0
commit e775bc64b8
7 changed files with 439 additions and 133 deletions

View file

@ -8,6 +8,7 @@ import (
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/browser"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghrepo"
@ -19,6 +20,7 @@ import (
"github.com/cli/cli/v2/test"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) {
@ -456,3 +458,151 @@ func Test_issueList(t *testing.T) {
})
}
}
func TestIssueList_withProjectItems(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)
reg.Register(
httpmock.GraphQL(`query IssueList\b`),
httpmock.GraphQLQuery(`{
"data": {
"repository": {
"hasIssuesEnabled": true,
"issues": {
"totalCount": 1,
"nodes": [
{
"projectItems": {
"nodes": [
{
"id": "PVTI_lAHOAA3WC84AW6WNzgJ8rnQ",
"project": {
"id": "PVT_kwHOAA3WC84AW6WN",
"title": "Test Public Project"
},
"status": {
"optionId": "47fc9ee4",
"name": "In Progress"
}
}
],
"totalCount": 1
}
}
]
}
}
}
}`, func(_ string, params map[string]interface{}) {
require.Equal(t, map[string]interface{}{
"owner": "OWNER",
"repo": "REPO",
"limit": float64(30),
"states": []interface{}{"OPEN"},
}, params)
}))
client := &http.Client{Transport: reg}
issuesAndTotalCount, err := issueList(
client,
ghrepo.New("OWNER", "REPO"),
prShared.FilterOptions{
Entity: "issue",
},
30,
)
require.NoError(t, err)
require.Len(t, issuesAndTotalCount.Issues, 1)
require.Len(t, issuesAndTotalCount.Issues[0].ProjectItems.Nodes, 1)
require.Equal(t, issuesAndTotalCount.Issues[0].ProjectItems.Nodes[0].ID, "PVTI_lAHOAA3WC84AW6WNzgJ8rnQ")
expectedProject := api.ProjectV2ItemProject{
ID: "PVT_kwHOAA3WC84AW6WN",
Title: "Test Public Project",
}
require.Equal(t, issuesAndTotalCount.Issues[0].ProjectItems.Nodes[0].Project, expectedProject)
expectedStatus := api.ProjectV2ItemStatus{
OptionID: "47fc9ee4",
Name: "In Progress",
}
require.Equal(t, issuesAndTotalCount.Issues[0].ProjectItems.Nodes[0].Status, expectedStatus)
}
func TestIssueList_Search_withProjectItems(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)
reg.Register(
httpmock.GraphQL(`query IssueSearch\b`),
httpmock.GraphQLQuery(`{
"data": {
"repository": {
"hasIssuesEnabled": true
},
"search": {
"issueCount": 1,
"nodes": [
{
"projectItems": {
"nodes": [
{
"id": "PVTI_lAHOAA3WC84AW6WNzgJ8rl0",
"project": {
"id": "PVT_kwHOAA3WC84AW6WN",
"title": "Test Public Project"
},
"status": {
"optionId": "47fc9ee4",
"name": "In Progress"
}
}
],
"totalCount": 1
}
}
]
}
}
}`, func(_ string, params map[string]interface{}) {
require.Equal(t, map[string]interface{}{
"owner": "OWNER",
"repo": "REPO",
"type": "ISSUE",
"limit": float64(30),
"query": "just used to force the search API branch repo:OWNER/REPO type:issue",
}, params)
}))
client := &http.Client{Transport: reg}
issuesAndTotalCount, err := issueList(
client,
ghrepo.New("OWNER", "REPO"),
prShared.FilterOptions{
Entity: "issue",
Search: "just used to force the search API branch",
},
30,
)
require.NoError(t, err)
require.Len(t, issuesAndTotalCount.Issues, 1)
require.Len(t, issuesAndTotalCount.Issues[0].ProjectItems.Nodes, 1)
require.Equal(t, issuesAndTotalCount.Issues[0].ProjectItems.Nodes[0].ID, "PVTI_lAHOAA3WC84AW6WNzgJ8rl0")
expectedProject := api.ProjectV2ItemProject{
ID: "PVT_kwHOAA3WC84AW6WN",
Title: "Test Public Project",
}
require.Equal(t, issuesAndTotalCount.Issues[0].ProjectItems.Nodes[0].Project, expectedProject)
expectedStatus := api.ProjectV2ItemStatus{
OptionID: "47fc9ee4",
Name: "In Progress",
}
require.Equal(t, issuesAndTotalCount.Issues[0].ProjectItems.Nodes[0].Status, expectedStatus)
}

View file

@ -9,15 +9,18 @@ import (
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/browser"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/run"
prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/test"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) {
@ -320,3 +323,146 @@ func TestPRList_web(t *testing.T) {
})
}
}
func TestPRList_withProjectItems(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)
reg.Register(
httpmock.GraphQL(`query PullRequestList\b`),
httpmock.GraphQLQuery(`{
"data": {
"repository": {
"pullRequests": {
"totalCount": 1,
"nodes": [
{
"projectItems": {
"nodes": [
{
"id": "PVTI_lAHOAA3WC84AW6WNzgJ8rnQ",
"project": {
"id": "PVT_kwHOAA3WC84AW6WN",
"title": "Test Public Project"
},
"status": {
"optionId": "47fc9ee4",
"name": "In Progress"
}
}
],
"totalCount": 1
}
}
]
}
}
}
}`, func(_ string, params map[string]interface{}) {
require.Equal(t, map[string]interface{}{
"owner": "OWNER",
"repo": "REPO",
"limit": float64(30),
"state": []interface{}{"OPEN"},
}, params)
}))
client := &http.Client{Transport: reg}
prsAndTotalCount, err := listPullRequests(
client,
ghrepo.New("OWNER", "REPO"),
prShared.FilterOptions{
Entity: "pr",
State: "open",
},
30,
)
require.NoError(t, err)
require.Len(t, prsAndTotalCount.PullRequests, 1)
require.Len(t, prsAndTotalCount.PullRequests[0].ProjectItems.Nodes, 1)
require.Equal(t, prsAndTotalCount.PullRequests[0].ProjectItems.Nodes[0].ID, "PVTI_lAHOAA3WC84AW6WNzgJ8rnQ")
expectedProject := api.ProjectV2ItemProject{
ID: "PVT_kwHOAA3WC84AW6WN",
Title: "Test Public Project",
}
require.Equal(t, prsAndTotalCount.PullRequests[0].ProjectItems.Nodes[0].Project, expectedProject)
expectedStatus := api.ProjectV2ItemStatus{
OptionID: "47fc9ee4",
Name: "In Progress",
}
require.Equal(t, prsAndTotalCount.PullRequests[0].ProjectItems.Nodes[0].Status, expectedStatus)
}
func TestPRList_Search_withProjectItems(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)
reg.Register(
httpmock.GraphQL(`query PullRequestSearch\b`),
httpmock.GraphQLQuery(`{
"data": {
"search": {
"issueCount": 1,
"nodes": [
{
"projectItems": {
"nodes": [
{
"id": "PVTI_lAHOAA3WC84AW6WNzgJ8rl0",
"project": {
"id": "PVT_kwHOAA3WC84AW6WN",
"title": "Test Public Project"
},
"status": {
"optionId": "47fc9ee4",
"name": "In Progress"
}
}
],
"totalCount": 1
}
}
]
}
}
}`, func(_ string, params map[string]interface{}) {
require.Equal(t, map[string]interface{}{
"limit": float64(30),
"q": "just used to force the search API branch repo:OWNER/REPO state:open type:pr",
}, params)
}))
client := &http.Client{Transport: reg}
prsAndTotalCount, err := listPullRequests(
client,
ghrepo.New("OWNER", "REPO"),
prShared.FilterOptions{
Entity: "pr",
State: "open",
Search: "just used to force the search API branch",
},
30,
)
require.NoError(t, err)
require.Len(t, prsAndTotalCount.PullRequests, 1)
require.Len(t, prsAndTotalCount.PullRequests[0].ProjectItems.Nodes, 1)
require.Equal(t, prsAndTotalCount.PullRequests[0].ProjectItems.Nodes[0].ID, "PVTI_lAHOAA3WC84AW6WNzgJ8rl0")
expectedProject := api.ProjectV2ItemProject{
ID: "PVT_kwHOAA3WC84AW6WN",
Title: "Test Public Project",
}
require.Equal(t, prsAndTotalCount.PullRequests[0].ProjectItems.Nodes[0].Project, expectedProject)
expectedStatus := api.ProjectV2ItemStatus{
OptionID: "47fc9ee4",
Name: "In Progress",
}
require.Equal(t, prsAndTotalCount.PullRequests[0].ProjectItems.Nodes[0].Status, expectedStatus)
}