/
github.go
157 lines (132 loc) · 3.46 KB
/
github.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package github
import (
"fmt"
"io/ioutil"
"strings"
"golang.org/x/oauth2"
)
import githubapi "github.com/google/go-github/github"
func newClient(accessToken string) *githubapi.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
return githubapi.NewClient(tc)
}
func GetPrimaryEmail(accessToken string) string {
client := newClient(accessToken)
emails, _, err := client.Users.ListEmails(nil)
var email string
if err == nil && len(emails) > 0 {
for _, e := range emails {
if *e.Primary && *e.Verified {
email = *e.Email
}
}
}
return email
}
func GetCurrentUser(accessToken string) (*githubapi.User, error) {
client := newClient(accessToken)
user, _, err := client.Users.Get("")
return user, err
}
func ListOrganizations(accessToken string) ([]*githubapi.Organization, error) {
client := newClient(accessToken)
orgs, _, err := client.Organizations.List("", nil)
if err != nil {
return nil, err
}
var fullOrgs []*githubapi.Organization
for _, org := range orgs {
o, _, err := client.Organizations.Get(*org.Login)
if err != nil {
return nil, err
}
fullOrgs = append(fullOrgs, o)
}
return fullOrgs, nil
}
func ListUserRepositories(accessToken string, opts *githubapi.RepositoryListOptions) (*Repositories, error) {
client := newClient(accessToken)
repos, resp, err := client.Repositories.List("", opts)
if err != nil {
return nil, err
}
return &Repositories{
List: repos,
NextPage: resp.NextPage,
PrevPage: resp.PrevPage,
}, nil
}
func ListOrgRepositories(accessToken, owner string, opts *githubapi.RepositoryListByOrgOptions) (*Repositories, error) {
client := newClient(accessToken)
var orgOwner *githubapi.Organization
if err := checkMembership(client, owner); err != nil {
return nil, err
}
orgOwner, _, err := client.Organizations.Get(owner)
if err != nil {
return nil, err
}
repos, resp, err := client.Repositories.ListByOrg(owner, opts)
if err != nil {
return nil, err
}
return &Repositories{
Owner: orgOwner,
List: repos,
NextPage: resp.NextPage,
PrevPage: resp.PrevPage,
}, nil
}
func GetRepository(accessToken, login, nwo string) (*Repository, error) {
parts := strings.SplitN(nwo, "/", 2)
if len(parts) == 1 {
return nil, fmt.Errorf("unable to recognize repository %s", nwo)
}
client := newClient(accessToken)
if login != parts[0] {
if err := checkMembership(client, parts[0]); err != nil {
return nil, err
}
}
repo, _, err := client.Repositories.Get(parts[0], parts[1])
if err != nil {
return nil, err
}
repository := &Repository{
Repository: repo,
}
content, err := client.Repositories.DownloadContents(parts[0], parts[1], "README.md", nil)
if err != nil {
// ignore the content
return repository, nil
}
defer content.Close()
b, err := ioutil.ReadAll(content)
if err != nil {
// ignore the content
return repository, nil
}
repository.Readme = string(b)
return repository, nil
}
func checkMembership(client *githubapi.Client, owner string) error {
// Ensure the user belongs to the org sent, or if it's even an org.
_, _, err := client.Organizations.GetOrgMembership("", owner)
if err != nil {
return fmt.Errorf("the current user is not a member of the org %s: %v", owner, err)
}
return nil
}
type Repositories struct {
Owner *githubapi.Organization
List []githubapi.Repository
NextPage int
PrevPage int
}
type Repository struct {
*githubapi.Repository
Readme string
}