-
Notifications
You must be signed in to change notification settings - Fork 2
/
github.go
213 lines (176 loc) · 5.32 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package updater
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/shurcooL/githubv4"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"golang.org/x/oauth2"
)
const instrumentationName = "github.com/hellofresh/updater-go"
type (
// GithubLocator struct encapsulates information about github repo
GithubLocator struct {
client *githubv4.Client
tracer trace.Tracer
defaultTimeout time.Duration
owner string
repo string
acceptRelease ReleaseFilter
acceptAsset AssetFilter
}
ghReleaseAsset struct {
Name githubv4.String
URL githubv4.String
}
ghRelease struct {
Name githubv4.String
IsDraft githubv4.Boolean
IsPrerelease githubv4.Boolean
ReleaseAssets struct {
Nodes []ghReleaseAsset
} `graphql:"releaseAssets(first: 20)"`
}
queryRepoReleases struct {
Repository *struct {
Releases struct {
PageInfo struct {
EndCursor githubv4.String
HasNextPage githubv4.Boolean
}
Nodes []ghRelease
} `graphql:"releases(first: 10, after: $releaseCursor, orderBy: {field:CREATED_AT, direction: DESC})"`
} `graphql:"repository(owner: $repositoryOwner, name: $repositoryName)"`
}
)
var (
// ErrNoRepository error is returned if the repository is not found or user token has no access to it
ErrNoRepository = errors.New("no repository")
// ErrUnauthorized error is returned if user token does not have access to the repository or the token is invalid
ErrUnauthorized = errors.New("no access to the repository, probably bad credentials")
)
// NewGithubClient creates new github locator instance
func NewGithubClient(
ctx context.Context,
owner string,
repository string,
token string,
releaseFilter ReleaseFilter,
assetFilter AssetFilter,
connectionTimeout time.Duration,
opts ...GitHubLocatorOption,
) *GithubLocator {
l := &GithubLocator{
owner: owner,
repo: repository,
acceptRelease: releaseFilter,
acceptAsset: assetFilter,
defaultTimeout: connectionTimeout,
tracer: trace.NewNoopTracerProvider().Tracer(instrumentationName),
}
for _, opt := range opts {
opt.applyGitHubLocatorOption(l)
}
var httpClient *http.Client
if token != "" {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
httpClient = oauth2.NewClient(ctx, src)
}
httpClient.Transport = otelhttp.NewTransport(httpClient.Transport, formatHTTPSpanName())
l.client = githubv4.NewClient(httpClient)
return l
}
// ListReleases returns available GH releases list.
func (g *GithubLocator) ListReleases(ctx context.Context, amount int) (_ []Release, err error) {
ctx, span := g.tracer.Start(ctx, "github:list-releases", trace.WithAttributes(
attribute.String("github.owner", g.owner),
attribute.String("github.repository", g.repo),
attribute.Int("github.limit", amount),
))
defer func() {
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
span.End()
}()
ctx, cancel := ensureTimeout(ctx, g.defaultTimeout)
defer cancel()
variables := map[string]interface{}{
"repositoryOwner": githubv4.String(g.owner),
"repositoryName": githubv4.String(g.repo),
"releaseCursor": (*githubv4.String)(nil), // Null after argument to get first page.
}
var query queryRepoReleases
var releases []Release
for {
if err := g.client.Query(ctx, &query, variables); err != nil {
if strings.Contains(err.Error(), "non-200 OK status code: 401 Unauthorized") {
return nil, ErrUnauthorized
}
return nil, err
}
if query.Repository == nil {
return nil, ErrNoRepository
}
for _, release := range query.Repository.Releases.Nodes {
if !g.acceptRelease(string(release.Name), bool(release.IsDraft), bool(release.IsPrerelease)) {
continue
}
for _, asset := range release.ReleaseAssets.Nodes {
if !g.acceptAsset(string(asset.Name)) {
continue
}
releases = append(
releases,
Release{
Name: string(release.Name),
Asset: string(asset.Name),
URL: string(asset.URL),
},
)
break
}
}
// If we do not have enough releases, we will continue to fetch the next page
if len(releases) < amount && query.Repository.Releases.PageInfo.HasNextPage {
variables["releaseCursor"] = githubv4.NewString(query.Repository.Releases.PageInfo.EndCursor)
continue
}
break
}
return releases, nil
}
// GitHubLocatorOption is an option to configure GithubLocator.
type GitHubLocatorOption interface {
applyGitHubLocatorOption(l *GithubLocator)
}
type gitHubLocatorOptionFunc func(l *GithubLocator)
func (f gitHubLocatorOptionFunc) applyGitHubLocatorOption(l *GithubLocator) {
f(l)
}
// WithTracerProvider sets the TracerProvider.
func WithTracerProvider(tp trace.TracerProvider) GitHubLocatorOption {
return gitHubLocatorOptionFunc(func(o *GithubLocator) {
o.tracer = tp.Tracer(instrumentationName)
})
}
func ensureTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
if _, ok := ctx.Deadline(); !ok {
return context.WithTimeout(ctx, timeout)
}
return ctx, func() {}
}
func formatHTTPSpanName() otelhttp.Option {
return otelhttp.WithSpanNameFormatter(func(_ string, r *http.Request) string {
return fmt.Sprintf("http:%s:%s", r.Method, r.URL.Path)
})
}