Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Commit

Permalink
Issue-#2 Support approval categories for comment-added hook.
Browse files Browse the repository at this point in the history
By default, the Code-Review and Verified labels are supported.
Custom approval categories are supported as well; these can
be added using gerrit_hooks.hook.add_custom_approval_category().
  • Loading branch information
nils.diefenbach committed Sep 14, 2018
1 parent f3c8713 commit e8e814f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
27 changes: 25 additions & 2 deletions README.md
@@ -1,7 +1,7 @@
# Gerrit Hooks Argument Parsers

This library provides pre-built `argparse.ArgumentParser` instances
for gerrit's hooks (linked in the [Resources](#resources) section below).
This library provides pre-built `argparse.ArgumentParser` instances, which
return `argpase.NameSpace` object for gerrit's hooks (linked in the [Resources](#resources) section below).


It allows developers to get started with gerrit hooks faster, by
Expand Down Expand Up @@ -29,10 +29,33 @@ import gerrit_hooks
options = gerrit_hooks.parse_options()

print("Change ID: {}".format(options.change))

...

```

Custom approval categories are supported as well - these must be added
before calling `gerrit_hooks.parse_options()`:

```python
> gerrit/hooks/comment-added

#!/usr/bin/env/python3
import gerrit_hooks

# Let's assume you have a approval category with label 'level-of-amazingness
# We need to add this to the Hook Flag Definitions class
gerrit_hooks.add_custom_approval_rating('level-of-amazingness')

options = gerrit_hooks.parse_options()

# The approval category can be accessed by the following attributes:
print("Level of Amazingness is: {}".format(options.level_of_amazingness))
print("Level of Amazingness was: {}".format(options.level_of_amazingness_oldValue))

...

```

# Resources

Expand Down
16 changes: 12 additions & 4 deletions gerrit_hooks/containers.py
Expand Up @@ -67,10 +67,10 @@ class HookFlagDefinitions:
"--project <project name> --branch <branch> --topic <topic> " \
"--author <comment author> --author-username <username> " \
"--commit <commit> --comment <comment> " \
# These aren't currently supported!
"[--<approval category id> <score> " \
"--<approval category id> <score> " \
"--<approval category id>-oldValue <score> ...]"
"--Code-Review <score> " \
"--Verified <score> " \
"--Code-Review-oldValue <score>" \
"--Verified-oldValue <score>"

CHANGE_MERGED_FLAGS = "--change <change id> --change-url <change url> " \
"--change-owner <change owner> --change-owner-username <username> " \
Expand Down Expand Up @@ -151,6 +151,14 @@ class HookFlagDefinitions:
SupportedHooks.SUBMIT: SUBMIT_FLAGS
}

@classmethod
def add_approval_category(cls, label):
"""Extend the comment-added hooks flags with the given custom label.
:param str label: The label of the approva_category to add.
"""
cls.COMMENT_ADDED_FLAGS += "--{} <score> --{}-oldValue <score>"

def __getitem__(self, item):
"""Return the flags for the given hook name as a dict.
Expand Down
8 changes: 8 additions & 0 deletions gerrit_hooks/hook.py
Expand Up @@ -6,6 +6,14 @@
from gerrit_hooks.containers import SupportedHooks, HookFlagDefinitions


def add_custom_approval_category(label):
"""Add a custom approval category to the Hook flag definitions.
:param str label: Label of the approval category.
"""
HookFlagDefinitions.add_approval_category(label)


def _generate_parser(**cli_kwargs):
"""Set up an argument parser with the given expected flags as kwargs.
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -8,14 +8,14 @@

setup(
name='gerrit-hooks',
version='1.0.0b3',
version='1.0.0',
description='Utility for writing Gerrit Hooks in python',
long_description=long_description,
author='Nils Diefenbach',
author_email='nlsdfnbch.foss@kolabnow.com',

classifiers=[
'Development Status :: 4 - Beta',
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
Expand Down

0 comments on commit e8e814f

Please sign in to comment.