Skip to content

Commit

Permalink
new: Track operations for other actions. (#1476)
Browse files Browse the repository at this point in the history
* Switch to a meta enum.

* Fix tests.

* Update ts.

* Add to sync workspace.

* Add to install deps.

* Add track methods.

* Move constructors.

* Polish.

* Check df.

* Turn off cache.
  • Loading branch information
milesj authored May 24, 2024
1 parent a81b063 commit 792aafa
Show file tree
Hide file tree
Showing 33 changed files with 562 additions and 298 deletions.
1 change: 1 addition & 0 deletions .github/workflows/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
- uses: actions/setup-node@v4
- uses: moonrepo/setup-rust@v1
with:
cache: false # Temporary, CI running out of space
cache-base: '^(master|develop-)'
- run: cargo run -- --color --log trace ci --base ${{ github.base_ref || 'master' }}
env:
Expand Down
5 changes: 5 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/bun/platform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"
publish = false

[dependencies]
moon_action = { path = "../../../nextgen/action" }
moon_action_context = { path = "../../../nextgen/action-context" }
moon_bun_lang = { path = "../lang" }
moon_bun_tool = { path = "../tool" }
Expand Down
16 changes: 11 additions & 5 deletions crates/bun/platform/src/actions/install_deps.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use moon_action::Operation;
use moon_bun_tool::BunTool;
use moon_console::{Checkpoint, Console};
use moon_lang::has_vendor_installed_dependencies;
Expand All @@ -12,7 +13,9 @@ pub async fn install_deps(
bun: &BunTool,
working_dir: &Path,
console: &Console,
) -> miette::Result<()> {
) -> miette::Result<Vec<Operation>> {
let mut operations = vec![];

// When in CI, we can avoid installing dependencies because
// we can assume they've already been installed before moon runs!
if is_ci() && has_vendor_installed_dependencies(working_dir, "node_modules") {
Expand All @@ -21,7 +24,7 @@ pub async fn install_deps(
"In a CI environment and dependencies already exist, skipping install"
);

return Ok(());
return Ok(operations);

Check warning on line 27 in crates/bun/platform/src/actions/install_deps.rs

View check run for this annotation

Codecov / codecov/patch

crates/bun/platform/src/actions/install_deps.rs#L27

Added line #L27 was not covered by tests
}

debug!(target: LOG_TARGET, "Installing dependencies");
Expand All @@ -30,8 +33,11 @@ pub async fn install_deps(
.out
.print_checkpoint(Checkpoint::Setup, "bun install")?;

bun.install_dependencies(&(), working_dir, !is_test_env())
.await?;
operations.push(
Operation::task_execution("bun install")
.track_async(|| bun.install_dependencies(&(), working_dir, !is_test_env()))
.await?,
);

Ok(())
Ok(operations)
}
7 changes: 3 additions & 4 deletions crates/bun/platform/src/bun_platform.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::actions;
use crate::infer_tasks_from_scripts;
use moon_action::Operation;
use moon_action_context::ActionContext;
use moon_bun_tool::{get_bun_env_paths, BunTool};
use moon_common::Id;
Expand Down Expand Up @@ -321,15 +322,13 @@ impl Platform for BunPlatform {
_context: &ActionContext,
runtime: &Runtime,
working_dir: &Path,
) -> miette::Result<()> {
) -> miette::Result<Vec<Operation>> {
actions::install_deps(
self.toolchain.get_for_version(&runtime.requirement)?,
working_dir,
&self.console,
)
.await?;

Ok(())
.await
}

async fn sync_project(
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/src/commands/syncs/codeowners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ pub async fn sync(args: ArgsRef<SyncCodeownersArgs>, workspace: ResourceMut<Work
done(
format!(
"Successfully created {}",
color::path(codeowners_path.strip_prefix(&workspace.root).unwrap())
if let Some(path) = codeowners_path {
color::path(path.strip_prefix(&workspace.root).unwrap())
} else {
"code owners".into()
}
),
true,
);
Expand Down
15 changes: 12 additions & 3 deletions crates/core/action-pipeline/src/actions/install_deps.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::should_skip_action_matching;
use miette::IntoDiagnostic;
use moon_action::{Action, ActionStatus};
use moon_action::{Action, ActionStatus, Operation};
use moon_action_context::ActionContext;
use moon_cache_item::cache_item;
use moon_logger::{debug, warn};
Expand Down Expand Up @@ -38,7 +38,7 @@ fn get_installation_key(runtime: &Runtime, project: Option<&Project>) -> String
}

pub async fn install_deps(
_action: &mut Action,
action: &mut Action,
context: Arc<ActionContext>,
workspace: Arc<Workspace>,
runtime: &Runtime,
Expand Down Expand Up @@ -110,6 +110,8 @@ pub async fn install_deps(
};

// Determine the working directory and whether lockfiles and manifests have been modified
let mut operation = Operation::hash_generation();

let working_dir = project.map(|p| &p.root).unwrap_or_else(|| &workspace.root);
let manifest_path = working_dir.join(&manifest);
let lockfile_path = working_dir.join(&lockfile);
Expand Down Expand Up @@ -148,6 +150,11 @@ pub async fn install_deps(
// Install dependencies in the working directory
let hash = workspace.cache_engine.hash.save_manifest(hasher)?;

operation.meta.set_hash(&hash);
operation.finish(ActionStatus::Passed);

action.operations.push(operation);

let state_path = format!("deps{runtime}.json");
let mut state = workspace
.cache_engine
Expand Down Expand Up @@ -175,14 +182,16 @@ pub async fn install_deps(
color::path(working_dir)
);

platform
let operations = platform
.install_deps(&context, runtime, working_dir)
.await?;

state.data.last_hash = hash;
state.data.last_install_time = time::now_millis();
state.save()?;

action.operations.extend(operations);

env::remove_var("MOON_INSTALLING_DEPS");

return Ok(ActionStatus::Passed);
Expand Down
7 changes: 6 additions & 1 deletion crates/core/action-pipeline/src/actions/run_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ pub async fn run_task(

let task = project.get_task(&target.task_id)?;

// Must be set before running the task in case it fails and
// and error is bubbled up the stack
action.allow_failure = task.options.allow_failure;

let result = TaskRunner::new(&workspace, project, task, console)?
.run(&context, &action.node)
.await?;
let operations = result.operations;

action.set_operations(result.operations, &task.command);
action.flaky = operations.is_flaky();
action.status = operations.get_final_status();
action.operations = operations;

if action.has_failed() && action.allow_failure {
warn!(

Check warning on line 46 in crates/core/action-pipeline/src/actions/run_task.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/action-pipeline/src/actions/run_task.rs#L46

Added line #L46 was not covered by tests
Expand Down
19 changes: 15 additions & 4 deletions crates/core/action-pipeline/src/actions/sync_workspace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::should_skip_action;
use moon_action::{Action, ActionStatus};
use moon_action::{Action, ActionStatus, Operation};
use moon_action_context::ActionContext;
use moon_actions::{sync_codeowners, sync_vcs_hooks};
use moon_common::is_docker_container;
Expand All @@ -14,7 +14,7 @@ use std::sync::Arc;
const LOG_TARGET: &str = "moon:action:sync-workspace";

pub async fn sync_workspace(
_action: &mut Action,
action: &mut Action,
_context: Arc<ActionContext>,
workspace: Arc<Workspace>,
project_graph: Arc<ProjectGraph>,
Expand Down Expand Up @@ -47,7 +47,14 @@ pub async fn sync_workspace(
color::property("codeowners.syncOnRun"),
);

sync_codeowners(&workspace, &project_graph, false).await?;
action.operations.push(
Operation::sync_operation("Codeowners")
.track_async_with_check(
|| sync_codeowners(&workspace, &project_graph, false),
|result| result.is_some(),
)
.await?,

Check warning on line 56 in crates/core/action-pipeline/src/actions/sync_workspace.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/action-pipeline/src/actions/sync_workspace.rs#L56

Added line #L56 was not covered by tests
);
}

if workspace.config.vcs.sync_hooks {
Expand All @@ -58,7 +65,11 @@ pub async fn sync_workspace(
color::property("vcs.syncHooks"),
);

sync_vcs_hooks(&workspace, false).await?;
action.operations.push(
Operation::sync_operation("VCS hooks")
.track_async_with_check(|| sync_vcs_hooks(&workspace, false), |result| result)
.await?,
);
}

Ok(ActionStatus::Passed)
Expand Down
4 changes: 2 additions & 2 deletions crates/core/action-pipeline/src/subscribers/moonbase.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ci_env::get_environment;
use moon_action::{ActionStatus, OperationType};
use moon_action::ActionStatus;
use moon_api::graphql::{
self, add_job_to_run, create_run, update_job, update_run, AddJobToRun, CreateRun, GraphQLQuery,
UpdateJob, UpdateRun,
Expand Down Expand Up @@ -323,7 +323,7 @@ impl Subscriber for MoonbaseSubscriber {
let attempts = action
.operations
.iter()
.filter(|op| matches!(op.type_of, OperationType::TaskExecution))
.filter(|op| op.meta.is_task_execution())
.collect::<Vec<_>>();

if !attempts.is_empty() {

Check warning on line 329 in crates/core/action-pipeline/src/subscribers/moonbase.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/action-pipeline/src/subscribers/moonbase.rs#L323-L329

Added lines #L323 - L329 were not covered by tests
Expand Down
21 changes: 12 additions & 9 deletions crates/core/actions/src/sync_codeowners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub async fn sync_codeowners(
workspace: &Workspace,
project_graph: &ProjectGraph,
force: bool,
) -> miette::Result<PathBuf> {
) -> miette::Result<Option<PathBuf>> {
let mut generator = CodeownersGenerator::new(&workspace.root, workspace.config.vcs.provider)?;

// Sort the projects based on config
Expand Down Expand Up @@ -44,18 +44,21 @@ pub async fn sync_codeowners(
// Force run the generator and bypass cache
if force {
generator.generate()?;

Check warning on line 46 in crates/core/actions/src/sync_codeowners.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/actions/src/sync_codeowners.rs#L46

Added line #L46 was not covered by tests

return Ok(Some(file_path));

Check warning on line 48 in crates/core/actions/src/sync_codeowners.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/actions/src/sync_codeowners.rs#L48

Added line #L48 was not covered by tests
}
// Only generate if the hash has changed
else {
workspace
.cache_engine
.execute_if_changed("codeowners.json", codeowners_hash, || async {
generator.generate()
})
.await?;
else if workspace
.cache_engine
.execute_if_changed("codeowners.json", codeowners_hash, || async {
generator.generate()
})
.await?

Check warning on line 56 in crates/core/actions/src/sync_codeowners.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/actions/src/sync_codeowners.rs#L56

Added line #L56 was not covered by tests
{
return Ok(Some(file_path));
}

Ok(file_path)
Ok(None)

Check warning on line 61 in crates/core/actions/src/sync_codeowners.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/actions/src/sync_codeowners.rs#L61

Added line #L61 was not covered by tests
}

pub async fn unsync_codeowners(workspace: &Workspace) -> miette::Result<PathBuf> {
Expand Down
10 changes: 5 additions & 5 deletions crates/core/actions/src/sync_vcs_hooks.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use moon_vcs_hooks::{HooksGenerator, HooksHash};
use moon_workspace::Workspace;

pub async fn sync_vcs_hooks(workspace: &Workspace, force: bool) -> miette::Result<()> {
pub async fn sync_vcs_hooks(workspace: &Workspace, force: bool) -> miette::Result<bool> {
let vcs_config = &workspace.config.vcs;
let generator = HooksGenerator::new(&workspace.root, &workspace.vcs, vcs_config);

// Force run the generator and bypass cache
if force {
return generator.generate().await;
generator.generate().await?;

Check warning on line 10 in crates/core/actions/src/sync_vcs_hooks.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/actions/src/sync_vcs_hooks.rs#L10

Added line #L10 was not covered by tests

return Ok(true);

Check warning on line 12 in crates/core/actions/src/sync_vcs_hooks.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/actions/src/sync_vcs_hooks.rs#L12

Added line #L12 was not covered by tests
}

// Hash all the hook commands
Expand All @@ -23,9 +25,7 @@ pub async fn sync_vcs_hooks(workspace: &Workspace, force: bool) -> miette::Resul
.execute_if_changed("vcsHooks.json", hooks_hash, || async {
generator.generate().await
})
.await?;

Ok(())
.await
}

pub async fn unsync_vcs_hooks(workspace: &Workspace) -> miette::Result<()> {
Expand Down
1 change: 1 addition & 0 deletions crates/core/platform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"
publish = false

[dependencies]
moon_action = { path = "../../../nextgen/action" }
moon_action_context = { path = "../../../nextgen/action-context" }
moon_common = { path = "../../../nextgen/common" }
moon_config = { path = "../../../nextgen/config" }
Expand Down
5 changes: 3 additions & 2 deletions crates/core/platform/src/platform.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use async_trait::async_trait;
use moon_action::Operation;
use moon_action_context::ActionContext;
use moon_common::Id;
use moon_config::{
Expand Down Expand Up @@ -115,8 +116,8 @@ pub trait Platform: Send + Sync {
context: &ActionContext,
runtime: &Runtime,
working_dir: &Path,
) -> miette::Result<()> {
Ok(())
) -> miette::Result<Vec<Operation>> {
Ok(vec![])

Check warning on line 120 in crates/core/platform/src/platform.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/platform/src/platform.rs#L119-L120

Added lines #L119 - L120 were not covered by tests
}

/// Sync a project (and its dependencies) when applicable.
Expand Down
1 change: 1 addition & 0 deletions crates/deno/platform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"
publish = false

[dependencies]
moon_action = { path = "../../../nextgen/action" }
moon_action_context = { path = "../../../nextgen/action-context" }
moon_common = { path = "../../../nextgen/common" }
moon_config = { path = "../../../nextgen/config" }
Expand Down
Loading

0 comments on commit 792aafa

Please sign in to comment.