diff --git a/src/conventional_commits.rs b/src/conventional_commits.rs deleted file mode 100644 index 09ef439..0000000 --- a/src/conventional_commits.rs +++ /dev/null @@ -1,7 +0,0 @@ -pub fn is_conventional_commit(commit_message: &str) -> eyre::Result { - let re = regex::Regex::new( - r"(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z ]+\))?: .{1,72}", - )?; - - Ok(re.is_match(commit_message)) -} diff --git a/src/main.rs b/src/main.rs index e010f2e..a133f2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,20 @@ -mod bump; -mod conventional_commits; mod gitea; +mod semantic; +mod validate_pr; use clap::Command; -use tracing_subscriber::EnvFilter; +use tracing_subscriber::{filter::LevelFilter, EnvFilter}; const VERSION: &'static str = ""; #[tokio::main] async fn main() -> eyre::Result<()> { + dotenv::dotenv().unwrap(); color_eyre::install().unwrap(); tracing_subscriber::fmt() .pretty() - .with_env_filter(EnvFilter::from_default_env()) + .with_env_filter(EnvFilter::default().add_directive(LevelFilter::INFO.into())) .init(); let matches = clap::Command::new("releaser") @@ -24,12 +25,44 @@ async fn main() -> eyre::Result<()> { .subcommand( Command::new("validate").about("Validate the semantic versioning of the software"), ) + .subcommand( + Command::new("validate:pr") + .about("Validate the semantic versioning of the software") + .arg(clap::Arg::new("owner").long("owner").required(true)) + .arg(clap::Arg::new("repo").long("repo").required(true)) + .arg( + clap::Arg::new("pull_request_id") + .long("pull-request-id") + .required(true) + .value_parser(clap::value_parser!(u32).range(0..)), + ) + .arg( + clap::Arg::new("current_version") + .long("current-version") + .required(true), + ), + ) .subcommand(Command::new("bump").about("Bump the semantic versioning in git tags")) .get_matches(); match matches.subcommand() { Some(("release", sub_matches)) => {} Some(("validate", sub_matches)) => {} + Some(("validate:pr", sub_matches)) => { + let owner = sub_matches.get_one::("owner").unwrap(); + let repo = sub_matches.get_one::("repo").unwrap(); + let pull_request_id = sub_matches.get_one::("pull_request_id").unwrap(); + let current_version = sub_matches.get_one::("current_version").unwrap(); + + match validate_pr::validate_pr(&owner, &repo, *pull_request_id, ¤t_version) + .await? + { + Some(version) => { + tracing::info!(version = version, "bumping version") + } + None => tracing::info!(version = current_version, "no version bump required!"), + } + } Some(("bump", sub_matches)) => {} _ => unreachable!(), } diff --git a/src/bump.rs b/src/semantic.rs similarity index 94% rename from src/bump.rs rename to src/semantic.rs index 5a032e8..095b13f 100644 --- a/src/bump.rs +++ b/src/semantic.rs @@ -80,7 +80,16 @@ fn increment_major(v: &mut Version) { } pub fn bump_semver(semver: &str, bump_type: BumpType) -> Result { - let mut version = Version::parse(semver)?; + let mut contains_start_v = false; + + let semver = if semver.starts_with("v") { + contains_start_v = true; + semver.replace("v", "") + } else { + semver.to_string() + }; + + let mut version = Version::parse(&semver)?; match bump_type { BumpType::Major => increment_major(&mut version), @@ -88,7 +97,11 @@ pub fn bump_semver(semver: &str, bump_type: BumpType) -> Result increment_patch(&mut version), } - Ok(version.to_string()) + if contains_start_v { + Ok(format!("v{version}")) + } else { + Ok(version.to_string()) + } } #[cfg(test)] diff --git a/src/validate_pr.rs b/src/validate_pr.rs new file mode 100644 index 0000000..227be6f --- /dev/null +++ b/src/validate_pr.rs @@ -0,0 +1,18 @@ +pub async fn validate_pr( + owner: &str, + repo: &str, + pull_request_id: u32, + current_version: &str, +) -> eyre::Result> { + let base_url = &std::env::var("GITEA_BASE_URL").unwrap(); + let token = &std::env::var("GITEA_ACCESS_TOKEN").unwrap(); + + let commit_titles = + crate::gitea::get_pull_request_commits(base_url, owner, repo, pull_request_id, token) + .await?; + + match crate::semantic::get_most_significant_bump(&commit_titles) { + Some(bump) => Ok(Some(crate::semantic::bump_semver(current_version, bump)?)), + None => Ok(None), + } +}