Skip to content

Commit 33b08af

Browse files
committed
Adding the DatabaseID to be able to match the graphQL
1 parent 58dc124 commit 33b08af

2 files changed

Lines changed: 47 additions & 38 deletions

File tree

pkg/github/projects_resolver.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,28 @@ type projectFieldsQueryUser struct {
4545
} `graphql:"user(login: $owner)"`
4646
}
4747

48-
// projectFieldsConnection is a paginated list of project fields with the field
49-
// union expanded so we get IDs, names, types, and options in one hop.
48+
// projectFieldsConnection is a paginated list of project fields. We select `id`
49+
// to discriminate the union variant and `databaseId` for the numeric ID REST needs.
5050
type projectFieldsConnection struct {
5151
Nodes []struct {
5252
ProjectV2Field struct {
53-
ID githubv4.ID
54-
Name githubv4.String
55-
DataType githubv4.String
53+
ID githubv4.ID
54+
DatabaseID githubv4.Int `graphql:"databaseId"`
55+
Name githubv4.String
56+
DataType githubv4.String
5657
} `graphql:"... on ProjectV2Field"`
5758
ProjectV2IterationField struct {
58-
ID githubv4.ID
59-
Name githubv4.String
60-
DataType githubv4.String
59+
ID githubv4.ID
60+
DatabaseID githubv4.Int `graphql:"databaseId"`
61+
Name githubv4.String
62+
DataType githubv4.String
6163
} `graphql:"... on ProjectV2IterationField"`
6264
ProjectV2SingleSelectField struct {
63-
ID githubv4.ID
64-
Name githubv4.String
65-
DataType githubv4.String
66-
Options []struct {
65+
ID githubv4.ID
66+
DatabaseID githubv4.Int `graphql:"databaseId"`
67+
Name githubv4.String
68+
DataType githubv4.String
69+
Options []struct {
6770
ID githubv4.String
6871
Name githubv4.String
6972
}
@@ -111,20 +114,20 @@ func listAllProjectFields(ctx context.Context, gqlClient *githubv4.Client, owner
111114
opts = append(opts, ResolvedFieldOption{ID: string(o.ID), Name: string(o.Name)})
112115
}
113116
all = append(all, ResolvedField{
114-
ID: fmt.Sprintf("%v", n.ProjectV2SingleSelectField.ID),
117+
ID: fmt.Sprintf("%d", n.ProjectV2SingleSelectField.DatabaseID),
115118
Name: string(n.ProjectV2SingleSelectField.Name),
116119
DataType: string(n.ProjectV2SingleSelectField.DataType),
117120
Options: opts,
118121
})
119122
case n.ProjectV2IterationField.ID != nil && n.ProjectV2IterationField.ID != "":
120123
all = append(all, ResolvedField{
121-
ID: fmt.Sprintf("%v", n.ProjectV2IterationField.ID),
124+
ID: fmt.Sprintf("%d", n.ProjectV2IterationField.DatabaseID),
122125
Name: string(n.ProjectV2IterationField.Name),
123126
DataType: string(n.ProjectV2IterationField.DataType),
124127
})
125128
case n.ProjectV2Field.ID != nil && n.ProjectV2Field.ID != "":
126129
all = append(all, ResolvedField{
127-
ID: fmt.Sprintf("%v", n.ProjectV2Field.ID),
130+
ID: fmt.Sprintf("%d", n.ProjectV2Field.DatabaseID),
128131
Name: string(n.ProjectV2Field.Name),
129132
DataType: string(n.ProjectV2Field.DataType),
130133
})

pkg/github/projects_resolver_test.go

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@ type projectFieldsTestQuery struct {
2121
Fields struct {
2222
Nodes []struct {
2323
ProjectV2Field struct {
24-
ID githubv4.ID
25-
Name githubv4.String
26-
DataType githubv4.String
24+
ID githubv4.ID
25+
DatabaseID githubv4.Int `graphql:"databaseId"`
26+
Name githubv4.String
27+
DataType githubv4.String
2728
} `graphql:"... on ProjectV2Field"`
2829
ProjectV2IterationField struct {
29-
ID githubv4.ID
30-
Name githubv4.String
31-
DataType githubv4.String
30+
ID githubv4.ID
31+
DatabaseID githubv4.Int `graphql:"databaseId"`
32+
Name githubv4.String
33+
DataType githubv4.String
3234
} `graphql:"... on ProjectV2IterationField"`
3335
ProjectV2SingleSelectField struct {
34-
ID githubv4.ID
35-
Name githubv4.String
36-
DataType githubv4.String
37-
Options []struct {
36+
ID githubv4.ID
37+
DatabaseID githubv4.Int `graphql:"databaseId"`
38+
Name githubv4.String
39+
DataType githubv4.String
40+
Options []struct {
3841
ID githubv4.String
3942
Name githubv4.String
4043
}
@@ -56,12 +59,15 @@ func fieldsQueryVars(owner string, projectNumber int) map[string]any {
5659
}
5760

5861
// statusFieldNode is a single-select field response node for use in mock data.
59-
func statusFieldNode(id, name string, options []map[string]any) map[string]any {
62+
// `nodeID` is the global node ID (e.g. "PVTSSF_lADO...") and `databaseID` is
63+
// the numeric database ID the REST API expects.
64+
func statusFieldNode(nodeID string, databaseID int, name string, options []map[string]any) map[string]any {
6065
return map[string]any{
61-
"id": id,
62-
"name": name,
63-
"dataType": "SINGLE_SELECT",
64-
"options": options,
66+
"id": nodeID,
67+
"databaseId": databaseID,
68+
"name": name,
69+
"dataType": "SINGLE_SELECT",
70+
"options": options,
6571
}
6672
}
6773

@@ -89,7 +95,7 @@ func Test_ResolveProjectFieldByName_Success(t *testing.T) {
8995
projectFieldsTestQuery{},
9096
fieldsQueryVars("octo-org", 7),
9197
githubv4mock.DataResponse(fieldsResponse([]map[string]any{
92-
statusFieldNode("12345", "Status", []map[string]any{
98+
statusFieldNode("PVTSSF_lADOBBcDeFg123", 12345, "Status", []map[string]any{
9399
{"id": "OPT_a", "name": "Todo"},
94100
{"id": "OPT_b", "name": "In Progress"},
95101
{"id": "OPT_c", "name": "Done"},
@@ -117,7 +123,7 @@ func Test_ResolveProjectFieldByName_NotFound_ReturnsStructuredError(t *testing.T
117123
projectFieldsTestQuery{},
118124
fieldsQueryVars("octo-org", 7),
119125
githubv4mock.DataResponse(fieldsResponse([]map[string]any{
120-
statusFieldNode("12345", "Status", nil),
126+
statusFieldNode("PVTSSF_lADOBBcDeFg123", 12345, "Status", nil),
121127
})),
122128
),
123129
)
@@ -139,8 +145,8 @@ func Test_ResolveProjectFieldByName_Ambiguous_ReturnsStructuredError(t *testing.
139145
projectFieldsTestQuery{},
140146
fieldsQueryVars("octo-org", 7),
141147
githubv4mock.DataResponse(fieldsResponse([]map[string]any{
142-
statusFieldNode("12345", "Status", nil),
143-
statusFieldNode("67890", "Status", nil),
148+
statusFieldNode("PVTSSF_lADOBBcDeFg123", 12345, "Status", nil),
149+
statusFieldNode("PVTSSF_lADOBBcDeFg678", 67890, "Status", nil),
144150
})),
145151
),
146152
)
@@ -340,8 +346,8 @@ func Test_ResolveFieldNamesToIDs_Success(t *testing.T) {
340346
projectFieldsTestQuery{},
341347
fieldsQueryVars("octo-org", 1),
342348
githubv4mock.DataResponse(fieldsResponse([]map[string]any{
343-
statusFieldNode("100", "Status", nil),
344-
statusFieldNode("200", "Priority", nil),
349+
statusFieldNode("PVTSSF_lADOBBcDeFg100", 100, "Status", nil),
350+
statusFieldNode("PVTSSF_lADOBBcDeFg200", 200, "Priority", nil),
345351
})),
346352
),
347353
)
@@ -416,7 +422,7 @@ func Test_ProjectsWrite_UpdateProjectItem_ByName(t *testing.T) {
416422
projectFieldsTestQuery{},
417423
fieldsQueryVars("octo-org", 1),
418424
githubv4mock.DataResponse(fieldsResponse([]map[string]any{
419-
statusFieldNode("101", "Status", []map[string]any{
425+
statusFieldNode("PVTSSF_lADOBBcDeFg101", 101, "Status", []map[string]any{
420426
{"id": "OPT_in_progress", "name": "In Progress"},
421427
}),
422428
})),
@@ -453,7 +459,7 @@ func Test_ProjectsWrite_UpdateProjectItem_NameNotFound_StructuredError(t *testin
453459
projectFieldsTestQuery{},
454460
fieldsQueryVars("octo-org", 1),
455461
githubv4mock.DataResponse(fieldsResponse([]map[string]any{
456-
statusFieldNode("101", "Status", nil),
462+
statusFieldNode("PVTSSF_lADOBBcDeFg101", 101, "Status", nil),
457463
})),
458464
),
459465
)

0 commit comments

Comments
 (0)