perlbugzilla

Bugzilla set reporter on creating Bug via WebService


I am creating a Bug on Bugzilla 5.2+ via rest-API. I try to achieve to set the reporter to a known Bugzilla user which differs from the used API-User.

I already tried that by creating an Extension.pm like follows.

package Bugzilla::Extension::MyExtension;
use strict;
use base qw(Bugzilla::Extension);

our $VERSION = '1.0';
use constant NAME => 'MyExtension';

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

    my $object = $args->{'object'};

    if ($object->isa('Bugzilla::Bug')) {
        my $user = new Bugzilla::User(3);

        $object->{'reporter'} = $user;
        $object->{'creator'} = $user;
        $object->{'url'} = $user->id;
        $object->update;
    }
}

__PACKAGE__->NAME;

The extension works, not throwing any errors, but it still does not change any values persistent, especially the reporter. It will, however, display the changed reporter after the bug got transmitted, but will switch back as soon as the page gets reloaded.

Additionally, i would like to get the value for the reporter from my api-request-payload, as i have a relation between the sending client user and the bugzilla user.


Solution

  • object_end_of_create and the corresponding bug_end_of_create are called after all of the database writes have happened and before the database transaction is closed. The intention of that hook is if you added a new field which is not represented in that object's primary table, that you do the database write for your new table insert during that hook. You can't change core values there because they've already been written.

    The place to hook to be able to change the values is bug_end_of_create_validators. This is mainly intended to add your own field validators, but it's called after all of the built in values have been validated, and before they get written to the database, and you can make changes to the params object there during the hook that will be kept (because validators have to be able to delete garbage data that doesn't validate but isn't fatal).