diff --git a/app_dart/lib/src/service/want_lgtm/want_lgtm_evaluator.dart b/app_dart/lib/src/service/want_lgtm/want_lgtm_evaluator.dart new file mode 100644 index 000000000..ba9774d1f --- /dev/null +++ b/app_dart/lib/src/service/want_lgtm/want_lgtm_evaluator.dart @@ -0,0 +1,279 @@ +// Copyright 2026 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'want_lgtm_models.dart'; + +const int _kMaxTitleLength = 255; + +String _clampTitleLength(String title) { + if (title.length <= _kMaxTitleLength) { + return title; + } + return '${title.substring(0, _kMaxTitleLength - 3)}...'; +} + +/// Evaluates a PR's review status against a parsed `WANT_LGTM=` directive. +WantLgtmVerdict evaluateWantLgtm({ + required ParsedWantLgtmDirective directive, + required WantLgtmGraphQLSnapshot snapshot, + required Set rollerAccounts, +}) { + return switch (directive) { + NoWantLgtmDirective() => const WantLgtmSatisfied( + approvedBy: [], + latestApprovalAt: null, + title: 'WANT_LGTM satisfied', + summary: 'No WANT_LGTM directive specified.', + ), + InvalidWantLgtmDirective(:final invalidTokens) => WantLgtmInvalid( + invalidTokens: invalidTokens, + title: _clampTitleLength( + 'Invalid WANT_LGTM directive: ${invalidTokens.join(', ')}', + ), + summary: + 'The following tokens in WANT_LGTM= are invalid GitHub logins: ' + '${invalidTokens.join(', ')}. Note that GitHub teams (@org/team) are not supported.', + ), + ValidWantLgtmDirective(:final requiresAll, :final explicitLogins) => + _evaluateValidDirective( + requiresAll: requiresAll, + explicitLogins: explicitLogins, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ), + }; +} + +/// Computes the cohort of reviewers requested via GitHub sidebar / timeline. +Set _resolveAllCohort( + WantLgtmGraphQLSnapshot snapshot, + Set rollerAccounts, +) { + final normalizedAuthor = snapshot.authorLogin.trim().toLowerCase(); + final normalizedRollers = { + for (final s in rollerAccounts) s.trim().toLowerCase(), + }; + + final timelineRequested = {}; + for (final event in snapshot.timelineEvents) { + final login = event.reviewerLogin.toLowerCase(); + switch (event.action) { + case TimelineReviewAction.requested: + timelineRequested.add(login); + case TimelineReviewAction.removed: + timelineRequested.remove(login); + } + } + + return { + for (final s in snapshot.openReviewRequestLogins) s.toLowerCase(), + ...timelineRequested, + }..removeWhere( + (login) => login == normalizedAuthor || normalizedRollers.contains(login), + ); +} + +/// Evaluates review status across all required users. +({ + List approvedBy, + List waitingOn, + List changesRequestedBy, + DateTime? latestApprovalAt, +}) +_evaluateUsers(Set requiredUsers, WantLgtmGraphQLSnapshot snapshot) { + final approvedBy = []; + final waitingOn = []; + final changesRequestedBy = []; + DateTime? latestApprovalAt; + + final sortedUsers = requiredUsers.toList()..sort(); + for (final user in sortedUsers) { + final status = _determineUserReviewStatus(user, snapshot); + switch (status) { + case _UserReviewStatus.approved: + approvedBy.add(user); + final userApprovalAt = _findLatestApprovalTime(user, snapshot.reviews); + if (userApprovalAt != null) { + if (latestApprovalAt == null || + userApprovalAt.isAfter(latestApprovalAt)) { + latestApprovalAt = userApprovalAt; + } + } + case _UserReviewStatus.changesRequested: + changesRequestedBy.add(user); + case _UserReviewStatus.waiting: + waitingOn.add(user); + } + } + + return ( + approvedBy: approvedBy, + waitingOn: waitingOn, + changesRequestedBy: changesRequestedBy, + latestApprovalAt: latestApprovalAt, + ); +} + +/// Builds the final verdict from categorized reviewer groups. +WantLgtmVerdict _buildVerdict({ + required List approvedBy, + required List waitingOn, + required List changesRequestedBy, + required DateTime? latestApprovalAt, +}) { + if (changesRequestedBy.isNotEmpty || waitingOn.isNotEmpty) { + final title = changesRequestedBy.isNotEmpty + ? '${changesRequestedBy.join(', ')} requested changes' + : 'Waiting on ${waitingOn.join(', ')}'; + + final summaryBuffer = StringBuffer('### WANT_LGTM Pending\n\n'); + if (changesRequestedBy.isNotEmpty) { + summaryBuffer.writeln( + '- **Changes requested by**: ${changesRequestedBy.join(', ')}', + ); + } + if (waitingOn.isNotEmpty) { + summaryBuffer.writeln('- **Waiting on**: ${waitingOn.join(', ')}'); + } + if (approvedBy.isNotEmpty) { + summaryBuffer.writeln('- **Approved by**: ${approvedBy.join(', ')}'); + } + + return WantLgtmPending( + waitingOn: waitingOn, + changesRequestedBy: changesRequestedBy, + title: _clampTitleLength(title), + summary: summaryBuffer.toString().trim(), + ); + } + + return WantLgtmSatisfied( + approvedBy: approvedBy, + latestApprovalAt: latestApprovalAt, + title: _clampTitleLength('Approved by ${approvedBy.join(', ')}'), + summary: 'All required reviewers have approved: ${approvedBy.join(', ')}', + ); +} + +WantLgtmVerdict _evaluateValidDirective({ + required bool requiresAll, + required List explicitLogins, + required WantLgtmGraphQLSnapshot snapshot, + required Set rollerAccounts, +}) { + final requiredUsers = { + for (final s in explicitLogins) s.toLowerCase(), + }; + + if (requiresAll) { + requiredUsers.addAll(_resolveAllCohort(snapshot, rollerAccounts)); + if (requiredUsers.isEmpty) { + return const WantLgtmPending( + waitingOn: [], + changesRequestedBy: [], + title: 'No reviewers requested', + summary: 'No reviewers have been requested on GitHub.', + ); + } + } + + final (:approvedBy, :waitingOn, :changesRequestedBy, :latestApprovalAt) = + _evaluateUsers(requiredUsers, snapshot); + + return _buildVerdict( + approvedBy: approvedBy, + waitingOn: waitingOn, + changesRequestedBy: changesRequestedBy, + latestApprovalAt: latestApprovalAt, + ); +} + +enum _UserReviewStatus { approved, changesRequested, waiting } + +DateTime? _findLatestApprovalTime( + String user, + List reviews, +) { + for (var i = reviews.length - 1; i >= 0; i--) { + final review = reviews[i]; + if (review.authorLogin.toLowerCase() == user && + review.state == WantLgtmReviewState.approved) { + return review.submittedAt; + } + } + return null; +} + +WantLgtmReviewRecord? _findLatestReview( + String user, + List reviews, +) { + for (var i = reviews.length - 1; i >= 0; i--) { + final review = reviews[i]; + if (review.authorLogin.toLowerCase() == user) { + return review; + } + } + return null; +} + +({bool isRequested, DateTime? requestedAt}) _findUnremovedTimelineRequest( + String user, + List timelineEvents, +) { + var isRequested = false; + DateTime? latestRequestTime; + + for (final event in timelineEvents) { + if (event.reviewerLogin.toLowerCase() != user) { + continue; + } + switch (event.action) { + case TimelineReviewAction.requested: + isRequested = true; + latestRequestTime = event.createdAt; + case TimelineReviewAction.removed: + isRequested = false; + latestRequestTime = null; + } + } + + return (isRequested: isRequested, requestedAt: latestRequestTime); +} + +_UserReviewStatus _determineUserReviewStatus( + String user, + WantLgtmGraphQLSnapshot snapshot, +) { + if (snapshot.openReviewRequestLogins.any( + (login) => login.toLowerCase() == user, + )) { + return _UserReviewStatus.waiting; + } + + final latestReview = _findLatestReview(user, snapshot.reviews); + if (latestReview == null || + latestReview.state == WantLgtmReviewState.dismissed) { + return _UserReviewStatus.waiting; + } + + final (:isRequested, :requestedAt) = _findUnremovedTimelineRequest( + user, + snapshot.timelineEvents, + ); + if (isRequested) { + final reviewTime = latestReview.submittedAt; + if (reviewTime == null || + requestedAt == null || + requestedAt.isAfter(reviewTime)) { + return _UserReviewStatus.waiting; + } + } + + if (latestReview.state == WantLgtmReviewState.changesRequested) { + return _UserReviewStatus.changesRequested; + } + + return _UserReviewStatus.approved; +} diff --git a/app_dart/test/service/want_lgtm/want_lgtm_evaluator_test.dart b/app_dart/test/service/want_lgtm/want_lgtm_evaluator_test.dart new file mode 100644 index 000000000..871f9600e --- /dev/null +++ b/app_dart/test/service/want_lgtm/want_lgtm_evaluator_test.dart @@ -0,0 +1,506 @@ +// Copyright 2026 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:cocoon_server_test/test_logging.dart'; +import 'package:cocoon_service/src/service/want_lgtm/want_lgtm_evaluator.dart'; +import 'package:cocoon_service/src/service/want_lgtm/want_lgtm_models.dart'; +import 'package:test/test.dart'; + +void main() { + useTestLoggerPerTest(); + + final rollerAccounts = {'skia-flutter-autoroll', 'engine-flutter-autoroll'}; + + WantLgtmGraphQLSnapshot makeSnapshot({ + String authorLogin = 'author', + Set openReviewRequestLogins = const {}, + List reviews = const [], + List timelineEvents = const [], + }) { + return WantLgtmGraphQLSnapshot( + prState: 'OPEN', + body: 'PR body', + headRefOid: 'abcdef123456', + authorLogin: authorLogin, + openReviewRequestLogins: openReviewRequestLogins, + reviews: reviews, + timelineEvents: timelineEvents, + queryCount: 1, + ); + } + + group('evaluateWantLgtm - RFC 610.0001 Section 3.3 Truth Table', () { + test('Row 1: alice,bob | alice APPROVED -> pending (Waiting on bob)', () { + const directive = ValidWantLgtmDirective( + requiresAll: false, + explicitLogins: ['alice', 'bob'], + ); + final snapshot = makeSnapshot( + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'alice', + state: WantLgtmReviewState.approved, + submittedAt: DateTime.utc(2026, 1, 1, 10, 0), + ), + ], + ); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + final pending = verdict as WantLgtmPending; + expect(pending.waitingOn, ['bob']); + expect(pending.changesRequestedBy, isEmpty); + expect(pending.title, 'Waiting on bob'); + }); + + test( + 'Row 2: alice | alice APPROVED, then CHANGES_REQUESTED -> pending (alice requested changes)', + () { + const directive = ValidWantLgtmDirective( + requiresAll: false, + explicitLogins: ['alice'], + ); + final snapshot = makeSnapshot( + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'alice', + state: WantLgtmReviewState.approved, + submittedAt: DateTime.utc(2026, 1, 1, 10, 0), + ), + WantLgtmReviewRecord( + authorLogin: 'alice', + state: WantLgtmReviewState.changesRequested, + submittedAt: DateTime.utc(2026, 1, 1, 11, 0), + ), + ], + ); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + final pending = verdict as WantLgtmPending; + expect(pending.changesRequestedBy, ['alice']); + expect(pending.title, 'alice requested changes'); + }, + ); + + test('Row 3: alice | alice APPROVED, then COMMENTED -> satisfied', () { + const directive = ValidWantLgtmDirective( + requiresAll: false, + explicitLogins: ['alice'], + ); + // Non-comment reviews are stored in snapshot; comments are not in reviews list. + final snapshot = makeSnapshot( + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'alice', + state: WantLgtmReviewState.approved, + submittedAt: DateTime.utc(2026, 1, 1, 10, 0), + ), + ], + ); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + final satisfied = verdict as WantLgtmSatisfied; + expect(satisfied.approvedBy, ['alice']); + expect(satisfied.latestApprovalAt, DateTime.utc(2026, 1, 1, 10, 0)); + expect(satisfied.title, 'Approved by alice'); + }); + + test( + 'Row 4: alice | alice APPROVED, then re-requested -> pending (Waiting on alice)', + () { + const directive = ValidWantLgtmDirective( + requiresAll: false, + explicitLogins: ['alice'], + ); + final snapshot = makeSnapshot( + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'alice', + state: WantLgtmReviewState.approved, + submittedAt: DateTime.utc(2026, 1, 1, 10, 0), + ), + ], + timelineEvents: [ + WantLgtmTimelineEvent( + reviewerLogin: 'alice', + action: TimelineReviewAction.requested, + createdAt: DateTime.utc(2026, 1, 1, 11, 0), + ), + ], + ); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + final pending = verdict as WantLgtmPending; + expect(pending.waitingOn, ['alice']); + expect(pending.title, 'Waiting on alice'); + }, + ); + + test( + 'Row 5: alice | alice never requested, drive-by APPROVED -> satisfied', + () { + const directive = ValidWantLgtmDirective( + requiresAll: false, + explicitLogins: ['alice'], + ); + final snapshot = makeSnapshot( + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'alice', + state: WantLgtmReviewState.approved, + submittedAt: DateTime.utc(2026, 1, 1, 10, 0), + ), + ], + ); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + final satisfied = verdict as WantLgtmSatisfied; + expect(satisfied.approvedBy, ['alice']); + expect(satisfied.title, 'Approved by alice'); + }, + ); + + test( + 'Row 6: all | alice & bob requested; alice APPROVED; bob COMMENTED -> pending (Waiting on bob)', + () { + const directive = ValidWantLgtmDirective( + requiresAll: true, + explicitLogins: [], + ); + // When bob commented, GitHub dropped him from open requests, but timeline records his request. + final snapshot = makeSnapshot( + openReviewRequestLogins: {}, + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'alice', + state: WantLgtmReviewState.approved, + submittedAt: DateTime.utc(2026, 1, 1, 10, 0), + ), + ], + timelineEvents: [ + WantLgtmTimelineEvent( + reviewerLogin: 'alice', + action: TimelineReviewAction.requested, + createdAt: DateTime.utc(2026, 1, 1, 9, 0), + ), + WantLgtmTimelineEvent( + reviewerLogin: 'bob', + action: TimelineReviewAction.requested, + createdAt: DateTime.utc(2026, 1, 1, 9, 1), + ), + ], + ); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + final pending = verdict as WantLgtmPending; + expect(pending.waitingOn, ['bob']); + expect(pending.title, 'Waiting on bob'); + }, + ); + + test( + 'Row 7: all | alice requested then removed; bob requested & APPROVED -> satisfied', + () { + const directive = ValidWantLgtmDirective( + requiresAll: true, + explicitLogins: [], + ); + final snapshot = makeSnapshot( + openReviewRequestLogins: {}, + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'bob', + state: WantLgtmReviewState.approved, + submittedAt: DateTime.utc(2026, 1, 1, 10, 0), + ), + ], + timelineEvents: [ + WantLgtmTimelineEvent( + reviewerLogin: 'alice', + action: TimelineReviewAction.requested, + createdAt: DateTime.utc(2026, 1, 1, 9, 0), + ), + WantLgtmTimelineEvent( + reviewerLogin: 'bob', + action: TimelineReviewAction.requested, + createdAt: DateTime.utc(2026, 1, 1, 9, 5), + ), + WantLgtmTimelineEvent( + reviewerLogin: 'alice', + action: TimelineReviewAction.removed, + createdAt: DateTime.utc(2026, 1, 1, 9, 10), + ), + ], + ); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + final satisfied = verdict as WantLgtmSatisfied; + expect(satisfied.approvedBy, ['bob']); + expect(satisfied.title, 'Approved by bob'); + }, + ); + + test( + 'Row 8: all | no reviewers requested -> pending (No reviewers requested)', + () { + const directive = ValidWantLgtmDirective( + requiresAll: true, + explicitLogins: [], + ); + final snapshot = makeSnapshot( + openReviewRequestLogins: {}, + reviews: [], + timelineEvents: [], + ); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + final pending = verdict as WantLgtmPending; + expect(pending.title, 'No reviewers requested'); + expect(pending.waitingOn, isEmpty); + expect(pending.changesRequestedBy, isEmpty); + }, + ); + + test( + 'Row 9: all,carol | no sidebar reviewers requested; carol APPROVED -> satisfied', + () { + const directive = ValidWantLgtmDirective( + requiresAll: true, + explicitLogins: ['carol'], + ); + final snapshot = makeSnapshot( + openReviewRequestLogins: {}, + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'carol', + state: WantLgtmReviewState.approved, + submittedAt: DateTime.utc(2026, 1, 1, 10, 0), + ), + ], + timelineEvents: [], + ); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + final satisfied = verdict as WantLgtmSatisfied; + expect(satisfied.approvedBy, ['carol']); + expect(satisfied.title, 'Approved by carol'); + }, + ); + + test('Row 10: invalid directive returns WantLgtmInvalid', () { + const directive = InvalidWantLgtmDirective( + invalidTokens: ['@flutter/engine'], + ); + final snapshot = makeSnapshot(); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + final invalid = verdict as WantLgtmInvalid; + expect(invalid.invalidTokens, ['@flutter/engine']); + expect(invalid.title, contains('Invalid WANT_LGTM directive')); + }); + }); + + group('evaluateWantLgtm - Special cases and exclusions', () { + test('excludes PR author and roller accounts from all cohort', () { + const directive = ValidWantLgtmDirective( + requiresAll: true, + explicitLogins: [], + ); + final snapshot = makeSnapshot( + authorLogin: 'pr-author', + openReviewRequestLogins: {'pr-author', 'skia-flutter-autoroll'}, + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'alice', + state: WantLgtmReviewState.approved, + submittedAt: DateTime.utc(2026, 1, 1, 10, 0), + ), + ], + timelineEvents: [ + WantLgtmTimelineEvent( + reviewerLogin: 'alice', + action: TimelineReviewAction.requested, + createdAt: DateTime.utc(2026, 1, 1, 9, 0), + ), + WantLgtmTimelineEvent( + reviewerLogin: 'pr-author', + action: TimelineReviewAction.requested, + createdAt: DateTime.utc(2026, 1, 1, 9, 0), + ), + WantLgtmTimelineEvent( + reviewerLogin: 'skia-flutter-autoroll', + action: TimelineReviewAction.requested, + createdAt: DateTime.utc(2026, 1, 1, 9, 0), + ), + ], + ); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + expect((verdict as WantLgtmSatisfied).approvedBy, ['alice']); + }); + + test( + 'enforces strict unremovedRequestAt.isAfter(latestApprovalAt) for re-requests', + () { + const directive = ValidWantLgtmDirective( + requiresAll: false, + explicitLogins: ['alice'], + ); + final tApproval = DateTime.utc(2026, 1, 1, 12, 0, 0); + + // Re-request at tApproval (same second as initial request/approval) does not invalidate approval. + final sameSecondSnapshot = makeSnapshot( + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'alice', + state: WantLgtmReviewState.approved, + submittedAt: tApproval, + ), + ], + timelineEvents: [ + WantLgtmTimelineEvent( + reviewerLogin: 'alice', + action: TimelineReviewAction.requested, + createdAt: tApproval, + ), + ], + ); + + final sameSecondVerdict = evaluateWantLgtm( + directive: directive, + snapshot: sameSecondSnapshot, + rollerAccounts: rollerAccounts, + ); + expect(sameSecondVerdict, isA()); + + // Re-request at tApproval + 1s is strictly after approval, so alice is waiting. + final afterSnapshot = makeSnapshot( + reviews: [ + WantLgtmReviewRecord( + authorLogin: 'alice', + state: WantLgtmReviewState.approved, + submittedAt: tApproval, + ), + ], + timelineEvents: [ + WantLgtmTimelineEvent( + reviewerLogin: 'alice', + action: TimelineReviewAction.requested, + createdAt: tApproval.add(const Duration(seconds: 1)), + ), + ], + ); + + final afterVerdict = evaluateWantLgtm( + directive: directive, + snapshot: afterSnapshot, + rollerAccounts: rollerAccounts, + ); + expect(afterVerdict, isA()); + expect((afterVerdict as WantLgtmPending).waitingOn, ['alice']); + }, + ); + + test( + 'caps titles at <= 255 characters when many reviewers are pending', + () { + final manyLogins = List.generate(40, (i) => 'reviewer$i'); + final directive = ValidWantLgtmDirective( + requiresAll: false, + explicitLogins: manyLogins, + ); + final snapshot = makeSnapshot(); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + expect(verdict.title.length, lessThanOrEqualTo(255)); + expect(verdict.title.endsWith('...'), isTrue); + }, + ); + + test('NoWantLgtmDirective returns satisfied', () { + const directive = NoWantLgtmDirective(); + final snapshot = makeSnapshot(); + + final verdict = evaluateWantLgtm( + directive: directive, + snapshot: snapshot, + rollerAccounts: rollerAccounts, + ); + + expect(verdict, isA()); + expect((verdict as WantLgtmSatisfied).approvedBy, isEmpty); + }); + }); +}