Skip to content

Commit

Permalink
Bug 1825961: set approval-mozilla-{repo} flags on uplift revision a…
Browse files Browse the repository at this point in the history
…ttachments (mozilla-bteam#2054)

When a new Phabricator revision attachment is being added to a bug,
check if the repo associated with the revision is an uplift repo and
set the appropriate `approval-mozilla-{repo}` flag if necessary.

To determine if a repo is an uplift repository, we add the `projects`
attachment from Phabricator to the Conudit API query which returns
repository data. We add a `projects_raw` and `projects` field which
is the raw API response and an array of built `Project` objects in
Bugzilla, respectively. Then we add an `is_uplift_repo` function which
iterates over the `project` attribute and checks for a project with
the name matching `uplift`.
  • Loading branch information
cgsheeh authored Apr 26, 2023
1 parent 3066e4b commit 33b34d5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
45 changes: 43 additions & 2 deletions extensions/PhabBugz/lib/Repository.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use Moo;
use Types::Standard -all;

use Bugzilla::Util qw(trim);
use Bugzilla::Extension::PhabBugz::Project;
use Bugzilla::Extension::PhabBugz::Types qw(:types);
use Bugzilla::Extension::PhabBugz::Util qw(request);

#########################
Expand All @@ -33,12 +35,14 @@ has modification_ts => (is => 'ro', isa => Str);
has view_policy => (is => 'ro', isa => Str);
has edit_policy => (is => 'ro', isa => Str);
has push_policy => (is => 'ro', isa => Str);
has projects_raw => (is => 'ro', isa => Dict [projectPHIDs => ArrayRef [Str]]);
has projects => (is => 'lazy', isa => ArrayRef [Project]);

sub new_from_query {
my ($class, $params) = @_;

my $data
= {queryKey => 'all', constraints => $params};
= {queryKey => 'all', constraints => $params, attachments => {projects => 1}};

my $result = request('diffusion.repository.search', $data);

Expand All @@ -49,6 +53,19 @@ sub new_from_query {
return undef;
}

sub is_uplift_repo {
# Return a boolean indicating if this repository is an uplift repo.
my ($self) = @_;

foreach my $project (@{$self->projects}) {
if ($project->name eq 'uplift') {
return 1;
}
}

return 0;
}

sub BUILDARGS {
my ($class, $params) = @_;

Expand All @@ -64,6 +81,7 @@ sub BUILDARGS {
$params->{view_policy} = $params->{fields}->{policy}->{view};
$params->{edit_policy} = $params->{fields}->{policy}->{edit};
$params->{push_policy} = $params->{fields}->{policy}->{'diffusion.push'};
$params->{projects_raw} = $params->{attachments}->{projects};

delete $params->{attachments};
delete $params->{fields};
Expand Down Expand Up @@ -103,7 +121,11 @@ sub BUILDARGS {
# "diffusion.push": "no-one"
# }
# },
# "attachments": {}
# "attachments": {
# "projectsPHIDs": [
# "PHID-PROJ-pioonhc34mzisujjvyvd"
# ]
# }
# }
# ],
# "maps": {},
Expand All @@ -118,4 +140,23 @@ sub BUILDARGS {
# }
# }

############
# Builders #
############

sub _build_projects {
my ($self) = @_;

return $self->{projects} if $self->{projects};
return [] unless $self->projects_raw->{projectPHIDs};

my @projects;
foreach my $phid (@{$self->projects_raw->{projectPHIDs}}) {
push @projects,
Bugzilla::Extension::PhabBugz::Project->new_from_query({phids => [$phid]});
}

return $self->{projects} = \@projects;
}

1;
45 changes: 45 additions & 0 deletions extensions/PhabBugz/lib/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,45 @@ our @EXPORT = qw(
set_phab_user
);

# Set approval flags on Phabricator revision bug attachments.
sub set_attachment_approval_flags {
my ($attachment, $revision, $user, $timestamp) = @_;

# The repo short name is the appropriate value that aligns with flag names.
my $repo_name = $revision->repository->short_name;
my $approval_flag_name = "approval-mozilla-$repo_name";
INFO("Setting $approval_flag_name flag on revision attachment.");

my @old_flags;
my @new_flags;

# Find the current approval flag state if it exists.
foreach my $flag (@{$attachment->flags}) {
# Ignore for all flags except the approval flag.
next if $flag->name ne $approval_flag_name;

# Set the flag to `?`. If already '?', it will be a non-change.
INFO("Set existing `$approval_flag_name` flag to `?`.");
push @old_flags, {id => $flag->id, status => '?'};
last;
}

# If we didn't find an existing approval flag to update, add it now.
if (!@old_flags) {
my $approval_flag = Bugzilla::FlagType->new({name => $approval_flag_name});
if ($approval_flag) {
push @new_flags, {
setter => $user,
status => '?',
type_id => $approval_flag->id,
};
}
}

$attachment->set_flags(\@old_flags, \@new_flags);
$attachment->update($timestamp);
}

sub create_revision_attachment {
state $check = compile(Bug, Revision, Str, User);
my ($bug, $revision, $timestamp, $submitter) = $check->(@_);
Expand All @@ -58,6 +97,7 @@ sub create_revision_attachment {
my $attachment
= first { trim($_->data) eq $revision_uri } @review_attachments;


if (!defined $attachment) {
# No attachment is present, so we can now create new one

Expand All @@ -80,6 +120,11 @@ sub create_revision_attachment {
mimetype => PHAB_CONTENT_TYPE,
});

# When the revision belongs to an uplift repo, set appropriate approval flags.
if ($revision->repository && $revision->repository->is_uplift_repo()) {
set_attachment_approval_flags($attachment, $revision, $submitter, $timestamp);
}

# Insert a comment about the new attachment into the database.
$bug->add_comment(
$revision->summary,
Expand Down

0 comments on commit 33b34d5

Please sign in to comment.