/
service.go
187 lines (151 loc) · 4.89 KB
/
service.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
package github
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"path"
"strings"
"github.com/dpb587/gget/pkg/gitutil"
"github.com/dpb587/gget/pkg/service"
"github.com/google/go-github/v29/github"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Service struct {
log *logrus.Logger
clientFactory *ClientFactory
}
func NewService(log *logrus.Logger, clientFactory *ClientFactory) *Service {
return &Service{
log: log,
clientFactory: clientFactory,
}
}
var _ service.RefResolver = &Service{}
var _ service.ConditionalRefResolver = &Service{}
func (s Service) ServiceName() string {
return "github"
}
func (s Service) IsKnownServer(_ context.Context, lookupRef service.LookupRef) bool {
return lookupRef.Ref.Server == "github.com"
}
func (s Service) IsDetectedServer(_ context.Context, lookupRef service.LookupRef) bool {
res, err := s.clientFactory.httpClientFactory().Get(fmt.Sprintf("https://%s/api/v3/", lookupRef.Ref.Server))
if err != nil {
s.log.Debugf("github detection attempt error: %s", errors.Wrap(err, "requesting GET /api/v3/"))
return false
} else if res.StatusCode != http.StatusOK {
return false
}
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
s.log.Debugf("github detection attempt error: %s", errors.Wrap(err, "reading body"))
return false
}
return strings.Contains(string(buf), `"organization_repositories_url"`)
}
func (s Service) ResolveRef(ctx context.Context, lookupRef service.LookupRef) (service.ResolvedRef, error) {
client, err := s.clientFactory.Get(ctx, lookupRef)
if err != nil {
return nil, errors.Wrap(err, "building client")
}
ref := lookupRef.Ref
ref.Service = s.ServiceName()
rr := &refResolver{
client: client,
lookupRef: lookupRef,
canonicalRef: ref,
}
if ref.Ref == "" {
release, err := s.resolveLatest(ctx, client, lookupRef)
if err != nil {
return nil, errors.Wrap(err, "resolving latest")
}
rr.canonicalRef.Ref = release.GetTagName()
return &ReleaseRef{
refResolver: rr,
release: release,
}, nil
}
{ // tag
gitref, resp, err := client.Git.GetRefs(ctx, rr.canonicalRef.Owner, rr.canonicalRef.Repository, path.Join("tags", rr.canonicalRef.Ref))
if resp.StatusCode == http.StatusNotFound {
// oh well
} else if err != nil {
return nil, errors.Wrap(err, "attempting tag resolution")
} else if len(gitref) == 1 {
return rr.resolveTag(ctx, gitref[0], true)
}
}
{ // head
gitref, resp, err := client.Git.GetRefs(ctx, rr.canonicalRef.Owner, rr.canonicalRef.Repository, path.Join("heads", rr.canonicalRef.Ref))
if resp.StatusCode == http.StatusNotFound {
// oh well
} else if err != nil {
return nil, errors.Wrap(err, "attempting branch resolution")
} else if len(gitref) == 1 {
return rr.resolveHead(ctx, gitref[0])
}
}
if gitutil.PotentialCommitRE.MatchString(rr.canonicalRef.Ref) { // commit
// client.Git.GetCommit does not resolve partial commits
commitref, resp, err := client.Repositories.GetCommit(ctx, rr.canonicalRef.Owner, rr.canonicalRef.Repository, rr.canonicalRef.Ref)
if resp.StatusCode == http.StatusNotFound {
// oh well
} else if err != nil {
return nil, errors.Wrap(err, "attempting commit resolution")
} else {
rr.canonicalRef.Ref = commitref.GetSHA()
return rr.resolveCommit(ctx, commitref.GetSHA())
}
}
return nil, fmt.Errorf("unable to resolve as tag, branch, nor commit: %s", rr.canonicalRef.Ref)
}
func (s Service) resolveLatest(ctx context.Context, client *github.Client, lookupRef service.LookupRef) (*github.RepositoryRelease, error) {
if lookupRef.IsComplexRef() {
opts := github.ListOptions{
PerPage: 25,
}
for {
releases, resp, err := client.Repositories.ListReleases(ctx, lookupRef.Ref.Owner, lookupRef.Ref.Repository, &opts)
if err != nil {
return nil, errors.Wrap(err, "iterating releases")
} else if resp.StatusCode == http.StatusNotFound {
return nil, errors.New("repository not found")
}
for _, release := range releases {
{
var stability = "stable"
if release.GetPrerelease() {
stability = "pre-release"
}
if !lookupRef.SatisfiesStability(stability) {
continue
}
}
tagName := release.GetTagName()
match, err := lookupRef.SatisfiesVersion(tagName)
if err != nil {
s.log.Debugf("skipping invalid semver tag: %s", tagName)
continue
} else if !match {
continue
}
return release, nil
}
opts.Page = resp.NextPage
if opts.Page == 0 {
break
}
}
return nil, fmt.Errorf("failed to find release matching constraints: %s", strings.Join(lookupRef.ComplexRefModes(), ", "))
}
release, resp, err := client.Repositories.GetLatestRelease(ctx, lookupRef.Ref.Owner, lookupRef.Ref.Repository)
if err != nil {
return nil, errors.Wrap(err, "getting latest release")
} else if resp.StatusCode == http.StatusNotFound {
return nil, errors.New("repository not found")
}
return release, nil
}