From fc25a4e9ed860d9dc52d3243b522256cebabc553 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 21 Jan 2020 15:37:42 -0600 Subject: [PATCH 1/3] check for disabled issues in issue view command --- api/queries_issue.go | 31 +++++++++++++++++++++++++++++++ command/issue.go | 8 ++++++++ 2 files changed, 39 insertions(+) diff --git a/api/queries_issue.go b/api/queries_issue.go index 7ba12f6ad..c1ffb5253 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -91,6 +91,37 @@ func IssueCreate(client *Client, repo *Repository, params map[string]interface{} return &result.CreateIssue.Issue, nil } +func HasIssuesEnabled(client *Client, ghRepo Repo) (bool, error) { + type response struct { + Repository struct { + HasIssuesEnabled bool + } + } + + query := ` + query($owner: String!, $repo: String!) { + repository(owner: $owner, name: $repo) { + hasIssuesEnabled + } + }` + + owner := ghRepo.RepoOwner() + repo := ghRepo.RepoName() + variables := map[string]interface{}{ + "owner": owner, + "repo": repo, + } + + var resp response + err := client.GraphQL(query, variables, &resp) + if err != nil { + return false, err + } + + return resp.Repository.HasIssuesEnabled, nil + +} + func IssueStatus(client *Client, ghRepo Repo, currentUsername string) (*IssuesPayload, error) { type response struct { Repository struct { diff --git a/command/issue.go b/command/issue.go index 139ab3337..49bf66a7e 100644 --- a/command/issue.go +++ b/command/issue.go @@ -215,6 +215,14 @@ func issueView(cmd *cobra.Command, args []string) error { return err } + issuesEnabled, err := api.HasIssuesEnabled(apiClient, baseRepo) + if err != nil { + return err + } + if !issuesEnabled { + return fmt.Errorf("the '%s/%s' repository has disabled issues", baseRepo.RepoOwner(), baseRepo.RepoName()) + } + issue, err := issueFromArg(apiClient, baseRepo, args[0]) if err != nil { return err From f09b05e628ea8a0af0c9c501154a1c3b40c64128 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 21 Jan 2020 15:47:36 -0600 Subject: [PATCH 2/3] test for disabled issues --- command/issue_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/command/issue_test.go b/command/issue_test.go index e9f616f68..532c3cc35 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -202,6 +202,13 @@ func TestIssueView(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { + "id": "REPOID", + "hasIssuesEnabled": true + } } } + `)) + http.StubResponse(200, bytes.NewBufferString(` { "data": { "repository": { "issue": { "number": 123, @@ -235,6 +242,13 @@ func TestIssueView_preview(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { + "id": "REPOID", + "hasIssuesEnabled": true + } } } + `)) + http.StubResponse(200, bytes.NewBufferString(` { "data": { "repository": { "issue": { "number": 123, @@ -280,6 +294,13 @@ func TestIssueView_notFound(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { + "id": "REPOID", + "hasIssuesEnabled": true + } } } + `)) + http.StubResponse(200, bytes.NewBufferString(` { "errors": [ { "message": "Could not resolve to an Issue with the number of 9999." } @@ -303,10 +324,34 @@ func TestIssueView_notFound(t *testing.T) { } } +func TestIssueView_disabledIssues(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { + "id": "REPOID", + "hasIssuesEnabled": false + } } } + `)) + + _, err := RunCommand(issueViewCmd, `issue view 6666`) + if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" { + t.Errorf("error running command `issue view`: %v", err) + } +} + func TestIssueView_urlArg(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { + "id": "REPOID", + "hasIssuesEnabled": true + } } } + `)) + http.StubResponse(200, bytes.NewBufferString(` { "data": { "repository": { "issue": { "number": 123, From 8c84fe3e3c44f8f411fed83c89a33c261e8f80d2 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 22 Jan 2020 12:37:00 -0600 Subject: [PATCH 3/3] just augment existing queries --- api/queries_issue.go | 39 +++++++-------------------------------- command/issue.go | 8 -------- command/issue_test.go | 34 +++------------------------------- 3 files changed, 10 insertions(+), 71 deletions(-) diff --git a/api/queries_issue.go b/api/queries_issue.go index c1ffb5253..50fcbd434 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -91,37 +91,6 @@ func IssueCreate(client *Client, repo *Repository, params map[string]interface{} return &result.CreateIssue.Issue, nil } -func HasIssuesEnabled(client *Client, ghRepo Repo) (bool, error) { - type response struct { - Repository struct { - HasIssuesEnabled bool - } - } - - query := ` - query($owner: String!, $repo: String!) { - repository(owner: $owner, name: $repo) { - hasIssuesEnabled - } - }` - - owner := ghRepo.RepoOwner() - repo := ghRepo.RepoName() - variables := map[string]interface{}{ - "owner": owner, - "repo": repo, - } - - var resp response - err := client.GraphQL(query, variables, &resp) - if err != nil { - return false, err - } - - return resp.Repository.HasIssuesEnabled, nil - -} - func IssueStatus(client *Client, ghRepo Repo, currentUsername string) (*IssuesPayload, error) { type response struct { Repository struct { @@ -267,13 +236,15 @@ func IssueList(client *Client, ghRepo Repo, state string, labels []string, assig func IssueByNumber(client *Client, ghRepo Repo, number int) (*Issue, error) { type response struct { Repository struct { - Issue Issue + Issue Issue + HasIssuesEnabled bool } } query := ` query($owner: String!, $repo: String!, $issue_number: Int!) { repository(owner: $owner, name: $repo) { + hasIssuesEnabled issue(number: $issue_number) { title body @@ -306,5 +277,9 @@ func IssueByNumber(client *Client, ghRepo Repo, number int) (*Issue, error) { return nil, err } + if !resp.Repository.HasIssuesEnabled { + return nil, fmt.Errorf("the '%s/%s' repository has disabled issues", ghRepo.RepoOwner(), ghRepo.RepoName()) + } + return &resp.Repository.Issue, nil } diff --git a/command/issue.go b/command/issue.go index 49bf66a7e..139ab3337 100644 --- a/command/issue.go +++ b/command/issue.go @@ -215,14 +215,6 @@ func issueView(cmd *cobra.Command, args []string) error { return err } - issuesEnabled, err := api.HasIssuesEnabled(apiClient, baseRepo) - if err != nil { - return err - } - if !issuesEnabled { - return fmt.Errorf("the '%s/%s' repository has disabled issues", baseRepo.RepoOwner(), baseRepo.RepoName()) - } - issue, err := issueFromArg(apiClient, baseRepo, args[0]) if err != nil { return err diff --git a/command/issue_test.go b/command/issue_test.go index 532c3cc35..9cf4156b4 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -203,14 +203,7 @@ func TestIssueView(t *testing.T) { http := initFakeHTTP() http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": true - } } } - `)) - - http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { "issue": { + { "data": { "repository": { "hasIssuesEnabled": true, "issue": { "number": 123, "url": "https://github.com/OWNER/REPO/issues/123" } } } } @@ -243,14 +236,7 @@ func TestIssueView_preview(t *testing.T) { http := initFakeHTTP() http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": true - } } } - `)) - - http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { "issue": { + { "data": { "repository": { "hasIssuesEnabled": true, "issue": { "number": 123, "body": "**bold story**", "title": "ix of coins", @@ -294,13 +280,6 @@ func TestIssueView_notFound(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() - http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": true - } } } - `)) - http.StubResponse(200, bytes.NewBufferString(` { "errors": [ { "message": "Could not resolve to an Issue with the number of 9999." } @@ -346,14 +325,7 @@ func TestIssueView_urlArg(t *testing.T) { http := initFakeHTTP() http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": true - } } } - `)) - - http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { "issue": { + { "data": { "repository": { "hasIssuesEnabled": true, "issue": { "number": 123, "url": "https://github.com/OWNER/REPO/issues/123" } } } }