Skip to content
Draft
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
15 changes: 14 additions & 1 deletion internal/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"math"

"github.com/cli/go-gh/v2/pkg/api"
graphql "github.com/cli/shurcooL-graphql"
Expand Down Expand Up @@ -319,6 +320,11 @@ func (c *Client) FindPRDetailsForBranch(branch string) (*PRDetails, error) {

// FindPRByNumber fetches a pull request by its number.
func (c *Client) FindPRByNumber(number int) (*PullRequest, error) {
gqlNumber, err := toGraphQLInt(number)
if err != nil {
return nil, err
}

var query struct {
Repository struct {
PullRequest struct {
Expand All @@ -339,7 +345,7 @@ func (c *Client) FindPRByNumber(number int) (*PullRequest, error) {
variables := map[string]interface{}{
"owner": graphql.String(c.owner),
"name": graphql.String(c.repo),
"number": graphql.Int(number),
"number": gqlNumber,
}

if err := c.gql.Query("FindPRByNumber", &query, variables); err != nil {
Expand All @@ -364,6 +370,13 @@ func (c *Client) FindPRByNumber(number int) (*PullRequest, error) {
}, nil
}

func toGraphQLInt(n int) (graphql.Int, error) {
if n < math.MinInt32 || n > math.MaxInt32 {
return 0, fmt.Errorf("number %d is out of GraphQL Int range", n)
}
return graphql.Int(n), nil
}

type RemoteStack struct {
ID int `json:"id"`
PullRequests []int `json:"pull_requests"`
Expand Down
14 changes: 14 additions & 0 deletions internal/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"testing"

graphql "github.com/cli/shurcooL-graphql"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -46,3 +47,16 @@ func TestPullRequest_IsQueued(t *testing.T) {
assert.False(t, pr.IsQueued())
})
}

func TestToGraphQLInt(t *testing.T) {
t.Run("in range", func(t *testing.T) {
got, err := toGraphQLInt(123)
assert.NoError(t, err)
assert.Equal(t, graphql.Int(123), got)
})

t.Run("out of range", func(t *testing.T) {
_, err := toGraphQLInt(1 << 40)
assert.Error(t, err)
})
}
Loading