-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries_org.go
111 lines (95 loc) · 2.91 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
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
package api
import (
"fmt"
"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
}
// OrganizationTeam fetch the team in an organization with the given slug
func OrganizationTeam(client *Client, hostname string, org string, teamSlug string) (*OrgTeam, error) {
type responseData struct {
Organization struct {
Team OrgTeam `graphql:"team(slug: $teamSlug)"`
} `graphql:"organization(login: $owner)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(org),
"teamSlug": githubv4.String(teamSlug),
}
var query responseData
err := client.Query(hostname, "OrganizationTeam", &query, variables)
if err != nil {
return nil, err
}
if query.Organization.Team.ID == "" {
return nil, fmt.Errorf("could not resolve to a Team with the slug of '%s'", teamSlug)
}
return &query.Organization.Team, nil
}
// 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
}