Skip to content

Commit dd9ca9b

Browse files
Clean up project item-list query addition changes (cli#12714)
2 parents 8dcfd33 + ccfe693 commit dd9ca9b

3 files changed

Lines changed: 48 additions & 46 deletions

File tree

pkg/cmd/project/item-list/item_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func runList(config listConfig) error {
114114
return err
115115
}
116116
if !features.ProjectItemQuery {
117-
return fmt.Errorf("the `--query` flag is not supported on this GitHub host; most likely you are targeting a version of GHES that does not yet have the query field available")
117+
return fmt.Errorf("the `--query` flag is not supported on this GitHub host")
118118
}
119119
}
120120

pkg/cmd/project/item-list/item_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,5 +732,5 @@ func TestRunList_QueryUnsupported(t *testing.T) {
732732
}
733733

734734
err := runList(config)
735-
assert.EqualError(t, err, "the `--query` flag is not supported on this GitHub host; most likely you are targeting a version of GHES that does not yet have the query field available")
735+
assert.EqualError(t, err, "the `--query` flag is not supported on this GitHub host")
736736
}

pkg/cmd/project/shared/queries/queries.go

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ type Project struct {
147147
}
148148
}
149149

150-
type projectDTOBase struct {
150+
// Below, you will find the query structs to represent fetching a project via the GraphQL API.
151+
// Prior to GHES 3.20, the query argument did not exist on the items connection, so we have
152+
// one base struct and two structs that embed and add the Items connection with and without the query argument.
153+
// The expectation is that these will then be converted into the Project domain struct above.
154+
type projectQueryBase struct {
151155
Number int32
152156
URL string
153157
ShortDescription string
@@ -165,29 +169,28 @@ type projectDTOBase struct {
165169
Login string
166170
} `graphql:"... on Organization"`
167171
}
172+
Fields ProjectFields `graphql:"fields(first: $firstFields, after: $afterFields)"`
168173
}
169174

