Skip to content

Commit fa013fc

Browse files
authored
Bug 1985621: trigger uplift changes when hash of last form changes
1 parent c54a910 commit fa013fc

File tree

3 files changed

+81
-14
lines changed

3 files changed

+81
-14
lines changed

extensions/PhabBugz/Extension.pm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ sub db_schema_abstract_schema {
125125
user_phid => {TYPE => 'VARCHAR(255)', NOTNULL => 1,},
126126
]
127127
};
128+
$args->{'schema'}->{'phab_uplift_form_state'} = {
129+
FIELDS => [
130+
id => {TYPE => 'INTSERIAL', NOTNULL => 1, PRIMARYKEY => 1,},
131+
revision_phid => {TYPE => 'VARCHAR(255)', NOTNULL => 1,},
132+
uplift_form_hash => {TYPE => 'VARCHAR(255)', NOTNULL => 1,},
133+
]
134+
};
128135
}
129136

130137
sub install_filesystem {

extensions/PhabBugz/lib/Feed.pm

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,67 @@ sub group_query {
354354
}
355355
}
356356

357-
sub _is_uplift_request_form_change {
358-
# Return `true` if the story text is an uplift request change.
357+
sub get_last_uplift_hash {
358+
my ($revision_phid) = @_;
359+
INFO('Retrieving last uplift form hash for revision ' . $revision_phid);
360+
361+
return Bugzilla->dbh->selectrow_array(
362+
'SELECT uplift_form_hash FROM phab_uplift_form_state WHERE revision_phid = ?',
363+
undef,
364+
$revision_phid
365+
);
366+
}
367+
368+
sub update_last_uplift_hash {
369+
my ($revision_phid, $uplift_hash) = @_;
370+
my $dbh = Bugzilla->dbh;
371+
372+
INFO(
373+
'Updating last uplift hash' . $uplift_hash . ' for revision' . $revision_phid
374+
);
375+
376+
$dbh->do(
377+
'DELETE FROM phab_uplift_form_state WHERE revision_phid = ?',
378+
undef,
379+
$revision_phid
380+
);
381+
$dbh->do(
382+
'INSERT INTO phab_uplift_form_state (revision_phid, uplift_form_hash) VALUES (?, ?)',
383+
undef,
384+
$revision_phid,
385+
$uplift_hash
386+
);
387+
}
388+
389+
sub is_uplift_request_form_story_change {
390+
# Return `true` if the uplift request form has changed since last check.
359391
my ($story_text) = @_;
360392

361393
return $story_text =~ /\s+uplift request field/;
362394
}
363395

396+
sub has_uplift_request_form_changed {
397+
# Return `true` if the uplift request form has changed since last check.
398+
my ($revision) = @_;
399+
400+
# Take no action if the form is empty.
401+
if (ref $revision->uplift_request ne 'HASH'
402+
|| !keys %{$revision->uplift_request})
403+
{
404+
INFO('Uplift request form field empty, ignoring.');
405+
return;
406+
}
407+
408+
my $previous_hash = get_last_uplift_hash($revision->phid);
409+
if (!$previous_hash) {
410+
# If the form is not empty and there is no previous hash,
411+
# the form has changed.
412+
return 1;
413+
}
414+
415+
return $revision->uplift_hash ne $previous_hash;
416+
}
417+
364418
sub readable_answer {
365419
my ($answer) = @_;
366420

@@ -414,14 +468,6 @@ sub process_uplift_request_form_change {
414468
my ($timestamp) = Bugzilla->dbh->selectrow_array('SELECT NOW()');
415469
my $phab_bot_user = Bugzilla::User->new({name => PHAB_AUTOMATION_USER});
416470

417-
# Take no action if the form is empty.
418-
if (ref $revision->uplift_request ne 'HASH'
419-
|| !keys %{$revision->uplift_request})
420-
{
421-
INFO('Uplift request form field cleared, ignoring.');
422-
return;
423-
}
424-
425471
INFO('Commenting the uplift form on the bug.');
426472

427473
my $comment_content = format_uplift_request_as_markdown(
@@ -515,6 +561,7 @@ sub process_uplift_request_form_change {
515561
}
516562

517563
INFO("Finished processing uplift request form change for $revision_phid.");
564+
update_last_uplift_hash($revision->phid, $revision->uplift_hash);
518565
}
519566

520567
sub process_revision_change {
@@ -554,9 +601,13 @@ sub process_revision_change {
554601
my $restore_prev_user = set_phab_user();
555602
my $bug = $revision->bug;
556603

557-
# Change is a submission of the uplift request form.
558-
if (_is_uplift_request_form_change($story_text)) {
559-
return process_uplift_request_form_change($revision, $bug);
604+
# Process uplift request form changes if the hash has changed since phab-bot last
605+
# saw it.
606+
if (has_uplift_request_form_changed($revision)) {
607+
process_uplift_request_form_change($revision, $bug);
608+
609+
# Return early if updating from a story change.
610+
return if is_uplift_request_form_story_change($story_text);
560611
}
561612

562613
# Check to make sure bug id is valid and author can see it

extensions/PhabBugz/lib/Revision.pm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ package Bugzilla::Extension::PhabBugz::Revision;
1010
use 5.10.1;
1111
use Moo;
1212

13-
use Mojo::JSON qw(true);
13+
use Digest::SHA qw(sha1_hex);
14+
use Mojo::JSON qw(encode_json true);
1415
use Scalar::Util qw(blessed weaken);
1516
use Types::Standard -all;
1617
use Type::Utils;
@@ -321,6 +322,14 @@ sub secured_title {
321322
return $self->is_private ? '(secure)' : $self->title;
322323
}
323324

325+
sub uplift_hash {
326+
my ($self) = @_;
327+
328+
# Mojo's `encode_json` should sort keys by default.
329+
my $uplift_json_encoded = encode_json($self->uplift_request);
330+
return sha1_hex($uplift_json_encoded);
331+
}
332+
324333
#########################
325334
# Builders #
326335
#########################

0 commit comments

Comments
 (0)