Skip to content

Commit 10c373b

Browse files
author
Mitchell Hentges
authoredOct 15, 2021
Merge pull request #4 from mozilla-conduit/1734925-emails-to-watchers
Bug 1734925: Send emails to reviewer group "watchers"
2 parents a2c71e7 + 80e3c9f commit 10c373b

File tree

5 files changed

+48
-16
lines changed

5 files changed

+48
-16
lines changed
 

‎moz-extensions/src/email/adapter/GroupPhabricatorReviewer.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ class GroupPhabricatorReviewer implements PhabricatorReviewer {
55
private string $name;
66
/** @var PhabricatorUser[] */
77
private array $rawUsers;
8+
private array $rawWatcherUsers;
89

910
/**
1011
* @param string $name
1112
* @param PhabricatorUser[] $rawUsers
13+
* @param PhabricatorUser[] $rawWatcherUsers
1214
*/
13-
public function __construct(string $name, array $rawUsers) {
15+
public function __construct(string $name, array $rawUsers, array $rawWatcherUsers) {
1416
$this->name = $name;
1517
$this->rawUsers = $rawUsers;
18+
$this->rawWatcherUsers = $rawWatcherUsers;
1619
}
1720

1821
public function name(): string {
@@ -24,4 +27,11 @@ public function toRecipients(string $actorEmail): array {
2427
return EmailRecipient::from($user, $actorEmail);
2528
}, $this->rawUsers)));
2629
}
27-
}
30+
31+
public function getWatchersAsRecipients(string $actorEmail): array
32+
{
33+
return array_values(array_filter(array_map(function($user) use ($actorEmail) {
34+
return EmailRecipient::from($user, $actorEmail);
35+
}, $this->rawWatcherUsers)));
36+
}
37+
}

‎moz-extensions/src/email/adapter/PhabricatorReviewer.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ public function name(): string;
88
* @return EmailRecipient[]
99
*/
1010
public function toRecipients(string $actorEmail): array;
11-
}
11+
12+
/**
13+
* @return EmailRecipient[]
14+
*/
15+
public function getWatchersAsRecipients(string $actorEmail): array;
16+
}

‎moz-extensions/src/email/adapter/PhabricatorUserStore.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ public function findReviewerByPHID(string $PHID): PhabricatorReviewer {
6262
// This is a group reviewer
6363
$project = self::fetchProject($PHID);
6464
$users = array_values($this->queryAll($project->getMemberPHIDs()));
65-
return new GroupPhabricatorReviewer($project->getDisplayName(), $users);
65+
$watchers = array_values($this->queryAll($project->getWatcherPHIDs()));
66+
return new GroupPhabricatorReviewer($project->getDisplayName(), $users, $watchers);
6667
} else if (substr($PHID, 0, strlen('PHID-OPKG')) == 'PHID-OPKG') {
6768
// This is a "code owner" reviewer
6869
$package = self::fetchPackage($PHID);
6970
$users = array_values($this->queryAll($package->getOwnerPHIDs()));
70-
return new GroupPhabricatorReviewer($package->getName(), $users);
71+
return new GroupPhabricatorReviewer($package->getName(), $users, []);
7172
} else {
7273
// PHID type must be "PHID-USER".
7374
// So, this is a single user reviewer.
@@ -95,6 +96,7 @@ private static function fetchProject(string $PHID): PhabricatorProject {
9596
return (new PhabricatorProjectQuery())
9697
->setViewer(PhabricatorUser::getOmnipotentUser())
9798
->needMembers(true)
99+
->needWatchers(true)
98100
->withPHIDs([$PHID])
99101
->executeOne();
100102
}
@@ -105,4 +107,4 @@ private static function fetchPackage(string $PHID): PhabricatorOwnersPackage {
105107
->withPHIDs([$PHID])
106108
->executeOne();
107109
}
108-
}
110+
}

‎moz-extensions/src/email/adapter/UserPhabricatorReviewer.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ public function name(): string {
1515
public function toRecipients(string $actorEmail): array {
1616
return array_filter([EmailRecipient::from($this->rawUser, $actorEmail)]);
1717
}
18-
}
18+
19+
public function getWatchersAsRecipients(string $actorEmail): array
20+
{
21+
return [];
22+
}
23+
}

‎moz-extensions/src/email/resolve/ResolveUsers.php

+19-9
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,9 @@ public function resolveReviewersAsRecipients(): array {
4646
* @return EmailReviewer[]
4747
*/
4848
public function resolveReviewers(bool $revisionChangedToNeedReview): array {
49-
$rawReviewers = $this->rawRevision->getReviewers();
49+
$rawReviewers = $this->resolveUnresignedReviewers();
5050
$reviewers = [];
5151
foreach ($rawReviewers as $reviewerPHID => $rawReviewer) {
52-
if ($rawReviewer->isResigned()) {
53-
// In the future, we could show resigned reviewers in the email body
54-
continue;
55-
}
56-
5752
$allReviewerStatuses = array_map(function($reviewer) {
5853
return $reviewer->getReviewerStatus();
5954
}, $rawReviewers);
@@ -64,18 +59,22 @@ public function resolveReviewers(bool $revisionChangedToNeedReview): array {
6459
$reviewers[] = new EmailReviewer($reviewer->name(), $isActionable, $status, $reviewer->toRecipients($this->actorEmail));
6560
}
6661
return $reviewers;
67-
6862
}
6963

7064
/**
7165
* @return EmailRecipient[]
7266
*/
7367
public function resolveSubscribersAsRecipients(): array {
7468
$recipientPHIDs = PhabricatorSubscribersQuery::loadSubscribersForPHID($this->rawRevision->getPHID());
75-
7669
$recipientUsers = array_map(fn (string $phid) => $this->userStore->findAllBySubscribersById($phid), $recipientPHIDs);
7770
$recipientUsers = array_merge(...$recipientUsers); // Flatten array of arrays
7871
$recipients = array_map(fn (PhabricatorUser $user) => EmailRecipient::from($user, $this->actorEmail), $recipientUsers);
72+
73+
foreach (array_keys($this->resolveUnresignedReviewers()) as $reviewerPHID) {
74+
$reviewer = $this->userStore->findReviewerByPHID($reviewerPHID);
75+
$recipients = array_merge($recipients, $reviewer->getWatchersAsRecipients($this->actorEmail));
76+
}
77+
7978
return array_values(array_filter($recipients));
8079
}
8180

@@ -95,4 +94,15 @@ public function resolveAllPossibleRecipients(): array
9594

9695
return array_merge($subscribers, $reviewers, $authorList);
9796
}
98-
}
97+
98+
/**
99+
* @return Phabricatoruser[]
100+
*/
101+
private function resolveUnresignedReviewers(): array {
102+
$reviewers = $this->rawRevision->getReviewers();
103+
return array_filter($reviewers, function($rawReviewer) {
104+
// In the future, we could show resigned reviewers in the email body
105+
return !$rawReviewer->isResigned();
106+
});
107+
}
108+
}

0 commit comments

Comments
 (0)
Failed to load comments.