Skip to content

Commit

Permalink
Pre commit hook (#184)
Browse files Browse the repository at this point in the history
* Refactor tests into their own directory

* Fix merge conflicts

* Clean up and fix import

* Add vert instructions for generating version

* Add pre-commit hook

* Update chart testing section slightly to be more aligned

* Update pre-commit hook to automatically bump versions

* Update developer docs and appVersion

* Only run appVersion update when chart version changes

* When running locally, point to upstream instead of origin
  • Loading branch information
etsauer authored Sep 2, 2020
1 parent 4a2e678 commit 60931fd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
36 changes: 36 additions & 0 deletions _test/bump_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/python3
import getopt
import semver
import sys
import yaml


def bump_patch(chart_path):
chart_dot_yaml = "%s/Chart.yaml" % chart_path
chart_file = open(chart_dot_yaml)

chart = yaml.load(chart_file, Loader=yaml.FullLoader)

version = semver.VersionInfo.parse(chart['version'])

print(str(version.bump_patch()))


def main(argv):
try:
opts, args = getopt.getopt(argv, "i:", ["increment="])
except getopt.GetoptError:
print("bad options")
sys.exit(2)
for opt, arg in opts:
if opt in ("-i", "-increment"):
bump_patch(arg)
sys.exit()


if __name__ == "__main__":
main(sys.argv[1:])
pelorus_chart = "charts/pelorus"
operator_chart = "charts/operators"

bump_patch(pelorus_chart)
33 changes: 32 additions & 1 deletion _test/pre-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
#!/bin/bash

ct lint
bump_version() {
chart=${1}
version=$(python _test/bump_version.py -i ${chart})
sed -i "s/version:\ .*$/version:\ ${version}/" ${chart}/Chart.yaml
echo "Updated chart ${chart} version to ${version}."
}

set_app_version() {
version=$(vert -g ^1 $(git describe))
sed -i "s/appVersion:\ .*$/appVersion:\ ${version}/" charts/pelorus/Chart.yaml
}

chart_lint=$(ct lint --remote upstream 2>&1)
failed=$?
failed_charts=$(echo "${chart_lint}" | sed -n 's/.*\(path:\ \"\)\(.*\)\".*Chart\ version.*$/\2/p')

if [ ${failed} -ne 0 ] && [ "${failed_charts}" != "" ]; then
for chart in ${failed_charts}; do
bump_version ${chart}
done
# Recursively call script to ensure tests pass now
./$0

# Ensure app version is correct
set_app_version
if [ "$(git diff charts/pelorus/Chart.yaml)" != "" ]; then
echo "Updated charts/pelorus appVersion to $(git describe)."
fi

echo "Please re-commit."
exit 1
fi
4 changes: 2 additions & 2 deletions charts/pelorus/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 1.2.2
version: 1.2.3

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: 1.2.1+20.gb9cac53
appVersion: 1.2.1+19.gd313016
16 changes: 8 additions & 8 deletions docs/Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ We use Helm's [chart-testing](https://github.com/helm/chart-testing) tool to ens

### Updating the chart versions

When any of our Helm charts are updated, we need to bump the version number. This allows for a seemless upgrade experience. In order to maintain versioning with our repository, our version numbers are derived from our tagged releases. Git provides a command which will give us that version, via `git describe`. However, the [git generated version number is not fully semver compatible](https://github.com/semver/semver/issues/200). To bridge that gap we use a tool called [Version Tester (vert)](https://github.com/Masterminds/vert). Here are the steps to generate the version:
When any of our Helm charts are updated, we need to bump the version number. This allows for a seemless upgrade experience. In order to maintain versioning with our repository, our version numbers are derived from our tagged releases. Git provides a command which will give us that version, via `git describe`. However, the [git generated version number is not fully semver compatible](https://github.com/semver/semver/issues/200). To bridge that gap we use a tool called [Version Tester (vert)](https://github.com/Masterminds/vert). We have also written a pre-commit hook script that will automatically detect when a version bump is needed and will make the required change. Here are the steps to get the hook set up.

1. Install Helm's [chart-testing](https://github.com/helm/chart-testing) tool.
1. Install the latest release of [vert](https://github.com/Masterminds/vert/releases/)
2. Run `vert -g ^1 $(git describe)`.
3. Insert the version into the `version` field of your `Chart.yaml` file.

Before committing, you can run `ct lint` to confirm the version bump worked. Or, you can automate this process by adding our pre-commit script to your local git hooks.
1. Run `vert -g ^1 $(git describe)` to test that its working.
1. Copy the pre-commit hook into your git hooks directory.
```
cp _test/pre-commit .git/hooks/pre-commit
```
```
cp _test/pre-commit .git/hooks/pre-commit
```
This script will use Helm's built-in linter to check whether a version bump is necessary, and if it is, it will take the current `version` from Chart.yaml and increment it one patch version. It will also keep `appVersion` fo the Pelorus chart up to date with the repo version using `git describe`.
## Dashboard Development
Expand Down
3 changes: 2 additions & 1 deletion exporters/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pytest
coverage
coverage
semver

0 comments on commit 60931fd

Please sign in to comment.