Skip to content

Commit 7c32a36

Browse files
tatianabTatiana Bradley
authored andcommitted
internal/issues: add ctx to NewClient and expose struct fields
Change-Id: Ic4b4f9bff9fba1f914d99f61360b33d14a34d899 Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/459640 Reviewed-by: Julie Qiu <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Tatiana Bradley <[email protected]> Reviewed-by: Tatiana Bradley <[email protected]>
1 parent 2be64cd commit 7c32a36

File tree

9 files changed

+31
-31
lines changed

9 files changed

+31
-31
lines changed

cmd/issue/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func main() {
5050
if err != nil {
5151
log.Fatal(err)
5252
}
53-
c := issues.NewClient(&issues.Config{Owner: owner, Repo: repoName, Token: *githubToken})
53+
c := issues.NewClient(ctx, &issues.Config{Owner: owner, Repo: repoName, Token: *githubToken})
5454
ghsaClient := ghsa.NewClient(ctx, *githubToken)
5555
switch cmd {
5656
case "triage":

cmd/vulnreport/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func setupCreate(ctx context.Context, args []string) ([]int, *createCfg, error)
280280
}
281281
return githubIDs, &createCfg{
282282
repo: repo,
283-
issuesClient: issues.NewClient(&issues.Config{Owner: owner, Repo: repoName, Token: *githubToken}),
283+
issuesClient: issues.NewClient(ctx, &issues.Config{Owner: owner, Repo: repoName, Token: *githubToken}),
284284
ghsaClient: ghsa.NewClient(ctx, *githubToken),
285285
existingByFile: existingByFile,
286286
existingByIssue: existingByIssue,

cmd/worker/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func createIssuesCommand(ctx context.Context) error {
256256
if err != nil {
257257
return err
258258
}
259-
client := issues.NewClient(&issues.Config{Owner: owner, Repo: repoName, Token: cfg.GitHubAccessToken})
259+
client := issues.NewClient(ctx, &issues.Config{Owner: owner, Repo: repoName, Token: cfg.GitHubAccessToken})
260260
repo, err := gitrepo.Clone(ctx, "https://github.com/golang/vulndb")
261261
if err != nil {
262262
return err

internal/issues/githubtest/setup.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package githubtest
88

99
import (
10+
"context"
1011
"net/http"
1112
"net/http/httptest"
1213
"net/url"
@@ -25,15 +26,15 @@ const (
2526

2627
// Setup sets up a test HTTP server along with a issues.Client that is
2728
// configured to talk to that test server.
28-
func Setup(t *testing.T, cfg *issues.Config) (*issues.Client, *http.ServeMux) {
29+
func Setup(ctx context.Context, t *testing.T, cfg *issues.Config) (*issues.Client, *http.ServeMux) {
2930
mux := http.NewServeMux()
3031
apiHandler := http.NewServeMux()
3132

3233
apiHandler.Handle(testBaseURLPath+"/", http.StripPrefix(testBaseURLPath, mux))
3334
server := httptest.NewServer(apiHandler)
3435

3536
url, _ := url.Parse(server.URL + testBaseURLPath + "/")
36-
client := issues.NewTestClient(cfg, url)
37+
client := issues.NewTestClient(ctx, cfg, url)
3738
t.Cleanup(server.Close)
3839
return client, mux
3940
}

internal/issues/issues.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ type GetIssuesOptions struct {
3737
Labels []string
3838
}
3939

40-
// Client is a client that can create and retrieve issues.
40+
// Client is a shallow client for a github.Client.
4141
type Client struct {
42-
client *github.Client
43-
owner string
44-
repo string
42+
GitHub *github.Client
43+
Owner string
44+
Repo string
4545
}
4646

4747
// Config is used to initialize a new Client.
@@ -61,28 +61,28 @@ type Config struct {
6161

6262
// NewClient creates a Client that will create issues in
6363
// the a GitHub repo.
64-
func NewClient(cfg *Config) *Client {
64+
func NewClient(ctx context.Context, cfg *Config) *Client {
6565
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: cfg.Token})
66-
tc := oauth2.NewClient(context.Background(), ts)
66+
tc := oauth2.NewClient(ctx, ts)
6767
c := github.NewClient(tc)
6868
return &Client{
69-
client: c,
70-
owner: cfg.Owner,
71-
repo: cfg.Repo,
69+
GitHub: c,
70+
Owner: cfg.Owner,
71+
Repo: cfg.Repo,
7272
}
7373
}
7474

7575
// 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
76+
func NewTestClient(ctx context.Context, cfg *Config, baseURL *url.URL) *Client {
77+
c := NewClient(ctx, cfg)
78+
c.GitHub.BaseURL = baseURL
79+
c.GitHub.UploadURL = baseURL
8080
return c
8181
}
8282

8383
// Destination returns the URL of the Github repo.
8484
func (c *Client) Destination() string {
85-
return fmt.Sprintf("https://github.com/%s/%s", c.owner, c.repo)
85+
return fmt.Sprintf("https://github.com/%s/%s", c.Owner, c.Repo)
8686
}
8787

8888
// Reference returns the URL of the given issue.
@@ -94,7 +94,7 @@ func (c *Client) Reference(num int) string {
9494
func (c *Client) IssueExists(ctx context.Context, number int) (_ bool, err error) {
9595
defer derrors.Wrap(&err, "IssueExists(%d)", number)
9696

97-
iss, _, err := c.client.Issues.Get(ctx, c.owner, c.repo, number)
97+
iss, _, err := c.GitHub.Issues.Get(ctx, c.Owner, c.Repo, number)
9898
if err != nil {
9999
return false, err
100100
}
@@ -138,7 +138,7 @@ func convertGithubIssueToIssue(ghIss *github.Issue) *Issue {
138138
// GetIssue returns the issue with the given issue number.
139139
func (c *Client) GetIssue(ctx context.Context, number int) (_ *Issue, err error) {
140140
defer derrors.Wrap(&err, "GetIssue(%d)", number)
141-
ghIss, _, err := c.client.Issues.Get(ctx, c.owner, c.repo, number)
141+
ghIss, _, err := c.GitHub.Issues.Get(ctx, c.Owner, c.Repo, number)
142142
if err != nil {
143143
return nil, err
144144
}
@@ -160,10 +160,9 @@ func (c *Client) GetIssues(ctx context.Context, opts GetIssuesOptions) (_ []*Iss
160160

161161
issues := []*Issue{}
162162
page := 1
163-
164163
for {
165164
clientOpts.ListOptions.Page = page
166-
pageIssues, resp, err := c.client.Issues.ListByRepo(ctx, c.owner, c.repo, clientOpts)
165+
pageIssues, resp, err := c.GitHub.Issues.ListByRepo(ctx, c.Owner, c.Repo, clientOpts)
167166
if err != nil {
168167
return nil, err
169168
}
@@ -190,7 +189,7 @@ func (c *Client) CreateIssue(ctx context.Context, iss *Issue) (number int, err e
190189
if len(iss.Labels) > 0 {
191190
req.Labels = &iss.Labels
192191
}
193-
giss, _, err := c.client.Issues.Create(ctx, c.owner, c.repo, req)
192+
giss, _, err := c.GitHub.Issues.Create(ctx, c.Owner, c.Repo, req)
194193
if err != nil {
195194
return 0, err
196195
}

internal/issues/issues_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var testConfig = &issues.Config{
2525
}
2626

2727
func TestClient(t *testing.T) {
28-
client, _ := githubtest.Setup(t, testConfig)
28+
client, _ := githubtest.Setup(context.Background(), t, testConfig)
2929
want := fmt.Sprintf("https://github.com/%s/%s", githubtest.TestOwner, githubtest.TestRepo)
3030
if got := client.Destination(); got != want {
3131
t.Fatalf("client.Destination(): %q; want = %q", got, want)
@@ -37,7 +37,7 @@ func TestClient(t *testing.T) {
3737
}
3838

3939
func TestCreateIssue(t *testing.T) {
40-
c, mux := githubtest.Setup(t, testConfig)
40+
c, mux := githubtest.Setup(context.Background(), t, testConfig)
4141
want := 15
4242
mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/issues", githubtest.TestOwner, githubtest.TestRepo), func(w http.ResponseWriter, r *http.Request) {
4343
testMethod(t, r, "POST")
@@ -55,7 +55,7 @@ func TestCreateIssue(t *testing.T) {
5555
}
5656

5757
func TestGetIssueAndIssueExists(t *testing.T) {
58-
c, mux := githubtest.Setup(t, testConfig)
58+
c, mux := githubtest.Setup(context.Background(), t, testConfig)
5959
want := &issues.Issue{
6060
Number: 7,
6161
Title: "title",
@@ -86,7 +86,7 @@ func TestGetIssueAndIssueExists(t *testing.T) {
8686
}
8787

8888
func TestGetIssues(t *testing.T) {
89-
c, mux := githubtest.Setup(t, testConfig)
89+
c, mux := githubtest.Setup(context.Background(), t, testConfig)
9090
iss := &issues.Issue{
9191
Number: 1,
9292
Title: "vuln worker test",

internal/issues/real_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestRealClient(t *testing.T) {
4949
if *githubToken == "" {
5050
t.Fatal("need -ghtoken")
5151
}
52-
testClient(t, NewClient(&Config{Owner: owner, Repo: repo, Token: *githubToken}))
52+
testClient(t, NewClient(context.Background(), &Config{Owner: owner, Repo: repo, Token: *githubToken}))
5353
})
5454
}
5555

internal/worker/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func NewServer(ctx context.Context, cfg Config) (_ *Server, err error) {
8787
if err != nil {
8888
return nil, err
8989
}
90-
s.issueClient = issues.NewClient(&issues.Config{
90+
s.issueClient = issues.NewClient(ctx, &issues.Config{
9191
Owner: owner,
9292
Repo: repoName,
9393
Token: cfg.GitHubAccessToken,

internal/worker/worker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestCreateIssues(t *testing.T) {
9797
event.NewExporter(log.NewLineHandler(os.Stderr), nil))
9898
mstore := store.NewMemStore()
9999

100-
ic, mux := githubtest.Setup(t, &issues.Config{
100+
ic, mux := githubtest.Setup(ctx, t, &issues.Config{
101101
Owner: githubtest.TestOwner,
102102
Repo: githubtest.TestRepo,
103103
Token: githubtest.TestToken,

0 commit comments

Comments
 (0)