forked from motiv-labs/janus
/
client.go
94 lines (70 loc) · 2.31 KB
/
client.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
package github
import (
"context"
"net/http"
"github.com/google/go-github/github"
)
// Client contains the methods that abstract an API
type Client interface {
CurrentUser(*http.Client) (*github.User, error)
Organizations(*http.Client) ([]string, error)
Teams(*http.Client) (OrganizationTeams, error)
}
type client struct {
baseURL string
}
// NewClient creates a new instance of client
func NewClient() Client {
return &client{}
}
// OrganizationTeams is a map of organization names and teams
type OrganizationTeams map[string][]string
// CurrentUser retrieves the current authenticated user for an http client
func (c *client) CurrentUser(httpClient *http.Client) (*github.User, error) {
client := github.NewClient(httpClient)
currentUser, _, err := client.Users.Get(context.TODO(), "")
if err != nil {
return nil, err
}
return currentUser, nil
}
// Teams retrieves the teams that the authenticated user belongs
func (c *client) Teams(httpClient *http.Client) (OrganizationTeams, error) {
client := github.NewClient(httpClient)
nextPage := 1
organizationTeams := OrganizationTeams{}
for nextPage != 0 {
teams, resp, err := client.Organizations.ListUserTeams(context.TODO(), &github.ListOptions{Page: nextPage})
if err != nil {
return nil, err
}
for _, team := range teams {
organizationName := *team.Organization.Login
if _, found := organizationTeams[organizationName]; !found {
organizationTeams[organizationName] = []string{}
}
// We add both forms (slug and name) of team
organizationTeams[organizationName] = append(organizationTeams[organizationName], *team.Name)
organizationTeams[organizationName] = append(organizationTeams[organizationName], *team.Slug)
}
nextPage = resp.NextPage
}
return organizationTeams, nil
}
// Organizations retrieves the organizations that the authenticated user belongs
func (c *client) Organizations(httpClient *http.Client) ([]string, error) {
client := github.NewClient(httpClient)
nextPage := 1
organizations := []string{}
for nextPage != 0 {
orgs, resp, err := client.Organizations.List(context.TODO(), "", &github.ListOptions{Page: nextPage})
if err != nil {
return nil, err
}
for _, org := range orgs {
organizations = append(organizations, *org.Login)
}
nextPage = resp.NextPage
}
return organizations, nil
}