-
Notifications
You must be signed in to change notification settings - Fork 100
WIP - Implement code freeze mechanism at autosubmit #4998
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
Draft
Piinks
wants to merge
3
commits into
flutter:main
Choose a base branch
from
Piinks:noDesignMerge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+429
−0
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // 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 'dart:io'; | ||
|
|
||
| import 'package:native_assets_cli/native_assets_cli.dart'; | ||
|
|
||
| void main(List<String> args) async { | ||
| await build(args, (config, output) async { | ||
| // 1. Read the source file (the code freeze config) | ||
| final configFile = File('lib/configuration/code_freeze.yaml'); | ||
| final content = await configFile.readAsString(); | ||
|
|
||
| // 2. Define the path for the generated code | ||
| final outputFile = File('lib/src/generated_config.dart'); | ||
|
|
||
| // 3. Write the file as a raw string constant | ||
| await outputFile.writeAsString(""" | ||
| // GENERATED CODE - DO NOT MODIFY BY HAND | ||
| // Generated by hook/build.dart | ||
|
|
||
| const String codeFreezeConfigContent = | ||
| r'''$content'''; | ||
| """); | ||
|
|
||
| // 4. Tell Dart that this hook depends on the config file | ||
| output.addDependency(configFile.uri); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| flutter/flutter: | ||
| frozen_labels: | ||
| - "f: material design" | ||
| - "f: cupertino" | ||
| frozen_paths: | ||
| - "packages/flutter/lib/src/material/" | ||
| - "packages/flutter/lib/src/cupertino/" |
90 changes: 90 additions & 0 deletions
90
auto_submit/lib/configuration/code_freeze_configuration.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // 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:github/github.dart'; | ||
| import 'package:json_annotation/json_annotation.dart'; | ||
| import 'package:meta/meta.dart'; | ||
| import 'package:yaml/yaml.dart'; | ||
|
|
||
| part 'code_freeze_configuration.g.dart'; | ||
|
|
||
| /// Configuration for repository-specific code freezes. | ||
| @JsonSerializable(explicitToJson: true) | ||
| @immutable | ||
| final class CodeFreezeConfiguration { | ||
| const CodeFreezeConfiguration([this.repoFreezeCriteria = const <String, FreezeCriteria>{}]); | ||
|
|
||
| /// A mapping of repository slugs to their freeze criteria. | ||
| @JsonKey(name: 'repoFreezeCriteria') | ||
| final Map<String, FreezeCriteria> repoFreezeCriteria; | ||
|
|
||
| /// Parses the configuration from a YAML string. | ||
| factory CodeFreezeConfiguration.fromYaml(String yaml) { | ||
| final yamlDoc = loadYaml(yaml) as YamlMap; | ||
| final map = <String, dynamic>{ | ||
| 'repoFreezeCriteria': yamlDoc.asMap, | ||
| }; | ||
| return CodeFreezeConfiguration.fromJson(map); | ||
| } | ||
|
|
||
| /// Creates [CodeFreezeConfiguration] from a [json] object. | ||
| factory CodeFreezeConfiguration.fromJson(Map<String, dynamic> json) => _$CodeFreezeConfigurationFromJson(json); | ||
|
|
||
| /// Converts [CodeFreezeConfiguration] to a [json] object. | ||
| Map<String, dynamic> toJson() => _$CodeFreezeConfigurationToJson(this); | ||
|
|
||
| /// Returns the freeze criteria for the given [slug]. | ||
| FreezeCriteria getFreezeCriteria(RepositorySlug slug) { | ||
| return repoFreezeCriteria[slug.fullName] ?? const FreezeCriteria(); | ||
| } | ||
| } | ||
|
|
||
| /// Criteria used to determine if a PR is affected by a code freeze. | ||
| @JsonSerializable() | ||
| @immutable | ||
| final class FreezeCriteria { | ||
| const FreezeCriteria({ | ||
| this.frozenLabels = const <String>{}, | ||
| this.frozenPaths = const <String>{}, | ||
| }); | ||
|
|
||
| final Set<String> frozenLabels; | ||
| final Set<String> frozenPaths; | ||
|
|
||
| /// Creates [FreezeCriteria] from a [json] object. | ||
| factory FreezeCriteria.fromJson(Map<String, dynamic> json) => _$FreezeCriteriaFromJson(json); | ||
|
|
||
| /// Converts [FreezeCriteria] to a [json] object. | ||
| Map<String, dynamic> toJson() => _$FreezeCriteriaToJson(this); | ||
|
|
||
| bool get isEmpty => frozenLabels.isEmpty && frozenPaths.isEmpty; | ||
| } | ||
|
|
||
| extension _YamlMapToMap on YamlMap { | ||
| Map<String, dynamic> get asMap => <String, dynamic>{ | ||
| for (final MapEntry(:key, :value) in entries) | ||
| if (value is YamlMap) | ||
| '$key': value.asMap | ||
| else if (value is YamlList) | ||
| '$key': value.asList | ||
| else if (value is YamlScalar) | ||
| '$key': value.value | ||
| else | ||
| '$key': value, | ||
| }; | ||
| } | ||
|
|
||
| extension _YamlListToList on YamlList { | ||
| List<dynamic> get asList => <dynamic>[ | ||
| for (final node in nodes) | ||
| if (node is YamlMap) | ||
| node.asMap | ||
| else if (node is YamlList) | ||
| node.asList | ||
| else if (node is YamlScalar) | ||
| node.value | ||
| else | ||
| node, | ||
| ]; | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
auto_submit/lib/configuration/code_freeze_configuration.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // 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:github/github.dart' as github; | ||
|
|
||
| import '../model/auto_submit_query_result.dart'; | ||
| import 'validation.dart'; | ||
|
|
||
| /// Validates that a pull request is not affected by an active code freeze. | ||
| class CodeFreeze extends Validation { | ||
| CodeFreeze({required super.config}); | ||
|
|
||
| @override | ||
| Future<ValidationResult> validate( | ||
| QueryResult result, | ||
| github.PullRequest pr, | ||
| ) async { | ||
| final slug = pr.base!.repo!.slug(); | ||
| final criteria = config.codeFreezeConfiguration.getFreezeCriteria(slug); | ||
|
|
||
| if (criteria.isEmpty) { | ||
| return ValidationResult(true, Action.IGNORE_FAILURE, ''); | ||
| } | ||
|
|
||
| // Check labels first as it is cheaper. | ||
| final prLabels = | ||
| pr.labels?.map((label) => label.name).toSet() ?? <String>{}; | ||
| final matchedLabels = criteria.frozenLabels.intersection(prLabels); | ||
| if (matchedLabels.isNotEmpty) { | ||
| final message = | ||
| 'This pull request is blocked due to an active code freeze for the following labels: ${matchedLabels.join(", ")}.'; | ||
| return ValidationResult(false, Action.REMOVE_LABEL, message); | ||
| } | ||
|
|
||
| // Check paths if frozen paths are defined. | ||
| if (criteria.frozenPaths.isNotEmpty) { | ||
| final githubService = await config.createGithubService(slug); | ||
| final files = await githubService.getPullRequestFiles(slug, pr); | ||
| final matchedPaths = <String>{}; | ||
|
|
||
| for (final file in files) { | ||
| final filename = file.filename; | ||
| if (filename == null) continue; | ||
| for (final frozenPath in criteria.frozenPaths) { | ||
| if (filename.startsWith(frozenPath)) { | ||
| matchedPaths.add(frozenPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (matchedPaths.isNotEmpty) { | ||
| final message = | ||
| 'This pull request is blocked due to an active code freeze for the following paths: ${matchedPaths.join(", ")}.'; | ||
| return ValidationResult(false, Action.REMOVE_LABEL, message); | ||
| } | ||
| } | ||
|
|
||
| return ValidationResult(true, Action.IGNORE_FAILURE, ''); | ||
| } | ||
|
|
||
| @override | ||
| String get name => 'CodeFreeze'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
auto_submit/test/configuration/code_freeze_configuration_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // 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:auto_submit/configuration/code_freeze_configuration.dart'; | ||
| import 'package:github/github.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| group('CodeFreezeConfiguration', () { | ||
| test('parses YAML correctly', () { | ||
| const yaml = ''' | ||
| flutter/flutter: | ||
| frozen_labels: | ||
| - "f: material design" | ||
| - "f: cupertino" | ||
| frozen_paths: | ||
| - "packages/flutter/lib/src/material/" | ||
| - "packages/flutter/lib/src/cupertino/" | ||
| flutter/packages: | ||
| frozen_labels: | ||
| - "blocked" | ||
| '''; | ||
| final config = CodeFreezeConfiguration.fromYaml(yaml); | ||
|
|
||
| final flutterCriteria = config.getFreezeCriteria( | ||
| RepositorySlug('flutter', 'flutter'), | ||
| ); | ||
| expect( | ||
| flutterCriteria.frozenLabels, | ||
| containsAll(['f: material design', 'f: cupertino']), | ||
| ); | ||
| expect( | ||
| flutterCriteria.frozenPaths, | ||
| containsAll([ | ||
| 'packages/flutter/lib/src/material/', | ||
| 'packages/flutter/lib/src/cupertino/', | ||
| ]), | ||
| ); | ||
|
|
||
| final packagesCriteria = config.getFreezeCriteria( | ||
| RepositorySlug('flutter', 'packages'), | ||
| ); | ||
| expect(packagesCriteria.frozenLabels, contains('blocked')); | ||
| expect(packagesCriteria.frozenPaths, isEmpty); | ||
| }); | ||
|
|
||
| test('returns empty criteria for unknown slug', () { | ||
| final config = CodeFreezeConfiguration.fromYaml('{}'); | ||
| final criteria = config.getFreezeCriteria( | ||
| RepositorySlug('unknown', 'unknown'), | ||
| ); | ||
| expect(criteria.isEmpty, isTrue); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent place for JsonSerializable:
final yamlMap = loadYaml(yamlString) as YamlMap;
return YourJsonSerializable.fromJson(Map<String, dynamic>.from(yamlMap));