Add issue create test

This commit is contained in:
Mislav Marohnić 2019-11-08 15:20:24 +01:00
parent 10c248d691
commit 25142d4895
3 changed files with 79 additions and 32 deletions

View file

@ -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
}

31
api/queries_repo.go Normal file
View file

@ -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
}

View file

@ -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")
}