diff --git a/app_dart/lib/src/request_handlers/flaky_handler_utils.dart b/app_dart/lib/src/request_handlers/flaky_handler_utils.dart index 795b389e4..19448034a 100644 --- a/app_dart/lib/src/request_handlers/flaky_handler_utils.dart +++ b/app_dart/lib/src/request_handlers/flaky_handler_utils.dart @@ -145,11 +145,8 @@ class IssueUpdateBuilder { List get issueLabels { final List existingLabels = existingIssue.labels.map((IssueLabel label) => label.name).toList(); // Update the priority. - if (!existingLabels.contains(kP0Label) && !isBelow) { + if (!isBelow && !_containsPriorityLabel(existingLabels)) { existingLabels.add(kP0Label); - existingLabels.remove(kP1Label); - existingLabels.remove(kP2Label); - existingLabels.remove(kP3Label); } return existingLabels; } @@ -170,6 +167,16 @@ ${_issueBuilderLink(statistic.name)} } return result; } + + bool _containsPriorityLabel(List labels) { + for (final String label in labels) { + if (label == kP0Label || label == kP1Label || label == kP2Label || label == kP3Label) { + return true; + } + } + + return false; + } } /// A builder to build the pull request title and body for marking test flaky diff --git a/app_dart/test/request_handlers/update_existing_flaky_issues_test.dart b/app_dart/test/request_handlers/update_existing_flaky_issues_test.dart index 77c8d2ddb..9ba9434f2 100644 --- a/app_dart/test/request_handlers/update_existing_flaky_issues_test.dart +++ b/app_dart/test/request_handlers/update_existing_flaky_issues_test.dart @@ -90,7 +90,7 @@ void main() { const int existingIssueNumber = 1234; final List existingLabels = [ IssueLabel(name: 'some random label'), - IssueLabel(name: 'P2'), + IssueLabel(name: 'P0'), ]; // When queries flaky data from BigQuery. when(mockBigqueryService.listBuilderStatistic(kBigQueryProjectId)).thenAnswer((Invocation invocation) { @@ -155,7 +155,7 @@ void main() { expect(result['Status'], 'success'); }); - test('Add only one comment on existing issue when a builder has been marked as unflaky', () async { + test('Respects existing lower priority label', () async { const int existingIssueNumber = 1234; final List existingLabels = [ IssueLabel(name: 'some random label'), @@ -165,6 +165,75 @@ void main() { when(mockBigqueryService.listBuilderStatistic(kBigQueryProjectId)).thenAnswer((Invocation invocation) { return Future>.value(semanticsIntegrationTestResponse); }); + when(mockBigqueryService.listBuilderStatistic(kBigQueryProjectId, bucket: 'staging')) + .thenAnswer((Invocation invocation) { + return Future>.value(stagingCiyamlTestResponse); + }); + // when gets existing flaky issues. + when(mockIssuesService.listByRepo(captureAny, state: captureAnyNamed('state'), labels: captureAnyNamed('labels'))) + .thenAnswer((Invocation invocation) { + return Stream.fromIterable([ + Issue( + assignee: User(login: 'some dude'), + number: existingIssueNumber, + state: 'open', + labels: existingLabels, + title: expectedSemanticsIntegrationTestResponseTitle, + body: expectedSemanticsIntegrationTestResponseBody, + createdAt: + DateTime.now().subtract(const Duration(days: UpdateExistingFlakyIssue.kFreshPeriodForOpenFlake + 1)), + ), + ]); + }); + // when firing github request. + // This is for replacing labels. + when( + mockGitHubClient.request( + captureAny, + captureAny, + body: captureAnyNamed('body'), + ), + ).thenAnswer((Invocation invocation) { + return Future.value(Response('[]', 200)); + }); + final Map result = await utf8.decoder + .bind((await tester.get(handler)).serialize() as Stream>) + .transform(json.decoder) + .single as Map; + + // Verify comment is created correctly. + List captured = verify(mockIssuesService.createComment(captureAny, captureAny, captureAny)).captured; + expect(captured.length, 3); + expect(captured[0].toString(), Config.flutterSlug.toString()); + expect(captured[1], existingIssueNumber); + expect(captured[2], expectedSemanticsIntegrationTestIssueComment); + + // Verify labels are applied correctly. + captured = verify( + mockGitHubClient.request( + captureAny, + captureAny, + body: captureAnyNamed('body'), + ), + ).captured; + expect(captured.length, 3); + expect(captured[0].toString(), 'PUT'); + expect(captured[1], '/repos/${Config.flutterSlug.fullName}/issues/$existingIssueNumber/labels'); + expect(captured[2], GitHubJson.encode(['some random label', 'P2'])); + + expect(result['Status'], 'success'); + }); + + test('Add only one comment on existing issue when a builder has been marked as unflaky', () async { + const int existingIssueNumber = 1234; + final List existingLabels = [ + IssueLabel(name: 'some random label'), + IssueLabel(name: 'P0'), + ]; + // When queries flaky data from BigQuery. + when(mockBigqueryService.listBuilderStatistic(kBigQueryProjectId)).thenAnswer((Invocation invocation) { + return Future>.value(semanticsIntegrationTestResponse); + }); when(mockBigqueryService.listBuilderStatistic(kBigQueryProjectId, bucket: 'staging')) .thenAnswer((Invocation invocation) { return Future>.value(stagingSameBuilderSemanticsIntegrationTestResponse);