Skip to content

Commit 3abc2be

Browse files
committed
Switch issue create to optimized resolver and update tests
1 parent 00f23b8 commit 3abc2be

File tree

4 files changed

+156
-58
lines changed

4 files changed

+156
-58
lines changed

api/queries_repo_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,136 @@ func Test_RepoCreate(t *testing.T) {
4646
t.Errorf("expected homepageUrl to be %q, got %q", "http://example.com", homepage)
4747
}
4848
}
49+
func Test_RepoMetadata(t *testing.T) {
50+
http := &httpmock.Registry{}
51+
client := NewClient(ReplaceTripper(http))
52+
53+
repo := ghrepo.FromFullName("OWNER/REPO")
54+
input := RepoMetadataInput{
55+
Assignees: true,
56+
Reviewers: true,
57+
Labels: true,
58+
Projects: true,
59+
Milestones: true,
60+
}
61+
62+
http.Register(
63+
httpmock.GraphQL(`\bassignableUsers\(`),
64+
httpmock.StringResponse(`
65+
{ "data": { "repository": { "assignableUsers": {
66+
"nodes": [
67+
{ "login": "hubot", "id": "HUBOTID" },
68+
{ "login": "MonaLisa", "id": "MONAID" }
69+
],
70+
"pageInfo": { "hasNextPage": false }
71+
} } } }
72+
`))
73+
http.Register(
74+
httpmock.GraphQL(`\blabels\(`),
75+
httpmock.StringResponse(`
76+
{ "data": { "repository": { "labels": {
77+
"nodes": [
78+
{ "name": "feature", "id": "FEATUREID" },
79+
{ "name": "TODO", "id": "TODOID" },
80+
{ "name": "bug", "id": "BUGID" }
81+
],
82+
"pageInfo": { "hasNextPage": false }
83+
} } } }
84+
`))
85+
http.Register(
86+
httpmock.GraphQL(`\bmilestones\(`),
87+
httpmock.StringResponse(`
88+
{ "data": { "repository": { "milestones": {
89+
"nodes": [
90+
{ "title": "GA", "id": "GAID" },
91+
{ "title": "Big One.oh", "id": "BIGONEID" }
92+
],
93+
"pageInfo": { "hasNextPage": false }
94+
} } } }
95+
`))
96+
http.Register(
97+
httpmock.GraphQL(`\brepository\(.+\bprojects\(`),
98+
httpmock.StringResponse(`
99+
{ "data": { "repository": { "projects": {
100+
"nodes": [
101+
{ "name": "Cleanup", "id": "CLEANUPID" },
102+
{ "name": "Roadmap", "id": "ROADMAPID" }
103+
],
104+
"pageInfo": { "hasNextPage": false }
105+
} } } }
106+
`))
107+
http.Register(
108+
httpmock.GraphQL(`\borganization\(.+\bprojects\(`),
109+
httpmock.StringResponse(`
110+
{ "data": { "organization": { "projects": {
111+
"nodes": [
112+
{ "name": "Triage", "id": "TRIAGEID" }
113+
],
114+
"pageInfo": { "hasNextPage": false }
115+
} } } }
116+
`))
117+
http.Register(
118+
httpmock.GraphQL(`\borganization\(.+\bteams\(`),
119+
httpmock.StringResponse(`
120+
{ "data": { "organization": { "teams": {
121+
"nodes": [
122+
{ "slug": "owners", "id": "OWNERSID" },
123+
{ "slug": "Core", "id": "COREID" }
124+
],
125+
"pageInfo": { "hasNextPage": false }
126+
} } } }
127+
`))
128+
129+
result, err := RepoMetadata(client, repo, input)
130+
if err != nil {
131+
t.Fatalf("unexpected error: %v", err)
132+
}
133+
134+
expectedMemberIDs := []string{"MONAID", "HUBOTID"}
135+
memberIDs, err := result.MembersToIDs([]string{"monalisa", "hubot"})
136+
if err != nil {
137+
t.Errorf("error resolving members: %v", err)
138+
}
139+
if !sliceEqual(memberIDs, expectedMemberIDs) {
140+
t.Errorf("expected members %v, got %v", expectedMemberIDs, memberIDs)
141+
}
142+
143+
expectedTeamIDs := []string{"COREID", "OWNERSID"}
144+
teamIDs, err := result.TeamsToIDs([]string{"OWNER/core", "/owners"})
145+
if err != nil {
146+
t.Errorf("error resolving teams: %v", err)
147+
}
148+
if !sliceEqual(teamIDs, expectedTeamIDs) {
149+
t.Errorf("expected teams %v, got %v", expectedTeamIDs, teamIDs)
150+
}
151+
152+
expectedLabelIDs := []string{"BUGID", "TODOID"}
153+
labelIDs, err := result.LabelsToIDs([]string{"bug", "todo"})
154+
if err != nil {
155+
t.Errorf("error resolving labels: %v", err)
156+
}
157+
if !sliceEqual(labelIDs, expectedLabelIDs) {
158+
t.Errorf("expected labels %v, got %v", expectedLabelIDs, labelIDs)
159+
}
160+
161+
expectedProjectIDs := []string{"TRIAGEID", "ROADMAPID"}
162+
projectIDs, err := result.ProjectsToIDs([]string{"triage", "roadmap"})
163+
if err != nil {
164+
t.Errorf("error resolving projects: %v", err)
165+
}
166+
if !sliceEqual(projectIDs, expectedProjectIDs) {
167+
t.Errorf("expected projects %v, got %v", expectedProjectIDs, projectIDs)
168+
}
169+
170+
expectedMilestoneID := "BIGONEID"
171+
milestoneID, err := result.MilestoneToID("big one.oh")
172+
if err != nil {
173+
t.Errorf("error resolving milestone: %v", err)
174+
}
175+
if milestoneID != expectedMilestoneID {
176+
t.Errorf("expected milestone %v, got %v", expectedMilestoneID, milestoneID)
177+
}
178+
}
49179

