Warn about repo issues disabled on issue create

This commit is contained in:
Mislav Marohnić 2019-12-19 15:26:26 +01:00
parent 915dd8b0ef
commit 66534e504b
4 changed files with 37 additions and 17 deletions

View file

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

View file

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

View file

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

View file

@ -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(`