diff --git a/README.md b/README.md index 1ad4f50..15279fe 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Current supported pre-receive (server side) checks * Ruby syntax * Puppet-lint * Yaml (hiera data) syntax +* Check tag against metadata.json Installing dependencies ======================= diff --git a/commit_hooks/config.cfg b/commit_hooks/config.cfg index d073242..50776cc 100644 --- a/commit_hooks/config.cfg +++ b/commit_hooks/config.cfg @@ -2,6 +2,7 @@ CHECK_PUPPET_LINT="enabled" # enabled, permissive or disabled (permissive runs b USE_PUPPET_FUTURE_PARSER="enabled" # enabled or disabled CHECK_INITIAL_COMMIT="disabled" # enabled or disabled CHECK_RSPEC="enabled" # enabled or disabled +CHECK_TAG_METADATA="enabled" # enabled or disabled export PUPPET_LINT_OPTIONS="" # puppet-lint options to use if no rc file is present. Defaults to "--no-140chars-check" export PUPPET_LINT_FAIL_ON_WARNINGS="true" # Set the puppet-lint option --fail-on-warnings UNSET_RUBY_ENV="enabled" # enabled or disabled. Required for Gitlab. diff --git a/commit_hooks/metadata_version_matches_tag.sh b/commit_hooks/metadata_version_matches_tag.sh new file mode 100755 index 0000000..78917c6 --- /dev/null +++ b/commit_hooks/metadata_version_matches_tag.sh @@ -0,0 +1,60 @@ +#!/bin/bash -u + +# This script expects $1 to be passed and for $1 to be the filesystem location +# +# This script expects $2 to be passed and for it to be the tag name + +errors=0 + +module_path=$1 +tag=$2 + +error_msg=$(mktemp /tmp/error_msg_tag-version.XXXXXX) +metadata=$(mktemp /tmp/metadata_version.XXXXXX) + +echo -e "$(tput setaf 6)Checking metadata matches tag for $tag $(tput sgr0)" + +ruby -e "require 'json'; metadata=JSON.parse(File.read('$module_path/metadata.json')); puts metadata['version']" 2> $error_msg > $metadata +if [ $? -ne 0 ]; then + cat $error_msg | sed -e "s/^/$(tput setaf 1)/" -e "s/$/$(tput sgr0)/" + errors=`expr $errors + 1` + echo -e "$(tput setaf 1)Error: json syntax error in $module_path (see above)$(tput sgr0)" + exit 1 +fi +rm -f $error_msg + +meta_tag=$(cat $metadata; rm -f $metadata) +case $meta_tag in + $tag) + true + ;; + [vV]$tag) + true + ;; + "v $tag") + true + ;; + "V $tag") + true + ;; + [vV].$tag) + true + ;; + "v. $tag") + true + ;; + "V. $tag") + true + ;; + *) + echo -e "$(tput setaf 1)Error: metadata.json contains $meta_tag but tag is $tag $(tput sgr0)" + errors=`expr $errors + 1` + ;; +esac + +if [[ $errors -ne 0 ]]; then + echo -e "$(tput setaf 1)Error: error(s) found in metadata.json file. Commit will be aborted.$(tput sgr0)" + exit 1 +fi + +exit 0 diff --git a/pre-receive b/pre-receive index 20ccc23..421a388 100755 --- a/pre-receive +++ b/pre-receive @@ -31,6 +31,7 @@ export TERM # Decide if we want puppet-lint # Decide if we want the puppet future parser (already on puppet 4?) CHECK_PUPPET_LINT="enabled" +CHECK_TAG_METADATA="enabled" USE_PUPPET_FUTURE_PARSER="enabled" CHECK_INITIAL_COMMIT="disabled" if [[ -e ${subhook_root}/config.cfg ]] ; then @@ -67,6 +68,20 @@ while read -r oldrev newrev refname; do files_list=$(git diff --name-only "$oldrev" "$newrev" --diff-filter=ACM) fi + if [[ $CHECK_TAG_METADATA != 'disabled' ]]; then + # check that the version in the tag is the version in metadata.json + case "$refname" in + refs/tags/*) + tagname=$(basename $refname) + ${subhook_root}/metadata_version_matches_tag.sh "$tmptree" "$tagname" + RC=$? + if [[ $RC -ne 0 ]]; then + failures=$((failures + 1)) + fi + ;; + esac + fi + for changedfile in $files_list; do tmpmodule="$tmptree/$changedfile" [[ -f "$tmpmodule" ]] || continue