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 all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/release-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
push:
tags:
- v0.*.*
- v1.*.*

jobs:
tests:
name: Release sanity tests
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2

- name: Checking code health
run: make tools lint test

- name: Checking the version
run: make check-version
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This workflow will ensure that we get notified when a tag was pushed and the version was not updated.

12 changes: 11 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,36 @@ link-git-hooks:
find .git/hooks -type l -exec rm {} \;
find githooks -type f -exec ln -sf ../../{} .git/hooks/ \;

.PHONY: build
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated for consistency

build:
go install ./$(PKG_NAME)

.PHONY: test
test:
go test $(SOURCE_FILES) -coverprofile $(COVERAGE) -timeout=30s -parallel=4 -cover -race

.PHONY: fmt
fmt:
@echo "==> Fixing source code with gofmt..."
gofmt -s -w ./$(PKG_NAME)
goimports -w ./$(PKG_NAME)

.PHONY: lint-fix
lint-fix:
golangci-lint run --fix

.PHONY: lint
lint:
golangci-lint run $(SOURCE_FILES)

.PHONY: check
check: test lint-fix

.PHONY: tools
tools:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s $(GOLANGCI_VERSION)

.PHONY: build test fmt lint lint-fix check tools
TAG=$(patsubst v%,%,$(shell git describe --tags --dirty --always))
.PHONY: check-version
check-version:
scripts/check-version.sh "$(TAG)"
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 [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

This library is being initially developed for [mongocli](https://github.com/mongodb/mongocli),
Expand Down
22 changes: 22 additions & 0 deletions githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
#
# A simple pre-push hook which ensures that the tag being pushed to GitHub
# matches the version defined in code.
#
# 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
make check-version TAG="$tag"
fi
11 changes: 9 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 @@ -20,9 +21,15 @@ import (

const (
defaultBaseURL = "https://cloud.mongodb.com/api/atlas/v1.0/"
userAgent = "go-mongodbatlas"
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 @@ -263,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, c.UserAgent)
c.UserAgent = fmt.Sprintf("%s %s", ua, userAgent)
return nil
}
}
Expand Down
18 changes: 9 additions & 9 deletions mongodbatlas/mongodbatlas_test.go
Original file line number Diff line number Diff line change
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 := "testing/0.0.1"
ua := fmt.Sprintf("testing/%s", Version)
c, err := New(nil, SetUserAgent(ua))

if err != nil {
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", Version)
c, err := New(nil, SetUserAgent(ua))

if err != nil {
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,7 +450,7 @@ func TestDo_completion_callback(t *testing.T) {
}

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

if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions scripts/check-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash
#
# A simple script which ensures that the specified tag
# matches the version defined in the VERSION_FILE.
#
set -ueo pipefail
declare -r VERSION_FILE="mongodbatlas/mongodbatlas.go"

if [[ $# -lt 1 ]]; then
echo
echo "Error: TAG not specified"
echo "Usage: check-version.sh TAG"
echo
exit 1
fi
tag="$1"

echo
echo "Checking '$tag'..."
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 "Version = " "$VERSION_FILE"
echo "---------"
echo
exit 1
fi
echo "OK."
echo