X Tutup
Skip to content

Commit 66534e5

Browse files
committed
Warn about repo issues disabled on issue create
1 parent 915dd8b commit 66534e5

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

api/queries_issue.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ const fragments = `
4242
`
4343

4444
func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*Issue, error) {
45-
repoID, err := GitHubRepoId(client, ghRepo)
45+
repo, err := GitHubRepo(client, ghRepo)
4646
if err != nil {
4747
return nil, err
4848
}
4949

50+
if !repo.HasIssuesEnabled {
51+
return nil, fmt.Errorf("the '%s/%s' repository has disabled issues", ghRepo.RepoOwner(), ghRepo.RepoName())
52+
}
53+
5054
query := `
5155
mutation CreateIssue($input: CreateIssueInput!) {
5256
createIssue(input: $input) {
@@ -57,7 +61,7 @@ func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*I
5761
}`
5862

5963
inputParams := map[string]interface{}{
60-
"repositoryId": repoID,
64+
"repositoryId": repo.ID,
6165
}
6266
for key, val := range params {
6367
inputParams[key] = val

api/queries_pr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func PullRequestForBranch(client *Client, ghRepo Repo, branch string) (*PullRequ
376376
}
377377

378378
func CreatePullRequest(client *Client, ghRepo Repo, params map[string]interface{}) (*PullRequest, error) {
379-
repoID, err := GitHubRepoId(client, ghRepo)
379+
repo, err := GitHubRepo(client, ghRepo)
380380
if err != nil {
381381
return nil, err
382382
}
@@ -391,7 +391,7 @@ func CreatePullRequest(client *Client, ghRepo Repo, params map[string]interface{
391391
}`
392392

393393
inputParams := map[string]interface{}{
394-
"repositoryId": repoID,
394+
"repositoryId": repo.ID,
395395
}
396396
for key, val := range params {
397397
inputParams[key] = val

api/queries_repo.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,46 @@
11
package api
22

3-
import "fmt"
3+
import (
4+
"fmt"
45

5-
func GitHubRepoId(client *Client, ghRepo Repo) (string, error) {
6+
"github.com/pkg/errors"
7+
)
8+
9+
// Repository contains information about a GitHub repo
10+
type Repository struct {
11+
ID string
12+
HasIssuesEnabled bool
13+
}
14+
15+
// GitHubRepo looks up the node ID of a named repository
16+
func GitHubRepo(client *Client, ghRepo Repo) (*Repository, error) {
617
owner := ghRepo.RepoOwner()
718
repo := ghRepo.RepoName()
819

920
query := `
10-
query FindRepoID($owner:String!, $name:String!) {
11-
repository(owner:$owner, name:$name) {
12-
id
13-
}
21+
query($owner: String!, $name: String!) {
22+
repository(owner: $owner, name: $name) {
23+
id
24+
hasIssuesEnabled
25+
}
1426
}`
1527
variables := map[string]interface{}{
1628
"owner": owner,
1729
"name": repo,
1830
}
1931

2032
result := struct {
21-
Repository struct {
22-
Id string
23-
}
33+
Repository Repository
2434
}{}
2535
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)
36+
37+
if err != nil || result.Repository.ID == "" {
38+
newErr := fmt.Errorf("failed to determine repository ID for '%s/%s'", owner, repo)
39+
if err != nil {
40+
newErr = errors.Wrap(err, newErr.Error())
41+
}
42+
return nil, newErr
2843
}
2944

30-
return result.Repository.Id, nil
45+
return &result.Repository, nil
3146
}

command/issue_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ func TestIssueCreate(t *testing.T) {
231231

232232
http.StubResponse(200, bytes.NewBufferString(`
233233
{ "data": { "repository": {
234-
"id": "REPOID"
234+
"id": "REPOID",
235+
"hasIssuesEnabled": true
235236
} } }
236237
`))
237238
http.StubResponse(200, bytes.NewBufferString(`

0 commit comments

Comments
 (0)
X Tutup