Skip to content

Added check for metadata.json version against a git tag #69

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======================
Expand Down
1 change: 1 addition & 0 deletions commit_hooks/config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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.
60 changes: 60 additions & 0 deletions commit_hooks/metadata_version_matches_tag.sh
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions pre-receive
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down