Skip to content

Commit

Permalink
pkg/github/repositories: fetch repos using direct organization query …
Browse files Browse the repository at this point in the history
…instead of search API
  • Loading branch information
malhomaid committed Dec 11, 2024
1 parent fcf1933 commit e586ade
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions pkg/github/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package github

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

"github.com/grafana/github-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/shurcooL/githubv4"
)

Expand All @@ -22,12 +21,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 +38,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 +84,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 +98,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

0 comments on commit e586ade

Please sign in to comment.