Skip to content

Commit eea3c28

Browse files
authored
Allow vacuuming a specific repository and branch. (#4682)
Closes flutter/flutter#168833. Better supports branches such as flutter/flutter#168738.
1 parent 2eb6035 commit eea3c28

File tree

9 files changed

+112
-23
lines changed

9 files changed

+112
-23
lines changed

app_dart/lib/src/request_handlers/vacuum_github_commits.dart

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,31 @@ final class VacuumGithubCommits extends ApiRequestHandler {
2626
}) : _scheduler = scheduler;
2727

2828
final Scheduler _scheduler;
29-
static const String branchParam = 'branch';
29+
30+
static const _paramRepo = 'repo';
31+
static const _paramBranch = 'branch';
3032

3133
@override
3234
Future<Response> get(Request request) async {
33-
for (var slug in config.supportedRepos) {
34-
final branch =
35-
request.uri.queryParameters[branchParam] ??
36-
Config.defaultBranch(slug);
37-
await _vacuumRepository(slug, branch: branch);
35+
final Iterable<gh.RepositorySlug> repos;
36+
if (request.uri.queryParameters[_paramRepo] case final specific?) {
37+
repos = [gh.RepositorySlug.full(specific)];
38+
} else {
39+
repos = config.supportedRepos;
40+
}
41+
42+
final String? branch;
43+
if (request.uri.queryParameters[_paramBranch] case final specific?) {
44+
branch = specific;
45+
} else {
46+
branch = null;
47+
}
48+
49+
for (final slug in repos) {
50+
await _vacuumRepository(
51+
slug,
52+
branch: branch ?? Config.defaultBranch(slug),
53+
);
3854
}
3955

4056
return Response.emptyOk;

app_dart/test/request_handlers/vacuum_github_commits_test.dart

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,31 +97,51 @@ void main() {
9797
});
9898

9999
test('succeeds when GitHub returns no commits', () async {
100-
fakeGithubCommitShas = <String>[];
101-
config.supportedBranchesValue = <String>['master'];
100+
fakeGithubCommitShas = [];
101+
config.supportedBranchesValue = ['master'];
102102
final body = await tester.get(handler);
103103

104104
expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
105105
expect(await body.body.toList(), isEmpty);
106106
});
107107

108+
test('succeeds on specific repo and branch set', () async {
109+
fakeGithubCommitShas = ['123'];
110+
config.supportedReposValue = {RepositorySlug('flutter', 'cocoon')};
111+
112+
tester.request.uri = tester.request.uri.replace(
113+
queryParameters: {'repo': 'flutter/cocoon', 'branch': 'a-new-branch'},
114+
);
115+
116+
await tester.get(handler);
117+
expect(
118+
firestore,
119+
existsInStorage(fs.Commit.metadata, [
120+
isCommit
121+
.hasSha('123')
122+
.hasRepositoryPath('flutter/cocoon')
123+
.hasBranch('a-new-branch'),
124+
]),
125+
);
126+
});
127+
108128
test('does not fail on empty commit list', () async {
109-
fakeGithubCommitShas = <String>[];
129+
fakeGithubCommitShas = [];
110130
expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
111131
await tester.get(handler);
112132
expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
113133
});
114134

115135
test('does not add recent commits', () async {
116-
fakeGithubCommitShas = <String>['${DateTime.now().millisecondsSinceEpoch}'];
136+
fakeGithubCommitShas = ['${DateTime.now().millisecondsSinceEpoch}'];
117137

118138
expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
119139
await tester.get(handler);
120140
expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
121141
});
122142

123143
test('inserts all relevant fields of the commit', () async {
124-
fakeGithubCommitShas = <String>['1'];
144+
fakeGithubCommitShas = ['1'];
125145
expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
126146
await tester.get(handler);
127147
expect(

dashboard/lib/service/appengine_cocoon.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,15 @@ class AppEngineCocoonService implements CocoonService {
189189
}
190190

191191
@override
192-
Future<CocoonResponse<bool>> vacuumGitHubCommits(String idToken) async {
193-
final refreshGitHubCommitsUrl = apiEndpoint('/api/vacuum-github-commits');
192+
Future<CocoonResponse<bool>> vacuumGitHubCommits(
193+
String idToken, {
194+
required String repo,
195+
required String branch,
196+
}) async {
197+
final refreshGitHubCommitsUrl = apiEndpoint(
198+
'/api/vacuum-github-commits',
199+
queryParameters: {'repo': repo, 'branch': branch},
200+
);
194201
final response = await _client.get(
195202
refreshGitHubCommitsUrl,
196203
headers: <String, String>{'X-Flutter-IdToken': idToken},

dashboard/lib/service/cocoon.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ abstract class CocoonService {
7070
});
7171

7272
/// Force update Cocoon to get the latest commits.
73-
Future<CocoonResponse<bool>> vacuumGitHubCommits(String idToken);
73+
Future<CocoonResponse<bool>> vacuumGitHubCommits(
74+
String idToken, {
75+
required String repo,
76+
required String branch,
77+
});
7478
}
7579

7680
/// Wrapper class for data this state serves.

dashboard/lib/service/dev_cocoon.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ class DevelopmentCocoonService implements CocoonService {
126126
}
127127

128128
@override
129-
Future<CocoonResponse<bool>> vacuumGitHubCommits(String idToken) async {
129+
Future<CocoonResponse<bool>> vacuumGitHubCommits(
130+
String idToken, {
131+
required String repo,
132+
required String branch,
133+
}) async {
130134
return const CocoonResponse<bool>.error(
131135
'Unable to vacuum against fake data. Try building the app to use prod data.',
132136
statusCode: 501 /* Not implemented */,

dashboard/lib/state/build.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ class BuildState extends ChangeNotifier {
360360
}
361361
final response = await cocoonService.vacuumGitHubCommits(
362362
await authService.idToken,
363+
repo: currentRepo,
364+
branch: currentBranch,
363365
);
364366
if (response.error != null) {
365367
_errors.send('$errorMessageRefreshGitHubCommits: ${response.error}');

dashboard/test/service/appengine_cocoon_test.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,11 @@ void main() {
254254

255255
test('should return true if request succeeds', () async {
256256
await expectLater(
257-
service.vacuumGitHubCommits('fakeIdToken'),
257+
service.vacuumGitHubCommits(
258+
'fakeIdToken',
259+
repo: 'flutter',
260+
branch: 'master',
261+
),
258262
completion(
259263
isA<CocoonResponse<bool>>().having((r) => r.data, 'data', isTrue),
260264
),
@@ -268,7 +272,11 @@ void main() {
268272
}),
269273
);
270274
await expectLater(
271-
service.vacuumGitHubCommits('fakeIdToken'),
275+
service.vacuumGitHubCommits(
276+
'fakeIdToken',
277+
repo: 'flutter',
278+
branch: 'master',
279+
),
272280
completion(
273281
isA<CocoonResponse<bool>>().having((r) => r.error, 'data', isNotNull),
274282
),

dashboard/test/state/build_test.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,13 @@ void main() {
544544
final result = await buildState.refreshGitHubCommits();
545545

546546
expect(result, isFalse);
547-
verifyNever(cocoonService.vacuumGitHubCommits(any));
547+
verifyNever(
548+
cocoonService.vacuumGitHubCommits(
549+
any,
550+
branch: anyNamed('branch'),
551+
repo: anyNamed('repo'),
552+
),
553+
);
548554
});
549555

550556
testWidgets(
@@ -553,7 +559,13 @@ void main() {
553559
const idToken = 'id_token';
554560
when(authService.isAuthenticated).thenReturn(true);
555561
when(authService.idToken).thenAnswer((_) async => idToken);
556-
when(cocoonService.vacuumGitHubCommits(idToken)).thenAnswer(
562+
when(
563+
cocoonService.vacuumGitHubCommits(
564+
idToken,
565+
branch: anyNamed('branch'),
566+
repo: anyNamed('repo'),
567+
),
568+
).thenAnswer(
557569
(_) async => const CocoonResponse.error('Bad user', statusCode: 401),
558570
);
559571

@@ -574,7 +586,11 @@ void main() {
574586
when(authService.isAuthenticated).thenReturn(true);
575587
when(authService.idToken).thenAnswer((_) async => idToken);
576588
when(
577-
cocoonService.vacuumGitHubCommits(idToken),
589+
cocoonService.vacuumGitHubCommits(
590+
idToken,
591+
branch: anyNamed('branch'),
592+
repo: anyNamed('repo'),
593+
),
578594
).thenAnswer((_) async => const CocoonResponse.data(true));
579595

580596
final buildState = BuildState(

dashboard/test/utils/mocks.mocks.dart

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)