Skip to content

Bug 1409957 - Create polling daemon to query Phabricator for recent transcations and update bug data according to revision changes - #264

Merged
dylanwh merged 19 commits into
masterfrom
phabbugz-feed
Nov 29, 2017
Merged

Bug 1409957 - Create polling daemon to query Phabricator for recent transcations and update bug data according to revision changes#264
dylanwh merged 19 commits into
masterfrom
phabbugz-feed

Conversation

@dklawren

Copy link
Copy Markdown
Collaborator

No description provided.

…ent transcations and update bug data according to revision changes
@dklawren
dklawren requested a review from darkwing October 19, 2017 03:50
Comment thread extensions/PhabBugz/lib/Feed.pm Outdated
next;
}
my ($revision) = get_revisions_by_phids([$object_phid]);
$self->logger->info("REVSION: " . $revision->{'id'} . ": " .

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.

REVSION typo

@floatingatoll

floatingatoll commented Oct 19, 2017 via email

Copy link
Copy Markdown

@darkwing
darkwing requested a review from dylanwh October 19, 2017 19:57
@darkwing

Copy link
Copy Markdown
Contributor

Adding @dylanwh as a reviewer as I'm not nearly qualified to approve this large a change.


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.

Comment thread extensions/PhabBugz/Extension.pm Outdated
}

