forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries_org.go
85 lines (73 loc) · 2.17 KB
/
queries_org.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
package api
import (
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/shurcooL/githubv4"
)
// OrganizationProjects fetches all open projects for an organization.
func OrganizationProjects(client *Client, repo ghrepo.Interface) ([]RepoProject, error) {
type responseData struct {
Organization struct {
Projects struct {
Nodes []RepoProject
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"projects(states: [OPEN], first: 100, orderBy: {field: NAME, direction: ASC}, after: $endCursor)"`
} `graphql:"organization(login: $owner)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(repo.RepoOwner()),
"endCursor": (*githubv4.String)(nil),
}
var projects []RepoProject
for {
var query responseData
err := client.Query(repo.RepoHost(), "OrganizationProjectList", &query, variables)
if err != nil {
return nil, err
}
projects = append(projects, query.Organization.Projects.Nodes...)
if !query.Organization.Projects.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(query.Organization.Projects.PageInfo.EndCursor)
}
return projects, nil
}
type OrgTeam struct {
ID string
Slug string
}
// OrganizationTeams fetches all the teams in an organization
func OrganizationTeams(client *Client, repo ghrepo.Interface) ([]OrgTeam, error) {
type responseData struct {
Organization struct {
Teams struct {
Nodes []OrgTeam
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"teams(first: 100, orderBy: {field: NAME, direction: ASC}, after: $endCursor)"`
} `graphql:"organization(login: $owner)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(repo.RepoOwner()),
"endCursor": (*githubv4.String)(nil),
}
var teams []OrgTeam
for {
var query responseData
err := client.Query(repo.RepoHost(), "OrganizationTeamList", &query, variables)
if err != nil {
return nil, err
}
teams = append(teams, query.Organization.Teams.Nodes...)
if !query.Organization.Teams.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(query.Organization.Teams.PageInfo.EndCursor)
}
return teams, nil
}