在Bugzilla扩展中,如何检测添加的新评论?

时间:2015-12-25 00:11:27

标签: perl bugzilla

需要挂钩哪个钩子以确定何时将新注释添加到错误中?

我的用例是,无论何时添加评论,我都需要处理所有评论,并根据该评论执行一些操作。但是,此操作“很昂贵”,因此除非确实添加了评论,否则我不想执行操作。

到目前为止,我发现确定这一点的唯一方法是使用代码挂钩object_end_of_update

sub object_end_of_update {
  my ($self, $args) = @_;

  my ($object, $old_object, $changes) = @$args{qw(object old_object changes)};
  print STDERR "--- Object Type: " . ref $object;
  if ($object->isa('Bugzilla::Bug')) {
    # Load comments on the old object here, otherwise by the time we get
    # to bug_end_of_update it is too late, and we cannot determine if a
    # new comment has been added or not.
    $old_object->comments({order=>'oldest_to_newest'});
  }
}

然后挂钩到bug_end_of_update,然后我可以做类似的事情:

sub bug_end_of_update {
  my ($self, $args) = @_;

  my ($bug, $old_bug, $timestamp, $changes) = @$args{qw(bug old_bug timestamp changes)};
  # Note that this will only work if the old comments have already been
  # loaded in object_end_of_update, otherwise when we get the old comments
  # here, it just goes to the DB, and gets all of the comments, including
  # the new one, if there is one.
  my $oldComments = $old_bug->comments({order=>'oldest_to_newest'});
  my $newComments = $bug->comments({order=>'oldest_to_newest'});
  if (scalar(@$newComments) > scalar(@$oldComments)) {
    # If we added a new comment, then perform processing.
    do_slow_action($bug);
  }
}

然而,这感觉很脆弱,即使它不是,也绝对不是明确的代码。

确定评论是否已添加到bugzilla错误的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

而不是在Bugzilla源代码中添加钩子。 您可以添加在字段注释上发生更新时触发的数据库触发器。

优点是,只要数据库模式对于较新版本没有太大变化,触发器就会继续工作。 (更多信息:Database triggers)。

以下是一个例子:

use bugs;

create TRIGGER new_comment 
AFTER UPDATE ON bugs_fulltext 
FOR EACH ROW BEGIN 

IF NEW.comments <> OLD.comments 
THEN 

.... //execute your script

END IF 
END;