@@ -2,11 +2,13 @@ package services
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"os"
6
7
7
8
"github.com/cloudquery/plugin-sdk/v4/transformers"
8
9
9
10
"github.com/cloudquery/plugin-sdk/v4/schema"
11
+ gh "github.com/google/go-github/v57/github"
10
12
"github.com/guardian/cq-source-github-languages/internal/github"
11
13
)
12
14
@@ -24,24 +26,62 @@ func LanguagesTable() *schema.Table {
24
26
}
25
27
}
26
28
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
+
27
68
func fetchLanguages (ctx context.Context , meta schema.ClientMeta , parent * schema.Resource , res chan <- any ) error {
28
69
// TODO authenticate via GitHub App
29
- token := os .Getenv ("GITHUB_TOKEN " )
70
+ token := os .Getenv ("GITHUB_ACCESS_TOKEN " )
30
71
c := github .CustomClient (token )
31
- allRepos , _ , err := c .GitHubClient .Repositories .ListByOrg (ctx , "guardian" , nil )
72
+
73
+ repos , err := fetchRepositories (c .GitHubClient )
32
74
if err != nil {
33
75
return err
34
76
}
35
77
36
- // TODO only fetch languages for repositories with `topic = production`
37
- for _ , repo := range allRepos [1 :10 ] {
78
+ for _ , repo := range repos {
38
79
langs , err := c .GetLanguages (* repo .Owner .Login , * repo .Name )
39
80
if err != nil {
40
81
return err
41
82
}
42
83
43
84
res <- langs
44
-
45
85
}
46
86
return nil
47
87
}
0 commit comments