Skip to content

Commit

Permalink
feat: release categories for changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Coenraad Human committed May 28, 2024
1 parent 6f0217a commit d4f1dec
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"kind": "bin"
}
},
"args": ["version"],
"args": ["changelog"],
"cwd": "${workspaceFolder}"
},
{
Expand Down
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ edition = "2021"
clap = { version = "4.5.4", features = ["derive"] }
git2 = { version = "0.18.3" }
fancy-regex = { version = "0.13.0" }
chrono = { version = "0.4.38" }
lazy_static = { version = "1.4.0" }
Inflector = { version = "0.11.4" }

openssl = { version = "0.10.64", features = ["vendored"] }
libz-sys = { version = "1.1.16" }
lazy_static = "1.4.0"
Inflector = "0.11.4"
libz-sys = { version = "1.1.16" }
38 changes: 29 additions & 9 deletions src/commands/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ extern crate inflector;
use inflector::Inflector;

use crate::conventional::create_conventional_commit;
use crate::git::{retrieve_branch_commits, Commit};
use crate::git::{retrieve_branch_commits, retrieve_tags};
use crate::semantic::{add_commit_to_version, Version};


pub fn run(path: Option<String>, _commit_git_hook: Option<String>, scope_filter: Option<String>) {
let commits: Vec<Commit> = retrieve_branch_commits(path);
let commits = retrieve_branch_commits(path.clone());
let tags = retrieve_tags(path.clone());

let mut version = Version::new(0, 0, 0);

let mut simple_changelog = "".to_owned();

let mut unreleased_count: usize = 0;

simple_changelog.insert_str(0, "</table>\n");
simple_changelog.insert_str(0, "</tbody>\n");

for commit in commits.iter().rev() {
match commit.message {
Some(ref message) => {
Expand All @@ -22,30 +28,44 @@ pub fn run(path: Option<String>, _commit_git_hook: Option<String>, scope_filter:
Some(conventional_commit) => {

simple_changelog.insert_str(0,
&format!("|{}|{}|{}.|{}|{}|{}|{}|\n",
&format!("<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>\n",
version.format(),
conventional_commit.commit_type,
conventional_commit.commit_description.to_sentence_case(),
if conventional_commit.is_breaking { 'X' } else { ' ' },
if conventional_commit.is_deprecrated { 'X' } else { ' ' },
commit.author.to_string(),
commit.committer.to_string()
commit.author.to_changelog_string(),
commit.committer.to_changelog_string(),
).as_str()
);

},
None => todo!(),
}

let found_version_as_tag = tags.clone()
.into_iter()
.filter(|tag| tag.contains(version.format().as_str()))
.count() > 0;

if found_version_as_tag {
simple_changelog.insert_str(0, &format!("<tr><td colspan=\"7\"><em><strong>Release: {}</strong></em></td></tr>\n", version.format()));
unreleased_count = 0;
} else {
unreleased_count = unreleased_count + 1;
}
},
None => {},
}
}

simple_changelog.insert_str(0, "|---|---|---|---|---|---|---|\n");

simple_changelog.insert_str(0, "|Version|Commit Type|Description|Breaking Change|Deprecation|Author|Committer|\n");
if unreleased_count != 0 {
simple_changelog.insert_str(0, "<tr><td colspan=\"7\"><em><strong>Unreleased</strong></em></td></tr>\n");
}

simple_changelog.insert_str(0, &format!("## {}\n\n", version.format()));
simple_changelog.insert_str(0, "<tbody>\n");
simple_changelog.insert_str(0, "<thead><tr><th>Version</th><th>Commit Type</th><th>Description</th><th>Breaking Change</th><th>Deprecation</th><th>Author</th><th>Committer</th></tr></thead>\n");
simple_changelog.insert_str(0, "<table>\n");

simple_changelog.insert_str(0, "# Changelog\n\n");

Expand Down
59 changes: 39 additions & 20 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::env;

use git2::Repository;

pub struct Author {
Expand All @@ -11,18 +10,18 @@ pub struct Author {

impl Author {

pub fn to_string(&self) -> String {
pub fn to_changelog_string(&self) -> String {
let name = match self.name {
Some(ref value) => value,
None => "Not Available",
};

let email = match self.name {
let email = match self.email {
Some(ref value) => value,
None => "not available",
None => return format!("{}", name),
};

format!("{} <{}>", name, email)
format!("<a href=\"{}\">{}</a>", email, name)
}

}
Expand All @@ -36,18 +35,18 @@ pub struct Committer {

impl Committer {

pub fn to_string(&self) -> String {
pub fn to_changelog_string(&self) -> String {
let name = match self.name {
Some(ref value) => value,
None => "Not Available",
};

let email = match self.name {
let email = match self.email {
Some(ref value) => value,
None => "not available",
None => return format!("{}", name),
};

format!("{} <{}>", name, email)
format!("<a href=\"{}\">{}</a>", email, name)
}

}
Expand All @@ -57,16 +56,22 @@ pub struct Commit {
pub message: Option<String>,
pub author: Author,
pub committer: Committer,
pub is_tagged: bool,

}

impl Commit {

}

pub fn retrieve_branch_commits(path: Option<String>) -> Vec<Commit> {
let found_path = match path {
fn retrieve_git_repository(path: String) -> Repository {
match Repository::open(path) {
Ok(repository) => repository,
Err(_) => panic!("Couldn't open repository"),
}
}

fn determine_path(path: Option<String>) -> String {
match path {
Some(path) => path,
None => {
match env::current_dir() {
Expand All @@ -77,12 +82,28 @@ pub fn retrieve_branch_commits(path: Option<String>) -> Vec<Commit> {
Err(_) => panic!("System can't find current directory"),
}
},
};
}
}

let repo: Repository = match Repository::open(found_path) {
Ok(repository) => repository,
Err(_) => panic!("Couldn't open repository"),
};
pub fn retrieve_tags(path: Option<String>) -> Vec<String> {
let found_path = determine_path(path);
let repo = retrieve_git_repository(found_path);

match repo.tag_names(None) {
Ok(found_tags) => {
found_tags
.into_iter()
.filter(|value| value.is_some())
.map(|value| value.unwrap().to_owned())
.collect()
},
Err(_) => Vec::new(),
}
}

pub fn retrieve_branch_commits(path: Option<String>) -> Vec<Commit> {
let found_path = determine_path(path);
let repo = retrieve_git_repository(found_path);

let mut revwalk = match repo.revwalk() {
Ok(found_revwalk) => found_revwalk,
Expand All @@ -97,8 +118,6 @@ pub fn retrieve_branch_commits(path: Option<String>) -> Vec<Commit> {
return revwalk.map(|step| -> Commit {
match step {
Ok(oid) => {
let is_tagged = repo.find_tag(oid).is_ok();

match repo.find_commit(oid) {
Ok(commit) => {
let message = match commit.message() {
Expand Down Expand Up @@ -128,7 +147,7 @@ pub fn retrieve_branch_commits(path: Option<String>) -> Vec<Commit> {
},
};

Commit { message, author, committer, is_tagged }
Commit { message, author, committer }
},
Err(_) => panic!("Could not retrieve oid of commit"),
}
Expand Down

0 comments on commit d4f1dec

Please sign in to comment.