sub _get_instance {
my $cache = Bugzilla->request_cache;

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.

You're storing this in the request cache, but the daemon isn't ever clearing the request cache. What is the lifecycle of this object intended to be in the daemon? Always there?

Comment thread extensions/PhabBugz/lib/Daemon.pm Outdated
my $self = shift;
my $pidfile = $self->{gd_args}{pidfile};
if (!$pidfile) {
$pidfile = bz_locations()->{datadir} . '/' . $self->{gd_progname} . ".pid";

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.

$pidfile = File::Spec->catfile(bz_locations()->{datadir} , self->{gd_progname} . ".pid");

is more idiomatic, though I bet we're not using that elsewhere.


sub start {
my ($self) = @_;
while (1) {

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.

optional
It might be better to loop in the code that calls this, and also look into https://metacpan.org/pod/Daemon::Generic::While1.

required
Meanwhile if this daemon does anything with bugs, it will eventually run out of memory. You at least need to call Bugzilla->_cleanup() at the end of every run of the loop.

Comment thread extensions/PhabBugz/lib/Util.pm Outdated
ThrowUserError('invalid_phabricator_revision_id')
unless (exists $result->{result}{data} && @{ $result->{result}{data} });

return @{$result->{result}{data}};

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.

if this is potentially large, it might be best to return the reference and reference it at the place it is used. Otherwise it will do a copy of the full array.

…ent transcations and update bug data according to revision changes
New Revision.pm class that will eventually handle all loading and updating of a revision

How to test:

1. You need to be running a mozilla/phabext image that contains the
   latest phabricator-extensions code that has the feed.query_epoch Conduit
   API call.
2. cd /path/to/mozilla-conduit/bmo-extensions
3. docker-compose up -d --build
4. docker exec -it bmoextensions_bmo.test_1 su - bugzilla
5. cd /var/www/html/bmo
6. perl extensions/PhabBugz/bin/phabbugz_feed.pl -f -d start
7. In Phabricator, create a new revision with the bug id of an
   existing bug in the bugzilla database.
8. BMO should update the revision with either public or private policies
   depending on the bugs permissions.
@dklawren dklawren changed the title WIP: Bug 1409957 - Create polling daemon to query Phabricator for recent transcations and update bug data according to revision changes Bug 1409957 - Create polling daemon to query Phabricator for recent transcations and update bug data according to revision changes Oct 31, 2017
@dklawren
dklawren requested review from darkwing and dylanwh October 31, 2017 19:37
Comment thread extensions/PhabBugz/lib/Util.pm Outdated
return @{$result->{result}{data}};
}

<<<<<<< HEAD

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.

This seems to be a problem.

@darkwing

darkwing commented Nov 2, 2017

Copy link
Copy Markdown
Contributor

Note: I had to run checksetup.pl to get the DB table(s) to install.

@darkwing

darkwing commented Nov 2, 2017

Copy link
Copy Markdown
Contributor

Wow, so I stumbled upon a recurring error and I think it's an edge case with my machine but could become a real issue. In my auth testing, I would delete users but their old revisions would still be there, so it's possible feed items will have users that have been deleted and thus have no BMO ID.

Here's the error I get each time I try to run the script as told to:

$ perl extensions/PhabBugz/bin/phabbugz_feed.pl -f -d start
PIDFILE=data/phabbugz_feed.pl.pid
Starting up...
[Thu Nov  2 18:09:56 2017] INFO: FEED: Fetching new transactions
[Thu Nov  2 18:09:56 2017] DEBUG: LAST_TS: 0
[Thu Nov  2 18:09:56 2017] DEBUG: STORY: PHID-STRY-i3d3jule62chbehpbkrc
[Thu Nov  2 18:09:56 2017] DEBUG: OBJECT: PHID-DREV-tuimrl762iy3fthq23xc
[Thu Nov  2 18:09:57 2017] INFO: REVISION CHANGE FOUND: D41: Bugless Creation 6 | bug: 1 | ConduitTestUser updated D41: Bugless Creation 6.
[Thu Nov  2 18:09:57 2017] INFO: SUCCESS
[Thu Nov  2 18:09:57 2017] DEBUG: UPDATING LAST_TS: 0
[Thu Nov  2 18:09:57 2017] DEBUG: STORY: PHID-STRY-nwjbw6mtmx5snlzkp4tb
[Thu Nov  2 18:09:57 2017] DEBUG: OBJECT: PHID-DREV-5aytaddmqugd6x5xyydy
[Thu Nov  2 18:09:57 2017] INFO: REVISION CHANGE FOUND: D21: Blah | bug:  | Unknown Object (User) created D21: Blah.
Invalid parameter passed to Bugzilla::User->visible_bugs: It must be
numeric.
 at Bugzilla/User.pm line 1441.
	Bugzilla::User::visible_bugs(...) called at Bugzilla/User.pm line 1420
	Bugzilla::User::can_see_bug(...) called at Bugzilla/Attachment.pm line 425
	Bugzilla::Attachment::_check_bug(...) called at Bugzilla/Object.pm line 719
	Bugzilla::Object::run_create_validators(...) called at Bugzilla/Attachment.pm line 740
	Bugzilla::Attachment::run_create_validators(...) called at Bugzilla/Attachment.pm line 711
	Bugzilla::Attachment::create(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Util.pm line 113
	Bugzilla::Extension::PhabBugz::Util::create_revision_attachment(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Feed.pm line 133
	Bugzilla::Extension::PhabBugz::Feed::process_revision_change(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Feed.pm line 89
	Bugzilla::Extension::PhabBugz::Feed::feed_query(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Feed.pm line 33
	Bugzilla::Extension::PhabBugz::Feed::start(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Daemon.pm line 96
	Bugzilla::Extension::PhabBugz::Daemon::gd_run(...) called at /usr/local/share/perl5/Daemon/Generic.pm line 192
	Daemon::Generic::new(...) called at /usr/local/share/perl5/Daemon/Generic.pm line 37
	Daemon::Generic::newdaemon(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Daemon.pm line 24
	Bugzilla::Extension::PhabBugz::Daemon::start(...) called at extensions/PhabBugz/bin/phabbugz_feed.pl line 22


====================


$ perl extensions/PhabBugz/bin/phabbugz_feed.pl -f -d start
PIDFILE=data/phabbugz_feed.pl.pid
Starting up...
[Thu Nov  2 18:23:33 2017] INFO: FEED: Fetching new transactions
[Thu Nov  2 18:23:33 2017] DEBUG: LAST_TS: 1508871772
[Thu Nov  2 18:23:34 2017] DEBUG: STORY: PHID-STRY-ja2b6b32ttfl7qf2woe7
[Thu Nov  2 18:23:34 2017] DEBUG: OBJECT: PHID-DREV-nyumlf7qv26ms7mhcag5
[Thu Nov  2 18:23:34 2017] INFO: REVISION CHANGE FOUND: D46: Starts public | bug:  | QAAdmin created D46: Starts public.
Invalid parameter passed to Bugzilla::User->visible_bugs: It must be
numeric.
 at Bugzilla/User.pm line 1441.
	Bugzilla::User::visible_bugs(...) called at Bugzilla/User.pm line 1420
	Bugzilla::User::can_see_bug(...) called at Bugzilla/Attachment.pm line 425
	Bugzilla::Attachment::_check_bug(...) called at Bugzilla/Object.pm line 719
	Bugzilla::Object::run_create_validators(...) called at Bugzilla/Attachment.pm line 740
	Bugzilla::Attachment::run_create_validators(...) called at Bugzilla/Attachment.pm line 711
	Bugzilla::Attachment::create(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Util.pm line 113
	Bugzilla::Extension::PhabBugz::Util::create_revision_attachment(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Feed.pm line 133
	Bugzilla::Extension::PhabBugz::Feed::process_revision_change(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Feed.pm line 89
	Bugzilla::Extension::PhabBugz::Feed::feed_query(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Feed.pm line 33
	Bugzilla::Extension::PhabBugz::Feed::start(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Daemon.pm line 96
	Bugzilla::Extension::PhabBugz::Daemon::gd_run(...) called at /usr/local/share/perl5/Daemon/Generic.pm line 192
	Daemon::Generic::new(...) called at /usr/local/share/perl5/Daemon/Generic.pm line 37
	Daemon::Generic::newdaemon(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Daemon.pm line 24
	Bugzilla::Extension::PhabBugz::Daemon::start(...) called at extensions/PhabBugz/bin/phabbugz_feed.pl line 22    

Also, each successive attempt tries to execute another transaction; is it possible the failures aren't keeping things in order?

loading existing projects and creating new ones.
- More cleanup and bug fixes.
…into phabbugz-feed

* 'phabbugz-feed' of https://github.com/mozilla-bteam/bmo:
  Fixed merge issue with PhabBugz/lib/Util.pm
@dklawren
dklawren requested a review from darkwing November 3, 2017 19:24
@darkwing

darkwing commented Nov 3, 2017

Copy link
Copy Markdown
Contributor

With new updates still getting this error:
newerror

@darkwing

darkwing commented Nov 3, 2017

Copy link
Copy Markdown
Contributor

Here's the payload I'm receiving: https://gist.github.com/darkwing/bd161c535065d8c090a136377f3eb452

Comment thread extensions/PhabBugz/lib/Revision.pm Outdated
}
};

if ($params->{ids}) {

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.

Can we add a comment here as to why ids vs. phids vs. {}? Can you describe the cases here? Is it story types? Would help for maintenance.

- Refactored where phab_user is set and where we wrap in a DB
  transaction
- More debugging output that only displays when using -d
- Some bug fixes
Comment thread extensions/PhabBugz/lib/Feed.pm Outdated

# Skip changes done by phab-bot user
my $phab_users = get_phab_bmo_ids({ phids => [$author_phid] });
if (@$phab_users) {

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.

Do we want to avoid this if() if $skip is already flagged?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If get_phab_bmo_ids is expensive, then yes for certain.

- Fixes for Feed.pm to prevent looping
- Added code to update review flag statuses when revision is accepted/rejected.
@dklawren
dklawren requested a review from darkwing November 8, 2017 18:18
@dklawren

dklawren commented Nov 8, 2017

Copy link
Copy Markdown
Collaborator Author

How to test:

  1. git clone https://github.com/mozilla-conduit/bmo-extensions
  2. cd bmo-extensions
  3. docker-compose up --build
  4. Control-C to stop containers.
  5. docker-compose run phabricator check_database
  6. docker-compose up
  7. Set up Firefox proxy to localhost:1090 (or IP of your docker VM)
  8. From a different terminal: docker exec -it bmoextensions_bmo.test_1 su - bugzilla
  9. cd /var/www/html/bmo
  10. git pull
  11. git fetch origin
  12. git checkout -b phabbugz-feed origin/phabbugz-feed
  13. git merge master
  14. perl extensions/PhabBugz/bin/phabbugz_feed.pl -f -d start
  15. Login to http://phabricator.test using BMO auth delegation and conduit@mozilla.bugs as user. Password is password123456789!
  16. Create revisions on http://phabricator.test using bug id 1
    16 Go to http://bmo.test/show_bug.cgi?id=1 and observe attachment being created properly for the new revision.
  17. At http://bmo.test/enter_bug.cgi, create a new bug that is private to core-security.
  18. Create a new revision in Phabricator using the new bug id (likely bug id 2).
  19. Reload new revision and observe that a custom policy has been created.

Optional:

Due to code being in the Doorkeeper extension in Phabricator, you will probably see double changes being made to revisions. To stop this, you can do the following:

  1. From another terminal, docker exec -it bmoextensions_phabricator_1 /bin/sh
  2. vi /app/moz-extensions/src/doorkeeper/worker/DoorkeeperRevisionFeedWorker,php
  3. Right below "public function publishFeedStory" (line 43), add a single return; statement and save.
  4. /app/phabricator/bin/phd restart

@floatingatoll

floatingatoll commented Nov 9, 2017 via email

Copy link
Copy Markdown

@dylanwh

dylanwh commented Nov 9, 2017

Copy link
Copy Markdown
Contributor

okay, so there should be an eval the catches fatal errors and logs them.

@floatingatoll

floatingatoll commented Nov 9, 2017 via email

Copy link
Copy Markdown

@dylanwh dylanwh left a comment

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.

Okay, so we're cool that we iterate over the stories in a random order?

Comment thread extensions/PhabBugz/lib/Feed.pm Outdated
}

# Process each story
foreach my $story (keys %$transactions) {

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.

This will iterate over the stories in a random order.

@floatingatoll

floatingatoll commented Nov 9, 2017 via email

Copy link
Copy Markdown

@dylanwh

dylanwh commented Nov 9, 2017

Copy link
Copy Markdown
Contributor

@floatingatoll I don't know, I want to see how feed.query_epoch is defined.
Do table-like arrays in PHP have a defined order?

@dylanwh

dylanwh commented Nov 9, 2017

Copy link
Copy Markdown
Contributor

PHP arrays are ordered. The question is: is it okay to process the stories in a transaction in an arbitrary, random order?

@dklawren @darkwing ?

@floatingatoll

floatingatoll commented Nov 9, 2017 via email

Copy link
Copy Markdown

@dylanwh

dylanwh commented Nov 9, 2017

Copy link
Copy Markdown
Contributor

Fixed the exception handling in #269

@dylanwh

dylanwh commented Nov 9, 2017

Copy link
Copy Markdown
Contributor

Up until the data is turned into json, we get the ordering as returned by $query->execute() on the PHP side. I guess PhabricatorFeedQuery() would discuss that ordering? Or perhaps ordering is totally unimportant.

@dylanwh

dylanwh commented Nov 9, 2017

Copy link
Copy Markdown
Contributor

Comment thread extensions/PhabBugz/lib/Feed.pm Outdated
$story_text);
$self->logger->info($log_message);

my $bug = Bugzilla::Bug->new($revision->bug_id);

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.

It is possible for phabricator to initiate a writable SQL injection attack against BMO.
Two things -- the {fields} returned from phabricator should be validated, and the long form ->new({id => $revison->bug_id, cache => 1 }) should be used here

@dylanwh

dylanwh commented Nov 9, 2017

Copy link
Copy Markdown
Contributor

For the changes related to Revision.pm (only!) I have split off this bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1415993,
which will target merging into this one. @dklawren you can ignore any requested changes that impact Revision.pm

…ring and better error checking for API calls.

- Update update_project_members.pl to use Project.pm properly.

TODO:
- Use memcache inside get_phab_bmo_id() calls as the values are unlikely to change
- Update the Phabricator.pm Push connector to use Revision.pm
@dylanwh

dylanwh commented Nov 13, 2017

Copy link
Copy Markdown
Contributor
[Mon Nov 13 17:33:47 2017] ERROR: Invalid parameter '' passed to Bugzilla::User->visible_bugs: It must be
numeric.
 at Bugzilla/User.pm line 1441.
	Bugzilla::User::visible_bugs(...) called at Bugzilla/User.pm line 1420
	Bugzilla::User::can_see_bug(...) called at Bugzilla/Attachment.pm line 425
	Bugzilla::Attachment::_check_bug(...) called at Bugzilla/Object.pm line 719
	Bugzilla::Object::run_create_validators(...) called at Bugzilla/Attachment.pm line 740
	Bugzilla::Attachment::run_create_validators(...) called at Bugzilla/Attachment.pm line 711
	Bugzilla::Attachment::create(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Util.pm line 109
	Bugzilla::Extension::PhabBugz::Util::create_revision_attachment(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Feed.pm line 173
	Bugzilla::Extension::PhabBugz::Feed::process_revision_change(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Feed.pm line 104
	Bugzilla::Extension::PhabBugz::Feed::feed_query(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Feed.pm line 40
	eval {...} called at /var/www/html/bmo/extensions/PhabBugz/lib/Feed.pm line 38
	Bugzilla::Extension::PhabBugz::Feed::start(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Daemon.pm line 96
	Bugzilla::Extension::PhabBugz::Daemon::gd_run(...) called at /usr/local/share/perl5/Daemon/Generic.pm line 192
	Daemon::Generic::new(...) called at /usr/local/share/perl5/Daemon/Generic.pm line 37
	Daemon::Generic::newdaemon(...) called at /var/www/html/bmo/extensions/PhabBugz/lib/Daemon.pm line 24
	Bugzilla::Extension::PhabBugz::Daemon::start(...) called at extensions/PhabBugz/bin/phabbugz_feed.pl line 22

@dylanwh

dylanwh commented Nov 13, 2017

Copy link
Copy Markdown
Contributor

So $bug->id is not defined, as $bug is the "error bug" object.

108
109==>b	    my $attachment = Bugzilla::Attachment->create(
110 	        {
111 	            bug         => $bug,
112 	            creation_ts => $timestamp,
113 	            data        => $revision_uri,
  DB<16> v
111 	            bug         => $bug,
112 	            creation_ts => $timestamp,
113 	            data        => $revision_uri,
114 	            description => $revision_title,
115 	            filename    => 'phabricator-D' . $revision_id . '-url.txt',
116 	            ispatch     => 0,
117 	            isprivate   => 0,
118 	            mimetype    => PHAB_CONTENT_TYPE,
119 	        }
120 	    );
  DB<16> p $bug->id

@dylanwh

dylanwh commented Nov 14, 2017

Copy link
Copy Markdown
Contributor

Sometimes the feed response is:

{"error_info":"Constraint \"todump\" is not a valid constraint for this query.","error_code":"ERR-CONDUIT-CORE","result":null}

and other times it is

{
   "error_info" : null,
   "error_code" : null,
   "result" : {
      "cursor" : {
         "after" : null,
         "order" : null,
         "limit" : 100,
         "before" : null
      },
      "query" : {
         "queryKey" : "all"
      },
      "maps" : {},
      "data" : [
         {
            "attachments" : {
               "reviewers" : {
                  "reviewers" : []
               },
               "projects" : {
                  "projectPHIDs" : []
               },
               "subscribers" : {
                  "viewerIsSubscribed" : false,
                  "subscriberPHIDs" : [
                     "PHID-USER-23ia7vewbjgcqahewncu"
                  ],
                  "subscriberCount" : 1
               }
            },
            "fields" : {
               "bugzilla.bug-id" : "2",
               "dateModified" : 1500403974,
               "status" : {
                  "closed" : false,
                  "value" : "needs-review",
                  "name" : "Needs Review",
                  "color.ansi" : "magenta"
               },
               "policy" : {
                  "view" : "PHID-PLCY-5x6xprxaxhvghmddosqc",
                  "edit" : "PHID-PLCY-5x6xprxaxhvghmddosqc"
               },
               "authorPHID" : "PHID-USER-23ia7vewbjgcqahewncu",
               "title" : "test",
               "dateCreated" : 1500403552
            },
            "phid" : "PHID-DREV-nltdtsi6x2cy2gfpbgam",
            "type" : "DREV",
            "id" : 1
         }
      ]
   }
}

The first one is a bit confusing. The second one is wrong. Where is bug-id of 2 coming from?

@dklawren

Copy link
Copy Markdown
Collaborator Author

{"error_info":"Constraint "todump" is not a valid constraint for this query.","error_code":"ERR-CONDUIT-CORE","result":null}

I have not seen this personally. I do not see in the code where I try to set a 'todump' constraint. Seems a glitch in the API somewhere.

As for the bug #2 issue, as we discussed, it seems that the Phabricator DB dump that is part of the bmo-extensions repo needs to be fixed to remove the previously created revision that has 2 as the bug id. Or change generate_conduit_data.pl to create a #2 bug correctly. Not an issue for production systems.

dklawren and others added 4 commits November 16, 2017 17:07
- Store last ID in the BMO table instead of epoch and use ID for retrieving newest stories.
- Updates to different functons in Util.pm to use newer methods for getting data from phabricator.
- Moved API error checking into Util::request
@dylanwh
dylanwh dismissed darkwing’s stale review November 29, 2017 17:05

darkwing said it was okay.

@dylanwh
dylanwh merged commit 7da8e37 into master Nov 29, 2017
@dylanwh
dylanwh deleted the phabbugz-feed branch January 3, 2018 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants