@@ -9,6 +9,7 @@ package issues
9
9
import (
10
10
"context"
11
11
"fmt"
12
+ "net/url"
12
13
"time"
13
14
14
15
"github.com/google/go-github/v41/github"
@@ -37,56 +38,60 @@ type GetIssuesOptions struct {
37
38
}
38
39
39
40
// Client is a client that can create and retrieve issues.
40
- type Client interface {
41
- // Destination describes where issues will be created.
42
- Destination () string
43
-
44
- // Reference returns a string that refers to the issue with number.
45
- Reference (number int ) string
46
-
47
- // IssueExists reports whether an issue with the given ID exists.
48
- IssueExists (ctx context.Context , number int ) (bool , error )
49
-
50
- // CreateIssue creates a new issue.
51
- CreateIssue (ctx context.Context , iss * Issue ) (number int , err error )
52
-
53
- // GetIssue returns an issue with the given issue number.
54
- GetIssue (ctx context.Context , number int ) (iss * Issue , err error )
55
-
56
- GetIssues (ctx context.Context , opts GetIssuesOptions ) (issues []* Issue , err error )
57
- }
58
-
59
- type githubClient struct {
41
+ type Client struct {
60
42
client * github.Client
61
43
owner string
62
44
repo string
63
45
}
64
46
65
- // NewGitHubClient creates a Client that will create issues in
47
+ // Config is used to initialize a new Client.
48
+ type Config struct {
49
+ // Owner is the owner of a GitHub repo. For example, "golang" is the owner
50
+ // for github.com/golang/vulndb.
51
+ Owner string
52
+
53
+ // Repo is the name of a GitHub repo. For example, "vulndb" is the repo
54
+ // name for github.com/golang/vulndb.
55
+ Repo string
56
+
57
+ // Token is access token that authorizes and authenticates
58
+ // requests to the GitHub API.
59
+ Token string
60
+ }
61
+
62
+ // NewClient creates a Client that will create issues in
66
63
// the a GitHub repo.
67
- // A GitHub access token is required to create issues.
68
- func NewGitHubClient (owner , repo , accessToken string ) * githubClient {
69
- ts := oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : accessToken })
64
+ func NewClient (cfg * Config ) * Client {
65
+ ts := oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : cfg .Token })
70
66
tc := oauth2 .NewClient (context .Background (), ts )
71
- return & githubClient {
72
- client : github .NewClient (tc ),
73
- owner : owner ,
74
- repo : repo ,
67
+ c := github .NewClient (tc )
68
+ return & Client {
69
+ client : c ,
70
+ owner : cfg .Owner ,
71
+ repo : cfg .Repo ,
75
72
}
76
73
}
77
74
75
+ // NewTestClient creates a Client for use in tests.
76
+ func NewTestClient (cfg * Config , baseURL * url.URL ) * Client {
77
+ c := NewClient (cfg )
78
+ c .client .BaseURL = baseURL
79
+ c .client .UploadURL = baseURL
80
+ return c
81
+ }
82
+
78
83
// Destination implements Client.Destination.
79
- func (c * githubClient ) Destination () string {
84
+ func (c * Client ) Destination () string {
80
85
return fmt .Sprintf ("https://github.com/%s/%s" , c .owner , c .repo )
81
86
}
82
87
83
88
// Reference implements Client.Reference.
84
- func (c * githubClient ) Reference (num int ) string {
89
+ func (c * Client ) Reference (num int ) string {
85
90
return fmt .Sprintf ("%s/issues/%d" , c .Destination (), num )
86
91
}
87
92
88
93
// IssueExists implements Client.IssueExists.
89
- func (c * githubClient ) IssueExists (ctx context.Context , number int ) (_ bool , err error ) {
94
+ func (c * Client ) IssueExists (ctx context.Context , number int ) (_ bool , err error ) {
90
95
defer derrors .Wrap (& err , "IssueExists(%d)" , number )
91
96
92
97
iss , _ , err := c .client .Issues .Get (ctx , c .owner , c .repo , number )
@@ -131,7 +136,7 @@ func convertGithubIssueToIssue(ghIss *github.Issue) *Issue {
131
136
}
132
137
133
138
// GetIssue implements Client.GetIssue.
134
- func (c * githubClient ) GetIssue (ctx context.Context , number int ) (_ * Issue , err error ) {
139
+ func (c * Client ) GetIssue (ctx context.Context , number int ) (_ * Issue , err error ) {
135
140
defer derrors .Wrap (& err , "GetIssue(%d)" , number )
136
141
ghIss , _ , err := c .client .Issues .Get (ctx , c .owner , c .repo , number )
137
142
if err != nil {
@@ -143,7 +148,7 @@ func (c *githubClient) GetIssue(ctx context.Context, number int) (_ *Issue, err
143
148
}
144
149
145
150
// GetIssues implements Client.GetIssues
146
- func (c * githubClient ) GetIssues (ctx context.Context , opts GetIssuesOptions ) (_ []* Issue , err error ) {
151
+ func (c * Client ) GetIssues (ctx context.Context , opts GetIssuesOptions ) (_ []* Issue , err error ) {
147
152
defer derrors .Wrap (& err , "GetIssues()" )
148
153
clientOpts := & github.IssueListByRepoOptions {
149
154
State : opts .State ,
@@ -175,7 +180,7 @@ func (c *githubClient) GetIssues(ctx context.Context, opts GetIssuesOptions) (_
175
180
}
176
181
177
182
// CreateIssue implements Client.CreateIssue.
178
- func (c * githubClient ) CreateIssue (ctx context.Context , iss * Issue ) (number int , err error ) {
183
+ func (c * Client ) CreateIssue (ctx context.Context , iss * Issue ) (number int , err error ) {
179
184
defer derrors .Wrap (& err , "CreateIssue(%s)" , iss .Title )
180
185
181
186
req := & github.IssueRequest {
0 commit comments