|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:file/file.dart'; |
| 6 | +import 'package:path/path.dart' as p; |
| 7 | +import 'package:yaml/yaml.dart'; |
| 8 | + |
| 9 | +/// The system for managing pending changelog entries. |
| 10 | +/// |
| 11 | +/// When package opts into batch release (through ci_config.yaml), it uses a "pending |
| 12 | +/// changelog" system. When a PR makes a change that requires a changelog entry, |
| 13 | +/// the entry is written to a new YAML file in the `pending_changelogs` directory |
| 14 | +/// of the package, rather than editing `CHANGELOG.md` directly. |
| 15 | +/// |
| 16 | +/// ## Directory Structure |
| 17 | +/// For batch release packages, it has a `pending_changelogs` directory containing: |
| 18 | +/// - A `template.yaml` file (which is ignored by the release tooling). |
| 19 | +/// - One or more YAML files for pending changes (e.g., `fix_issue_123.yaml`). |
| 20 | +/// |
| 21 | +/// ## File Format |
| 22 | +/// The YAML file must contain the following keys: |
| 23 | +/// - `changelog`: The text of the changelog entry. |
| 24 | +/// - `version`: The type of version bump (`major`, `minor`, `patch`, or `skip`). |
| 25 | +/// |
| 26 | +/// Example: |
| 27 | +/// ```yaml |
| 28 | +/// changelog: Fixes a bug in the foo widget. |
| 29 | +/// version: patch |
| 30 | +/// ``` |
| 31 | +/// |
| 32 | +/// During a release (specifically the `update-changelogs` command), all |
| 33 | +/// pending entries are aggregated, the package version is updated based on the |
| 34 | +/// highest priority change, and `CHANGELOG.md` is updated. |
| 35 | +
|
| 36 | +/// The type of version change described by a changelog entry. |
| 37 | +/// |
| 38 | +/// The order of the enum values is important as it is used to determine which version |
| 39 | +/// take priority when multiple version changes are specified. The top most value |
| 40 | +/// (the samller the index) has the highest priority. |
| 41 | +enum VersionChange { |
| 42 | + /// A major version change (e.g., 1.2.3 -> 2.0.0). |
| 43 | + major, |
| 44 | + |
| 45 | + /// A minor version change (e.g., 1.2.3 -> 1.3.0). |
| 46 | + minor, |
| 47 | + |
| 48 | + /// A patch version change (e.g., 1.2.3 -> 1.2.4). |
| 49 | + patch, |
| 50 | + |
| 51 | + /// No version change. |
| 52 | + skip, |
| 53 | +} |
| 54 | + |
| 55 | +/// Represents a single entry in the pending changelog. |
| 56 | +class PendingChangelogEntry { |
| 57 | + /// Creates a new pending changelog entry. |
| 58 | + PendingChangelogEntry({ |
| 59 | + required this.changelog, |
| 60 | + required this.version, |
| 61 | + required this.file, |
| 62 | + }); |
| 63 | + |
| 64 | + /// Creates a PendingChangelogEntry from a YAML string. |
| 65 | + /// |
| 66 | + /// Throws if the YAML is not a valid pending changelog entry. |
| 67 | + factory PendingChangelogEntry.parse(String yamlContent, File file) { |
| 68 | + final dynamic yaml = loadYaml(yamlContent); |
| 69 | + if (yaml is! YamlMap) { |
| 70 | + throw FormatException( |
| 71 | + 'Expected a YAML map, but found ${yaml.runtimeType}.', |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + final dynamic changelogYaml = yaml['changelog']; |
| 76 | + if (changelogYaml is! String) { |
| 77 | + throw FormatException( |
| 78 | + 'Expected "changelog" to be a string, but found ${changelogYaml.runtimeType}.', |
| 79 | + ); |
| 80 | + } |
| 81 | + final String changelog = changelogYaml.trim(); |
| 82 | + |
| 83 | + final versionString = yaml['version'] as String?; |
| 84 | + if (versionString == null) { |
| 85 | + throw const FormatException('Missing "version" key.'); |
| 86 | + } |
| 87 | + final VersionChange version = VersionChange.values.firstWhere( |
| 88 | + (VersionChange e) => e.name == versionString, |
| 89 | + orElse: () => |
| 90 | + throw FormatException('Invalid version type: $versionString'), |
| 91 | + ); |
| 92 | + |
| 93 | + return PendingChangelogEntry( |
| 94 | + changelog: changelog, |
| 95 | + version: version, |
| 96 | + file: file, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /// The template file name used to draft a pending changelog file. |
| 101 | + /// This file will not be picked up by the batch release process. |
| 102 | + static const String _batchReleaseChangelogTemplateFileName = 'template.yaml'; |
| 103 | + |
| 104 | + /// Returns true if the file is a template file. |
| 105 | + static bool isTemplate(File file) { |
| 106 | + return p.basename(file.path) == _batchReleaseChangelogTemplateFileName; |
| 107 | + } |
| 108 | + |
| 109 | + /// The changelog messages for this entry. |
| 110 | + final String changelog; |
| 111 | + |
| 112 | + /// The type of version change for this entry. |
| 113 | + final VersionChange version; |
| 114 | + |
| 115 | + /// The file that this entry was parsed from. |
| 116 | + final File file; |
| 117 | +} |
0 commit comments