Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch repos using direct organization query instead of search API #401

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 18 additions & 26 deletions pkg/github/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package github

import (
"context"
"fmt"
"strings"
"time"

"github.com/grafana/github-datasource/pkg/models"
Expand All @@ -22,12 +20,14 @@ import (
// }
// }
type QueryListRepositories struct {
Search struct {
Nodes []struct {
Repository Repository `graphql:"... on Repository"`
}
Organization struct {
Repositories struct {
Nodes []struct {
Repository `graphql:"... on Repository"`
}
PageInfo models.PageInfo
} `graphql:"search(query: $query, type: REPOSITORY, first: 100, after: $cursor)"`
} `graphql:"repositories(first: 100, after: $cursor)"`
} `graphql:"organization(login: $owner)"`
}

// Repository is a code repository
Expand All @@ -37,12 +37,12 @@ type Repository struct {
Login string
}
NameWithOwner string
URL string
ForkCount int64
IsFork bool
IsMirror bool
IsPrivate bool
CreatedAt githubv4.DateTime
URL string
ForkCount int64
IsFork bool
IsMirror bool
IsPrivate bool
CreatedAt githubv4.DateTime
}

// Repositories is a list of GitHub repositories
Expand Down Expand Up @@ -83,17 +83,12 @@ func (r Repositories) Frames() data.Frames {

// GetAllRepositories retrieves all available repositories for an organization
func GetAllRepositories(ctx context.Context, client models.Client, opts models.ListRepositoriesOptions) (Repositories, error) {
query := strings.Join([]string{
fmt.Sprintf("org:%s", opts.Owner),
opts.Repository,
}, " ")

var (
variables = map[string]interface{}{
"cursor": (*githubv4.String)(nil),
"query": githubv4.String(query),
"owner": githubv4.String(opts.Owner),
}

repos = []Repository{}
)

Expand All @@ -102,18 +97,15 @@ func GetAllRepositories(ctx context.Context, client models.Client, opts models.L
if err := client.Query(ctx, q, variables); err != nil {
return nil, err
}
r := make([]Repository, len(q.Search.Nodes))

for i, v := range q.Search.Nodes {
r[i] = v.Repository
for _, v := range q.Organization.Repositories.Nodes {
repos = append(repos, v.Repository)
}

repos = append(repos, r...)

if !q.Search.PageInfo.HasNextPage {
if !q.Organization.Repositories.PageInfo.HasNextPage {
break
}
variables["cursor"] = q.Search.PageInfo.EndCursor
variables["cursor"] = q.Organization.Repositories.PageInfo.EndCursor
}

return repos, nil
Expand Down