Skip to content

feat: GitHub Action customizations #382

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 65 additions & 18 deletions mono_repo/lib/src/commands/github/github_yaml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'dart:collection';

import 'package:path/path.dart';

import '../../basic_config.dart';
import '../../ci_shared.dart';
import '../../github_config.dart';
Expand All @@ -16,6 +18,7 @@ import '../../user_exception.dart';
import '../../yaml.dart';
import 'action_info.dart';
import 'job.dart';
import 'overrides.dart';
import 'step.dart';

const _onCompletionStage = '_on_completion';
Expand Down Expand Up @@ -301,20 +304,40 @@ extension on CIJobEntry {
id: pubStepId,
// Run this regardless of the success of other steps other than the
// pub step.
ifCondition: "always() && steps.checkout.conclusion == 'success'",
ifContent: "always() && steps.checkout.conclusion == 'success'",
workingDirectory: package,
),
);
for (var i = 0; i < commands.length; i++) {
final task = job.tasks[i];
final overrides = task.type.overrides;
var workingDirectory = overrides?.workingDirectory;
if (workingDirectory != null) {
workingDirectory = posix.normalize(
posix.join(package, workingDirectory),
);
}
var ifCondition = overrides?.ifContent;
if (ifCondition != null) {
ifCondition += " && steps.$pubStepId.conclusion == 'success'";
}
commandEntries.add(
_CommandEntry(
'$package; ${job.tasks[i].command}',
_commandForOs(job.tasks[i].command),
type: job.tasks[i].type,
// Run this regardless of the success of other steps other than the
// pub step.
ifCondition: "always() && steps.$pubStepId.conclusion == 'success'",
workingDirectory: package,
overrides?.name ?? '$package; ${task.command}',
_commandForOs(task.command),
type: task.type,
id: overrides?.id,
ifContent: ifCondition ??
// Run this regardless of the success of other steps other than
// the pub step.
"always() && steps.$pubStepId.conclusion == 'success'",
workingDirectory: workingDirectory ?? package,
uses: overrides?.uses,
withContent: overrides?.withContent,
shell: overrides?.shell,
env: overrides?.env,
timeoutMinutes: overrides?.timeoutMinutes,
continueOnError: overrides?.continueOnError,
),
);
}
Expand Down Expand Up @@ -420,30 +443,54 @@ class _CommandEntryBase {
[Step.run(name: name, run: run)];
}

class _CommandEntry extends _CommandEntryBase {
class _CommandEntry extends _CommandEntryBase implements GitHubActionOverrides {
final TaskType? type;

@override
final String? id;
final String? ifCondition;

@override
final String? ifContent;

@override
final String workingDirectory;

@override
final String? uses;

@override
final Map<String, String>? env;

@override
final Map<String, dynamic>? withContent;

@override
final String? shell;

@override
final bool? continueOnError;

@override
final int? timeoutMinutes;

_CommandEntry(
super.name,
super.run, {
required this.workingDirectory,
this.type,
this.id,
this.ifCondition,
this.ifContent,
this.uses,
this.env,
this.withContent,
this.shell,
this.continueOnError,
this.timeoutMinutes,
});

@override
Iterable<Step> runContent(BasicConfiguration config) => [
Step.run(
id: id,
name: name,
ifContent: ifCondition,
workingDirectory: workingDirectory,
run: run,
),
Step.fromOverrides(this),
...?type?.afterEachSteps(workingDirectory, config),
];
}
Expand Down
42 changes: 42 additions & 0 deletions mono_repo/lib/src/commands/github/overrides.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2020, 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.

abstract class GitHubActionOverrides {
/// The step's identifier, which can be used to refer to the step and its
/// outputs in the [ifContent] property of this and other steps.
String? get id;

/// The name of the step.
String? get name;

/// The shell command to run for this step.
String? get run;

/// The GitHub action identifier, e.g. `actions/checkout@v3`.
String? get uses;

/// The inputs to the action.
///
/// A map of key-value pairs which are passed to the action's `with`
/// parameter.
Map<String, dynamic>? get withContent;

/// The condition on which to run this action.
String? get ifContent;

/// The directory in which to run this action.
String? get workingDirectory;

/// The shell override for this action.
String? get shell;

/// The environment variables for the step.
Map<String, String>? get env;

/// Prevents a job from failing when a step fails.
bool? get continueOnError;

/// The number of minutes to allow the step to run.
int? get timeoutMinutes;
}
67 changes: 61 additions & 6 deletions mono_repo/lib/src/commands/github/step.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:json_annotation/json_annotation.dart';

import '../../yaml.dart';
import 'overrides.dart';

part 'step.g.dart';

Expand All @@ -13,21 +14,41 @@ part 'step.g.dart';
includeIfNull: false,
constructor: '_',
)
class Step implements YamlLike {
class Step implements GitHubActionOverrides, YamlLike {
@override
final String? id;
@override
final String? name;
@override
final String? run;

@override
@JsonKey(name: 'if')
final String? ifContent;

@override
@JsonKey(name: 'working-directory')
final String? workingDirectory;

final Map? env;
@override
final Map<String, String>? env;

@override
final String? uses;
@override
@JsonKey(name: 'with')
final Map? withContent;
final Map<String, dynamic>? withContent;

@override
final String? shell;

@override
@JsonKey(name: 'continue-on-error')
final bool? continueOnError;

@override
@JsonKey(name: 'timeout-minutes')
final int? timeoutMinutes;

Step._({
this.id,
Expand All @@ -38,6 +59,9 @@ class Step implements YamlLike {
this.ifContent,
this.workingDirectory,
this.env,
this.shell,
this.continueOnError,
this.timeoutMinutes,
}) {
if (run == null) {
if (uses == null) {
Expand All @@ -64,25 +88,56 @@ class Step implements YamlLike {
}
}

factory Step.fromOverrides(GitHubActionOverrides overrides) {
if (overrides.uses != null) {
return Step._(
id: overrides.id,
name: overrides.name,
uses: overrides.uses,
withContent: overrides.withContent,
ifContent: overrides.ifContent,
continueOnError: overrides.continueOnError,
timeoutMinutes: overrides.timeoutMinutes,
);
}
return Step._(
id: overrides.id,
name: overrides.name,
run: overrides.run,
workingDirectory: overrides.workingDirectory,
env: overrides.env,
shell: overrides.shell,
ifContent: overrides.ifContent,
continueOnError: overrides.continueOnError,
timeoutMinutes: overrides.timeoutMinutes,
);
}

Step.run({
this.id,
required String this.name,
required this.run,
required String this.run,
this.ifContent,
this.workingDirectory,
this.env,
this.shell,
this.continueOnError,
this.timeoutMinutes,
}) : uses = null,
withContent = null;

Step.uses({
this.id,
required String this.name,
required this.uses,
required String this.uses,
this.withContent,
this.ifContent,
this.continueOnError,
this.timeoutMinutes,
}) : run = null,
env = null,
workingDirectory = null;
workingDirectory = null,
shell = null;

factory Step.fromJson(Map json) => _$StepFromJson(json);

Expand Down
23 changes: 20 additions & 3 deletions mono_repo/lib/src/commands/github/step.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading