diff --git a/.github/workflows/autofix.yml b/.github/workflows/autofix.yml new file mode 100644 index 0000000..45f1a8d --- /dev/null +++ b/.github/workflows/autofix.yml @@ -0,0 +1,50 @@ +# ------------------------------------------------------------------- +# ------------------------------- WARNING --------------------------- +# ------------------------------------------------------------------- +# +# This file was automatically generated by gh-workflows using the +# gh-workflow-gen bin. You should add and commit this file to your +# git repository. **DO NOT EDIT THIS FILE BY HAND!** Any manual changes +# will be lost if the file is regenerated. +# +# To make modifications, update your `build.rs` configuration to adjust +# the workflow description as needed, then regenerate this file to apply +# those changes. +# +# ------------------------------------------------------------------- +# ----------------------------- END WARNING ------------------------- +# ------------------------------------------------------------------- + +name: autofix.ci +env: + RUSTFLAGS: -Dwarnings +on: + pull_request: + types: + - opened + - synchronize + - reopened + branches: + - main + push: + branches: + - main +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout Code + uses: actions/checkout@v4 + - name: Setup Rust Toolchain + uses: actions-rust-lang/setup-rust-toolchain@v1 + with: + toolchain: nightly + components: clippy, rustfmt + - name: Cargo Fmt + run: cargo +nightly fmt --all + - name: Cargo Clippy + run: cargo +nightly clippy --fix --allow-dirty --all-features --workspace -- -D warnings + - uses: autofix-ci/action@ff86a557419858bb967097bfc916833f5647fa8c diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4f1dd49..ba24eb5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,10 +58,9 @@ jobs: toolchain: nightly components: clippy, rustfmt - name: Cargo Fmt - run: cargo +nightly fmt --all + run: cargo +nightly fmt --all --check - name: Cargo Clippy - run: cargo +nightly clippy --fix --allow-dirty --all-features --workspace -- -D warnings - - uses: autofix-ci/action@ff86a557419858bb967097bfc916833f5647fa8c + run: cargo +nightly clippy --all-features --workspace -- -D warnings release: needs: - build @@ -83,12 +82,15 @@ jobs: uses: release-plz/action@v0.5 with: command: release + concurrency: + group: release-${{github.ref}} + cancel-in-progress: false release-pr: needs: - build - lint if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }} - name: Release PR + name: Release Pr runs-on: ubuntu-latest permissions: contents: write diff --git a/Cargo.lock b/Cargo.lock index 39464ee..c9ac573 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,6 +155,7 @@ version = "0.1.4" dependencies = [ "derive_setters", "gh-workflow", + "heck", "insta", "pretty_assertions", ] diff --git a/crates/gh-workflow-tailcall/Cargo.toml b/crates/gh-workflow-tailcall/Cargo.toml index f562ffd..1d63755 100644 --- a/crates/gh-workflow-tailcall/Cargo.toml +++ b/crates/gh-workflow-tailcall/Cargo.toml @@ -14,6 +14,7 @@ keywords = ["github", "actions", "workflow", "generator"] [dependencies] derive_setters = { version = "0.1.6" } gh-workflow = { path = "../gh-workflow", version = "0.5.5" } +heck = "0.5.0" [dev-dependencies] insta = "1.40.0" diff --git a/crates/gh-workflow-tailcall/src/workflow.rs b/crates/gh-workflow-tailcall/src/workflow.rs index 0a114d1..0034e22 100644 --- a/crates/gh-workflow-tailcall/src/workflow.rs +++ b/crates/gh-workflow-tailcall/src/workflow.rs @@ -7,8 +7,10 @@ use ctx::Context; use derive_setters::Setters; +use generate::Generate; use gh_workflow::error::Result; use gh_workflow::{Workflow as GHWorkflow, *}; +use heck::ToTitleCase; use release_plz::{Command, Release}; use toolchain::Toolchain; @@ -43,45 +45,50 @@ impl Default for Workflow { impl Workflow { /// Generates and tests the workflow file. pub fn generate(self) -> Result<()> { - let workflow: GHWorkflow = self.into(); - workflow.generate() + self.to_ci_workflow().generate()?; + Generate::new(self.to_autofix_workflow()) + .name("autofix.yml") + .generate()?; + Ok(()) } /// Converts the workflow into a Github workflow. - pub fn to_github_workflow(&self) -> GHWorkflow { + fn to_autofix_workflow(&self) -> GHWorkflow { + GHWorkflow::new("autofix.ci") + .add_env(self.workflow_flags()) + .on(self.workflow_event()) + .add_job("lint", self.lint_job(true)) + } + + /// Converts the workflow into a Github workflow. + fn to_ci_workflow(&self) -> GHWorkflow { GHWorkflow::new(self.name.clone()) .add_env(self.workflow_flags()) .on(self.workflow_event()) + // TODO: adding build and lint should not be required .add_job("build", self.test_job()) - .add_job("lint", self.lint_job()) - .add_job_when(self.auto_release, "release", self.release_job()) - .add_job_when(self.auto_release, "release-pr", self.release_pr_job()) + .add_job("lint", self.lint_job(false)) + .add_job_when( + self.auto_release, + "release", + self.release_job(Command::Release), + ) + .add_job_when( + self.auto_release, + "release-pr", + self.release_job(Command::ReleasePR), + ) } - fn release_pr_job(&self) -> Job { - Job::new("Release PR") - .cond(self.workflow_cond()) + fn release_job(&self, cmd: Command) -> Job { + Job::new(cmd.to_string().to_title_case()) .concurrency( Concurrency::new(Expression::new("release-${{github.ref}}")) .cancel_in_progress(false), ) - .add_needs(self.test_job()) - .add_needs(self.lint_job()) - .add_env(Env::github()) - .add_env(Env::new( - "CARGO_REGISTRY_TOKEN", - "${{ secrets.CARGO_REGISTRY_TOKEN }}", - )) - .permissions(self.write_permissions()) - .add_step(Step::checkout()) - .add_step(Release::default().command(Command::ReleasePR)) - } - - fn release_job(&self) -> Job { - Job::new("Release") .cond(self.workflow_cond()) .add_needs(self.test_job()) - .add_needs(self.lint_job()) + .add_needs(self.lint_job(false)) .add_env(Env::github()) .add_env(Env::new( "CARGO_REGISTRY_TOKEN", @@ -89,10 +96,10 @@ impl Workflow { )) .permissions(self.write_permissions()) .add_step(Step::checkout()) - .add_step(Release::default().command(Command::Release)) + .add_step(Release::default().command(cmd)) } - fn lint_job(&self) -> Job { + fn lint_job(&self, auto_fix: bool) -> Job { Job::new("Lint") .permissions(Permissions::default().contents(Level::Read)) .add_step(Step::checkout()) @@ -102,18 +109,18 @@ impl Workflow { .name("Cargo Fmt") .nightly() .add_args("--all") - .add_args_when(!self.auto_fix, "--check"), + .add_args_when(!auto_fix, "--check"), ) .add_step( Cargo::new("clippy") .name("Cargo Clippy") .nightly() - .add_args_when(self.auto_fix, "--fix") - .add_args_when(self.auto_fix, "--allow-dirty") + .add_args_when(auto_fix, "--fix") + .add_args_when(auto_fix, "--allow-dirty") .add_args("--all-features --workspace -- -D warnings"), ) .add_step_when( - self.auto_fix, + auto_fix, Step::uses( "autofix-ci", "action", @@ -169,9 +176,3 @@ impl Workflow { RustFlags::deny("warnings") } } - -impl From for GHWorkflow { - fn from(value: Workflow) -> Self { - value.to_github_workflow() - } -}