-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfirehose.dart
339 lines (277 loc) · 9.62 KB
/
firehose.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// ignore_for_file: always_declare_return_types
import 'dart:io';
import 'dart:math';
import 'package:firehose/src/repo.dart';
import 'src/github.dart';
import 'src/pub.dart';
import 'src/utils.dart';
const String _botSuffix = '[bot]';
const String _githubActionsUser = 'github-actions[bot]';
const String _publishBotTag = '## Package publishing';
const String _ignoreWarningsLabel = 'publish-ignore-warnings';
class Firehose {
final Directory directory;
Firehose(this.directory);
/// Validate the packages in the repository.
///
/// This method is intended to run in the context of a PR. It will:
/// - determine the set of packages in the repo
/// - validate that the changelog version == the pubspec version
/// - provide feedback on the PR (via a PR comment) about packages which are
/// ready to publish
Future<void> validate() async {
var github = Github();
// Do basic validation of our expected env var.
if (!expectEnv(github.githubAuthToken, 'GITHUB_TOKEN')) return;
if (!expectEnv(github.repoSlug, 'GITHUB_REPOSITORY')) return;
if (!expectEnv(github.issueNumber, 'ISSUE_NUMBER')) return;
if (!expectEnv(github.sha, 'GITHUB_SHA')) return;
if ((github.actor ?? '').endsWith(_botSuffix)) {
print('Skipping package validation for ${github.actor} PRs.');
return;
}
var results = await verify(github);
var markdownTable = '''
| Package | Version | Status | Publish tag (post-merge) |
| :--- | ---: | :--- | ---: |
${results.describeAsMarkdown()}
Documentation at https://github.com/dart-lang/ecosystem/wiki/Publishing-automation.
''';
github.appendStepSummary(markdownTable);
var existingCommentId = await allowFailure(
github.findCommentId(
github.repoSlug!,
github.issueNumber!,
user: _githubActionsUser,
searchTerm: _publishBotTag,
),
logError: print,
);
if (results.hasSuccess) {
var commentText = '$_publishBotTag\n\n$markdownTable';
if (existingCommentId == null) {
await allowFailure(
github.createComment(
github.repoSlug!, github.issueNumber!, commentText),
logError: print,
);
} else {
await allowFailure(
github.updateComment(
github.repoSlug!, existingCommentId, commentText),
logError: print,
);
}
} else {
if (results.hasError && exitCode == 0) {
exitCode = 1;
}
if (existingCommentId != null) {
await allowFailure(
github.deleteComment(github.repoSlug!, existingCommentId),
logError: print,
);
}
}
github.close();
}
Future<VerificationResults> verify(Github github) async {
var repo = Repository();
var packages = repo.locatePackages();
var pub = Pub();
var results = VerificationResults();
for (var package in packages) {
var repoTag = repo.calculateRepoTag(package);
print('');
print('Validating $package:${package.name}');
print('pubspec:');
var pubspecVersion = package.pubspec.version;
if (pubspecVersion == null) {
var result = Result.fail(
package,
"no version specified (perhaps you need a' publish_to: none' entry?)",
);
print(result);
results.addResult(result);
continue;
}
print(' - version: $pubspecVersion');
var changelogVersion = package.changelog.latestVersion;
print('changelog:');
print(package.changelog.describeLatestChanges.trimRight());
if (pubspecVersion != changelogVersion) {
var result = Result.fail(
package,
'pubspec version ($pubspecVersion) and changelog ($changelogVersion) '
"don't agree",
);
print(result);
results.addResult(result);
continue;
}
if (await pub.hasPublishedVersion(package.name, pubspecVersion)) {
var result = Result.info(package, 'already published at pub.dev');
print(result);
results.addResult(result);
} else if (package.pubspec.isPreRelease) {
var result = Result.info(
package,
'pre-release version (no publish necessary)',
);
print(result);
results.addResult(result);
} else {
var code = await runCommand('dart',
args: ['pub', 'publish', '--dry-run'], cwd: package.directory);
final ignoreWarnings = github.prLabels.contains(_ignoreWarningsLabel);
if (code != 0 && !ignoreWarnings) {
exitCode = code;
var message =
'pub publish dry-run failed; add the `$_ignoreWarningsLabel` '
'label to ignore';
github.notice(message: message);
results.addResult(Result.fail(package, message));
} else {
var result = Result.success(package, '**ready to publish**', repoTag,
repo.calculateReleaseUri(package, github));
print(result);
results.addResult(result);
}
}
}
pub.close();
return results;
}
/// Publish the indicated package in the repository.
///
/// This is intended to be run on a github workflow in response to a git tag.
/// It will:
/// - validate the tag
/// - validate the package exists
/// - validate changelog and pubspec versions
/// - perform a publish
Future publish() async {
var success = await _publish();
if (!success && exitCode == 0) {
exitCode = 1;
}
}
Future<bool> _publish() async {
var github = Github();
if (!expectEnv(github.refName, 'GITHUB_REF_NAME')) return false;
// Validate the git tag.
var tag = Tag(github.refName!);
if (!tag.valid) {
stderr.writeln("Git tag not in expected format: '$tag'");
return false;
}
print("Publishing '$tag'");
print('');
var repo = Repository();
var packages = repo.locatePackages();
print('');
print('Repository packages:');
for (var package in packages) {
print(' $package');
}
print('');
// Find package to publish.
Package package;
if (repo.isSinglePackageRepo) {
if (packages.isEmpty) {
stderr.writeln('No publishable package found.');
return false;
}
package = packages.first;
} else {
var name = tag.package;
if (name == null) {
stderr.writeln("Tag does not include package name ('$tag').");
return false;
}
if (!packages.any((p) => p.name == name)) {
stderr.writeln("Tag does not match a repo package ('$tag').");
return false;
}
package = packages.firstWhere((p) => p.name == name);
}
print('');
print('Publishing ${'package:${package.name}'}');
print('');
print('pubspec:');
var pubspecVersion = package.pubspec.version;
print(' version: $pubspecVersion');
print('changelog:');
print(package.changelog.describeLatestChanges);
var changelogVersion = package.changelog.latestVersion;
if (pubspecVersion != tag.version) {
stderr.writeln(
"Pubspec version ($pubspecVersion) and git tag ($tag) don't agree.");
return false;
}
if (pubspecVersion != changelogVersion) {
stderr.writeln('Pubspec version ($pubspecVersion) and changelog version '
"($changelogVersion) don't agree.");
return false;
}
await runCommand('dart', args: ['pub', 'get'], cwd: package.directory);
print('');
var result = await runCommand('dart',
args: ['pub', 'publish', '--force'], cwd: package.directory);
if (result != 0) {
exitCode = result;
}
return result == 0;
}
}
class VerificationResults {
final List<Result> results = [];
void addResult(Result result) => results.add(result);
Severity get severity =>
Severity.values[results.map((e) => e.severity.index).fold(0, max)];
bool get hasSuccess => results.any((r) => r.severity == Severity.success);
bool get hasError => results.any((r) => r.severity == Severity.error);
String describeAsMarkdown({bool withTag = true}) {
results.sort((a, b) => Enum.compareByIndex(a.severity, b.severity));
return results.map((r) {
var sev = r.severity == Severity.error ? '(error) ' : '';
var tagColumn = '';
if (withTag) {
var tag = r.gitTag == null ? '' : '`${r.gitTag}`';
var publishReleaseUri = r.publishReleaseUri;
if (publishReleaseUri != null) {
tag = '[$tag]($publishReleaseUri)';
}
tagColumn = ' | $tag';
}
return '| package:${r.package.name} | ${r.package.version} | '
'$sev${r.message}$tagColumn |';
}).join('\n');
}
}
class Result {
final Severity severity;
final Package package;
final String message;
final String? gitTag;
final Uri? publishReleaseUri;
Result(this.severity, this.package, this.message,
[this.gitTag, this.publishReleaseUri]);
factory Result.fail(Package package, String message) =>
Result(Severity.error, package, message);
factory Result.info(Package package, String message) =>
Result(Severity.info, package, message);
factory Result.success(Package package, String message,
[String? gitTag, Uri? publishReleaseUri]) =>
Result(Severity.success, package, message, gitTag, publishReleaseUri);
@override
String toString() {
final details = gitTag == null ? '' : ' ($gitTag)';
return severity == Severity.error
? 'error: $message$details'
: '$message$details';
}
}