Skip to content

Commit 3a21bb9

Browse files
committed
get all repos, not just the first page.
filter so we only match on unarchived prod repos add a counter so we know how many we are grabbing per page
1 parent 3ea0613 commit 3a21bb9

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

resources/services/table.go

+45-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package services
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67

78
"github.com/cloudquery/plugin-sdk/v4/transformers"
89

910
"github.com/cloudquery/plugin-sdk/v4/schema"
11+
gh "github.com/google/go-github/v57/github"
1012
"github.com/guardian/cq-source-github-languages/internal/github"
1113
)
1214

@@ -24,24 +26,62 @@ func LanguagesTable() *schema.Table {
2426
}
2527
}
2628

29+
func contains(s []string, str string) bool {
30+
for _, v := range s {
31+
if v == str {
32+
return true
33+
}
34+
}
35+
return false
36+
}
37+
38+
func fetchRepositories(ghClient *gh.Client) ([]*gh.Repository, error) {
39+
opts := &gh.RepositoryListByOrgOptions{
40+
ListOptions: gh.ListOptions{
41+
PerPage: 100,
42+
}}
43+
44+
var allRepos []*gh.Repository
45+
for {
46+
repos, resp, err := ghClient.Repositories.ListByOrg(context.Background(), "guardian", opts)
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
for _, repo := range repos {
52+
//we are filtering here to only include repos we care about for OKR purposes.
53+
//the filters can be removed after we are sure we won't hit the rate limit
54+
if !*repo.Archived && contains(repo.Topics, "production") {
55+
allRepos = append(allRepos, repo)
56+
}
57+
}
58+
59+
fmt.Println("Counted ", len(allRepos), " repos so far")
60+
if resp.NextPage == 0 {
61+
break
62+
}
63+
opts.Page = resp.NextPage
64+
}
65+
return allRepos, nil
66+
}
67+
2768
func fetchLanguages(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error {
2869
// TODO authenticate via GitHub App
29-
token := os.Getenv("GITHUB_TOKEN")
70+
token := os.Getenv("GITHUB_ACCESS_TOKEN")
3071
c := github.CustomClient(token)
31-
allRepos, _, err := c.GitHubClient.Repositories.ListByOrg(ctx, "guardian", nil)
72+
73+
repos, err := fetchRepositories(c.GitHubClient)
3274
if err != nil {
3375
return err
3476
}
3577

36-
// TODO only fetch languages for repositories with `topic = production`
37-
for _, repo := range allRepos[1:10] {
78+
for _, repo := range repos {
3879
langs, err := c.GetLanguages(*repo.Owner.Login, *repo.Name)
3980
if err != nil {
4081
return err
4182
}
4283

4384
res <- langs
44-
4585
}
4686
return nil
4787
}

0 commit comments

Comments
 (0)