-
Notifications
You must be signed in to change notification settings - Fork 3
/
repos.go
116 lines (96 loc) · 2.9 KB
/
repos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"time"
"github.com/shurcooL/githubv4"
)
func getReposFromOrganization(client Client, org string, topic string) ([]Repo, error) {
var repoQuery struct {
Organization struct {
Repositories struct {
PageInfo struct {
HasNextPage githubv4.Boolean
EndCursor githubv4.String
}
Edges []RepositoryEdge
} `graphql:"repositories(first: 100, after: $cursor, orderBy: {field: CREATED_AT, direction: DESC}, isArchived: false)"`
} `graphql:"organization(login: $org)"`
}
var repos []Repo
variables := map[string]interface{}{
"org": githubv4.String(org),
"cursor": (*githubv4.String)(nil), // Null after argument to get first page.
}
for {
err := client.Query(context.Background(), &repoQuery, variables)
if err != nil {
return repos, err
}
repos = append(repos, extractRepoDataFromEdges(repoQuery.Organization.Repositories.Edges, topic)...)
if !repoQuery.Organization.Repositories.PageInfo.HasNextPage {
break
} else {
variables["cursor"] = githubv4.NewString(repoQuery.Organization.Repositories.PageInfo.EndCursor)
}
// Sleep for at least a second. https://docs.github.com/en/rest/guides/best-practices-for-integrators
time.Sleep(2 * time.Second)
}
return repos, nil
}
func getReposFromUser(client Client, user string, topic string) ([]Repo, error) {
var repoQuery struct {
User struct {
Repositories struct {
PageInfo struct {
HasNextPage githubv4.Boolean
EndCursor githubv4.String
}
Edges []RepositoryEdge
} `graphql:"repositories(first: 100, after: $cursor, orderBy: {field: CREATED_AT, direction: DESC}, isArchived: false)"`
} `graphql:"user(login: $user)"`
}
var repos []Repo
variables := map[string]interface{}{
"user": githubv4.String(user),
"cursor": (*githubv4.String)(nil), // Null after argument to get first page.
}
for {
err := client.Query(context.Background(), &repoQuery, variables)
if err != nil {
return repos, err
}
repos = append(repos, extractRepoDataFromEdges(repoQuery.User.Repositories.Edges, topic)...)
if !repoQuery.User.Repositories.PageInfo.HasNextPage {
break
} else {
variables["cursor"] = githubv4.NewString(repoQuery.User.Repositories.PageInfo.EndCursor)
}
// Sleep for at least a second. https://docs.github.com/en/rest/guides/best-practices-for-integrators
time.Sleep(2 * time.Second)
}
return repos, nil
}
func extractRepoDataFromEdges(edges []RepositoryEdge, topic string) []Repo {
var repos []Repo
for _, edge := range edges {
repo := edge.Node
var r Repo
r.NameWithOwner = repo.NameWithOwner
if topic != "" {
if containsTopic(repo.RepositoryTopics.Nodes, topic) {
repos = append(repos, r)
}
} else {
repos = append(repos, r)
}
}
return repos
}
func containsTopic(nodes []RepositoryTopicNode, topic string) bool {
for _, node := range nodes {
if node.Topic.Name == topic {
return true
}
}
return false
}