Skip to content

Commit 25142d4

Browse files
committed
Add issue create test
1 parent 10c248d commit 25142d4

File tree

3 files changed

+79
-32
lines changed

3 files changed

+79
-32
lines changed

api/queries_issue.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package api
22

3-
import "fmt"
4-
53
func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*Issue, error) {
6-
repoId, err := GitHubRepoId(client, ghRepo)
4+
repoID, err := GitHubRepoId(client, ghRepo)
75
if err != nil {
86
return nil, err
97
}
@@ -18,7 +16,7 @@ func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*I
1816
}`
1917

2018
inputParams := map[string]interface{}{
21-
"repositoryId": repoId,
19+
"repositoryId": repoID,
2220
}
2321
for key, val := range params {
2422
inputParams[key] = val
@@ -40,31 +38,3 @@ func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*I
4038

4139
return &result.CreateIssue.Issue, nil
4240
}
43-
44-
func GitHubRepoId(client *Client, ghRepo Repo) (string, error) {
45-
owner := ghRepo.RepoOwner()
46-
repo := ghRepo.RepoName()
47-
48-
query := `
49-
query FindRepoID($owner:String!, $name:String!) {
50-
repository(owner:$owner, name:$name) {
51-
id
52-
}
53-
}`
54-
variables := map[string]interface{}{
55-
"owner": owner,
56-
"name": repo,
57-
}
58-
59-
result := struct {
60-
Repository struct {
61-
Id string
62-
}
63-
}{}
64-
err := client.GraphQL(query, variables, &result)
65-
if err != nil || result.Repository.Id == "" {
66-
return "", fmt.Errorf("failed to determine GH repo ID: %s", err)
67-
}
68-
69-
return result.Repository.Id, nil
70-
}

api/queries_repo.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package api
2+
3+
import "fmt"
4+
5+
func GitHubRepoId(client *Client, ghRepo Repo) (string, error) {
6+
owner := ghRepo.RepoOwner()
7+
repo := ghRepo.RepoName()
8+
9+
query := `
10+
query FindRepoID($owner:String!, $name:String!) {
11+
repository(owner:$owner, name:$name) {
12+
id
13+
}
14+
}`
15+
variables := map[string]interface{}{
16+
"owner": owner,
17+
"name": repo,
18+
}
19+
20+
result := struct {
21+
Repository struct {
22+
Id string
23+
}
24+
}{}
25+
err := client.GraphQL(query, variables, &result)
26+
if err != nil || result.Repository.Id == "" {
27+
return "", fmt.Errorf("failed to determine GH repo ID: %s", err)
28+
}
29+
30+
return result.Repository.Id, nil
31+
}

command/issue_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package command
22

33
import (
4+
"bytes"
5+
"encoding/json"
6+
"io/ioutil"
47
"os"
58
"os/exec"
69
"regexp"
@@ -69,3 +72,46 @@ func TestIssueView(t *testing.T) {
6972
t.Errorf("got: %q", url)
7073
}
7174
}
75+
76+
func TestIssueCreate(t *testing.T) {
77+
initBlankContext("OWNER/REPO", "master")
78+
http := initFakeHTTP()
79+
80+
http.StubResponse(200, bytes.NewBufferString(`
81+
{ "data": { "repository": {
82+
"id": "REPOID"
83+
} } }
84+
`))
85+
http.StubResponse(200, bytes.NewBufferString(`
86+
{ "data": { "createIssue": { "issue": {
87+
"URL": "https://github.com/OWNER/REPO/issues/12"
88+
} } } }
89+
`))
90+
91+
out := bytes.Buffer{}
92+
issueCreateCmd.SetOut(&out)
93+
94+
RootCmd.SetArgs([]string{"issue", "create", "-m", "hello", "-m", "ab", "-m", "cd"})
95+
_, err := RootCmd.ExecuteC()
96+
if err != nil {
97+
t.Errorf("error running command `issue create`: %v", err)
98+
}
99+
100+
bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body)
101+
reqBody := struct {
102+
Variables struct {
103+
Input struct {
104+
RepositoryID string
105+
Title string
106+
Body string
107+
}
108+
}
109+
}{}
110+
json.Unmarshal(bodyBytes, &reqBody)
111+
112+
eq(t, reqBody.Variables.Input.RepositoryID, "REPOID")
113+
eq(t, reqBody.Variables.Input.Title, "hello")
114+
eq(t, reqBody.Variables.Input.Body, "ab\n\ncd")
115+
116+
eq(t, out.String(), "https://github.com/OWNER/REPO/issues/12\n")
117+
}

0 commit comments

Comments
 (0)