Giter Site home page Giter Site logo

chrisongthb / yet-another-puppet-git-hook Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 45 KB

Provide linting and syntax checks for working with puppet code

License: GNU General Public License v3.0

Shell 95.06% Ruby 4.94%
git pre-commit pre-commit-hook git-pre-commit puppet lint linting yamllint puppet-lint epp

yet-another-puppet-git-hook's Introduction

yet-another-puppet-git-hook

1. Overview

This repo provides a pre-commit hook, which checks the linting and syntax of .pp, .epp, .yaml files and of the Puppetfile.

2. Installation

2.1. Dependencies

The hook requires the following software:

Please check, that your PATH is valid (all commands are executable), e.g. add things like export PATH="${PATH}:$(find ~/.gem/ruby/ -maxdepth 2 -type d -name bin)" to your .bashrc

2.2. Manually configure pre-commit hook

  • clone this repo
  • navigate to your puppet repo
  • cd .git/hooks/
  • ln -s <path-to-this-repo>/pre-commit
  • chmod +x pre-commit

2.3. Automatically configure pre-commit hook

Either include this profile (tested on Ubuntu 18.04) in your role or safe the included /usr/share/git-core/templates/hooks/post-checkout directly. This script will install the pre-commit hook, when a repo is cloned. Afterwards, it will do a git pull on the yet-another-puppet-git-hook repo (pre-commit hook) by every branch checkout.

# installs and configures https://github.com/chrisongthb/yet-another-puppet-git-hook

class profile::git_pre_commit_hook {

  $gem_proxy = lookup('profile::git_pre_commit_hook::gem_install_proxy', Optional[Stdlib::HTTPUrl], first, Undef)

  # provide required packages
  ensure_packages( 'yamllint', {'ensure' => 'present'})
  if $gem_proxy {
    ensure_packages(
      [
        'r10k',
        'puppet-lint',
        'puppet-lint-legacy_facts-check',
        'puppet-lint-strict_indent-check',
      ], {
        ensure   => 'installed',
        provider => 'gem',
        install_options => { '--http-proxy' => $gem_proxy }
      }
    )
  }
  else {
    ensure_packages(
      [
        'r10k',
        'puppet-lint',
        'puppet-lint-legacy_facts-check',
        'puppet-lint-strict_indent-check',
      ], {
        ensure   => 'installed',
        provider => 'gem'
      }
    )
  }

  # Rollout post-checkout in git-init(1) template,
  # which manages pre-commit hook in local git repos
  # lint:ignore:single_quote_string_with_variables
  file { '/usr/share/git-core/templates/hooks/post-checkout':
    ensure  => 'file',
    owner   => 'root',
    group   => 'root',
    mode    => '0755',
    content => '#!/bin/bash
# This file is managed by Puppet

# Do nothing, if we do not checkout a branch:
#   $3 is a flag indicating whether the checkout was a branch checkout
#   the post-checkout hook is also run after git clone and the flag is always "1"
#   see docs for more information
#   https://git-scm.com/docs/githooks#_post_checkout
#
# These are all parameters given to post-checkout:
# 0000000000000000000000000000000000000000 702529a575af026b10a82c5a855df7c455056d09 1
# We could also use the null-ref as test, but we want the hooks to be up to date.
# Hence, we do an update on every branch switch.
#
if [ "$3" == "1" ]; then
  # just to be sure we are in repo root
  cd $(git rev-parse --show-toplevel)
  repo_uri=$(git config --get remote.origin.url)

  echo "Configuring pre-commit hook..."
  if [ -d ~/.yet-another-puppet-git-hook/.git/ ]; then
    cd ~/.yet-another-puppet-git-hook
    GIT_DIR=~/.yet-another-puppet-git-hook/.git/ git fetch --all --quiet
    GIT_DIR=~/.yet-another-puppet-git-hook/.git/ git reset --hard --quiet origin/main --
    cd $OLDPWD
  else
    rm -rf ~/.yet-another-puppet-git-hook/
    git clone https://github.com/chrisongthb/yet-another-puppet-git-hook.git ~/.yet-another-puppet-git-hook/
  fi
  ln -s --force ~/.yet-another-puppet-git-hook/pre-commit .git/hooks/
  chmod +x .git/hooks/pre-commit
fi
',
  }
  # lint:endignore
}

3. Usage

Just follow your current workflow. The pre-commit hook will only check those files, that are staged for this commit (not the whole repo). I recommend using dot files in the puppet repo root (e.g. .puppet-lint.rc, .yamllint), instead of changing the check invocation.

4. Extending pre-commit

Try using the function _puppet_git_hooks_check in pre-commit. In case the check invocation is complex, just define your own function in a new file beneath modules/ and call the function in pre-commit. You may want to use the _puppet_git_hooks_say method for colored output, see also modules/libs.sh. All files matching *.sh in modules/ gonna be sourced.

5. References

I used ideas from https://github.com/gini/puppet-git-hooks and https://github.com/drwahl/puppet-git-hooks

6. Limitations

  • Does not check erb templates
  • Does not support whitespaces in filenames
  • This is tested on Ubuntu 16.04 and 18.04

7. Support

Please create bug reports and feature requests in GitHub issues.

yet-another-puppet-git-hook's People

Contributors

chrisongthb avatar

Watchers

 avatar

Forkers

flhe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.