Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
60bd095
WIP: Bug 1409957 - Create polling daemon to query Phabricator for rec…
dklawren Oct 19, 2017
e969e03
Remove some boilerplate using Moo
dylanwh Oct 19, 2017
d2662bc
WIP: Bug 1409957 - Create polling daemon to query Phabricator for rec…
dklawren Oct 19, 2017
71d177d
Cleanups and refactoring from last commit.
dklawren Oct 31, 2017
d7f344c
Removed get_feed_transactions from Util.pm
dklawren Oct 31, 2017
67ac4de
Fixed merge issue with PhabBugz/lib/Util.pm
dklawren Nov 2, 2017
c6ba403
- Added Project.pm for wrapping phabricator projects. Can be used for
dklawren Nov 3, 2017
f0837dd
Merge branch 'phabbugz-feed' of https://github.com/mozilla-bteam/bmo …
dklawren Nov 3, 2017
5c7ff11
- Support for obsoleting attachments
dklawren Nov 6, 2017
11334e2
- Use get_phab_bmo_ids instead of get_members_by_phid
dklawren Nov 6, 2017
62af289
- Fixed some typos in Revision.pm
dklawren Nov 8, 2017
81c2de1
catch exceptions that bubble up to main loop (#269)
dylanwh Nov 9, 2017
ea49d29
- Fixed some of the review comments by Dylan such as transaction orde…
dklawren Nov 9, 2017
ce0a219
- Changed to use feed.query_id instead of feed.query_epoch.
dklawren Nov 16, 2017
7ff004a
add some validation / type checking
dylanwh Nov 22, 2017
e4a3aed
Merge branch 'master' into phabbugz-feed
dylanwh Nov 27, 2017
a4aa6b9
Merge branch 'master' into phabbugz-feed
dylanwh Nov 28, 2017
4d9cdcf
Merge branch 'master' into phabbugz-feed
dylanwh Nov 29, 2017
74d433d
Merge branch 'master' into phabbugz-feed
dylanwh Nov 29, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions extensions/PhabBugz/Extension.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ package Bugzilla::Extension::PhabBugz;
use 5.10.1;
use strict;
use warnings;

use parent qw(Bugzilla::Extension);

use Bugzilla::Constants;
use Bugzilla::Extension::PhabBugz::Feed;
use Bugzilla::Extension::PhabBugz::Logger;

our $VERSION = '0.01';

BEGIN {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather see you just call Bugzilla::Extensions::PhabBugz->get_instance() (remove the _). That way the person reading the code will know which file to look in.

*Bugzilla::User::phab_phid = sub { return $_[0]->{phab_phid}; };
*Bugzilla::User::phab_review_status = sub { return $_[0]->{phab_review_status}; };
}

sub config_add_panels {
my ($self, $args) = @_;
my $modules = $args->{panel_modules};
Expand All @@ -40,4 +50,47 @@ sub webservice {
$args->{dispatch}->{PhabBugz} = "Bugzilla::Extension::PhabBugz::WebService";
}

#
# installation/config hooks
#

sub db_schema_abstract_schema {
my ($self, $args) = @_;
$args->{'schema'}->{'phabbugz'} = {
FIELDS => [
id => {
TYPE => 'INTSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
name => {
TYPE => 'VARCHAR(255)',
NOTNULL => 1,
},
value => {
TYPE => 'MEDIUMTEXT',
NOTNULL => 1
}
],
INDEXES => [
phabbugz_idx => {
FIELDS => ['name'],
TYPE => 'UNIQUE',
},
],
};
}

sub install_filesystem {
my ($self, $args) = @_;
my $files = $args->{'files'};

my $extensionsdir = bz_locations()->{'extensionsdir'};
my $scriptname = $extensionsdir . "/PhabBugz/bin/phabbugzd.pl";

$files->{$scriptname} = {
perms => Bugzilla::Install::Filesystem::WS_EXECUTE
};
}

__PACKAGE__->NAME;
50 changes: 50 additions & 0 deletions extensions/PhabBugz/bin/phabbugz_feed.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/perl

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

use 5.10.1;
use strict;
use warnings;

use lib qw(. lib local/lib/perl5);

BEGIN {
use Bugzilla;
Bugzilla->extensions;
}

use Bugzilla::Extension::PhabBugz::Daemon;
Bugzilla::Extension::PhabBugz::Daemon->start();

=head1 NAME

phabbugz_feed.pl - Query Phabricator for interesting changes and update bugs related to revisions.

=head1 SYNOPSIS

phabbugz_feed.pl [OPTIONS] COMMAND

OPTIONS:
-f Run in the foreground (don't detach)
-d Output a lot of debugging information
-p file Specify the file where phabbugz_feed.pl should store its current
process id. Defaults to F<data/phabbugz_feed.pl.pid>.
-n name What should this process call itself in the system log?
Defaults to the full path you used to invoke the script.

COMMANDS:
start Starts a new phabbugz_feed daemon if there isn't one running already
stop Stops a running phabbugz_feed daemon
restart Stops a running phabbugz_feed if one is running, and then
starts a new one.
check Report the current status of the daemon.
install On some *nix systems, this automatically installs and
configures phabbugz_feed.pl as a system service so that it will
start every time the machine boots.
uninstall Removes the system service for phabbugz_feed.pl.
help Display this usage info
41 changes: 23 additions & 18 deletions extensions/PhabBugz/bin/update_project_members.pl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
use Bugzilla::Error;
use Bugzilla::Group;

use Bugzilla::Extension::PhabBugz::Project;
use Bugzilla::Extension::PhabBugz::Util qw(
create_project
get_members_by_bmo_id
get_project_phid
set_project_members
get_phab_bmo_ids
);

Bugzilla->usage_mode(USAGE_MODE_CMDLINE);
Expand Down Expand Up @@ -55,23 +53,22 @@
my $sync_groups = Bugzilla::Group->match({ name => [ split('[,\s]+', $phab_sync_groups) ] });

foreach my $group (@$sync_groups) {
my @users = get_group_members($group);

# Create group project if one does not yet exist
my $phab_project_name = 'bmo-' . $group->name;
my $project_phid = get_project_phid($phab_project_name);
if (!$project_phid) {
$project_phid = create_project($phab_project_name, 'BMO Security Group for ' . $group->name);
my $project = Bugzilla::Extension::PhabBugz::Project->new({
name => $phab_project_name
});
if (!$project->id) {
$project = Bugzilla::Extension::PhabBugz::Project->create({
name => $phab_project_name,
description => 'BMO Security Group for ' . $group->name
});
}

# Get the internal user ids for the bugzilla group members
my $phab_user_ids = [];
if (@users) {
$phab_user_ids = get_members_by_bmo_id(\@users);
}
my @group_members = get_group_members($group);

# Set the project members to the exact list
set_project_members($project_phid, $phab_user_ids);
$project->set_members(\@group_members);
$project->update();
}

sub get_group_members {
Expand All @@ -84,5 +81,13 @@ sub get_group_members {
$users{$user->id} = $user;
}
}
return values %users;
}

# Look up the phab ids for these users
my $phab_users = get_phab_bmo_ids({ ids => [ keys %users ] });
foreach my $phab_user (@{ $phab_users }) {
$users{$phab_user->{id}}->{phab_phid} = $phab_user->{phid};
}

# We only need users who have accounts in phabricator
return grep { $_->phab_phid } values %users;
}
2 changes: 2 additions & 0 deletions extensions/PhabBugz/lib/Constants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ our @EXPORT = qw(
PHAB_AUTOMATION_USER
PHAB_ATTACHMENT_PATTERN
PHAB_CONTENT_TYPE
PHAB_POLL_SECONDS
);

use constant PHAB_ATTACHMENT_PATTERN => qr/^phabricator-D(\d+)/;
use constant PHAB_AUTOMATION_USER => 'phab-bot@bmo.tld';
use constant PHAB_CONTENT_TYPE => 'text/x-phabricator-request';
use constant PHAB_POLL_SECONDS => 5;

1;
100 changes: 100 additions & 0 deletions extensions/PhabBugz/lib/Daemon.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::PhabBugz::Daemon;

use 5.10.1;
use strict;
use warnings;

use Bugzilla::Constants;
use Bugzilla::Extension::PhabBugz::Feed;
use Bugzilla::Extension::PhabBugz::Logger;

use Carp qw(confess);
use Daemon::Generic;
use File::Basename;
use File::Spec;
use Pod::Usage;

sub start {
newdaemon();
}

#
# daemon::generic config
#

sub gd_preconfig {
my $self = shift;
my $pidfile = $self->{gd_args}{pidfile};
if (!$pidfile) {
$pidfile = File::Spec->catfile(bz_locations()->{datadir}, $self->{gd_progname} . ".pid");
}
return (pidfile => $pidfile);
}

sub gd_getopt {
my $self = shift;
$self->SUPER::gd_getopt();
if ($self->{gd_args}{progname}) {
$self->{gd_progname} = $self->{gd_args}{progname};
} else {
$self->{gd_progname} = basename($0);
}
$self->{_original_zero} = $0;
$0 = $self->{gd_progname};
}

sub gd_postconfig {
my $self = shift;
$0 = delete $self->{_original_zero};
}

sub gd_more_opt {
my $self = shift;
return (
'pidfile=s' => \$self->{gd_args}{pidfile},
'n=s' => \$self->{gd_args}{progname},
);
}

sub gd_usage {
pod2usage({ -verbose => 0, -exitval => 'NOEXIT' });
return 0;
};

sub gd_redirect_output {
my $self = shift;

my $filename = File::Spec->catfile(bz_locations()->{datadir}, $self->{gd_progname} . ".log");
open(STDERR, ">>", $filename) or (print "could not open stderr: $!" && exit(1));
close(STDOUT);
open(STDOUT, ">&", STDERR) or die "redirect STDOUT -> STDERR: $!";
$SIG{HUP} = sub {
close(STDERR);
open(STDERR, ">>", $filename) or (print "could not open stderr: $!" && exit(1));
};
}

sub gd_setup_signals {
my $self = shift;
$self->SUPER::gd_setup_signals();
$SIG{TERM} = sub { $self->gd_quit_event(); }
}

sub gd_run {
my $self = shift;
$::SIG{__DIE__} = \&Carp::confess if $self->{debug};
my $phabbugz = Bugzilla::Extension::PhabBugz::Feed->new();
$phabbugz->is_daemon(1);
$phabbugz->logger(
Bugzilla::Extension::PhabBugz::Logger->new(debugging => $self->{debug}));
$phabbugz->start();
}

1;
Loading