Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hook to ensure the client's version is updated before pushing a tag/release #115

Merged
merged 6 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ 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)

## Roadmap

This library is being initially developed for [mongocli](https://github.com/mongodb/mongocli),
Expand Down
35 changes: 35 additions & 0 deletions githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
#
# 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"

# Determine if a tag is being pushed
tag=
input=$(</dev/stdin)
for arg in $input; do
if [[ "$arg" =~ ^refs\/tags ]]; then
tag="${arg/refs\/tags\//}"
if [[ "$tag" =~ ^v ]]; then
# Remove the 'v' prefix
tag="${tag:1}"
fi
fi
done

# Releasing a tag
if [[ -n "$tag" ]]; then
echo ""
echo "Releasing '$tag'..."
if ! grep -q "ClientVersion = \"$tag\"" "$VERSION_FILE"; then
echo ""
echo "ERROR: Mismatch between the release version ('$tag') and '$VERSION_FILE'"
echo "$VERSION_FILE:"
echo "---------"
grep "ClientVersion = " "$VERSION_FILE"
echo "---------"
echo
exit 1
fi
fi
5 changes: 2 additions & 3 deletions mongodbatlas/mongodbatlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

const (
defaultBaseURL = "https://cloud.mongodb.com/api/atlas/v1.0/"
userAgent = "go-mongodbatlas"
jsonMediaType = "application/json"
gzipMediaType = "application/gzip"
)
Expand Down Expand Up @@ -185,7 +184,7 @@ func NewClient(httpClient *http.Client) *Client {

baseURL, _ := url.Parse(defaultBaseURL)

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

c.APIKeys = &APIKeysServiceOp{Client: c}
c.CloudProviderSnapshots = &CloudProviderSnapshotsServiceOp{Client: c}
Expand Down Expand Up @@ -263,7 +262,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, c.UserAgent)
c.UserAgent = fmt.Sprintf("%s %s", ua, DefaultUserAgent())
return nil
}
}
Expand Down
16 changes: 8 additions & 8 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 != userAgent {
t.Errorf("NewClient UserAgent = %v, expected %v", c.UserAgent, userAgent)
if c.UserAgent != DefaultUserAgent() {
MihaiBojin marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("NewClient UserAgent = %v, expected %v", c.UserAgent, DefaultUserAgent())
}
}

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

func TestNewRequest_withCustomUserAgent(t *testing.T) {
ua := "testing/0.0.1"
ua := fmt.Sprintf("testing/%s", ClientVersion)
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, userAgent)
expected := fmt.Sprintf("%s %s", ua, DefaultUserAgent())
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 := "testing/0.0.1"
ua := fmt.Sprintf("testing/%s", ClientVersion)
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, userAgent)
expected := fmt.Sprintf("%s %s", ua, DefaultUserAgent())
if got := req.Header.Get("User-Agent"); got != expected {
t.Errorf("New() UserAgent = %s; expected %s", got, expected)
}
Expand Down Expand Up @@ -450,14 +450,14 @@ func TestDo_completion_callback(t *testing.T) {
}

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

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

expected := fmt.Sprintf("%s %s", ua, userAgent)
expected := fmt.Sprintf("%s %s", ua, DefaultUserAgent())
if got := c.UserAgent; got != expected {
t.Errorf("New() UserAgent = %s; expected %s", got, expected)
}
Expand Down
15 changes: 15 additions & 0 deletions mongodbatlas/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mongodbatlas
MihaiBojin marked this conversation as resolved.
Show resolved Hide resolved

import "fmt"

const (
// UserAgent the default user agent string reported by this client
UserAgent = "go-mongodbatlas"
MihaiBojin marked this conversation as resolved.
Show resolved Hide resolved
// ClientVersion the version of the current API client
ClientVersion = "0.4.0" // Should be set to the next version planned to be released
MihaiBojin marked this conversation as resolved.
Show resolved Hide resolved
)

// DefaultUserAgent constructs the default user agent
func DefaultUserAgent() string {
MihaiBojin marked this conversation as resolved.
Show resolved Hide resolved
MihaiBojin marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf("%s/%s", UserAgent, ClientVersion)
}