-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
125 lines (97 loc) · 3.21 KB
/
github.go
File metadata and controls
125 lines (97 loc) · 3.21 KB
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
// Copyright (c) 2023, the WebKit for Windows project authors. Please see the
// AUTHORS file for details. All rights reserved. Use of this source code is
// governed by a BSD-style license that can be found in the LICENSE file.
package reqcheck
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/google/go-github/v75/github"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)
type githubClient struct {
client *github.Client
}
func NewGitHub(uri, token string) (Client, error) {
return NewGitHubClient(uri, token, http.DefaultClient)
}
func NewGitHubClient(uri, token string, cl *http.Client) (Client, error) {
// Parse the url
githubURL, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("could not parse github link: %w", err)
}
// Create the client
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(
context.WithValue(context.Background(), oauth2.HTTPClient, cl),
ts,
)
client := github.NewClient(tc)
if githubURL.Hostname() != "github.com" {
relBaseURL, _ := url.Parse("./api/v3/")
relUploadURL, _ := url.Parse("./api/uploads/")
client.BaseURL = githubURL.ResolveReference(relBaseURL)
client.UploadURL = githubURL.ResolveReference(relUploadURL)
}
logrus.WithFields(logrus.Fields{
"github-url": githubURL.String(),
"base-url": client.BaseURL.String(),
"upload-url": client.BaseURL.String(),
}).Debug("connecting to github instance")
return &githubClient{client: client}, nil
}
func (c *githubClient) ListReleases(ctx context.Context, owner, name string, opt ListOptions) ([]Release, error) {
ghOpts := &github.ListOptions{
Page: opt.Page,
PerPage: opt.PerPage,
}
logrus.WithFields(logrus.Fields{
"owner": owner,
"name": name,
"page": ghOpts.Page,
"per-page": ghOpts.PerPage,
}).Debug("listing github releases")
releases, _, err := c.client.Repositories.ListReleases(ctx, owner, name, ghOpts)
if err != nil {
return nil, fmt.Errorf("error getting releases from repository %s/%s: %w", owner, name, err)
}
r := make([]Release, 0, ghOpts.PerPage)
for _, release := range releases {
tagName := release.GetTagName()
logrus.WithFields(logrus.Fields{
"tag": tagName,
"commit": release.GetTargetCommitish(),
}).Debug("found release")
r = append(r, Release{Tag: tagName, SemVer: generateVersion(tagName, versionMatcher)})
}
return r, nil
}
func (c *githubClient) ListTags(ctx context.Context, owner, name string, opt ListOptions) ([]Release, error) {
ghOpts := &github.ListOptions{
Page: opt.Page,
PerPage: opt.PerPage,
}
logrus.WithFields(logrus.Fields{
"owner": owner,
"name": name,
"page": ghOpts.Page,
"per-page": ghOpts.PerPage,
}).Debug("listing github tags")
tags, _, err := c.client.Repositories.ListTags(ctx, owner, name, ghOpts)
if err != nil {
return nil, fmt.Errorf("error getting tags from repository %s/%s: %w", owner, name, err)
}
r := make([]Release, 0, ghOpts.PerPage)
for _, tag := range tags {
tagName := tag.GetName()
logrus.WithFields(logrus.Fields{
"tag": tagName,
"commit": tag.GetCommit().GetSHA(),
}).Debug("found tag")
r = append(r, Release{Tag: tagName, SemVer: generateVersion(tagName, versionMatcher)})
}
return r, nil
}