Skip to content

Commit

Permalink
Merge pull request #63 from grafana/61-org-query-term
Browse files Browse the repository at this point in the history
feat: Support organization queries in pull requests
  • Loading branch information
kminehart authored Sep 15, 2020
2 parents 18dfb40 + c712192 commit 4529dfe
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 43 deletions.
74 changes: 47 additions & 27 deletions pkg/github/pull_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,22 @@ type PullRequestAuthor struct {

// PullRequest is a GitHub pull request
type PullRequest struct {
Title string
State githubv4.PullRequestState
Author PullRequestAuthor
Closed bool
IsDraft bool
Locked bool
Merged bool
ClosedAt githubv4.DateTime
CreatedAt githubv4.DateTime
UpdatedAt githubv4.DateTime
MergedAt githubv4.DateTime
Mergeable githubv4.MergeableState
MergedBy *PullRequestAuthor
Number int64
Title string
URL string
State githubv4.PullRequestState
Author PullRequestAuthor
Closed bool
IsDraft bool
Locked bool
Merged bool
ClosedAt githubv4.DateTime
CreatedAt githubv4.DateTime
UpdatedAt githubv4.DateTime
MergedAt githubv4.DateTime
Mergeable githubv4.MergeableState
MergedBy *PullRequestAuthor
Repository Repository
}

// PullRequests is a list of GitHub Pull Requests
Expand All @@ -65,7 +68,10 @@ func (p PullRequests) Frames() data.Frames {

frame := data.NewFrame(
"pull_requests",
data.NewField("number", nil, []int64{}),
data.NewField("title", nil, []string{}),
data.NewField("url", nil, []string{}),
data.NewField("repository", nil, []string{}),
data.NewField("state", nil, []string{}),
data.NewField("author_login", nil, []string{}),
data.NewField("author_email", nil, []string{}),
Expand Down Expand Up @@ -102,7 +108,10 @@ func (p PullRequests) Frames() data.Frames {
}

frame.AppendRow(
v.Number,
v.Title,
v.URL,
v.Repository.NameWithOwner,
string(v.State),
v.Author.User.Login,
v.Author.User.Email,
Expand All @@ -125,19 +134,10 @@ func (p PullRequests) Frames() data.Frames {

// GetAllPullRequests uses the graphql search endpoint API to search all pull requests in the repository
func GetAllPullRequests(ctx context.Context, client Client, opts models.ListPullRequestsOptions) (PullRequests, error) {
search := []string{
"is:pr",
fmt.Sprintf("repo:%s/%s", opts.Owner, opts.Repository),
}

if opts.Query != nil {
search = append(search, *opts.Query)
}

var (
variables = map[string]interface{}{
"cursor": (*githubv4.String)(nil),
"query": githubv4.String(strings.Join(search, " ")),
"query": githubv4.String(buildQuery(opts)),
}

pullRequests = []PullRequest{}
Expand Down Expand Up @@ -167,19 +167,39 @@ func GetAllPullRequests(ctx context.Context, client Client, opts models.ListPull

// GetPullRequestsInRange uses the graphql search endpoint API to find pull requests in the given time range.
func GetPullRequestsInRange(ctx context.Context, client Client, opts models.ListPullRequestsOptions, from time.Time, to time.Time) (PullRequests, error) {
search := []string{}
var q string

if opts.TimeField != models.PullRequestNone {
search = []string{fmt.Sprintf("%s:%s..%s", opts.TimeField.String(), from.Format(time.RFC3339), to.Format(time.RFC3339))}
q = fmt.Sprintf("%s:%s..%s", opts.TimeField.String(), from.Format(time.RFC3339), to.Format(time.RFC3339))
}

if opts.Query != nil {
search = append(search, *opts.Query)
q = fmt.Sprintf("%s %s", *opts.Query, q)
}

q := strings.Join(search, " ")
return GetAllPullRequests(ctx, client, models.ListPullRequestsOptions{
Repository: opts.Repository,
Owner: opts.Owner,
TimeField: opts.TimeField,
Query: &q,
})
}

// buildQuery builds the "query" field for Pull Request searches
func buildQuery(opts models.ListPullRequestsOptions) string {
search := []string{
"is:pr",
}

if opts.Repository == "" {
search = append(search, fmt.Sprintf("org:%s", opts.Owner))
} else {
search = append(search, fmt.Sprintf("repo:%s/%s", opts.Owner, opts.Repository))
}

if opts.Query != nil {
search = append(search, *opts.Query)
}

return strings.Join(search, " ")
}
51 changes: 47 additions & 4 deletions pkg/github/pull_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ func TestPullRequestsDataFrame(t *testing.T) {

pullRequests := PullRequests{
PullRequest{
Title: "PullRequest #1",
State: githubv4.PullRequestStateOpen,
Number: 1,
Title: "PullRequest #1",
URL: "https://github.com/grafana/github-datasource/pulls/1",
State: githubv4.PullRequestStateOpen,
Author: PullRequestAuthor{
User: firstUser,
},
Repository: Repository{
NameWithOwner: "grafana/github-datasource",
},
Closed: true,
IsDraft: false,
Locked: false,
Expand All @@ -81,11 +86,16 @@ func TestPullRequestsDataFrame(t *testing.T) {
MergedBy: nil,
},
PullRequest{
Title: "PullRequest #2",
State: githubv4.PullRequestStateOpen,
Number: 2,
Title: "PullRequest #2",
URL: "https://github.com/grafana/github-datasource/pulls/2",
State: githubv4.PullRequestStateOpen,
Author: PullRequestAuthor{
User: secondUser,
},
Repository: Repository{
NameWithOwner: "grafana/github-datasource",
},
Closed: true,
IsDraft: false,
Locked: false,
Expand Down Expand Up @@ -113,3 +123,36 @@ func TestPullRequestsDataFrame(t *testing.T) {
t.Fatal(err)
}
}

func TestBuildQuery(t *testing.T) {
t.Run("Searching pull requests with a Repository and organization should use the repo field", func(t *testing.T) {
opts := models.ListPullRequestsOptions{
Owner: "grafana",
Repository: "github-datasource",
}

var (
result = buildQuery(opts)
expect = "is:pr repo:grafana/github-datasource"
)

if result != expect {
t.Fatalf("Unexpected result from buildQuery. Expected '%s', received '%s'", expect, result)
}
})

t.Run("Issue #61 - Searching pull requests without a Repository should search the entire org", func(t *testing.T) {
opts := models.ListPullRequestsOptions{
Owner: "test",
Repository: "",
}

var (
result = buildQuery(opts)
expect = "is:pr org:test"
)
if result != expect {
t.Fatalf("Unexpected result from buildQuery. Expected '%s', received '%s'", expect, result)
}
})
}
2 changes: 1 addition & 1 deletion pkg/github/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func GetAllRepositories(ctx context.Context, client Client, opts models.ListRepo
repos = []Repository{}
)

for i := 0; i < PageNumberLimit; i++ {
for {
q := &QueryListRepositories{}
if err := client.Query(ctx, q, variables); err != nil {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion pkg/github/repository.go

This file was deleted.

Loading

0 comments on commit 4529dfe

Please sign in to comment.