Skip to content

Commit

Permalink
Merge pull request #8384 from cli/wm/fix-project-unmarshaling
Browse files Browse the repository at this point in the history
Fix project status unmarshaling
  • Loading branch information
andyfeller committed Nov 27, 2023
2 parents 06e438b + e775bc6 commit 6309446
Show file tree
Hide file tree
Showing 6 changed files with 493 additions and 15 deletions.
20 changes: 12 additions & 8 deletions api/queries_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,18 @@ type ProjectInfo struct {

type ProjectV2Item struct {
ID string `json:"id"`
Project struct {
ID string `json:"id"`
Title string `json:"title"`
}
Status struct {
OptionID string `json:"optionId" graphql:"... on ProjectV2ItemFieldSingleSelectValue{optionId}"`
Name string `json:"name" graphql:"... on ProjectV2ItemFieldSingleSelectValue{name}"`
} `graphql:"status:fieldValueByName(name: \"Status\")"`
Project ProjectV2ItemProject
Status ProjectV2ItemStatus
}

type ProjectV2ItemProject struct {
ID string `json:"id"`
Title string `json:"title"`
}

type ProjectV2ItemStatus struct {
OptionID string `json:"optionId"`
Name string `json:"name"`
}

func (p ProjectCards) ProjectNames() []string {
Expand Down
67 changes: 63 additions & 4 deletions api/queries_projects_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,27 @@ func UpdateProjectV2Items(client *Client, repo ghrepo.Interface, addProjectItems

// ProjectsV2ItemsForIssue fetches all ProjectItems for an issue.
func ProjectsV2ItemsForIssue(client *Client, repo ghrepo.Interface, issue *Issue) error {
type projectV2ItemStatus struct {
StatusFragment struct {
OptionID string `json:"optionId"`
Name string `json:"name"`
} `graphql:"... on ProjectV2ItemFieldSingleSelectValue"`
}

type projectV2Item struct {
ID string `json:"id"`
Project struct {
ID string `json:"id"`
Title string `json:"title"`
}
Status projectV2ItemStatus `graphql:"status:fieldValueByName(name: \"Status\")"`
}

type response struct {
Repository struct {
Issue struct {
ProjectItems struct {
Nodes []*ProjectV2Item
Nodes []*projectV2Item
PageInfo struct {
HasNextPage bool
EndCursor string
Expand All @@ -87,7 +103,20 @@ func ProjectsV2ItemsForIssue(client *Client, repo ghrepo.Interface, issue *Issue
if err != nil {
return err
}
items.Nodes = append(items.Nodes, query.Repository.Issue.ProjectItems.Nodes...)
for _, projectItemNode := range query.Repository.Issue.ProjectItems.Nodes {
items.Nodes = append(items.Nodes, &ProjectV2Item{
ID: projectItemNode.ID,
Project: ProjectV2ItemProject{
ID: projectItemNode.Project.ID,
Title: projectItemNode.Project.Title,
},
Status: ProjectV2ItemStatus{
OptionID: projectItemNode.Status.StatusFragment.OptionID,
Name: projectItemNode.Status.StatusFragment.Name,
},
})
}

if !query.Repository.Issue.ProjectItems.PageInfo.HasNextPage {
break
}
Expand All @@ -99,11 +128,27 @@ func ProjectsV2ItemsForIssue(client *Client, repo ghrepo.Interface, issue *Issue

// ProjectsV2ItemsForPullRequest fetches all ProjectItems for a pull request.
func ProjectsV2ItemsForPullRequest(client *Client, repo ghrepo.Interface, pr *PullRequest) error {
type projectV2ItemStatus struct {
StatusFragment struct {
OptionID string `json:"optionId"`
Name string `json:"name"`
} `graphql:"... on ProjectV2ItemFieldSingleSelectValue"`
}

type projectV2Item struct {
ID string `json:"id"`
Project struct {
ID string `json:"id"`
Title string `json:"title"`
}
Status projectV2ItemStatus `graphql:"status:fieldValueByName(name: \"Status\")"`
}

type response struct {
Repository struct {
PullRequest struct {
ProjectItems struct {
Nodes []*ProjectV2Item
Nodes []*projectV2Item
PageInfo struct {
HasNextPage bool
EndCursor string
Expand All @@ -125,7 +170,21 @@ func ProjectsV2ItemsForPullRequest(client *Client, repo ghrepo.Interface, pr *Pu
if err != nil {
return err
}
items.Nodes = append(items.Nodes, query.Repository.PullRequest.ProjectItems.Nodes...)

for _, projectItemNode := range query.Repository.PullRequest.ProjectItems.Nodes {
items.Nodes = append(items.Nodes, &ProjectV2Item{
ID: projectItemNode.ID,
Project: ProjectV2ItemProject{
ID: projectItemNode.Project.ID,
Title: projectItemNode.Project.Title,
},
Status: ProjectV2ItemStatus{
OptionID: projectItemNode.Status.StatusFragment.OptionID,
Name: projectItemNode.Status.StatusFragment.Name,
},
})
}

if !query.Repository.PullRequest.ProjectItems.PageInfo.HasNextPage {
break
}
Expand Down
62 changes: 59 additions & 3 deletions api/queries_projects_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestUpdateProjectV2Items(t *testing.T) {
Expand Down Expand Up @@ -185,6 +186,61 @@ func TestProjectsV2ItemsForPullRequest(t *testing.T) {
},
expectError: true,
},
{
name: "retrieves project items that have status columns",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query PullRequestProjectItems\b`),
httpmock.GraphQLQuery(`{
"data": {
"repository": {
"pullRequest": {
"projectItems": {
"nodes": [
{
"id": "PVTI_lADOB-vozM4AVk16zgK6U50",
"project": {
"id": "PVT_kwDOB-vozM4AVk16",
"title": "Test Project"
},
"status": {
"optionId": "47fc9ee4",
"name": "In Progress"
}
}
],
"pageInfo": {
"hasNextPage": false,
"endCursor": "MQ"
}
}
}
}
}
}`,
func(query string, inputs map[string]interface{}) {
require.Equal(t, float64(1), inputs["number"])
require.Equal(t, "OWNER", inputs["owner"])
require.Equal(t, "REPO", inputs["name"])
}),
)
},
expectItems: ProjectItems{
Nodes: []*ProjectV2Item{
{
ID: "PVTI_lADOB-vozM4AVk16zgK6U50",
Project: ProjectV2ItemProject{
ID: "PVT_kwDOB-vozM4AVk16",
Title: "Test Project",
},
Status: ProjectV2ItemStatus{
OptionID: "47fc9ee4",
Name: "In Progress",
},
},
},
},
},
}

for _, tt := range tests {
Expand All @@ -199,11 +255,11 @@ func TestProjectsV2ItemsForPullRequest(t *testing.T) {
pr := &PullRequest{Number: 1}
err := ProjectsV2ItemsForPullRequest(client, repo, pr)
if tt.expectError {
assert.Error(t, err)
require.Error(t, err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
assert.Equal(t, tt.expectItems, pr.ProjectItems)
require.Equal(t, tt.expectItems, pr.ProjectItems)
})
}
}
Expand Down
150 changes: 150 additions & 0 deletions pkg/cmd/issue/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}

0 comments on commit 6309446

Please sign in to comment.