Skip to content

Allow vacuuming a specific repository and branch. #4682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions app_dart/lib/src/request_handlers/vacuum_github_commits.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,31 @@ final class VacuumGithubCommits extends ApiRequestHandler {
}) : _scheduler = scheduler;

final Scheduler _scheduler;
static const String branchParam = 'branch';

static const _paramRepo = 'repo';
static const _paramBranch = 'branch';

@override
Future<Response> get(Request request) async {
for (var slug in config.supportedRepos) {
final branch =
request.uri.queryParameters[branchParam] ??
Config.defaultBranch(slug);
await _vacuumRepository(slug, branch: branch);
final Iterable<gh.RepositorySlug> repos;
if (request.uri.queryParameters[_paramRepo] case final specific?) {
repos = [gh.RepositorySlug.full(specific)];
} else {
repos = config.supportedRepos;
}

final String? branch;
if (request.uri.queryParameters[_paramBranch] case final specific?) {
branch = specific;
} else {
branch = null;
}

for (final slug in repos) {
await _vacuumRepository(
slug,
branch: branch ?? Config.defaultBranch(slug),
);
}

return Response.emptyOk;
Expand Down
30 changes: 25 additions & 5 deletions app_dart/test/request_handlers/vacuum_github_commits_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,51 @@ void main() {
});

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

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

test('succeeds on specific repo and branch set', () async {
fakeGithubCommitShas = ['123'];
config.supportedReposValue = {RepositorySlug('flutter', 'cocoon')};

tester.request.uri = tester.request.uri.replace(
queryParameters: {'repo': 'flutter/cocoon', 'branch': 'a-new-branch'},
);

await tester.get(handler);
expect(
firestore,
existsInStorage(fs.Commit.metadata, [
isCommit
.hasSha('123')
.hasRepositoryPath('flutter/cocoon')
.hasBranch('a-new-branch'),
]),
);
});

test('does not fail on empty commit list', () async {
fakeGithubCommitShas = <String>[];
fakeGithubCommitShas = [];
expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
await tester.get(handler);
expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
});

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

expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
await tester.get(handler);
expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
});

test('inserts all relevant fields of the commit', () async {
fakeGithubCommitShas = <String>['1'];
fakeGithubCommitShas = ['1'];
expect(firestore, existsInStorage(fs.Commit.metadata, isEmpty));
await tester.get(handler);
expect(
Expand Down
11 changes: 9 additions & 2 deletions dashboard/lib/service/appengine_cocoon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,15 @@ class AppEngineCocoonService implements CocoonService {
}

@override
Future<CocoonResponse<bool>> vacuumGitHubCommits(String idToken) async {
final refreshGitHubCommitsUrl = apiEndpoint('/api/vacuum-github-commits');
Future<CocoonResponse<bool>> vacuumGitHubCommits(
String idToken, {
required String repo,
required String branch,
}) async {
final refreshGitHubCommitsUrl = apiEndpoint(
'/api/vacuum-github-commits',
queryParameters: {'repo': repo, 'branch': branch},
);
final response = await _client.get(
refreshGitHubCommitsUrl,
headers: <String, String>{'X-Flutter-IdToken': idToken},
Expand Down
6 changes: 5 additions & 1 deletion dashboard/lib/service/cocoon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ abstract class CocoonService {
});

/// Force update Cocoon to get the latest commits.
Future<CocoonResponse<bool>> vacuumGitHubCommits(String idToken);
Future<CocoonResponse<bool>> vacuumGitHubCommits(
String idToken, {
required String repo,
required String branch,
});
}

/// Wrapper class for data this state serves.
Expand Down
6 changes: 5 additions & 1 deletion dashboard/lib/service/dev_cocoon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ class DevelopmentCocoonService implements CocoonService {
}

@override
Future<CocoonResponse<bool>> vacuumGitHubCommits(String idToken) async {
Future<CocoonResponse<bool>> vacuumGitHubCommits(
String idToken, {
required String repo,
required String branch,
}) async {
return const CocoonResponse<bool>.error(
'Unable to vacuum against fake data. Try building the app to use prod data.',
statusCode: 501 /* Not implemented */,
Expand Down
2 changes: 2 additions & 0 deletions dashboard/lib/state/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ class BuildState extends ChangeNotifier {
}
final response = await cocoonService.vacuumGitHubCommits(
await authService.idToken,
repo: currentRepo,
branch: currentBranch,
);
if (response.error != null) {
_errors.send('$errorMessageRefreshGitHubCommits: ${response.error}');
Expand Down
12 changes: 10 additions & 2 deletions dashboard/test/service/appengine_cocoon_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,11 @@ void main() {

test('should return true if request succeeds', () async {
await expectLater(
service.vacuumGitHubCommits('fakeIdToken'),
service.vacuumGitHubCommits(
'fakeIdToken',
repo: 'flutter',
branch: 'master',
),
completion(
isA<CocoonResponse<bool>>().having((r) => r.data, 'data', isTrue),
),
Expand All @@ -268,7 +272,11 @@ void main() {
}),
);
await expectLater(
service.vacuumGitHubCommits('fakeIdToken'),
service.vacuumGitHubCommits(
'fakeIdToken',
repo: 'flutter',
branch: 'master',
),
completion(
isA<CocoonResponse<bool>>().having((r) => r.error, 'data', isNotNull),
),
Expand Down
22 changes: 19 additions & 3 deletions dashboard/test/state/build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,13 @@ void main() {
final result = await buildState.refreshGitHubCommits();

expect(result, isFalse);
verifyNever(cocoonService.vacuumGitHubCommits(any));
verifyNever(
cocoonService.vacuumGitHubCommits(
any,
branch: anyNamed('branch'),
repo: anyNamed('repo'),
),
);
});

testWidgets(
Expand All @@ -553,7 +559,13 @@ void main() {
const idToken = 'id_token';
when(authService.isAuthenticated).thenReturn(true);
when(authService.idToken).thenAnswer((_) async => idToken);
when(cocoonService.vacuumGitHubCommits(idToken)).thenAnswer(
when(
cocoonService.vacuumGitHubCommits(
idToken,
branch: anyNamed('branch'),
repo: anyNamed('repo'),
),
).thenAnswer(
(_) async => const CocoonResponse.error('Bad user', statusCode: 401),
);

Expand All @@ -574,7 +586,11 @@ void main() {
when(authService.isAuthenticated).thenReturn(true);
when(authService.idToken).thenAnswer((_) async => idToken);
when(
cocoonService.vacuumGitHubCommits(idToken),
cocoonService.vacuumGitHubCommits(
idToken,
branch: anyNamed('branch'),
repo: anyNamed('repo'),
),
).thenAnswer((_) async => const CocoonResponse.data(true));

final buildState = BuildState(
Expand Down
18 changes: 15 additions & 3 deletions dashboard/test/utils/mocks.mocks.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.