Skip to content

Commit 27c2fc8

Browse files
authored
Fix publish workflow pins, add tag_prefix to health, and update docs (#480)
1 parent 05641a9 commit 27c2fc8

11 files changed

Lines changed: 386 additions & 130 deletions

File tree

‎.github/workflows/health.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ on:
130130
default: ''
131131
required: false
132132
type: string
133+
tag_prefix:
134+
description: The prefix to expect and generate for release git tags (e.g. 'v' or '').
135+
default: 'v'
136+
required: false
137+
type: string
133138
ignore_packages:
134139
description: A glob describing which packages to ignore for all checks.
135140
default: ''
@@ -349,6 +354,7 @@ jobs:
349354
local_debug: ${{ inputs.local_debug }}
350355
flutter_packages: ${{ inputs.flutter_packages }}
351356
ignore_publish: ${{ inputs.ignore_publish }}
357+
tag_prefix: ${{ inputs.tag_prefix }}
352358
ignore_packages: ${{ inputs.ignore_packages }}
353359
checkout_submodules: ${{ inputs.checkout_submodules }}
354360
health_yaml_name: ${{ inputs.health_yaml_name }}

‎.github/workflows/health_base.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ on:
7070
default: ''
7171
required: false
7272
type: string
73+
tag_prefix:
74+
default: 'v'
75+
required: false
76+
type: string
7377
ignore_packages:
7478
default: ''
7579
required: false
@@ -200,6 +204,7 @@ jobs:
200204
INPUTS_IGNORE_DONOTSUBMIT: ${{ inputs.ignore_donotsubmit }}
201205
INPUTS_IGNORE_UNUSEDDEPENDENCIES: ${{ inputs.ignore_unuseddependencies }}
202206
INPUTS_IGNORE_PUBLISH: ${{ inputs.ignore_publish }}
207+
INPUTS_TAG_PREFIX: ${{ inputs.tag_prefix }}
203208
INPUTS_EXPERIMENTS: ${{ inputs.experiments }}
204209
INPUTS_HEALTH_YAML_NAME: ${{ inputs.health_yaml_name }}
205210
INPUTS_LICENSE: ${{ inputs.license }}
@@ -227,6 +232,7 @@ jobs:
227232
--ignore_donotsubmit "${INPUTS_IGNORE_DONOTSUBMIT}" \
228233
--ignore_unuseddependencies "${INPUTS_IGNORE_UNUSEDDEPENDENCIES}" \
229234
--ignore_publish "${INPUTS_IGNORE_PUBLISH}" \
235+
--tag_prefix "${INPUTS_TAG_PREFIX}" \
230236
--experiments "${INPUTS_EXPERIMENTS}" \
231237
--health_yaml_name "${INPUTS_HEALTH_YAML_NAME}" \
232238
--no-local \

‎.github/workflows/publish_dart_flutter_team_lints.yaml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ jobs:
1111
publish:
1212
permissions:
1313
id-token: write
14-
attestations: write
15-
contents: read
16-
uses: dart-lang/setup-dart/.github/workflows/publish.yml@a2a85876fe519099fd3f528670216b240f975906
14+
uses: dart-lang/setup-dart/.github/workflows/publish.yml@a5bff6576dfc51b47eb77d4b7084bba6a4daf8d2
1715
with:
1816
working-directory: pkgs/dart_flutter_team_lints
17+
use-flutter: false

‎.github/workflows/publish_firehose.yaml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ jobs:
1111
publish:
1212
permissions:
1313
id-token: write
14-
attestations: write
15-
contents: read
16-
uses: dart-lang/setup-dart/.github/workflows/publish.yml@a2a85876fe519099fd3f528670216b240f975906
14+
uses: dart-lang/setup-dart/.github/workflows/publish.yml@a5bff6576dfc51b47eb77d4b7084bba6a4daf8d2
1715
with:
1816
working-directory: pkgs/firehose
17+
use-flutter: false

‎pkgs/blast_repo/lib/src/tweaks/auto_publish_tweak.dart‎

Lines changed: 145 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,184 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:async';
56
import 'dart:io';
67

7-
import '../exact_file_tweak.dart';
8+
import 'package:path/path.dart' as p;
9+
import 'package:yaml/yaml.dart' as yaml;
10+
11+
import '../repo_tweak.dart';
812
import '../utils.dart';
913

1014
final _instance = AutoPublishTweak._();
1115

12-
class AutoPublishTweak extends ExactFileTweak {
16+
class AutoPublishTweak extends RepoTweak {
1317
factory AutoPublishTweak() => _instance;
1418

1519
AutoPublishTweak._()
1620
: super(
1721
id: 'auto-publish',
1822
description:
19-
'configure a github action to enable package auto-publishing',
20-
filePath: '.github/workflows/publish.yaml',
23+
'configure github actions to enable package auto-publishing',
2124
);
2225

2326
@override
2427
bool shouldRunByDefault(Directory checkout, String repoSlug) =>
2528
monoRepo(checkout, repoSlug);
2629

2730
@override
28-
String expectedContent(Directory checkout, String repoSlug) {
31+
FutureOr<FixResult> fix(Directory checkout, String repoSlug) {
2932
final org = repoSlug.split('/').first;
30-
final branch = gitDefaultBranch(checkout) ?? 'main';
31-
final glob = singlePackageRepo(checkout)
32-
? "'v[0-9]+.[0-9]+.[0-9]+'"
33-
: "'[A-z]+-v[0-9]+.[0-9]+.[0-9]+'";
34-
35-
// Substitute for the github org, default branch, and glob pattern values.
36-
return publishContents
37-
.replaceAll('{org}', org)
38-
.replaceAll('{branch}', branch)
39-
.replaceAll('{glob}', glob);
33+
final fixes = <String>[];
34+
35+
if (!monoRepo(checkout, repoSlug) && singlePackageRepo(checkout)) {
36+
const relPath = '.github/workflows/publish.yaml';
37+
final pubspec = yaml.loadYaml(
38+
File(p.join(checkout.path, 'pubspec.yaml')).readAsStringSync(),
39+
) as yaml.YamlMap?;
40+
if (pubspec != null &&
41+
pubspec['publish_to'] != 'none' &&
42+
pubspec['version'] != null) {
43+
final useFlutter = _usesFlutter(pubspec);
44+
final content = singlePackagePublishContents
45+
.replaceAll('{org}', org)
46+
.replaceAll('{use_flutter}', '$useFlutter');
47+
_writeIfChanged(checkout, relPath, content, fixes);
48+
}
49+
} else {
50+
final legacyFile = File(
51+
p.join(checkout.path, '.github/workflows/publish.yaml'),
52+
);
53+
if (legacyFile.existsSync()) {
54+
legacyFile.deleteSync();
55+
fixes.add('.github/workflows/publish.yaml has been removed.');
56+
}
57+
58+
final packages = _findPublishablePackages(checkout);
59+
for (final pkg in packages) {
60+
final relPath = '.github/workflows/publish_${pkg.name}.yaml';
61+
final content = monoRepoPackagePublishContents
62+
.replaceAll('{org}', org)
63+
.replaceAll('{package}', pkg.name)
64+
.replaceAll('{directory}', pkg.relativeDir)
65+
.replaceAll('{use_flutter}', '${pkg.useFlutter}');
66+
_writeIfChanged(checkout, relPath, content, fixes);
67+
}
68+
}
69+
70+
return fixes.isEmpty ? FixResult.noFixesMade : FixResult(fixes: fixes);
4071
}
4172
}
4273

43-
const publishContents = r'''
74+
void _writeIfChanged(
75+
Directory checkout,
76+
String relPath,
77+
String content,
78+
List<String> fixes,
79+
) {
80+
final file = File(p.join(checkout.path, relPath));
81+
if (!file.existsSync()) {
82+
file.parent.createSync(recursive: true);
83+
file.writeAsStringSync(content);
84+
fixes.add('$relPath has been created.');
85+
} else if (file.readAsStringSync() != content) {
86+
file.writeAsStringSync(content);
87+
fixes.add('$relPath has been updated.');
88+
}
89+
}
90+
91+
List<_PackageInfo> _findPublishablePackages(Directory root) {
92+
final results = <_PackageInfo>[];
93+
void walk(Directory dir) {
94+
final pubspecFile = File(p.join(dir.path, 'pubspec.yaml'));
95+
if (pubspecFile.existsSync()) {
96+
final map =
97+
yaml.loadYaml(pubspecFile.readAsStringSync()) as yaml.YamlMap?;
98+
if (map != null &&
99+
map['publish_to'] != 'none' &&
100+
map['version'] != null) {
101+
final name = map['name'] as String;
102+
final relDir = p.posix.joinAll(
103+
p.split(p.relative(dir.path, from: root.path)),
104+
);
105+
results.add(_PackageInfo(name, relDir, _usesFlutter(map)));
106+
return;
107+
}
108+
if (dir.path != root.path) {
109+
return;
110+
}
111+
}
112+
for (final child in dir.listSync().whereType<Directory>()) {
113+
final base = p.basename(child.path);
114+
if (!base.startsWith('.') &&
115+
base != 'example' &&
116+
base != 'test' &&
117+
base != 'test_data' &&
118+
base != 'test_fixtures') {
119+
walk(child);
120+
}
121+
}
122+
}
123+
124+
walk(root);
125+
results.sort((a, b) => a.name.compareTo(b.name));
126+
return results;
127+
}
128+
129+
bool _usesFlutter(yaml.YamlMap? pubspec) {
130+
if (pubspec == null) return false;
131+
final deps = pubspec['dependencies'] as yaml.YamlMap?;
132+
final devDeps = pubspec['dev_dependencies'] as yaml.YamlMap?;
133+
final env = pubspec['environment'] as yaml.YamlMap?;
134+
return (deps != null && deps.containsKey('flutter')) ||
135+
(devDeps != null && devDeps.containsKey('flutter_test')) ||
136+
(env != null && env.containsKey('flutter'));
137+
}
138+
139+
class _PackageInfo {
140+
final String name;
141+
final String relativeDir;
142+
final bool useFlutter;
143+
144+
_PackageInfo(this.name, this.relativeDir, this.useFlutter);
145+
}
146+
147+
const singlePackagePublishContents = r'''
44148
# A CI configuration to auto-publish pub packages.
45149
46150
name: Publish
47151
48152
on:
49-
pull_request:
50-
branches: [ {branch} ]
51153
push:
52-
tags: [ {glob} ]
154+
tags: [ 'v[0-9]+.[0-9]+.[0-9]+*' ]
155+
156+
jobs:
157+
publish:
158+
if: ${{ github.repository_owner == '{org}' }}
159+
permissions:
160+
id-token: write # Required for authentication using OIDC
161+
uses: dart-lang/setup-dart/.github/workflows/publish.yml@a5bff6576dfc51b47eb77d4b7084bba6a4daf8d2 # main
162+
with:
163+
use-flutter: {use_flutter}
164+
''';
165+
166+
const monoRepoPackagePublishContents = r'''
167+
# A CI configuration to publish package:{package} to pub.dev.
168+
169+
name: Publish package:{package}
170+
171+
on:
172+
push:
173+
tags:
174+
- '{package}-v[0-9]+.[0-9]+.[0-9]+*'
53175
54176
jobs:
55177
publish:
56178
if: ${{ github.repository_owner == '{org}' }}
57-
uses: dart-lang/ecosystem/.github/workflows/publish.yaml@main
58179
permissions:
59180
id-token: write # Required for authentication using OIDC
60-
pull-requests: write # Required for writing the pull request note
181+
uses: dart-lang/setup-dart/.github/workflows/publish.yml@a5bff6576dfc51b47eb77d4b7084bba6a4daf8d2 # main
182+
with:
183+
working-directory: {directory}
184+
use-flutter: {use_flutter}
61185
''';

‎pkgs/blast_repo/test/auto_publish_test.dart‎

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,100 @@ void main() {
2020
d.dir('workflows', [
2121
d.file('build.yaml', '# hello world'),
2222
]),
23-
])
23+
]),
2424
]).create();
2525
dir = d.dir('foo').io;
2626
});
2727

28-
test('customized to github org', () async {
28+
test('customized to github org (single-package)', () async {
29+
await d.dir('foo', [
30+
d.file('pubspec.yaml', 'name: foo\nversion: 1.0.0\n'),
31+
]).create();
32+
2933
var results = await tweak.fix(dir, 'my_org/my_repo');
3034
expect(results.fixes, isNotEmpty);
3135

3236
await d.dir('foo', [
3337
d.dir('.github', [
3438
d.dir('workflows', [
35-
d.file('publish.yaml', contains('my_org')),
39+
d.file(
40+
'publish.yaml',
41+
allOf(
42+
contains('my_org'),
43+
contains("'v[0-9]+.[0-9]+.[0-9]+*'"),
44+
contains('dart-lang/setup-dart/.github/workflows/publish.yml@'),
45+
isNot(contains('working-directory:')),
46+
isNot(contains('pull_request:')),
47+
),
48+
),
3649
]),
37-
])
50+
]),
3851
]).validate();
3952
});
4053

41-
test('detects mono-repo', () async {
54+
test('creates one workflow per publishable package in mono-repo', () async {
55+
await d.dir('foo', [
56+
d.dir('.github', [
57+
d.dir('workflows', [
58+
d.file('publish.yaml', '# legacy workflow'),
59+
]),
60+
]),
61+
d.dir('pkgs', [
62+
d.dir('pkg_a', [
63+
d.file('pubspec.yaml', 'name: pkg_a\nversion: 1.0.0\n'),
64+
]),
65+
d.dir('pkg_b', [
66+
d.file(
67+
'pubspec.yaml',
68+
'name: pkg_b\nversion: 2.0.0\n'
69+
'dependencies:\n flutter:\n sdk: flutter\n',
70+
),
71+
]),
72+
d.dir('internal_pkg', [
73+
d.file(
74+
'pubspec.yaml',
75+
'name: internal_pkg\nversion: 0.1.0\npublish_to: none\n',
76+
),
77+
]),
78+
]),
79+
]).create();
80+
4281
var results = await tweak.fix(dir, 'my_org/my_repo');
4382
expect(results.fixes, isNotEmpty);
83+
expect(
84+
io.File('${dir.path}/.github/workflows/publish.yaml').existsSync(),
85+
isFalse,
86+
);
87+
expect(
88+
io.File(
89+
'${dir.path}/.github/workflows/publish_internal_pkg.yaml',
90+
).existsSync(),
91+
isFalse,
92+
);
4493

4594
await d.dir('foo', [
4695
d.dir('.github', [
4796
d.dir('workflows', [
48-
d.file('publish.yaml', contains('[A-z]+-v')),
97+
d.file(
98+
'publish_pkg_a.yaml',
99+
allOf(
100+
contains('my_org'),
101+
contains("'pkg_a-v[0-9]+.[0-9]+.[0-9]+*'"),
102+
contains('working-directory: pkgs/pkg_a'),
103+
contains('use-flutter: false'),
104+
),
105+
),
106+
d.file(
107+
'publish_pkg_b.yaml',
108+
allOf(
109+
contains('my_org'),
110+
contains("'pkg_b-v[0-9]+.[0-9]+.[0-9]+*'"),
111+
contains('working-directory: pkgs/pkg_b'),
112+
contains('use-flutter: true'),
113+
),
114+
),
49115
]),
50-
])
116+
]),
51117
]).validate();
52118
});
53119
}

‎pkgs/firehose/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
packages.
3434
- Remove 3rd-party Coveralls action and `upload_coverage` input from health workflows.
3535
- Add `--tag-prefix` option to `firehose` and `tag-prefix` input to `publish.yaml` to allow configuring the release tag prefix (defaults to `'v'`).
36+
- Add `--tag_prefix` option to `firehose:health` and `tag_prefix` input to `health.yaml` for the `publish` health check, and log release links at the end of the `publish` health check output.
3637
- Run `pub get` before `dart format` in `groundskeeper`.
3738
- Add `groundskeeper` executable to format and fix packages.
3839
- Add parameter to `locatePackages` to allow locating all packages with `publish_to: none`.

0 commit comments

Comments
 (0)