Skip to content
22 changes: 16 additions & 6 deletions app_dart/lib/src/request_handlers/github/webhook_subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,14 @@ final class GithubWebhookSubscription extends SubscriptionHandler {
final result = await _processPullRequestClosed(pullRequestEvent);
return result.toResponse();
case 'edited':
await _addCICDForRollers(pullRequestEvent);
if (pullRequestEvent.changes != null &&
pullRequestEvent.changes!.base != null) {
await _addCICDForRollersAndMembers(pullRequestEvent);
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a slight behavior change, I made it this way because before the path for scheduling tests checked these conditions:
363e9cd#diff-4d5f5e21f5b7eda8e7775b9a678fb60bb6cfba3321799948fb2b9a55cefbe439L202-L206
And I figured that that was also true for the roller case, so it was hopefully fine to apply to the roller in addition to the member case. Let me know if not

}
await _checkForTests(pullRequestEvent);
break;
case 'opened':
await _addCICDForRollers(pullRequestEvent);
await _addCICDForRollersAndMembers(pullRequestEvent);
await _checkForTests(pullRequestEvent);
await _tryReleaseApproval(pullRequestEvent);
break;
Expand Down Expand Up @@ -232,8 +235,10 @@ final class GithubWebhookSubscription extends SubscriptionHandler {
case 'dequeued':
await _respondToPullRequestDequeued(pullRequestEvent);
break;
// Ignore the rest of the events.
case 'synchronize':
await _addCICDForRollersAndMembers(pullRequestEvent);
break;
// Ignore the rest of the events.
case 'ready_for_review':
case 'unlabeled':
case 'assigned':
Expand Down Expand Up @@ -569,12 +574,17 @@ final class GithubWebhookSubscription extends SubscriptionHandler {
);
}

Future<void> _addCICDForRollers(PullRequestEvent pullRequestEvent) async {
Future<void> _addCICDForRollersAndMembers(
PullRequestEvent pullRequestEvent,
) async {
final pr = pullRequestEvent.pullRequest!;
final slug = pr.base!.repo!.slug();

if (config.rollerAccounts.contains(pr.user!.login) &&
config.supportedRepos.contains(slug)) {
final isRoller = config.rollerAccounts.contains(pr.user!.login);
final association = pr.authorAssociation;
final isOrgMember = association == 'MEMBER' || association == 'OWNER';

if (config.supportedRepos.contains(slug) && (isRoller || isOrgMember)) {
final gitHubClient = await config.createGitHubClient(pullRequest: pr);
await gitHubClient.issues.addLabelsToIssue(slug, pr.number!, ['CICD']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ void main() {
number: issueNumber,
baseRef: 'master',
slug: Config.packagesSlug,
isOrgMember: false,
);

tester.message = pushMessage;
Expand Down Expand Up @@ -459,6 +460,7 @@ void main() {
baseRef: 'master',
slug: Config.packagesSlug,
includeChanges: true,
isOrgMember: false,
);

tester.message = pushMessage;
Expand Down Expand Up @@ -531,6 +533,7 @@ void main() {
action: 'opened',
number: issueNumber,
isDraft: true,
isOrgMember: false,
);

var batchRequestCalled = false;
Expand Down Expand Up @@ -935,6 +938,7 @@ void main() {
const issueNumber = 123;

tester.message = generateGithubWebhookMessage(
isOrgMember: false,
action: 'opened',
number: issueNumber,
);
Expand Down Expand Up @@ -1064,6 +1068,7 @@ void main() {
const issueNumber = 123;

tester.message = generateGithubWebhookMessage(
isOrgMember: false,
action: 'opened',
number: issueNumber,
);
Expand Down Expand Up @@ -1691,6 +1696,7 @@ void foo() {
const issueNumber = 123;

tester.message = generateGithubWebhookMessage(
isOrgMember: false,
action: 'opened',
number: issueNumber,
baseRef: kReleaseBaseRef,
Expand Down Expand Up @@ -1829,6 +1835,7 @@ void foo() {
const issueNumber = 123;

tester.message = generateGithubWebhookMessage(
isOrgMember: false,
action: 'opened',
number: issueNumber,
baseRef: 'master',
Expand Down Expand Up @@ -1865,6 +1872,7 @@ void foo() {
const issueNumber = 123;

tester.message = generateGithubWebhookMessage(
isOrgMember: false,
action: 'opened',
number: issueNumber,
baseRef: 'master',
Expand Down Expand Up @@ -1901,6 +1909,7 @@ void foo() {
const issueNumber = 123;

tester.message = generateGithubWebhookMessage(
isOrgMember: false,
action: 'opened',
number: issueNumber,
baseRef: 'master',
Expand Down Expand Up @@ -2331,6 +2340,7 @@ void foo() {
const issueNumber = 123;

tester.message = generateGithubWebhookMessage(
isOrgMember: false,
action: 'opened',
number: issueNumber,
baseRef: kReleaseBaseRef,
Expand Down Expand Up @@ -2553,6 +2563,7 @@ void foo() {
const issueNumber = 123;

tester.message = generateGithubWebhookMessage(
isOrgMember: false,
action: 'opened',
number: issueNumber,
);
Expand Down Expand Up @@ -2667,6 +2678,7 @@ void foo() {
tester.message = generateGithubWebhookMessage(
action: 'synchronize',
number: issueNumber,
isOrgMember: false,
);

final mockRepositoriesService = MockRepositoriesService();
Expand Down Expand Up @@ -3712,6 +3724,24 @@ void foo() {
expect(scheduler.triggerPresubmitTargetsCnt, 0);
});

test('opened PR with adds CICD label if author is MEMBER', () async {
tester.message = generateGithubWebhookMessage(action: 'opened');

await tester.post(webhook);

verify(issuesService.addLabelsToIssue(Config.flutterSlug, 123, ['CICD']));
});
test('opened PR does not add CICD label if author is not MEMBER', () async {
tester.message = generateGithubWebhookMessage(
action: 'opened',
isOrgMember: false,
);

await tester.post(webhook);

verifyNever(issuesService.addLabelsToIssue(Config.flutterSlug, 123, any));
});

test(
'labeled event with CICD label schedules tests on flutter/flutter',
() async {
Expand Down Expand Up @@ -3796,11 +3826,36 @@ void foo() {
tester.message = generateGithubWebhookMessage(
action: 'synchronize',
withCicdLabel: true,
isOrgMember: false,
);

await tester.post(webhook);
expect(scheduler.triggerPresubmitTargetsCnt, 0);
},
);

test('synchronize event adds CICD label if author is member', () async {
tester.message = generateGithubWebhookMessage(
action: 'synchronize',
isOrgMember: true,
);

await tester.post(webhook);
verify(issuesService.addLabelsToIssue(Config.flutterSlug, 123, ['CICD']));
});
test(
'synchronize event does not add CICD label if author is not member',
() async {
tester.message = generateGithubWebhookMessage(
action: 'synchronize',
isOrgMember: false,
);

await tester.post(webhook);
verifyNever(
issuesService.addLabelsToIssue(Config.flutterSlug, 123, any),
);
},
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ PushMessage generateGithubWebhookMessage({
bool withAutosubmit = false,
bool withRevertOf = false,
bool withCicdLabel = false,
bool isOrgMember = true,
DateTime? closedAt,
Iterable<String> additionalLabels = const [],
Map<String, Object?>? labeledLabel,
Expand All @@ -54,6 +55,7 @@ PushMessage generateGithubWebhookMessage({
withAutosubmit: withAutosubmit,
withRevertOf: withRevertOf,
withCicdLabel: withCicdLabel,
isOrgMember: isOrgMember,
closedAt: closedAt,
additionalLabels: additionalLabels,
labeledLabel: labeledLabel,
Expand Down Expand Up @@ -81,6 +83,7 @@ String _generatePullRequestEvent(
required bool withAutosubmit,
required bool withRevertOf,
bool withCicdLabel = false,
bool isOrgMember = true,
Iterable<String> additionalLabels = const [],
Map<String, Object?>? labeledLabel,
}) {
Expand Down Expand Up @@ -463,7 +466,7 @@ String _generatePullRequestEvent(
"href": "https://api.github.com/repos/${slug.fullName}/statuses/deadbeef"
}
},
"author_association": "MEMBER",
"author_association": "${isOrgMember ? 'MEMBER' : 'CONTRIBUTOR'}",
"draft" : $isDraft,
"merged": $merged,
"mergeable": $isMergeable,
Expand Down
Loading