170-
type projectDTOWithItemQuery struct {
171-
projectDTOBase
175+
type projectQueryWithQueryableItems struct {
176+
projectQueryBase
172177
Items struct {
173178
PageInfo PageInfo
174179
TotalCount int
175180
Nodes []ProjectItem
176181
} `graphql:"items(first: $firstItems, after: $afterItems, query: $query)"`
177-
Fields ProjectFields `graphql:"fields(first: $firstFields, after: $afterFields)"`
178182
}
179183

180-
type projectDTOWithoutItemQuery struct {
181-
projectDTOBase
184+
type projectQueryWithoutQueryableItems struct {
185+
projectQueryBase
182186
Items struct {
183187
PageInfo PageInfo
184188
TotalCount int
185189
Nodes []ProjectItem
186190
} `graphql:"items(first: $firstItems, after: $afterItems)"`
187-
Fields ProjectFields `graphql:"fields(first: $firstFields, after: $afterFields)"`
188191
}
189192

190-
func newProjectFromDTOBase(source projectDTOBase) *Project {
193+
func newProjectFromQueryBase(source projectQueryBase) *Project {
191194
project := &Project{
192195
Number: source.Number,
193196
URL: source.URL,
@@ -201,24 +204,23 @@ func newProjectFromDTOBase(source projectDTOBase) *Project {
201204
project.Owner.TypeName = source.Owner.TypeName
202205
project.Owner.User.Login = source.Owner.User.Login
203206
project.Owner.Organization.Login = source.Owner.Organization.Login
207+
project.Fields = source.Fields
204208
return project
205209
}
206210

207-
func newProjectFromDTOWithItemQuery(source projectDTOWithItemQuery) *Project {
208-
project := newProjectFromDTOBase(source.projectDTOBase)
211+
func newProjectFromQueryWithItemsQuery(source projectQueryWithQueryableItems) *Project {
212+
project := newProjectFromQueryBase(source.projectQueryBase)
209213
project.Items.PageInfo = source.Items.PageInfo
210214
project.Items.TotalCount = source.Items.TotalCount
211215
project.Items.Nodes = source.Items.Nodes
212-
project.Fields = source.Fields
213216
return project
214217
}
215218

216-
func newProjectFromDTOWithoutItemQuery(source projectDTOWithoutItemQuery) *Project {
217-
project := newProjectFromDTOBase(source.projectDTOBase)
219+
func newProjectFromQueryWithoutItemsQuery(source projectQueryWithoutQueryableItems) *Project {
220+
project := newProjectFromQueryBase(source.projectQueryBase)
218221
project.Items.PageInfo = source.Items.PageInfo
219222
project.Items.TotalCount = source.Items.TotalCount
220223
project.Items.Nodes = source.Items.Nodes
221-
project.Fields = source.Fields
222224
return project
223225
}
224226

@@ -615,24 +617,24 @@ func (c *Client) ProjectItems(o *Owner, number int32, limit int, queryStr string
615617
case UserOwner:
616618
variables["login"] = githubv4.String(o.Login)
617619
if queryStr == "" {
618-
query = &userOwnerWithItemsNoQuery{} // must be a pointer to work with graphql queries
620+
query = &userOwnerWithItemsNoQuery{}
619621
} else {
620-
query = &userOwnerWithItems{} // must be a pointer to work with graphql queries
622+
query = &userOwnerWithItems{}
621623
}
622624
queryName = "UserProjectWithItems"
623625
case OrgOwner:
624626
variables["login"] = githubv4.String(o.Login)
625627
if queryStr == "" {
626-
query = &orgOwnerWithItemsNoQuery{} // must be a pointer to work with graphql queries
628+
query = &orgOwnerWithItemsNoQuery{}
627629
} else {
628-
query = &orgOwnerWithItems{} // must be a pointer to work with graphql queries
630+
query = &orgOwnerWithItems{}
629631
}
630632
queryName = "OrgProjectWithItems"
631633
case ViewerOwner:
632634
if queryStr == "" {
633-
query = &viewerOwnerWithItemsNoQuery{} // must be a pointer to work with graphql queries
635+
query = &viewerOwnerWithItemsNoQuery{}
634636
} else {
635-
query = &viewerOwnerWithItems{} // must be a pointer to work with graphql queries
637+
query = &viewerOwnerWithItems{}
636638
}
637639
queryName = "ViewerProjectWithItems"
638640
}
@@ -673,7 +675,7 @@ func (q userOwnerWithItemsNoQuery) Nodes() []ProjectItem {
673675
}
674676

675677
func (q userOwnerWithItemsNoQuery) Project() *Project {
676-
return newProjectFromDTOWithoutItemQuery(q.Owner.Project)
678+
return newProjectFromQueryWithoutItemsQuery(q.Owner.Project)
677679
}
678680

679681
// userOwnerWithItems
@@ -690,7 +692,7 @@ func (q userOwnerWithItems) Nodes() []ProjectItem {
690692
}
691693

692694
func (q userOwnerWithItems) Project() *Project {
693-
return newProjectFromDTOWithItemQuery(q.Owner.Project)
695+
return newProjectFromQueryWithItemsQuery(q.Owner.Project)
694696
}
695697

696698
// orgOwnerWithItems
@@ -707,7 +709,7 @@ func (q orgOwnerWithItems) Nodes() []ProjectItem {
707709
}
708710

709711
func (q orgOwnerWithItems) Project() *Project {
710-
return newProjectFromDTOWithItemQuery(q.Owner.Project)
712+
return newProjectFromQueryWithItemsQuery(q.Owner.Project)
711713
}
712714

713715
// orgOwnerWithItemsNoQuery
@@ -724,7 +726,7 @@ func (q orgOwnerWithItemsNoQuery) Nodes() []ProjectItem {
724726
}
725727

726728
func (q orgOwnerWithItemsNoQuery) Project() *Project {
727-
return newProjectFromDTOWithoutItemQuery(q.Owner.Project)
729+
return newProjectFromQueryWithoutItemsQuery(q.Owner.Project)
728730
}
729731

730732
// viewerOwnerWithItems
@@ -741,7 +743,7 @@ func (q viewerOwnerWithItems) Nodes() []ProjectItem {
741743
}
742744

743745
func (q viewerOwnerWithItems) Project() *Project {
744-
return newProjectFromDTOWithItemQuery(q.Owner.Project)
746+
return newProjectFromQueryWithItemsQuery(q.Owner.Project)
745747
}
746748

747749
// viewerOwnerWithItemsNoQuery
@@ -758,7 +760,7 @@ func (q viewerOwnerWithItemsNoQuery) Nodes() []ProjectItem {
758760
}
759761

760762
func (q viewerOwnerWithItemsNoQuery) Project() *Project {
761-
return newProjectFromDTOWithoutItemQuery(q.Owner.Project)
763+
return newProjectFromQueryWithoutItemsQuery(q.Owner.Project)
762764
}
763765

764766
// userOwnerWithFields
@@ -775,7 +777,7 @@ func (q userOwnerWithFields) Nodes() []ProjectField {
775777
}
776778

777779
func (q userOwnerWithFields) Project() *Project {
778-
return newProjectFromDTOWithoutItemQuery(q.Owner.Project)
780+
return newProjectFromQueryWithoutItemsQuery(q.Owner.Project)
779781
}
780782

781783
// orgOwnerWithFields
@@ -792,7 +794,7 @@ func (q orgOwnerWithFields) Nodes() []ProjectField {
792794
}
793795

794796
func (q orgOwnerWithFields) Project() *Project {
795-
return newProjectFromDTOWithoutItemQuery(q.Owner.Project)
797+
return newProjectFromQueryWithoutItemsQuery(q.Owner.Project)
796798
}
797799

798800
// viewerOwnerWithFields
@@ -809,7 +811,7 @@ func (q viewerOwnerWithFields) Nodes() []ProjectField {
809811
}
810812

811813
func (q viewerOwnerWithFields) Project() *Project {
812-
return newProjectFromDTOWithoutItemQuery(q.Owner.Project)
814+
return newProjectFromQueryWithoutItemsQuery(q.Owner.Project)
813815
}
814816

815817
type projectAttribute interface {
@@ -988,14 +990,14 @@ func (c *Client) ProjectFields(o *Owner, number int32, limit int) (*Project, err
988990
switch o.Type {
989991
case UserOwner:
990992
variables["login"] = githubv4.String(o.Login)
991-
query = &userOwnerWithFields{} // must be a pointer to work with graphql queries
993+
query = &userOwnerWithFields{}
992994
queryName = "UserProjectWithFields"
993995
case OrgOwner:
994996
variables["login"] = githubv4.String(o.Login)
995-
query = &orgOwnerWithFields{} // must be a pointer to work with graphql queries
997+
query = &orgOwnerWithFields{}
996998
queryName = "OrgProjectWithFields"
997999
case ViewerOwner:
998-
query = &viewerOwnerWithFields{} // must be a pointer to work with graphql queries
1000+
query = &viewerOwnerWithFields{}
9991001
queryName = "ViewerProjectWithFields"
10001002
}
10011003
err := c.doQueryWithProgressIndicator(queryName, query, variables)
@@ -1037,16 +1039,16 @@ type viewerLoginOrgs struct {
10371039
}
10381040

10391041
type ownerWithLogin struct {
1040-
Project projectDTOWithoutItemQuery `graphql:"projectV2(number: $number)"`
1042+
Project projectQueryWithoutQueryableItems `graphql:"projectV2(number: $number)"`
10411043
Login string
10421044
}
10431045

10441046
type ownerWithProjectWithItemQuery struct {
1045-
Project projectDTOWithItemQuery `graphql:"projectV2(number: $number)"`
1047+
Project projectQueryWithQueryableItems `graphql:"projectV2(number: $number)"`
10461048
}
10471049

10481050
type ownerWithProjectWithoutItemQuery struct {
1049-
Project projectDTOWithoutItemQuery `graphql:"projectV2(number: $number)"`
1051+
Project projectQueryWithoutQueryableItems `graphql:"projectV2(number: $number)"`
10501052
}
10511053

10521054
// userOwner is used to query the project of a user.
@@ -1212,7 +1214,7 @@ type userProjects struct {
12121214
Projects struct {
12131215
TotalCount int
12141216
PageInfo PageInfo
1215-
Nodes []projectDTOWithoutItemQuery
1217+
Nodes []projectQueryWithoutQueryableItems
12161218
} `graphql:"projectsV2(first: $first, after: $after)"`
12171219
Login string
12181220
} `graphql:"user(login: $login)"`
@@ -1224,7 +1226,7 @@ type orgProjects struct {
12241226
Projects struct {
12251227
TotalCount int
12261228
PageInfo PageInfo
1227-
Nodes []projectDTOWithoutItemQuery
1229+
Nodes []projectQueryWithoutQueryableItems
12281230
} `graphql:"projectsV2(first: $first, after: $after)"`
12291231
Login string
12301232
} `graphql:"organization(login: $login)"`
@@ -1236,7 +1238,7 @@ type viewerProjects struct {
12361238
Projects struct {
12371239
TotalCount int
12381240
PageInfo PageInfo
1239-
Nodes []projectDTOWithoutItemQuery
1241+
Nodes []projectQueryWithoutQueryableItems
12401242
} `graphql:"projectsV2(first: $first, after: $after)"`
12411243
Login string
12421244
} `graphql:"viewer"`
@@ -1390,16 +1392,16 @@ func (c *Client) NewProject(canPrompt bool, o *Owner, number int32, fields bool)
13901392
var query userOwner
13911393
variables["login"] = githubv4.String(o.Login)
13921394
err := c.doQueryWithProgressIndicator("UserProject", &query, variables)
1393-
return newProjectFromDTOWithoutItemQuery(query.Owner.Project), err
1395+
return newProjectFromQueryWithoutItemsQuery(query.Owner.Project), err
13941396
} else if o.Type == OrgOwner {
13951397
variables["login"] = githubv4.String(o.Login)
13961398
var query orgOwner
13971399
err := c.doQueryWithProgressIndicator("OrgProject", &query, variables)
1398-
return newProjectFromDTOWithoutItemQuery(query.Owner.Project), err
1400+
return newProjectFromQueryWithoutItemsQuery(query.Owner.Project), err
13991401
} else if o.Type == ViewerOwner {
14001402
var query viewerOwner
14011403
err := c.doQueryWithProgressIndicator("ViewerProject", &query, variables)
1402-
return newProjectFromDTOWithoutItemQuery(query.Owner.Project), err
1404+
return newProjectFromQueryWithoutItemsQuery(query.Owner.Project), err
14031405
}
14041406
return nil, errors.New("unknown owner type")
14051407
}
@@ -1477,7 +1479,7 @@ func (c *Client) Projects(login string, t OwnerType, limit int, fields bool) (Pr
14771479
return projects, err
14781480
}
14791481
for _, p := range query.Owner.Projects.Nodes {
1480-
projects.Nodes = append(projects.Nodes, *newProjectFromDTOWithoutItemQuery(p))
1482+
projects.Nodes = append(projects.Nodes, *newProjectFromQueryWithoutItemsQuery(p))
14811483
}
14821484
hasNextPage = query.Owner.Projects.PageInfo.HasNextPage
14831485
cursor = &query.Owner.Projects.PageInfo.EndCursor
@@ -1488,7 +1490,7 @@ func (c *Client) Projects(login string, t OwnerType, limit int, fields bool) (Pr
14881490
return projects, err
14891491
}
14901492
for _, p := range query.Owner.Projects.Nodes {
1491-
projects.Nodes = append(projects.Nodes, *newProjectFromDTOWithoutItemQuery(p))
1493+
projects.Nodes = append(projects.Nodes, *newProjectFromQueryWithoutItemsQuery(p))
14921494
}
14931495
hasNextPage = query.Owner.Projects.PageInfo.HasNextPage
14941496
cursor = &query.Owner.Projects.PageInfo.EndCursor
@@ -1499,7 +1501,7 @@ func (c *Client) Projects(login string, t OwnerType, limit int, fields bool) (Pr
14991501
return projects, err
15001502
}
15011503
for _, p := range query.Owner.Projects.Nodes {
1502-
projects.Nodes = append(projects.Nodes, *newProjectFromDTOWithoutItemQuery(p))
1504+
projects.Nodes = append(projects.Nodes, *newProjectFromQueryWithoutItemsQuery(p))
15031505
}
15041506
hasNextPage = query.Owner.Projects.PageInfo.HasNextPage
15051507
cursor = &query.Owner.Projects.PageInfo.EndCursor

0 commit comments

Comments
 (0)