-
Notifications
You must be signed in to change notification settings - Fork 12
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
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1c48ef9
create auto commit api
dekkku f2d6ebf
add push capability to auto commit
dekkku 95c2985
workflow update
dekkku c9b1a02
impl default for autocommit
dekkku cfddf58
add typesafe way to skip committing on specified branches
dekkku 4fd068e
configure step to run only on pull requests
dekkku File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} | ||
|
||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use these settings to configure how and where do we wish to commit and push.