From 7468a3083aae95efd372c074f86228db4f1eef04 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 4 Oct 2012 03:11:39 -0400 Subject: [PATCH] Add an external filtering facility. Not quite the same as laurentb's design; this one passes all the commit and extractor data as a single JSON object, and expect to get back same. --- Makefile | 2 +- NEWS | 3 +++ filter-example.py | 13 +++++++++++++ hacking.txt | 3 ++- irkerhook.py | 16 ++++++++++++++-- irkerhook.xml | 30 ++++++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 4 deletions(-) create mode 100755 filter-example.py diff --git a/Makefile b/Makefile index eb0be38..8f12391 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ loc: @echo -n "LLOC: "; grep -vE '(^ *#|^ *$$)' irkerd irkerhook.py | wc -l SOURCES = README COPYING NEWS install.txt security.txt hacking.txt \ - irkerd irkerhook.py Makefile irkerd.xml irkerhook.xml + irkerd irkerhook.py filter-example.py Makefile irkerd.xml irkerhook.xml EXTRA_DIST = irker-logo.png org.catb.irkerd.plist version: diff --git a/NEWS b/NEWS index 114b811..04864be 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,8 @@ irker history +1.7 @ + Optional metadata filtering with a user-specified command. + 1.6 @ 2012-10-04 This is a stable release. In 1.5 trying to appease pylint broke the Mercurial hook. diff --git a/filter-example.py b/filter-example.py new file mode 100755 index 0000000..12908b4 --- /dev/null +++ b/filter-example.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python +# This is a trivial example of a metadata filter. +# All it does is change the name of the commit's author. +# It could do other things, including modifying the +# channels list +# +import sys, json +metadata = json.loads(sys.argv[1]) + +metadata['author'] = "The Great and Powerful Oz" + +print json.dumps(metadata) +# end diff --git a/hacking.txt b/hacking.txt index b9c397d..20e9990 100644 --- a/hacking.txt +++ b/hacking.txt @@ -57,7 +57,8 @@ Peter Scott contributed the original greenlet support. Laurent Bachelier fixed the Makefile so it -wouldn't break stuff and added the external filtering option. +wouldn't break stuff and wrote the first version of the external +filtering option. dak180 (name withheld by request) wrote the OS X launchd plist. diff --git a/irkerhook.py b/irkerhook.py index 202e1b2..b1b2981 100755 --- a/irkerhook.py +++ b/irkerhook.py @@ -368,6 +368,16 @@ def ship(extractor, commit, debug): "Ship a notification for the specified commit." metadata = extractor.commit_factory(commit) + # This is where we apply filtering + if extractor.filtercmd: + data = do('%s %s' % (shellquote(extractor.filtercmd), + shellquote(json.dumps(metadata.__dict__)))) + try: + metadata.__dict__.update(json.loads(data)) + except ValueError: + sys.stderr.write("irkerhook.py: could not decode JSON: %s\n" % data) + raise SystemExit, 1 + # Message reduction. The assumption here is that IRC can't handle # lines more than 510 characters long. If we exceed that length, we # try knocking out the file list, on the theory that for notification @@ -379,8 +389,10 @@ def ship(extractor, commit, debug): metadata.files = "" privmsg = str(metadata) - # Anti-spamming guard. - channels = extractor.channels.split(",") + # Anti-spamming guard. It's deliberate that we get maxchannels not from + # the user-filtered metadata but from the extractor data - means repo + # administrators can lock in that setting. + channels = metadata.channels.split(",") if extractor.maxchannels != 0: channels = channels[:extractor.maxchannels] diff --git a/irkerhook.xml b/irkerhook.xml index e10d5b8..0862dfb 100644 --- a/irkerhook.xml +++ b/irkerhook.xml @@ -296,6 +296,36 @@ exists. +Filtering + +It is possible to filter commits before sending them to +irkerd. + +You have to specify the option, which +will be the command irkerhook.py will +run. This command should accept one arguments, which is a JSON +representation of commit and extractor metadata (including the +channels variable). The command should emit to standard output a JSON +representation of (possibly altered) metadata. + +Below is an example filter: + + +#!/usr/bin/env python +# This is a trivial example of a metadata filter. +# All it does is change the name of the commit's author. +# +import sys, json +metadata = json.loads(sys.argv[1]) + +metadata['author'] = "The Great and Powerful Oz" + +print json.dumps(metadata) +# end + + + + OPTIONS -- 2.26.2