50180
func Test_RepoResolveMetadataIDs(t *testing.T) {
51181
http := &httpmock.Registry{}

command/issue.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -473,15 +473,14 @@ func issueCreate(cmd *cobra.Command, args []string) error {
473473

474474
if tb.HasMetadata() {
475475
if tb.MetadataResult == nil {
476-
metadataInput := api.RepoMetadataInput{
477-
Assignees: len(tb.Assignees) > 0,
478-
Labels: len(tb.Labels) > 0,
479-
Projects: len(tb.Projects) > 0,
480-
Milestones: len(tb.Milestones) > 0,
476+
resolveInput := api.RepoResolveInput{
477+
Assignees: tb.Assignees,
478+
Labels: tb.Labels,
479+
Projects: tb.Projects,
480+
Milestones: tb.Milestones,
481481
}
482482

483-
// TODO: for non-interactive mode, only translate given objects to GraphQL IDs
484-
tb.MetadataResult, err = api.RepoMetadata(apiClient, baseRepo, metadataInput)
483+
tb.MetadataResult, err = api.RepoResolveMetadataIDs(apiClient, baseRepo, resolveInput)
485484
if err != nil {
486485
return err
487486
}

command/issue_test.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -500,27 +500,15 @@ func TestIssueCreate_metadata(t *testing.T) {
500500
} } }
501501
`))
502502
http.Register(
503-
httpmock.GraphQL(`\bassignableUsers\(`),
503+
httpmock.GraphQL(`\bu000:`),
504504
httpmock.StringResponse(`
505-
{ "data": { "repository": { "assignableUsers": {
506-
"nodes": [
507-
{ "login": "hubot", "id": "HUBOTID" },
508-
{ "login": "MonaLisa", "id": "MONAID" }
509-
],
510-
"pageInfo": { "hasNextPage": false }
511-
} } } }
512-
`))
513-
http.Register(
514-
httpmock.GraphQL(`\blabels\(`),
515-
httpmock.StringResponse(`
516-
{ "data": { "repository": { "labels": {
517-
"nodes": [
518-
{ "name": "feature", "id": "FEATUREID" },
519-
{ "name": "TODO", "id": "TODOID" },
520-
{ "name": "bug", "id": "BUGID" }
521-
],
522-
"pageInfo": { "hasNextPage": false }
523-
} } } }
505+
{ "data": {
506+
"u000": { "login": "MonaLisa", "id": "MONAID" },
507+
"repository": {
508+
"l000": { "name": "bug", "id": "BUGID" },
509+
"l001": { "name": "TODO", "id": "TODOID" }
510+
}
511+
} }
524512
`))
525513
http.Register(
526514
httpmock.GraphQL(`\bmilestones\(`),

command/pr_create_test.go

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,19 @@ func TestPRCreate_metadata(t *testing.T) {
8787
] } } } }
8888
`))
8989
http.Register(
90-
httpmock.GraphQL(`\bassignableUsers\(`),
90+
httpmock.GraphQL(`\bteam\(`),
9191
httpmock.StringResponse(`
92-
{ "data": { "repository": { "assignableUsers": {
93-
"nodes": [
94-
{ "login": "hubot", "id": "HUBOTID" },
95-
{ "login": "MonaLisa", "id": "MONAID" }
96-
],
97-
"pageInfo": { "hasNextPage": false }
98-
} } } }
99-
`))
100-
http.Register(
101-
httpmock.GraphQL(`\blabels\(`),
102-
httpmock.StringResponse(`
103-
{ "data": { "repository": { "labels": {
104-
"nodes": [
105-
{ "name": "feature", "id": "FEATUREID" },
106-
{ "name": "TODO", "id": "TODOID" },
107-
{ "name": "bug", "id": "BUGID" }
108-
],
109-
"pageInfo": { "hasNextPage": false }
110-
} } } }
92+
{ "data": {
93+
"u000": { "login": "MonaLisa", "id": "MONAID" },
94+
"u001": { "login": "hubot", "id": "HUBOTID" },
95+
"repository": {
96+
"l000": { "name": "bug", "id": "BUGID" },
97+
"l001": { "name": "TODO", "id": "TODOID" }
98+
},
99+
"organization": {
100+
"t000": { "slug": "core", "id": "COREID" }
101+
}
102+
} }
111103
`))
112104
http.Register(
113105
httpmock.GraphQL(`\bmilestones\(`),
@@ -139,17 +131,6 @@ func TestPRCreate_metadata(t *testing.T) {
139131
"pageInfo": { "hasNextPage": false }
140132
} } } }
141133
`))
142-
http.Register(
143-
httpmock.GraphQL(`\borganization\(.+\bteams\(`),
144-
httpmock.StringResponse(`
145-
{ "data": { "organization": { "teams": {
146-
"nodes": [
147-
{ "slug": "owners", "id": "OWNERSID" },
148-
{ "slug": "Core", "id": "COREID" }
149-
],
150-
"pageInfo": { "hasNextPage": false }
151-
} } } }
152-
`))
153134
http.Register(
154135
httpmock.GraphQL(`\bcreatePullRequest\(`),
155136
httpmock.GraphQLMutation(`

0 commit comments

Comments
 (0)