Skip to content
109 changes: 30 additions & 79 deletions tool/dash_site/lib/src/commands/stage_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import 'dart:convert';
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:github/github.dart' as github;
import 'package:path/path.dart' as path;

import '../firebase.dart';
import '../preview_changes.dart';
import '../preview_comments.dart';
import '../sites.dart';
import '../utils.dart';
import 'build.dart';
Expand Down Expand Up @@ -164,13 +164,39 @@ final class StagePreviewCommand extends Command<int> {
return 0;
}

/// Records [status] for the selected site in
/// the pull request's preview comment.
///
/// Does nothing and returns `0` if there's no pull request context.
Future<int> updatePreviewComment(
PreviewStatus status, {
String? stagingUrl,
}) async {
if (prContext case final context?) {
return await updatePreviewCommentOnGitHub(
site: selectedSite,
status: status,
stagingUrl: stagingUrl,
prNumber: context.prNumber,
repoFullName: context.repoFullName,
commitSha: context.commitSha,
githubToken: context.githubToken,
);
}
return 0;
}

await updatePreviewComment(PreviewStatus.pending);

final firebaseToolsVersion = await validateFirebaseCli();
if (firebaseToolsVersion == null) {
await updatePreviewComment(PreviewStatus.failed);
return 1;
}

if (await buildSite(selectedSite, productionRelease: false)
case final buildResult when buildResult != 0) {
await updatePreviewComment(PreviewStatus.failed);
return buildResult;
}

Expand All @@ -189,6 +215,7 @@ final class StagePreviewCommand extends Command<int> {
expires: expires,
);
if (stagingUrl == null) {
await updatePreviewComment(PreviewStatus.failed);
return 1;
}

Expand All @@ -198,10 +225,9 @@ final class StagePreviewCommand extends Command<int> {
return 0;
}

return await _commentStagingUrlOnGitHub(
site: selectedSite,
return await updatePreviewComment(
PreviewStatus.ready,
stagingUrl: stagingUrl,
context: prContext,
);
}
}
Expand Down Expand Up @@ -270,81 +296,6 @@ String? _extractDeployedUrl(String firebaseJsonOutput) {
return null;
}

/// Posts a new preview comment on the pull request,
/// or updates the existing one identified by an HTML marker that
/// includes the [site]'s name.
///
/// Returns `0` on success or `1` if the GitHub API call fails.
Future<int> _commentStagingUrlOnGitHub({
required Site site,
required String stagingUrl,
required _PullRequestContext context,
}) async {
final commentMarker = '<!-- flutter-preview-${site.name} -->';
final commentBody =
'''
$commentMarker
Staged preview of the updated ${site.host} site (updated for commit ${context.commitSha}):

$stagingUrl''';

print('Commenting ${site.host} staging URL on the PR...');
final gitHub = github.GitHub(
auth: github.Authentication.withToken(context.githubToken),
);
try {
final repository = github.RepositorySlug.full(context.repoFullName);
final existingCommentId = await _findExistingPreviewCommentId(
gitHub: gitHub,
repository: repository,
issueNumber: context.prNumber,
commentMarker: commentMarker,
);

if (existingCommentId == null) {
await gitHub.issues.createComment(
repository,
context.prNumber,
commentBody,
);
} else {
await gitHub.issues.updateComment(
repository,
existingCommentId,
commentBody,
);
}
} on github.GitHubError catch (error) {
stderr.writeln('Error: Failed to comment on the pull request: $error');
return 1;
} finally {
gitHub.dispose();
}

return 0;
}

/// Returns the id of the first comment on the issue whose body
/// contains the specified [commentMarker].
///
/// Returns `null` if no matching comment exists.
Future<int?> _findExistingPreviewCommentId({
required github.GitHub gitHub,
required github.RepositorySlug repository,
required int issueNumber,
required String commentMarker,
}) async {
await for (final comment in gitHub.issues.listCommentsByIssue(
repository,
issueNumber,
)) {
if (comment.body?.contains(commentMarker) ?? false) {
return comment.id;
}
}
return null;
}

/// Builds a Firebase Hosting channel name for [site] that
/// incorporates [branchOrSha] and if specified, [prNumber],
/// and satisfies Firebase's naming constraints:
Expand Down
Loading
Loading