Skip to content

Commit

Permalink
Code Review
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaiBojin committed Jul 21, 2020
1 parent a6d4204 commit 399fd6a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ Each version of the client is tagged and the version is updated accordingly.

To see the list of past versions, run `git tag`.

To release a new version, first ensure that `[ClientVersion](./mongodbatlas/version.go)' is updated
(i.e., before running `git push origin vx.y.z`, verify that `ClientVersion=x.y.z` should match the tag being pushed to GitHub)
To release a new version, first ensure that `[Version](./mongodbatlas/mongodbatlas.go)' is updated
(i.e., before running `git push origin vx.y.z`, verify that `Version=x.y.z` should match the tag being pushed to GitHub)

## Roadmap

Expand Down
6 changes: 3 additions & 3 deletions githooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# A simple pre-push hook which ensures that the tag being pushed to GitHub
# matches the version defined in the VERSION_FILE.
#
declare -r VERSION_FILE="mongodbatlas/version.go"
declare -r VERSION_FILE="mongodbatlas/mongodbatlas.go"

# Determine if a tag is being pushed
tag=
Expand All @@ -22,12 +22,12 @@ done
if [[ -n "$tag" ]]; then
echo ""
echo "Releasing '$tag'..."
if ! grep -q "ClientVersion = \"$tag\"" "$VERSION_FILE"; then
if ! grep -q "Version = \"$tag\"" "$VERSION_FILE"; then
echo ""
echo "ERROR: Mismatch between the release version ('$tag') and '$VERSION_FILE'"
echo "$VERSION_FILE:"
echo "---------"
grep "ClientVersion = " "$VERSION_FILE"
grep "Version = " "$VERSION_FILE"
echo "---------"
echo
exit 1
Expand Down
12 changes: 10 additions & 2 deletions mongodbatlas/mongodbatlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"net/url"
"reflect"
"runtime"
"strconv"
"strings"

Expand All @@ -22,6 +23,13 @@ const (
defaultBaseURL = "https://cloud.mongodb.com/api/atlas/v1.0/"
jsonMediaType = "application/json"
gzipMediaType = "application/gzip"
libraryName = "go-mongodbatlas"
// Version the version of the current API client
Version = "0.4.0" // Should be set to the next version planned to be released
)

var (
userAgent = fmt.Sprintf("%s/%s (%s;%s)", libraryName, Version, runtime.GOOS, runtime.GOARCH)
)

type Doer interface {
Expand Down Expand Up @@ -184,7 +192,7 @@ func NewClient(httpClient *http.Client) *Client {

baseURL, _ := url.Parse(defaultBaseURL)

c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: DefaultUserAgent()}
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent}

c.APIKeys = &APIKeysServiceOp{Client: c}
c.CloudProviderSnapshots = &CloudProviderSnapshotsServiceOp{Client: c}
Expand Down Expand Up @@ -262,7 +270,7 @@ func SetBaseURL(bu string) ClientOpt {
// SetUserAgent is a client option for setting the user agent.
func SetUserAgent(ua string) ClientOpt {
return func(c *Client) error {
c.UserAgent = fmt.Sprintf("%s %s", ua, DefaultUserAgent())
c.UserAgent = fmt.Sprintf("%s %s", ua, userAgent)
return nil
}
}
Expand Down
28 changes: 14 additions & 14 deletions mongodbatlas/mongodbatlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func testClientDefaultBaseURL(t *testing.T, c *Client) {
}

func testClientDefaultUserAgent(t *testing.T, c *Client) {
if c.UserAgent != DefaultUserAgent() {
t.Errorf("NewClient UserAgent = %v, expected %v", c.UserAgent, DefaultUserAgent())
if c.UserAgent != userAgent {
t.Errorf("NewClient UserAgent = %v, expected %v", c.UserAgent, userAgent)
}
}

Expand Down Expand Up @@ -164,9 +164,9 @@ func TestNewRequest_withUserData(t *testing.T) {
}

// test default user-agent is attached to the request
userAgent := req.Header.Get("User-Agent")
if c.UserAgent != userAgent {
t.Errorf("NewRequest() User-Agent = %v, expected %v", userAgent, c.UserAgent)
uA := req.Header.Get("User-Agent")
if c.UserAgent != uA {
t.Errorf("NewRequest() User-Agent = %v, expected %v", uA, c.UserAgent)
}
}

Expand All @@ -177,7 +177,7 @@ func TestNewRequest_badURL(t *testing.T) {
}

func TestNewRequest_withCustomUserAgent(t *testing.T) {
ua := fmt.Sprintf("testing/%s", ClientVersion)
ua := fmt.Sprintf("testing/%s", Version)
c, err := New(nil, SetUserAgent(ua))

if err != nil {
Expand All @@ -186,7 +186,7 @@ func TestNewRequest_withCustomUserAgent(t *testing.T) {

req, _ := c.NewRequest(ctx, http.MethodGet, "/foo", nil)

expected := fmt.Sprintf("%s %s", ua, DefaultUserAgent())
expected := fmt.Sprintf("%s %s", ua, userAgent)
if got := req.Header.Get("User-Agent"); got != expected {
t.Errorf("New() UserAgent = %s; expected %s", got, expected)
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestNewGZipRequest_emptyBody(t *testing.T) {
}

func TestNewGZipRequest_withCustomUserAgent(t *testing.T) {
ua := fmt.Sprintf("testing/%s", ClientVersion)
ua := fmt.Sprintf("testing/%s", Version)
c, err := New(nil, SetUserAgent(ua))

if err != nil {
Expand All @@ -236,7 +236,7 @@ func TestNewGZipRequest_withCustomUserAgent(t *testing.T) {

req, _ := c.NewGZipRequest(ctx, http.MethodGet, "/foo")

expected := fmt.Sprintf("%s %s", ua, DefaultUserAgent())
expected := fmt.Sprintf("%s %s", ua, userAgent)
if got := req.Header.Get("User-Agent"); got != expected {
t.Errorf("New() UserAgent = %s; expected %s", got, expected)
}
Expand Down Expand Up @@ -268,9 +268,9 @@ func TestNewGZipRequest(t *testing.T) {
}

// test default user-agent is attached to the request
userAgent := req.Header.Get("User-Agent")
if c.UserAgent != userAgent {
t.Errorf("NewGZipRequest() User-Agent = %v, expected %v", userAgent, c.UserAgent)
uA := req.Header.Get("User-Agent")
if c.UserAgent != uA {
t.Errorf("NewGZipRequest() User-Agent = %v, expected %v", uA, c.UserAgent)
}
}

Expand Down Expand Up @@ -450,14 +450,14 @@ func TestDo_completion_callback(t *testing.T) {
}

func TestCustomUserAgent(t *testing.T) {
ua := fmt.Sprintf("testing/%s", ClientVersion)
ua := fmt.Sprintf("testing/%s", Version)
c, err := New(nil, SetUserAgent(ua))

if err != nil {
t.Fatalf("New() unexpected error: %v", err)
}

expected := fmt.Sprintf("%s %s", ua, DefaultUserAgent())
expected := fmt.Sprintf("%s %s", ua, userAgent)
if got := c.UserAgent; got != expected {
t.Errorf("New() UserAgent = %s; expected %s", got, expected)
}
Expand Down
15 changes: 0 additions & 15 deletions mongodbatlas/version.go

This file was deleted.

0 comments on commit 399fd6a

Please sign in to comment.