From e708608b9777c05831ba020542230e739e03079a Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Tue, 20 Jul 2021 16:32:05 -0400 Subject: [PATCH 1/3] Add Client method --- github/github.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/github/github.go b/github/github.go index ae0ee08c838..6381974e42f 100644 --- a/github/github.go +++ b/github/github.go @@ -188,6 +188,13 @@ type service struct { client *Client } +// Client returns the http.Client used by this GitHub client. +func (c *Client) Client() *http.Client { + c.clientMu.Lock() + defer c.clientMu.Unlock() + return c.client +} + // ListOptions specifies the optional parameters to various List methods that // support offset pagination. type ListOptions struct { From 9a0e1df83319b4fd06c31bb2b6fcd3d628895b43 Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Tue, 20 Jul 2021 16:39:52 -0400 Subject: [PATCH 2/3] Add test for coverage --- github/github_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/github/github_test.go b/github/github_test.go index 1eb0dba7337..f453c06bf01 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -232,6 +232,14 @@ func TestNewClient(t *testing.T) { } } +func TestClient(t *testing.T) { + c := NewClient(nil) + c2 := c.Client() + if c.client != c2 { + t.Error("Client returned different http.Client, but should be the same.") + } +} + func TestNewEnterpriseClient(t *testing.T) { baseURL := "https://custom-url/api/v3/" uploadURL := "https://custom-upload-url/api/uploads/" From 6867ee340521a06a8caa519d8907543fb579f0d4 Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Wed, 21 Jul 2021 13:47:02 -0400 Subject: [PATCH 3/3] Address review feedback to eliminate race condition --- github/github.go | 3 ++- github/github_test.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/github/github.go b/github/github.go index 6381974e42f..2f7653654ff 100644 --- a/github/github.go +++ b/github/github.go @@ -192,7 +192,8 @@ type service struct { func (c *Client) Client() *http.Client { c.clientMu.Lock() defer c.clientMu.Unlock() - return c.client + clientCopy := *c.client + return &clientCopy } // ListOptions specifies the optional parameters to various List methods that diff --git a/github/github_test.go b/github/github_test.go index f453c06bf01..d2240558da5 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -235,8 +235,8 @@ func TestNewClient(t *testing.T) { func TestClient(t *testing.T) { c := NewClient(nil) c2 := c.Client() - if c.client != c2 { - t.Error("Client returned different http.Client, but should be the same.") + if c.client == c2 { + t.Error("Client returned same http.Client, but should be different") } }