]> git.mjollnir.org Git - cil.git/commitdiff
Added initial hook framework infrastructure (closed #41b351fc).
authorAndrew Chilton <andychilton@gmail.com>
Sun, 13 Jul 2008 10:34:55 +0000 (22:34 +1200)
committerAndrew Chilton <andychilton@gmail.com>
Sun, 13 Jul 2008 10:34:55 +0000 (22:34 +1200)
* this includes some VCS information we can use in later hooks

Changes
bin/cil
issues/c_7b75b82c.cil [new file with mode: 0644]
issues/i_41b351fc.cil
lib/CIL.pm
lib/CIL/VCS/Factory.pm [new file with mode: 0644]
lib/CIL/VCS/Git.pm [new file with mode: 0644]
lib/CIL/VCS/Null.pm [new file with mode: 0644]

diff --git a/Changes b/Changes
index beaf0a4cb610ba5bb6f18066a9cd050dbca51358..f22211c46eb996d4f0248d923ffb933d1a29172d 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,3 +1,7 @@
+0.6.0 (Unreleased)
+
+  * New hook infrastructure, currently only 'issue_post_save' is allowed
+
 0.5.1 (2008-07-05)
 
   * New release minus all the cruft
diff --git a/bin/cil b/bin/cil
index 4ff4d3295d1be37492d199bf92a4d483adb4bb19..90a76815d42734532fad62ca4349d44993a86ff8 100755 (executable)
--- a/bin/cil
+++ b/bin/cil
@@ -106,6 +106,9 @@ my %BOOLEAN_ARGS = (
     $cil->read_config_user();
     $cil->read_config_file();
 
+    # add any hooks we want
+    # none yet
+
     &{"cmd_$command"}($cil, $args, @ARGV);
 }
 
@@ -759,6 +762,10 @@ sub add_issue_loop {
     my $unique_str = time . $issue->Inserted . $issue->Summary . $issue->Description;
     $issue->set_name( substr(md5_hex($unique_str), 0, 8) );
     $issue->save($cil);
+
+    # should probably be run from with $cil
+    $cil->run_hook('issue_post_save', $issue);
+
     display_issue($cil, $issue);
 
     return $issue;
@@ -879,6 +886,11 @@ sub print_fsck_errors {
     }
 }
 
+## ----------------------------------------------------------------------------
+# hooks
+
+# none yet
+
 ## ----------------------------------------------------------------------------
 # input/output
 
diff --git a/issues/c_7b75b82c.cil b/issues/c_7b75b82c.cil
new file mode 100644 (file)
index 0000000..a95c0ed
--- /dev/null
@@ -0,0 +1,7 @@
+Issue: 41b351fc
+CreatedBy: Andrew Chilton <andychilton@gmail.com>
+Inserted: 2008-07-13T10:29:22
+Updated: 2008-07-13T10:30:58
+
+Added the initial go at the hook system. It seems to work but until we actually
+do some, it'll just be a framework for the time being.
index 4c3d0404631b3101b0e8816d8bf44e015647e151..9e96c087d8f508c92e11ea5af8d88f9b52837547 100644 (file)
@@ -1,13 +1,14 @@
 Summary: Add some sort of hook system
-Status: New
+Status: Finished
 CreatedBy: Andrew Chilton <andychilton@gmail.com>
 AssignedTo: Andrew Chilton <andychilton@gmail.com>
 Label: Milestone-v0.5
 Label: Type-Enhancement
+Comment: 7b75b82c
 Inserted: 2008-07-13T05:08:27
-Updated: 2008-07-13T10:28:36
+Updated: 2008-07-13T10:34:43
 
-For example, when you add an issue, you might want to do the followin things:
+For example, when you add an issue, you might want to do the following things:
 
 * reindex all the issues (for faster search - #98203ce8)
 * generate an issues/index.html file (for poor man's cilweb - #0e004cde)
index 9a87bf91a253abb772c12ecc2a9ec914bca7ac8e..1049f592218604b9519ebfa7be9004bc3e5d4046 100644 (file)
@@ -23,8 +23,11 @@ package CIL;
 
 use strict;
 use warnings;
+use Carp;
 use File::Glob qw(:glob);
 
+use CIL::VCS::Factory;
+
 use base qw(Class::Accessor);
 __PACKAGE__->mk_accessors(qw(
     IssueDir
@@ -32,6 +35,7 @@ __PACKAGE__->mk_accessors(qw(
     LabelStrict LabelAllowed
     VCS
     UserName UserEmail
+    vcs hook
 ));
 
 my $defaults = {
@@ -47,6 +51,15 @@ my $defaults_user = {
     UserEmail => 'me@example.com',
 };
 
+my $allowed = {
+    vcs => {
+        'Git' => 1,
+    },
+    hook => {
+        'issue_post_save' => 1,
+    },
+};
+
 ## ----------------------------------------------------------------------------
 
 sub new {
@@ -251,7 +264,37 @@ sub read_config_file {
     $self->LabelStrict( $cfg->{LabelStrict} );
     $self->LabelAllowed( $cfg->{LabelAllowed} );
 
-    $self->VCS( $cfg->{VCS} );
+    # if we are allowed this VCS, create the hook instance
+    if ( exists $allowed->{vcs}{$cfg->{VCS}} ) {
+        $self->VCS( $cfg->{VCS} );
+        my $vcs = CIL::VCS::Factory->new( $cfg->{VCS} );
+        $self->vcs( $vcs );
+    }
+}
+
+sub register_hook {
+    my ($self, $hook_name, $code) = @_;
+
+    unless ( defined $allowed->{hook}{$hook_name} ) {
+        croak "hook '$hook_name' not allowed";
+    }
+
+    push @{$self->{hook}{$hook_name}}, $code;
+}
+
+sub run_hook {
+    my ($self, $hook_name, @rest) = @_;
+
+    print "s=$self, hn=$hook_name, r=@rest\n";
+
+    unless ( defined $allowed->{hook}{$hook_name} ) {
+        croak "hook '$hook_name' not allowed";
+    }
+
+    # call all the hooks with all the args
+    foreach my $code ( @{$self->hook->{$hook_name}} ) {
+        &$code( $self, @rest );
+    }
 }
 
 ## ----------------------------------------------------------------------------
diff --git a/lib/CIL/VCS/Factory.pm b/lib/CIL/VCS/Factory.pm
new file mode 100644 (file)
index 0000000..7b427e9
--- /dev/null
@@ -0,0 +1,45 @@
+## ----------------------------------------------------------------------------
+# cil is a Command line Issue List
+# Copyright (C) 2008 Andrew Chilton
+#
+# This file is part of 'cil'.
+#
+# cil is free software: you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+## ----------------------------------------------------------------------------
+
+package CIL::VCS::Factory;
+
+use strict;
+use warnings;
+use Carp;
+
+use base qw( Class::Factory );
+
+__PACKAGE__->register_factory_type( Git => 'CIL::VCS::Git' );
+
+foreach my $method_name ( qw(post_add) ) {
+    my $method = sub {
+        my ($self) = @_;
+        my $class = ref $self || $self;
+        die "Method '$method_name' not overriden in derived class '$class'";
+    };
+
+    no strict 'refs';
+    *{$method_name} = $method;
+}
+
+## ----------------------------------------------------------------------------
+1;
+## ----------------------------------------------------------------------------
diff --git a/lib/CIL/VCS/Git.pm b/lib/CIL/VCS/Git.pm
new file mode 100644 (file)
index 0000000..e4a8286
--- /dev/null
@@ -0,0 +1,44 @@
+## ----------------------------------------------------------------------------
+# cil is a Command line Issue List
+# Copyright (C) 2008 Andrew Chilton
+#
+# This file is part of 'cil'.
+#
+# cil is free software: you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+## ----------------------------------------------------------------------------
+
+package CIL::VCS::Git;
+
+use strict;
+use warnings;
+use Carp;
+
+use base qw(CIL::VCS::Factory);
+
+sub post_add {
+    my ($self, $issue) = @_;
+
+    my $issue_dir = $issue->cil->IssueDir();
+    my @files;
+    push @files, "$issue_dir/i_" . $issue->name . '.cil';
+    push @files, map { "$issue_dir/c_${_}.cil" } @{ $issue->CommentList };
+    push @files, map { "$issue_dir/a_${_}.cil" } @{ $issue->AttachmentList };
+
+    return [ "git add @files" ];
+}
+
+## ----------------------------------------------------------------------------
+1;
+## ----------------------------------------------------------------------------
diff --git a/lib/CIL/VCS/Null.pm b/lib/CIL/VCS/Null.pm
new file mode 100644 (file)
index 0000000..b062771
--- /dev/null
@@ -0,0 +1,35 @@
+## ----------------------------------------------------------------------------
+# cil is a Command line Issue List
+# Copyright (C) 2008 Andrew Chilton
+#
+# This file is part of 'cil'.
+#
+# cil is free software: you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+## ----------------------------------------------------------------------------
+
+package CIL::VCS::Null;
+
+use strict;
+use warnings;
+use Carp;
+
+foreach my $method_name ( qw(post_add) ) {
+    no strict 'refs';
+    *{"${class}::$method_name"} = sub {};
+}
+
+## ----------------------------------------------------------------------------
+1;
+## ----------------------------------------------------------------------------