diff --git a/api/queries_issue.go b/api/queries_issue.go index 19c0f8b24..3fb20e094 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -42,11 +42,15 @@ const fragments = ` ` func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*Issue, error) { - repoID, err := GitHubRepoId(client, ghRepo) + repo, err := GitHubRepo(client, ghRepo) if err != nil { return nil, err } + if !repo.HasIssuesEnabled { + return nil, fmt.Errorf("the '%s/%s' repository has disabled issues", ghRepo.RepoOwner(), ghRepo.RepoName()) + } + query := ` mutation CreateIssue($input: CreateIssueInput!) { createIssue(input: $input) { @@ -57,7 +61,7 @@ func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*I }` inputParams := map[string]interface{}{ - "repositoryId": repoID, + "repositoryId": repo.ID, } for key, val := range params { inputParams[key] = val diff --git a/api/queries_pr.go b/api/queries_pr.go index 01ece0369..a3feb959a 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -376,7 +376,7 @@ func PullRequestForBranch(client *Client, ghRepo Repo, branch string) (*PullRequ } func CreatePullRequest(client *Client, ghRepo Repo, params map[string]interface{}) (*PullRequest, error) { - repoID, err := GitHubRepoId(client, ghRepo) + repo, err := GitHubRepo(client, ghRepo) if err != nil { return nil, err } @@ -391,7 +391,7 @@ func CreatePullRequest(client *Client, ghRepo Repo, params map[string]interface{ }` inputParams := map[string]interface{}{ - "repositoryId": repoID, + "repositoryId": repo.ID, } for key, val := range params { inputParams[key] = val diff --git a/api/queries_repo.go b/api/queries_repo.go index 92010b880..f974b41cb 100644 --- a/api/queries_repo.go +++ b/api/queries_repo.go @@ -1,16 +1,28 @@ package api -import "fmt" +import ( + "fmt" -func GitHubRepoId(client *Client, ghRepo Repo) (string, error) { + "github.com/pkg/errors" +) + +// Repository contains information about a GitHub repo +type Repository struct { + ID string + HasIssuesEnabled bool +} + +// GitHubRepo looks up the node ID of a named repository +func GitHubRepo(client *Client, ghRepo Repo) (*Repository, error) { owner := ghRepo.RepoOwner() repo := ghRepo.RepoName() query := ` - query FindRepoID($owner:String!, $name:String!) { - repository(owner:$owner, name:$name) { - id - } + query($owner: String!, $name: String!) { + repository(owner: $owner, name: $name) { + id + hasIssuesEnabled + } }` variables := map[string]interface{}{ "owner": owner, @@ -18,14 +30,17 @@ func GitHubRepoId(client *Client, ghRepo Repo) (string, error) { } result := struct { - Repository struct { - Id string - } + Repository Repository }{} err := client.GraphQL(query, variables, &result) - if err != nil || result.Repository.Id == "" { - return "", fmt.Errorf("failed to determine GH repo ID: %s", err) + + if err != nil || result.Repository.ID == "" { + newErr := fmt.Errorf("failed to determine repository ID for '%s/%s'", owner, repo) + if err != nil { + newErr = errors.Wrap(err, newErr.Error()) + } + return nil, newErr } - return result.Repository.Id, nil + return &result.Repository, nil } diff --git a/command/issue_test.go b/command/issue_test.go index b2711a8cd..66878aa2b 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -231,7 +231,8 @@ func TestIssueCreate(t *testing.T) { http.StubResponse(200, bytes.NewBufferString(` { "data": { "repository": { - "id": "REPOID" + "id": "REPOID", + "hasIssuesEnabled": true } } } `)) http.StubResponse(200, bytes.NewBufferString(`