diff --git a/api/queries_repo_test.go b/api/queries_repo_test.go index 6e36f4abc..94be4001c 100644 --- a/api/queries_repo_test.go +++ b/api/queries_repo_test.go @@ -46,6 +46,136 @@ func Test_RepoCreate(t *testing.T) { t.Errorf("expected homepageUrl to be %q, got %q", "http://example.com", homepage) } } +func Test_RepoMetadata(t *testing.T) { + http := &httpmock.Registry{} + client := NewClient(ReplaceTripper(http)) + + repo := ghrepo.FromFullName("OWNER/REPO") + input := RepoMetadataInput{ + Assignees: true, + Reviewers: true, + Labels: true, + Projects: true, + Milestones: true, + } + + http.Register( + httpmock.GraphQL(`\bassignableUsers\(`), + httpmock.StringResponse(` + { "data": { "repository": { "assignableUsers": { + "nodes": [ + { "login": "hubot", "id": "HUBOTID" }, + { "login": "MonaLisa", "id": "MONAID" } + ], + "pageInfo": { "hasNextPage": false } + } } } } + `)) + http.Register( + httpmock.GraphQL(`\blabels\(`), + httpmock.StringResponse(` + { "data": { "repository": { "labels": { + "nodes": [ + { "name": "feature", "id": "FEATUREID" }, + { "name": "TODO", "id": "TODOID" }, + { "name": "bug", "id": "BUGID" } + ], + "pageInfo": { "hasNextPage": false } + } } } } + `)) + http.Register( + httpmock.GraphQL(`\bmilestones\(`), + httpmock.StringResponse(` + { "data": { "repository": { "milestones": { + "nodes": [ + { "title": "GA", "id": "GAID" }, + { "title": "Big One.oh", "id": "BIGONEID" } + ], + "pageInfo": { "hasNextPage": false } + } } } } + `)) + http.Register( + httpmock.GraphQL(`\brepository\(.+\bprojects\(`), + httpmock.StringResponse(` + { "data": { "repository": { "projects": { + "nodes": [ + { "name": "Cleanup", "id": "CLEANUPID" }, + { "name": "Roadmap", "id": "ROADMAPID" } + ], + "pageInfo": { "hasNextPage": false } + } } } } + `)) + http.Register( + httpmock.GraphQL(`\borganization\(.+\bprojects\(`), + httpmock.StringResponse(` + { "data": { "organization": { "projects": { + "nodes": [ + { "name": "Triage", "id": "TRIAGEID" } + ], + "pageInfo": { "hasNextPage": false } + } } } } + `)) + http.Register( + httpmock.GraphQL(`\borganization\(.+\bteams\(`), + httpmock.StringResponse(` + { "data": { "organization": { "teams": { + "nodes": [ + { "slug": "owners", "id": "OWNERSID" }, + { "slug": "Core", "id": "COREID" } + ], + "pageInfo": { "hasNextPage": false } + } } } } + `)) + + result, err := RepoMetadata(client, repo, input) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expectedMemberIDs := []string{"MONAID", "HUBOTID"} + memberIDs, err := result.MembersToIDs([]string{"monalisa", "hubot"}) + if err != nil { + t.Errorf("error resolving members: %v", err) + } + if !sliceEqual(memberIDs, expectedMemberIDs) { + t.Errorf("expected members %v, got %v", expectedMemberIDs, memberIDs) + } + + expectedTeamIDs := []string{"COREID", "OWNERSID"} + teamIDs, err := result.TeamsToIDs([]string{"OWNER/core", "/owners"}) + if err != nil { + t.Errorf("error resolving teams: %v", err) + } + if !sliceEqual(teamIDs, expectedTeamIDs) { + t.Errorf("expected teams %v, got %v", expectedTeamIDs, teamIDs) + } + + expectedLabelIDs := []string{"BUGID", "TODOID"} + labelIDs, err := result.LabelsToIDs([]string{"bug", "todo"}) + if err != nil { + t.Errorf("error resolving labels: %v", err) + } + if !sliceEqual(labelIDs, expectedLabelIDs) { + t.Errorf("expected labels %v, got %v", expectedLabelIDs, labelIDs) + } + + expectedProjectIDs := []string{"TRIAGEID", "ROADMAPID"} + projectIDs, err := result.ProjectsToIDs([]string{"triage", "roadmap"}) + if err != nil { + t.Errorf("error resolving projects: %v", err) + } + if !sliceEqual(projectIDs, expectedProjectIDs) { + t.Errorf("expected projects %v, got %v", expectedProjectIDs, projectIDs) + } + + expectedMilestoneID := "BIGONEID" + milestoneID, err := result.MilestoneToID("big one.oh") + if err != nil { + t.Errorf("error resolving milestone: %v", err) + } + if milestoneID != expectedMilestoneID { + t.Errorf("expected milestone %v, got %v", expectedMilestoneID, milestoneID) + } +} func Test_RepoResolveMetadataIDs(t *testing.T) { http := &httpmock.Registry{} diff --git a/command/issue.go b/command/issue.go index b5e44787c..902481c7e 100644 --- a/command/issue.go +++ b/command/issue.go @@ -473,15 +473,14 @@ func issueCreate(cmd *cobra.Command, args []string) error { if tb.HasMetadata() { if tb.MetadataResult == nil { - metadataInput := api.RepoMetadataInput{ - Assignees: len(tb.Assignees) > 0, - Labels: len(tb.Labels) > 0, - Projects: len(tb.Projects) > 0, - Milestones: len(tb.Milestones) > 0, + resolveInput := api.RepoResolveInput{ + Assignees: tb.Assignees, + Labels: tb.Labels, + Projects: tb.Projects, + Milestones: tb.Milestones, } - // TODO: for non-interactive mode, only translate given objects to GraphQL IDs - tb.MetadataResult, err = api.RepoMetadata(apiClient, baseRepo, metadataInput) + tb.MetadataResult, err = api.RepoResolveMetadataIDs(apiClient, baseRepo, resolveInput) if err != nil { return err } diff --git a/command/issue_test.go b/command/issue_test.go index 5a9d4c239..c3a83b75a 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -500,27 +500,15 @@ func TestIssueCreate_metadata(t *testing.T) { } } } `)) http.Register( - httpmock.GraphQL(`\bassignableUsers\(`), + httpmock.GraphQL(`\bu000:`), httpmock.StringResponse(` - { "data": { "repository": { "assignableUsers": { - "nodes": [ - { "login": "hubot", "id": "HUBOTID" }, - { "login": "MonaLisa", "id": "MONAID" } - ], - "pageInfo": { "hasNextPage": false } - } } } } - `)) - http.Register( - httpmock.GraphQL(`\blabels\(`), - httpmock.StringResponse(` - { "data": { "repository": { "labels": { - "nodes": [ - { "name": "feature", "id": "FEATUREID" }, - { "name": "TODO", "id": "TODOID" }, - { "name": "bug", "id": "BUGID" } - ], - "pageInfo": { "hasNextPage": false } - } } } } + { "data": { + "u000": { "login": "MonaLisa", "id": "MONAID" }, + "repository": { + "l000": { "name": "bug", "id": "BUGID" }, + "l001": { "name": "TODO", "id": "TODOID" } + } + } } `)) http.Register( httpmock.GraphQL(`\bmilestones\(`), diff --git a/command/pr_create_test.go b/command/pr_create_test.go index 85966aa72..7f1b772a4 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -87,27 +87,19 @@ func TestPRCreate_metadata(t *testing.T) { ] } } } } `)) http.Register( - httpmock.GraphQL(`\bassignableUsers\(`), + httpmock.GraphQL(`\bteam\(`), httpmock.StringResponse(` - { "data": { "repository": { "assignableUsers": { - "nodes": [ - { "login": "hubot", "id": "HUBOTID" }, - { "login": "MonaLisa", "id": "MONAID" } - ], - "pageInfo": { "hasNextPage": false } - } } } } - `)) - http.Register( - httpmock.GraphQL(`\blabels\(`), - httpmock.StringResponse(` - { "data": { "repository": { "labels": { - "nodes": [ - { "name": "feature", "id": "FEATUREID" }, - { "name": "TODO", "id": "TODOID" }, - { "name": "bug", "id": "BUGID" } - ], - "pageInfo": { "hasNextPage": false } - } } } } + { "data": { + "u000": { "login": "MonaLisa", "id": "MONAID" }, + "u001": { "login": "hubot", "id": "HUBOTID" }, + "repository": { + "l000": { "name": "bug", "id": "BUGID" }, + "l001": { "name": "TODO", "id": "TODOID" } + }, + "organization": { + "t000": { "slug": "core", "id": "COREID" } + } + } } `)) http.Register( httpmock.GraphQL(`\bmilestones\(`), @@ -139,17 +131,6 @@ func TestPRCreate_metadata(t *testing.T) { "pageInfo": { "hasNextPage": false } } } } } `)) - http.Register( - httpmock.GraphQL(`\borganization\(.+\bteams\(`), - httpmock.StringResponse(` - { "data": { "organization": { "teams": { - "nodes": [ - { "slug": "owners", "id": "OWNERSID" }, - { "slug": "Core", "id": "COREID" } - ], - "pageInfo": { "hasNextPage": false } - } } } } - `)) http.Register( httpmock.GraphQL(`\bcreatePullRequest\(`), httpmock.GraphQLMutation(`