Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/henvic/httpretty"
"github.com/shurcooL/graphql"
)

// ClientOption represents an argument to NewClient
Expand Down Expand Up @@ -235,6 +236,10 @@ func (c Client) GraphQL(query string, variables map[string]interface{}, data int
return handleResponse(resp, data)
}

func graphQLClient(h *http.Client) *graphql.Client {
return graphql.NewClient("https://api.github.com/graphql", h)
}

// REST performs a REST request and parses the response.
func (c Client) REST(method string, p string, body io.Reader, data interface{}) error {
url := "https://api.github.com/" + p
Expand Down
28 changes: 16 additions & 12 deletions api/queries_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const fragments = `
// IssueCreate creates an issue in a GitHub repository
func IssueCreate(client *Client, repo *Repository, params map[string]interface{}) (*Issue, error) {
query := `
mutation CreateIssue($input: CreateIssueInput!) {
mutation IssueCreate($input: CreateIssueInput!) {
createIssue(input: $input) {
issue {
url
Expand Down Expand Up @@ -140,7 +140,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string)
}

query := fragments + `
query($owner: String!, $repo: String!, $viewer: String!, $per_page: Int = 10) {
query IssueStatus($owner: String!, $repo: String!, $viewer: String!, $per_page: Int = 10) {
repository(owner: $owner, name: $repo) {
hasIssuesEnabled
assigned: issues(filterBy: {assignee: $viewer, states: OPEN}, first: $per_page, orderBy: {field: UPDATED_AT, direction: DESC}) {
Expand Down Expand Up @@ -212,7 +212,7 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str
}

query := fragments + `
query($owner: String!, $repo: String!, $limit: Int, $endCursor: String, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String, $author: String, $mention: String, $milestone: String) {
query IssueList($owner: String!, $repo: String!, $limit: Int, $endCursor: String, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String, $author: String, $mention: String, $milestone: String) {
repository(owner: $owner, name: $repo) {
hasIssuesEnabled
issues(first: $limit, after: $endCursor, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee, createdBy: $author, mentioned: $mention, milestone: $milestone}) {
Expand Down Expand Up @@ -306,7 +306,7 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
}

query := `
query($owner: String!, $repo: String!, $issue_number: Int!) {
query IssueByNumber($owner: String!, $repo: String!, $issue_number: Int!) {
repository(owner: $owner, name: $repo) {
hasIssuesEnabled
issue(number: $issue_number) {
Expand Down Expand Up @@ -383,12 +383,14 @@ func IssueClose(client *Client, repo ghrepo.Interface, issue Issue) error {
} `graphql:"closeIssue(input: $input)"`
}

input := githubv4.CloseIssueInput{
IssueID: issue.ID,
variables := map[string]interface{}{
"input": githubv4.CloseIssueInput{
IssueID: issue.ID,
},
}

v4 := githubv4.NewClient(client.http)
err := v4.Mutate(context.Background(), &mutation, input, nil)
gql := graphQLClient(client.http)
err := gql.MutateNamed(context.Background(), "IssueClose", &mutation, variables)

if err != nil {
return err
Expand All @@ -406,12 +408,14 @@ func IssueReopen(client *Client, repo ghrepo.Interface, issue Issue) error {
} `graphql:"reopenIssue(input: $input)"`
}

input := githubv4.ReopenIssueInput{
IssueID: issue.ID,
variables := map[string]interface{}{
"input": githubv4.ReopenIssueInput{
IssueID: issue.ID,
},
}

v4 := githubv4.NewClient(client.http)
err := v4.Mutate(context.Background(), &mutation, input, nil)
gql := graphQLClient(client.http)
err := gql.MutateNamed(context.Background(), "IssueReopen", &mutation, variables)

return err
}
8 changes: 4 additions & 4 deletions api/queries_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func OrganizationProjects(client *Client, owner string) ([]RepoProject, error) {
"endCursor": (*githubv4.String)(nil),
}

v4 := githubv4.NewClient(client.http)
gql := graphQLClient(client.http)

var projects []RepoProject
for {
err := v4.Query(context.Background(), &query, variables)
err := gql.QueryNamed(context.Background(), "OrganizationProjectList", &query, variables)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -90,11 +90,11 @@ func OrganizationTeams(client *Client, owner string) ([]OrgTeam, error) {
"endCursor": (*githubv4.String)(nil),
}

v4 := githubv4.NewClient(client.http)
gql := graphQLClient(client.http)

var teams []OrgTeam
for {
err := v4.Query(context.Background(), &query, variables)
err := gql.QueryNamed(context.Background(), "OrganizationTeamList", &query, variables)
if err != nil {
return nil, err
}
Expand Down
76 changes: 43 additions & 33 deletions api/queries_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
`

queryPrefix := `
query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
query PullRequestStatus($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
repository(owner: $owner, name: $repo) {
defaultBranchRef { name }
pullRequests(headRefName: $headRefName, first: $per_page, orderBy: { field: CREATED_AT, direction: DESC }) {
Expand All @@ -311,7 +311,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
`
if currentPRNumber > 0 {
queryPrefix = `
query($owner: String!, $repo: String!, $number: Int!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
query PullRequestStatus($owner: String!, $repo: String!, $number: Int!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
repository(owner: $owner, name: $repo) {
defaultBranchRef { name }
pullRequest(number: $number) {
Expand Down Expand Up @@ -408,7 +408,7 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
}

query := `
query($owner: String!, $repo: String!, $pr_number: Int!) {
query PullRequestByNumber($owner: String!, $repo: String!, $pr_number: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pr_number) {
id
Expand Down Expand Up @@ -518,7 +518,7 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
}

query := `
query($owner: String!, $repo: String!, $headRefName: String!) {
query PullRequestForBranch($owner: String!, $repo: String!, $headRefName: String!) {
repository(owner: $owner, name: $repo) {
pullRequests(headRefName: $headRefName, states: OPEN, first: 30) {
nodes {
Expand Down Expand Up @@ -634,7 +634,7 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
// CreatePullRequest creates a pull request in a GitHub repository
func CreatePullRequest(client *Client, repo *Repository, params map[string]interface{}) (*PullRequest, error) {
query := `
mutation CreatePullRequest($input: CreatePullRequestInput!) {
mutation PullRequestCreate($input: CreatePullRequestInput!) {
createPullRequest(input: $input) {
pullRequest {
id
Expand Down Expand Up @@ -681,7 +681,7 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter
}
if len(updateParams) > 0 {
updateQuery := `
mutation UpdatePullRequest($input: UpdatePullRequestInput!) {
mutation PullRequestCreateMetadata($input: UpdatePullRequestInput!) {
updatePullRequest(input: $input) { clientMutationId }
}`
updateParams["pullRequestId"] = pr.ID
Expand All @@ -705,7 +705,7 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter

if len(reviewParams) > 0 {
reviewQuery := `
mutation RequestReviews($input: RequestReviewsInput!) {
mutation PullRequestCreateRequestReviews($input: RequestReviewsInput!) {
requestReviews(input: $input) { clientMutationId }
}`
reviewParams["pullRequestId"] = pr.ID
Expand Down Expand Up @@ -749,16 +749,16 @@ func AddReview(client *Client, pr *PullRequest, input *PullRequestReviewInput) e
}

body := githubv4.String(input.Body)

gqlInput := githubv4.AddPullRequestReviewInput{
PullRequestID: pr.ID,
Event: &state,
Body: &body,
variables := map[string]interface{}{
"input": githubv4.AddPullRequestReviewInput{
PullRequestID: pr.ID,
Event: &state,
Body: &body,
},
}

v4 := githubv4.NewClient(client.http)

return v4.Mutate(context.Background(), &mutation, gqlInput, nil)
gql := graphQLClient(client.http)
return gql.MutateNamed(context.Background(), "PullRequestReviewAdd", &mutation, variables)
}

func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*PullRequestAndTotalCount, error) {
Expand Down Expand Up @@ -798,7 +798,7 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*P
// If assignee wasn't specified, use `Repository.pullRequest` for ability to
// query by multiple labels
query := fragment + `
query(
query PullRequestList(
$owner: String!,
$repo: String!,
$limit: Int!,
Expand Down Expand Up @@ -840,7 +840,7 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*P
// `Repository.pullRequests`, but this mode doesn't support multiple labels
if assignee, ok := vars["assignee"].(string); ok {
query = fragment + `
query(
query PullRequestList(
$q: String!,
$limit: Int!,
$endCursor: String,
Expand Down Expand Up @@ -938,12 +938,14 @@ func PullRequestClose(client *Client, repo ghrepo.Interface, pr *PullRequest) er
} `graphql:"closePullRequest(input: $input)"`
}

input := githubv4.ClosePullRequestInput{
PullRequestID: pr.ID,
variables := map[string]interface{}{
"input": githubv4.ClosePullRequestInput{
PullRequestID: pr.ID,
},
}

v4 := githubv4.NewClient(client.http)
err := v4.Mutate(context.Background(), &mutation, input, nil)
gql := graphQLClient(client.http)
err := gql.MutateNamed(context.Background(), "PullRequestClose", &mutation, variables)

return err
}
Expand All @@ -957,12 +959,14 @@ func PullRequestReopen(client *Client, repo ghrepo.Interface, pr *PullRequest) e
} `graphql:"reopenPullRequest(input: $input)"`
}

input := githubv4.ReopenPullRequestInput{
PullRequestID: pr.ID,
variables := map[string]interface{}{
"input": githubv4.ReopenPullRequestInput{
PullRequestID: pr.ID,
},
}

v4 := githubv4.NewClient(client.http)
err := v4.Mutate(context.Background(), &mutation, input, nil)
gql := graphQLClient(client.http)
err := gql.MutateNamed(context.Background(), "PullRequestReopen", &mutation, variables)

return err
}
Expand All @@ -984,13 +988,15 @@ func PullRequestMerge(client *Client, repo ghrepo.Interface, pr *PullRequest, m
} `graphql:"mergePullRequest(input: $input)"`
}

input := githubv4.MergePullRequestInput{
PullRequestID: pr.ID,
MergeMethod: &mergeMethod,
variables := map[string]interface{}{
"input": githubv4.MergePullRequestInput{
PullRequestID: pr.ID,
MergeMethod: &mergeMethod,
},
}

v4 := githubv4.NewClient(client.http)
err := v4.Mutate(context.Background(), &mutation, input, nil)
gql := graphQLClient(client.http)
err := gql.MutateNamed(context.Background(), "PullRequestMerge", &mutation, variables)

return err
}
Expand All @@ -1004,10 +1010,14 @@ func PullRequestReady(client *Client, repo ghrepo.Interface, pr *PullRequest) er
} `graphql:"markPullRequestReadyForReview(input: $input)"`
}

input := githubv4.MarkPullRequestReadyForReviewInput{PullRequestID: pr.ID}
variables := map[string]interface{}{
"input": githubv4.MarkPullRequestReadyForReviewInput{
PullRequestID: pr.ID,
},
}

v4 := githubv4.NewClient(client.http)
return v4.Mutate(context.Background(), &mutation, input, nil)
gql := graphQLClient(client.http)
return gql.MutateNamed(context.Background(), "PullRequestReadyForReview", &mutation, variables)
}

func BranchDeleteRemote(client *Client, repo ghrepo.Interface, branch string) error {
Expand Down
4 changes: 3 additions & 1 deletion api/queries_pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func TestBranchDeleteRemote(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
http := &httpmock.Registry{}
http.Register(httpmock.MatchAny, httpmock.StatusStringResponse(tt.responseStatus, tt.responseBody))
http.Register(
httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/branch"),
httpmock.StatusStringResponse(tt.responseStatus, tt.responseBody))

client := NewClient(ReplaceTripper(http))
repo, _ := ghrepo.FromFullName("OWNER/REPO")
Expand Down
Loading