Skip to content

Commit

Permalink
Merge pull request #37 from homebysix/1.6.0
Browse files Browse the repository at this point in the history
1.6.0
  • Loading branch information
homebysix committed Dec 26, 2019
2 parents 636cf68 + f5f7062 commit 537c26a
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
rev: v2.4.0
hooks:
- id: check-ast
- id: check-byte-order-marker
Expand All @@ -16,7 +16,7 @@ repos:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- repo: https://github.com/python/black
rev: 19.3b0
rev: 19.10b0
hooks:
- id: black
language_version: python3.7
8 changes: 8 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
files: '\.recipe$'
types: [text]

- id: check-git-config-email
name: Check Git Config Email
description: This hook checks to ensure the Git config email matches one of the specified domains.
entry: check-git-config-email
language: python
pass_filenames: false
always_run: true

- id: check-jamf-extension-attributes
name: Check Jamf EAs
description: This hook checks Jamf extension attributes for common issues.
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For any hook in this repo you wish to use, add the following to your pre-commit

```yaml
- repo: https://github.com/homebysix/pre-commit-macadmin
rev: v1.5.2
rev: v1.6.0
hooks:
- id: check-plists
# - id: ...
Expand All @@ -25,6 +25,11 @@ After adding a hook to your pre-commit config, it's not a bad idea to run `pre-c

### General

- __check-git-config-email__

This hook checks to ensure the Git config email matches one of the specified domains:
`args: ['--domains', 'pretendco.com', 'contoso.com', '--']`

- __check-plists__

This hook checks XML property list (plist) files for basic syntax errors.
Expand Down Expand Up @@ -114,7 +119,7 @@ When combining arguments that take lists (for example: `--required-keys`, `--cat

```yaml
- repo: https://github.com/homebysix/pre-commit-macadmin
rev: v1.5.2
rev: v1.6.0
hooks:
- id: check-munki-pkgsinfo
args: ['--catalogs', 'testing', 'stable', '--']
Expand All @@ -124,7 +129,7 @@ But if you also use the `--categories` argument, you would move the trailing `--

```yaml
- repo: https://github.com/homebysix/pre-commit-macadmin
rev: v1.5.2
rev: v1.6.0
hooks:
- id: check-munki-pkgsinfo
args: ['--catalogs', 'testing', 'stable', '--categories', 'Design', 'Engineering', 'Web Browsers', '--']
Expand All @@ -136,7 +141,7 @@ If it looks better to your eye, feel free to use a multi-line list for long argu

```yaml
- repo: https://github.com/homebysix/pre-commit-macadmin
rev: v1.5.2
rev: v1.6.0
hooks:
- id: check-munki-pkgsinfo
args: [
Expand Down
9 changes: 8 additions & 1 deletion pre_commit_hooks/check_autopkg_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from distutils.version import LooseVersion
from xml.parsers.expat import ExpatError

from pre_commit_hooks.util import validate_pkginfo_key_types, validate_required_keys
from pre_commit_hooks.util import (
validate_pkginfo_key_types,
validate_required_keys,
validate_restart_action_key,
)


def build_argument_parser():
Expand Down Expand Up @@ -384,10 +388,13 @@ def main(argv=None):
)
retval = 1

# If the Input key contains a pkginfo dict, make a best effort to validate its contents.
input_key = recipe.get("Input", recipe.get("input", recipe.get("INPUT")))
if input_key and "pkginfo" in input_key:
if not validate_pkginfo_key_types(input_key["pkginfo"], filename):
retval = 1
if not validate_restart_action_key(input_key["pkginfo"], filename):
retval = 1

# TODO: Additional pkginfo checks here.

Expand Down
41 changes: 41 additions & 0 deletions pre_commit_hooks/check_git_config_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""This hook checks to ensure the Git config email matches one of the specified domains."""

import argparse
import subprocess


def build_argument_parser():
"""Build and return the argument parser."""

parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"--domains",
nargs="+",
help="One or more domains that the Git config email address must match.",
)
return parser


def main(argv=None):
"""Main process."""

# Parse command line arguments.
argparser = build_argument_parser()
args = argparser.parse_args(argv)

retval = 0
if args.domains:
user_email = subprocess.check_output(["git", "config", "--get", "user.email"])
if not any((user_email.endswith(x) for x in args.domains)):
print("Git config email is from an unexpected domain.")
retval = 1

return retval


if __name__ == "__main__":
exit(main())
10 changes: 9 additions & 1 deletion pre_commit_hooks/check_munki_pkgsinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import plistlib
from xml.parsers.expat import ExpatError

from pre_commit_hooks.util import validate_pkginfo_key_types, validate_required_keys
from pre_commit_hooks.util import (
validate_pkginfo_key_types,
validate_required_keys,
validate_restart_action_key,
)


def build_argument_parser():
Expand Down Expand Up @@ -60,6 +64,10 @@ def main(argv=None):
if not validate_pkginfo_key_types(pkginfo, filename):
retval = 1

# Validate RestartAction key.
if not validate_restart_action_key(pkginfo, filename):
retval = 1

# Check for rogue categories.
if args.categories and pkginfo.get("category") not in args.categories:
print(
Expand Down
21 changes: 21 additions & 0 deletions pre_commit_hooks/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ def validate_required_keys(plist, filename, required_keys):
return passed


def validate_restart_action_key(pkginfo, filename):
"""Verifies that required_keys are present in dictionary plist."""
passed = True
allowed_values = (
"RequireShutdown",
"RequireRestart",
"RecommendRestart",
"RequireLogout",
"None",
)
if "RestartAction" in pkginfo:
if pkginfo["RestartAction"] not in allowed_values:
print(
"{}: RestartAction key set to unexpected value: {}".format(
filename, pkginfo["RestartAction"]
)
)
passed = False
return passed


def validate_pkginfo_key_types(pkginfo, filename):
"""Validation of pkginfo key types.
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
name="pre-commit-macadmin",
description="Pre-commit hooks for Mac admins, client engineers, and IT consultants.",
url="https://github.com/homebysix/pre-commit-macadmin",
version="1.5.2",
version="1.6.0",
author="Elliot Jordan",
author_email="elliot@elliotjordan.com",
packages=["pre_commit_hooks"],
Expand All @@ -16,6 +16,7 @@
"console_scripts": [
"check-autopkg-recipe-list = pre_commit_hooks.check_autopkg_recipe_list:main",
"check-autopkg-recipes = pre_commit_hooks.check_autopkg_recipes:main",
"check-git-config-email = pre_commit_hooks.check_git_config_email:main",
"check-jamf-extension-attributes = pre_commit_hooks.check_jamf_extension_attributes:main",
"check-jamf-scripts = pre_commit_hooks.check_jamf_scripts:main",
"check-jamf-profiles = pre_commit_hooks.check_jamf_profiles:main",
Expand Down

0 comments on commit 537c26a

Please sign in to comment.