From 25142d4895b41a31f186e3418759cb1692a671e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 8 Nov 2019 15:20:24 +0100 Subject: [PATCH] Add `issue create` test --- api/queries_issue.go | 34 ++------------------------------ api/queries_repo.go | 31 +++++++++++++++++++++++++++++ command/issue_test.go | 46 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 32 deletions(-) create mode 100644 api/queries_repo.go diff --git a/api/queries_issue.go b/api/queries_issue.go index c488a18b5..56a235c89 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -1,9 +1,7 @@ package api -import "fmt" - func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*Issue, error) { - repoId, err := GitHubRepoId(client, ghRepo) + repoID, err := GitHubRepoId(client, ghRepo) if err != nil { return nil, err } @@ -18,7 +16,7 @@ func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*I }` inputParams := map[string]interface{}{ - "repositoryId": repoId, + "repositoryId": repoID, } for key, val := range params { inputParams[key] = val @@ -40,31 +38,3 @@ func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*I return &result.CreateIssue.Issue, nil } - -func GitHubRepoId(client *Client, ghRepo Repo) (string, error) { - owner := ghRepo.RepoOwner() - repo := ghRepo.RepoName() - - query := ` - query FindRepoID($owner:String!, $name:String!) { - repository(owner:$owner, name:$name) { - id - } - }` - variables := map[string]interface{}{ - "owner": owner, - "name": repo, - } - - result := struct { - Repository struct { - Id string - } - }{} - err := client.GraphQL(query, variables, &result) - if err != nil || result.Repository.Id == "" { - return "", fmt.Errorf("failed to determine GH repo ID: %s", err) - } - - return result.Repository.Id, nil -} diff --git a/api/queries_repo.go b/api/queries_repo.go new file mode 100644 index 000000000..92010b880 --- /dev/null +++ b/api/queries_repo.go @@ -0,0 +1,31 @@ +package api + +import "fmt" + +func GitHubRepoId(client *Client, ghRepo Repo) (string, error) { + owner := ghRepo.RepoOwner() + repo := ghRepo.RepoName() + + query := ` + query FindRepoID($owner:String!, $name:String!) { + repository(owner:$owner, name:$name) { + id + } + }` + variables := map[string]interface{}{ + "owner": owner, + "name": repo, + } + + result := struct { + Repository struct { + Id string + } + }{} + err := client.GraphQL(query, variables, &result) + if err != nil || result.Repository.Id == "" { + return "", fmt.Errorf("failed to determine GH repo ID: %s", err) + } + + return result.Repository.Id, nil +} diff --git a/command/issue_test.go b/command/issue_test.go index 4571bc43e..29d494663 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -1,6 +1,9 @@ package command import ( + "bytes" + "encoding/json" + "io/ioutil" "os" "os/exec" "regexp" @@ -69,3 +72,46 @@ func TestIssueView(t *testing.T) { t.Errorf("got: %q", url) } } + +func TestIssueCreate(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { + "id": "REPOID" + } } } + `)) + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "createIssue": { "issue": { + "URL": "https://github.com/OWNER/REPO/issues/12" + } } } } + `)) + + out := bytes.Buffer{} + issueCreateCmd.SetOut(&out) + + RootCmd.SetArgs([]string{"issue", "create", "-m", "hello", "-m", "ab", "-m", "cd"}) + _, err := RootCmd.ExecuteC() + if err != nil { + t.Errorf("error running command `issue create`: %v", err) + } + + bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body) + reqBody := struct { + Variables struct { + Input struct { + RepositoryID string + Title string + Body string + } + } + }{} + json.Unmarshal(bodyBytes, &reqBody) + + eq(t, reqBody.Variables.Input.RepositoryID, "REPOID") + eq(t, reqBody.Variables.Input.Title, "hello") + eq(t, reqBody.Variables.Input.Body, "ab\n\ncd") + + eq(t, out.String(), "https://github.com/OWNER/REPO/issues/12\n") +}