Skip to content

Commit

Permalink
new: Allow actions to be skipped using env vars. (#1105)
Browse files Browse the repository at this point in the history
* Start on.

* Update tests.

* Update types.
  • Loading branch information
milesj authored Oct 8, 2023
1 parent 4141d18 commit ccb5491
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .yarn/versions/22a0caa2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ releases:
'@moonrepo/core-macos-x64': minor
'@moonrepo/core-windows-x64-msvc': minor
'@moonrepo/visualizer': minor
'@moonrepo/types': minor
'@moonrepo/report': minor

declined:
- '@moonrepo/runtime'
- website
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MOON_PROJECT_ID=node
MOON_PROJECT_ROOT=<WORKSPACE>/base
MOON_PROJECT_SNAPSHOT=<WORKSPACE>/.moon/cache/states/node/snapshot.json
MOON_PROJECT_SOURCE=base
MOON_RUNNING_ACTION=run-target
MOON_RUNNING_ACTION=run-task
MOON_TARGET=node:envVarsMoon
MOON_TOOLCHAIN_DIR=~/.proto
MOON_WORKING_DIR=<WORKSPACE>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MOON_PROJECT_ID=rust
MOON_PROJECT_ROOT=<WORKSPACE>
MOON_PROJECT_SNAPSHOT=<WORKSPACE>/.moon/cache/states/rust/snapshot.json
MOON_PROJECT_SOURCE=.
MOON_RUNNING_ACTION=run-target
MOON_RUNNING_ACTION=run-task
MOON_TARGET=rust:envVarsMoon
MOON_TOOLCHAIN_DIR=~/.proto
MOON_WORKING_DIR=<WORKSPACE>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ MOON_PROJECT_ID=unix
MOON_PROJECT_ROOT=<WORKSPACE>/unix
MOON_PROJECT_SNAPSHOT=<WORKSPACE>/.moon/cache/states/unix/snapshot.json
MOON_PROJECT_SOURCE=unix
MOON_RUNNING_ACTION=run-target
MOON_RUNNING_ACTION=run-task
MOON_TARGET=unix:envVarsMoon
MOON_TOOLCHAIN_DIR=~/.proto
MOON_WORKING_DIR=<WORKSPACE>
Expand Down
10 changes: 10 additions & 0 deletions crates/core/action-pipeline/src/actions/install_deps.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::should_skip_action_matching;
use miette::IntoDiagnostic;
use moon_action::{Action, ActionStatus};
use moon_action_context::ActionContext;
Expand Down Expand Up @@ -63,6 +64,15 @@ pub async fn install_deps(
return Ok(ActionStatus::Skipped);
}

if should_skip_action_matching("MOON_SKIP_INSTALL_DEPS", &install_key) {
debug!(
target: LOG_TARGET,
"Skipping install deps action because MOON_SKIP_INSTALL_DEPS is set",
);

return Ok(ActionStatus::Skipped);
}

// When the install is happening as a child process of another install, avoid recursion
if env::var("MOON_INSTALLING_DEPS").unwrap_or_default() == install_key {
debug!(
Expand Down
71 changes: 70 additions & 1 deletion crates/core/action-pipeline/src/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,74 @@
use std::env;

pub mod install_deps;
pub mod run_target;
pub mod run_task;
pub mod setup_tool;
pub mod sync_project;
pub mod sync_workspace;

pub fn should_skip_action(key: &str) -> bool {
env::var(key).is_ok_and(|v| matches_pattern(&v, ""))
}

pub fn should_skip_action_matching<V: AsRef<str>>(key: &str, pattern: V) -> bool {
env::var(key).is_ok_and(|v| matches_pattern(&v, pattern.as_ref()))
}

fn matches_pattern(value: &str, pattern: &str) -> bool {
if value.contains(',') {
return value.split(',').any(|v| matches_pattern(v, pattern));
}

let pattern = pattern.to_lowercase();

if value == "*" || value == "*:*" || value == "true" || value == pattern {
return true;
}

if pattern.contains(':') {
let mut left = pattern.split(':');
let mut right = value.split(':');

return match ((left.next(), left.next()), (right.next(), right.next())) {
#[allow(clippy::nonminimal_bool)]
((Some(a1), Some(a2)), (Some(b1), Some(b2))) => {
// foo:bar == foo:bar
a1 == b1 && a2 == b2 ||
// foo:bar == foo:*
a1 == b1 && b2 == "*" ||
// foo:bar == *:bar
a2 == b2 && b1 == "*"
}
((Some(a1), Some(_)), (Some(b1), None)) => {
// foo:bar == foo
a1 == b1
}
_ => false,
};
}

false
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn patterns() {
assert!(matches_pattern("*", ""));
assert!(matches_pattern("*:*", ""));
assert!(matches_pattern("true", ""));

assert!(matches_pattern("*", "node:20.0.0"));
assert!(matches_pattern("node:*", "node:20.0.0"));
assert!(matches_pattern("node", "node:20.0.0"));
assert!(matches_pattern("node:20.0.0", "node:20.0.0"));
assert!(!matches_pattern("rust", "node:20.0.0"));
assert!(!matches_pattern("node:19.0.0", "node:20.0.0"));

assert!(matches_pattern("foo,bar", "foo"));
assert!(matches_pattern("foo,bar", "bar"));
assert!(!matches_pattern("foo,bar", "baz"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use std::env;
use std::sync::Arc;
use tokio::sync::RwLock;

const LOG_TARGET: &str = "moon:action:run-target";
const LOG_TARGET: &str = "moon:action:run-task";

pub async fn run_target(
pub async fn run_task(
action: &mut Action,
context: Arc<RwLock<ActionContext>>,
emitter: Arc<RwLock<Emitter>>,
Expand All @@ -24,7 +24,7 @@ pub async fn run_target(
target: &Target,
runtime: &Runtime,
) -> miette::Result<ActionStatus> {
env::set_var("MOON_RUNNING_ACTION", "run-target");
env::set_var("MOON_RUNNING_ACTION", "run-task");

let emitter = emitter.read().await;
let workspace = workspace.read().await;
Expand All @@ -33,7 +33,7 @@ pub async fn run_target(

debug!(
target: LOG_TARGET,
"Running target {}",
"Running task {}",
color::label(&task.target)
);

Expand Down
13 changes: 13 additions & 0 deletions crates/core/action-pipeline/src/actions/setup_tool.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::should_skip_action_matching;
use moon_action::{Action, ActionStatus};
use moon_action_context::ActionContext;
use moon_cache_item::cache_item;
Expand Down Expand Up @@ -38,6 +39,18 @@ pub async fn setup_tool(
runtime.label()
);

if should_skip_action_matching(
"MOON_SKIP_SETUP_TOOL",
format!("{}:{}", runtime, runtime.requirement),
) {
debug!(
target: LOG_TARGET,
"Skipping setup tool action because MOON_SKIP_SETUP_TOOL is set",
);

return Ok(ActionStatus::Skipped);
}

let workspace = workspace.write().await;
let context = context.read().await;

Expand Down
10 changes: 10 additions & 0 deletions crates/core/action-pipeline/src/actions/sync_project.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::should_skip_action_matching;
use moon_action::{Action, ActionStatus};
use moon_action_context::ActionContext;
use moon_logger::debug;
Expand Down Expand Up @@ -34,6 +35,15 @@ pub async fn sync_project(
color::id(&project.id)
);

if should_skip_action_matching("MOON_SKIP_SYNC_PROJECT", &project.id) {
debug!(
target: LOG_TARGET,
"Skipping sync project action because MOON_SKIP_SYNC_PROJECT is set",
);

return Ok(ActionStatus::Skipped);
}

// Create a snapshot for tasks to reference
workspace
.cache_engine
Expand Down
10 changes: 10 additions & 0 deletions crates/core/action-pipeline/src/actions/sync_workspace.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::should_skip_action;
use moon_action::{Action, ActionStatus};
use moon_action_context::ActionContext;
use moon_actions::{sync_codeowners, sync_vcs_hooks};
Expand Down Expand Up @@ -28,6 +29,15 @@ pub async fn sync_workspace(

debug!(target: LOG_TARGET, "Syncing workspace");

if should_skip_action("MOON_SKIP_SYNC_WORKSPACE") {
debug!(
target: LOG_TARGET,
"Skipping sync workspace action because MOON_SKIP_SYNC_WORKSPACE is set",
);

return Ok(ActionStatus::Skipped);
}

if workspace.config.codeowners.sync_on_run {
debug!(
target: LOG_TARGET,
Expand Down
4 changes: 2 additions & 2 deletions crates/core/action-pipeline/src/processor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::actions::install_deps::install_deps;
use crate::actions::run_target::run_target;
use crate::actions::run_task::run_task;
use crate::actions::setup_tool::setup_tool;
use crate::actions::sync_project::sync_project;
use crate::actions::sync_workspace::sync_workspace;
Expand Down Expand Up @@ -178,7 +178,7 @@ pub async fn process_action(

local_emitter.emit(Event::TargetRunning { target }).await?;

let run_result = run_target(
let run_result = run_task(
&mut action,
context,
emitter,
Expand Down
2 changes: 1 addition & 1 deletion crates/node/platform/src/actions/run_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_hash::FxHashMap;
use starbase_styles::color;
use std::path::Path;

const LOG_TARGET: &str = "moon:node-platform:run-target";
const LOG_TARGET: &str = "moon:node-platform:run-task";

fn create_node_options(
node_config: &NodeConfig,
Expand Down
1 change: 1 addition & 0 deletions nextgen/action-graph/src/action_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::Serialize;
use std::hash::{Hash, Hasher};

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(tag = "action", content = "params")]
pub enum ActionNode {
/// Install tool dependencies in the workspace root.
InstallDeps { runtime: Runtime },
Expand Down
1 change: 1 addition & 0 deletions nextgen/platform-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt;
use std::hash::{Hash, Hasher};

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(untagged)]
pub enum RuntimeReq {
// Use tool available on PATH
Global,
Expand Down
5 changes: 3 additions & 2 deletions packages/types/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Duration {
}

export interface Runtime {
platform: Capitalize<PlatformType>;
version?: string;
platform: PlatformType;
requirement?: string;
overridden?: boolean;
}
46 changes: 31 additions & 15 deletions packages/types/src/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Attempt {
}

export interface Action {
allowFailure?: boolean;
attempts: Attempt[] | null;
createdAt: string;
duration: Duration | null;
Expand Down Expand Up @@ -75,37 +76,52 @@ export interface RunReport {
export type ActionNode =
| ActionNodeInstallDeps
| ActionNodeInstallProjectDeps
| ActionNodeRunPersistentTarget
| ActionNodeRunTarget
| ActionNodeRunTask
| ActionNodeSetupTool
| ActionNodeSyncProject;
| ActionNodeSyncProject
| ActionNodeSyncWorkspace;

export interface ActionNodeInstallDeps {
action: 'InstallDeps';
params: Runtime;
params: {
runtime: Runtime;
};
}

export interface ActionNodeInstallProjectDeps {
action: 'InstallProjectDeps';
params: [Runtime, string];
}

export interface ActionNodeRunTarget {
action: 'RunTarget';
params: [Runtime, string];
params: {
runtime: Runtime;
project: string;
};
}

export interface ActionNodeRunPersistentTarget {
action: 'RunPersistentTarget';
params: [Runtime, string];
export interface ActionNodeRunTask {
action: 'RunTask';
params: {
interactive: boolean;
persistent: boolean;
runtime: Runtime;
target: string;
};
}

export interface ActionNodeSetupTool {
action: 'SetupTool';
params: Runtime;
params: {
runtime: Runtime;
};
}

export interface ActionNodeSyncProject {
action: 'SyncProject';
params: [Runtime, string];
params: {
runtime: Runtime;
project: string;
};
}

export interface ActionNodeSyncWorkspace {
action: 'SyncWorkspace';
params: {};
}
7 changes: 7 additions & 0 deletions website/src/utils/renderGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export function renderGraph(element: HTMLElement, graph: cytoscape.ElementsDefin
width: 60,
},
},
{
selector: 'node[type="run-task"], node[type="sm"]',
style: {
// @ts-expect-error Types incorrect
'background-gradient-stop-colors': '#6e58d1 #4a2ec6 #3b259e',
},
},
{
selector: 'node[type="run-target"], node[type="sm"]',
style: {
Expand Down

0 comments on commit ccb5491

Please sign in to comment.