Prototype issue create

This commit is contained in:
Mislav Marohnić 2019-11-01 15:31:07 +01:00
parent 5d2ecf12ca
commit 401aef283f
3 changed files with 154 additions and 0 deletions

View file

@ -32,6 +32,7 @@ type IssuesPayload struct {
type Issue struct {
Number int
Title string
URL string
}
func Issues(client *Client, ghRepo Repo, currentUsername string) (*IssuesPayload, error) {

70
api/queries_issue.go Normal file
View file

@ -0,0 +1,70 @@
package api
import "fmt"
func IssueCreate(client *Client, ghRepo Repo, params map[string]interface{}) (*Issue, error) {
repoId, err := GitHubRepoId(client, ghRepo)
if err != nil {
return nil, err
}
query := `
mutation CreateIssue($input: CreateIssueInput!) {
createIssue(input: $input) {
issue {
url
}
}
}`
inputParams := map[string]interface{}{
"repositoryId": repoId,
}
for key, val := range params {
inputParams[key] = val
}
variables := map[string]interface{}{
"input": inputParams,
}
result := struct {
CreateIssue struct {
Issue Issue
}
}{}
err = client.GraphQL(query, variables, &result)
if err != nil {
return nil, err
}
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
}