-
Notifications
You must be signed in to change notification settings - Fork 0
/
genuine.go
172 lines (154 loc) · 4.09 KB
/
genuine.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package github
import (
"context"
"net/http"
"net/url"
github "github.com/google/go-github/v35/github"
"github.com/kyoh86/gogh/v2/internal/githubv4"
"golang.org/x/oauth2"
)
type genuineAdaptor struct {
host string
restClient *github.Client
gqlClient *githubv4.Client
}
func (a *genuineAdaptor) GetHost() string {
return a.host
}
const (
DefaultHost = "github.com"
DefaultAPIHost = "api.github.com"
)
type Option func(baseRESTURL *url.URL, uploadRESTURL *url.URL, baseGQLURL *url.URL)
func WithScheme(scheme string) Option {
return func(baseRESTURL *url.URL, uploadRESTURL *url.URL, baseGQLURL *url.URL) {
baseRESTURL.Scheme = scheme
uploadRESTURL.Scheme = scheme
baseGQLURL.Scheme = scheme
}
}
func NewAdaptor(ctx context.Context, host, token string, options ...Option) (Adaptor, error) {
var client *http.Client
if token != "" {
client = NewAuthClient(ctx, token)
}
if host == DefaultHost || host == DefaultAPIHost {
return newGenuineAdaptor(DefaultHost, client), nil
}
baseRESTURL := &url.URL{
Scheme: "https://",
Host: host,
Path: "/api/v3",
}
uploadRESTURL := &url.URL{
Scheme: "https://",
Host: host,
Path: "/api/uploads",
}
baseGQLURL := &url.URL{
Scheme: "https://",
Host: host,
Path: "/api/graphql",
}
for _, option := range options {
option(baseRESTURL, uploadRESTURL, baseGQLURL)
}
return newGenuineEnterpriseAdaptor(
host,
baseRESTURL.String(),
uploadRESTURL.String(),
baseGQLURL.String(),
client,
)
}
func NewAuthClient(ctx context.Context, accessToken string) *http.Client {
return oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}))
}
func newGenuineAdaptor(host string, httpClient *http.Client) Adaptor {
return &genuineAdaptor{
host: host,
restClient: github.NewClient(httpClient),
gqlClient: githubv4.NewClient(httpClient, "https://"+DefaultAPIHost+"/graphql"),
}
}
func newGenuineEnterpriseAdaptor(
host string,
baseRESTURL, uploadRESTURL, baseGQLURL string,
httpClient *http.Client,
) (Adaptor, error) {
restClient, err := github.NewEnterpriseClient(baseRESTURL, uploadRESTURL, httpClient)
if err != nil {
return nil, err
}
return &genuineAdaptor{
host: host,
restClient: restClient,
gqlClient: githubv4.NewClient(httpClient, baseGQLURL),
}, nil
}
func (a *genuineAdaptor) UserGet(ctx context.Context, user string) (*User, *Response, error) {
return a.restClient.Users.Get(ctx, user)
}
func (a *genuineAdaptor) RepositoryList(
ctx context.Context,
opts *RepositoryListOptions,
) ([]*RepositoryFragment, PageInfoFragment, error) {
repos, err := a.gqlClient.ListRepos(
ctx,
opts.Limit,
opts.After,
opts.IsFork,
opts.Privacy,
opts.OwnerAffiliations,
opts.OrderBy,
)
if err != nil {
return nil, PageInfoFragment{}, err
}
ingrepos := make([]*RepositoryFragment, 0, len(repos.Viewer.Repositories.Edges))
for _, edge := range repos.Viewer.Repositories.Edges {
ingrepos = append(ingrepos, edge.Node)
}
return ingrepos, repos.Viewer.Repositories.PageInfo, nil
}
func (a *genuineAdaptor) RepositoryCreate(
ctx context.Context,
org string,
repo *Repository,
) (*Repository, *Response, error) {
return a.restClient.Repositories.Create(ctx, org, repo)
}
func (a *genuineAdaptor) RepositoryCreateFork(
ctx context.Context,
owner string,
repo string,
opts *RepositoryCreateForkOptions,
) (*Repository, *Response, error) {
return a.restClient.Repositories.CreateFork(ctx, owner, repo, opts)
}
func (a *genuineAdaptor) RepositoryGet(
ctx context.Context,
owner string,
repo string,
) (*Repository, *Response, error) {
return a.restClient.Repositories.Get(ctx, owner, repo)
}
func (a *genuineAdaptor) RepositoryDelete(
ctx context.Context,
owner string,
repo string,
) (*Response, error) {
return a.restClient.Repositories.Delete(ctx, owner, repo)
}
func (a *genuineAdaptor) RepositoryCreateFromTemplate(
ctx context.Context,
templateOwner, templateRepo string,
templateRepoReq *TemplateRepoRequest,
) (*Repository, *Response, error) {
return a.restClient.Repositories.CreateFromTemplate(
ctx,
templateOwner,
templateRepo,
templateRepoReq,
)
}