Skip to content
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

chore: create typesafe auto commit api #57

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
run: cargo +nightly fmt --check
- name: Cargo Clippy
run: cargo +nightly clippy --all-features --workspace -- -D warnings
- name: Auto Commit
run: git config --global user.name 'github-actions' && git config --global user.email '[email protected]' && git add . && git commit -m "lint changes" || echo 'No changes to commit' && git push
release:
needs: build
name: Release
Expand Down
93 changes: 93 additions & 0 deletions src/auto_commit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use derive_setters::Setters;

use crate::{Run, Step};

#[derive(Setters)]
#[setters(strip_option, into)]
pub struct AutoCommit {
/// The commit message to be used.
pub message: String,

/// The unique identifier of the Step.
pub id: Option<String>,

/// Name of the Step.
pub name: Option<String>,

/// Git user name for the commit. Defaults to 'github-actions' if not set.
pub user_name: Option<String>,

/// Git user email for the commit. Defaults to '[email protected]' if not set.
pub user_email: Option<String>,

/// Files to include in the commit. If None, all changes are committed.
pub files: Option<Vec<String>>,

/// Whether to push the changes after committing. Defaults to false if not set.
pub push: Option<bool>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub push: Option<bool>,
pub on_pull_request: Option<bool>,
pub on_branches: Option<Vec<String>>,

We should use these settings to configure how and where do we wish to commit and push.

}

impl AutoCommit {
/// Creates a new `AutoCommit` with the specified commit message.
pub fn new<T: ToString>(msg: T) -> AutoCommit {
AutoCommit {
message: msg.to_string(),
id: Default::default(),
name: Default::default(),
dekkku marked this conversation as resolved.
Show resolved Hide resolved
user_name: Default::default(),
user_email: Default::default(),
files: Default::default(),
push: Default::default(),
}
}
}

impl From<AutoCommit> for Step<Run> {
fn from(value: AutoCommit) -> Self {
let mut commands = Vec::new();

// Configure Git user name
let user_name = value.user_name.unwrap_or_else(|| "github-actions".to_string());
commands.push(format!("git config --global user.name '{}'", user_name));

// Configure Git user email
let user_email = value.user_email.unwrap_or_else(|| "[email protected]".to_string());
commands.push(format!("git config --global user.email '{}'", user_email));

// Add specific files if provided
if let Some(files) = &value.files {
if !files.is_empty() {
let files_str = files.join(" ");
commands.push(format!("git add {}", files_str));
} else {
commands.push("git add .".to_string());
}
} else {
// If no specific files are provided, stage all changes
commands.push("git add .".to_string());
}

// Add the commit command
commands.push(format!("git commit -m \"{}\" || echo 'No changes to commit'", value.message));

// Add push command if enabled
if value.push.unwrap_or(false) {
commands.push("git push".to_string());
}

// Combine all commands into a single shell script
let full_command = commands.join(" && ");

let mut step = Step::run(full_command);

if let Some(id) = value.id {
step = step.id(id);
}

if let Some(name) = value.name {
step = step.name(name);
}

step
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod auto_commit;
mod cargo;
pub mod error;
mod event;
Expand All @@ -7,6 +8,7 @@ mod rust_flag;
pub mod toolchain;
pub(crate) mod workflow;

pub use auto_commit::*;
pub use cargo::*;
pub use event::*;
pub use rust_flag::*;
Expand Down
5 changes: 5 additions & 0 deletions tests/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ fn generate() {
.nightly()
.args("--all-features --workspace -- -D warnings")
.name("Cargo Clippy"),
)
.add_step(
AutoCommit::new("lint changes")
dekkku marked this conversation as resolved.
Show resolved Hide resolved
.name("Auto Commit")
.push(true),
);

let event = Event::default()
Expand Down