From 666c7fed36a8a819fed8a7c0a4ddbf507bcda353 Mon Sep 17 00:00:00 2001 From: Ahmed Sagdati Date: Fri, 6 Sep 2024 17:40:22 +0300 Subject: [PATCH 1/4] add change log script to the workspace --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index b56dc47ce..843846295 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ members = [ "scripts/check-docs", "scripts/fuel-core-version", "scripts/versions-replacer", + "scripts/change-log", "wasm-tests", ] From 7cc5024ccf06bf15f8cd08b360136bc0775dc43a Mon Sep 17 00:00:00 2001 From: segfault-magnet Date: Fri, 6 Sep 2024 17:55:59 +0200 Subject: [PATCH 2/4] reformat --- scripts/change-log/src/get_full_changelog.rs | 75 ++++++++++++-------- scripts/change-log/src/get_latest_release.rs | 2 +- scripts/change-log/src/main.rs | 16 +++-- 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/scripts/change-log/src/get_full_changelog.rs b/scripts/change-log/src/get_full_changelog.rs index f5449370b..ff0bf9383 100644 --- a/scripts/change-log/src/get_full_changelog.rs +++ b/scripts/change-log/src/get_full_changelog.rs @@ -36,11 +36,11 @@ pub async fn get_changelog_info( .list_pulls(commit_sha.to_string()) .send() .await?; - + if pr_info.items.is_empty() { return Err("No PR found for this commit SHA".into()); } - + let pr = &pr_info.items[0]; // Skip PRs from the user "fuel-service-user" @@ -64,7 +64,11 @@ pub async fn get_changelog_info( let pr_number = pr.number; let pr_title = title_description.clone(); let pr_author = pr.user.as_ref().map_or("", |user| &user.login).to_string(); - let pr_url = pr.html_url.as_ref().map_or("", |url| url.as_str()).to_string(); + let pr_url = pr + .html_url + .as_ref() + .map_or("", |url| url.as_str()) + .to_string(); let bullet_point = format!( "- [#{}]({}) - {}, by @{}", @@ -74,34 +78,43 @@ pub async fn get_changelog_info( let breaking_changes_regex = Regex::new(r"(?s)# Breaking Changes\s*(.*)").unwrap(); let breaking_changes = breaking_changes_regex .captures(&pr.body.as_ref().unwrap_or(&String::new())) - .map_or_else(|| String::new(), |cap| { - cap.get(1).map_or(String::new(), |m| { - m.as_str() - .split("\n# ") - .next() - .unwrap_or("") - .trim() - .to_string() - }) - }); + .map_or_else( + || String::new(), + |cap| { + cap.get(1).map_or(String::new(), |m| { + m.as_str() + .split("\n# ") + .next() + .unwrap_or("") + .trim() + .to_string() + }) + }, + ); let release_notes_regex = Regex::new(r"(?s)In this release, we:\s*(.*)").unwrap(); let release_notes = release_notes_regex .captures(&pr.body.as_ref().unwrap_or(&String::new())) - .map_or_else(|| String::new(), |cap| { - cap.get(1).map_or(String::new(), |m| { - m.as_str() - .split("\n# ") - .next() - .unwrap_or("") - .trim() - .to_string() - }) - }); + .map_or_else( + || String::new(), + |cap| { + cap.get(1).map_or(String::new(), |m| { + m.as_str() + .split("\n# ") + .next() + .unwrap_or("") + .trim() + .to_string() + }) + }, + ); let migration_note = format!( "### [{} - {}]({})\n\n{}", - pr_number, capitalize(&title_description), pr_url, breaking_changes + pr_number, + capitalize(&title_description), + pr_url, + breaking_changes ); Ok(ChangelogInfo { @@ -124,8 +137,12 @@ pub async fn get_changelogs( base: &str, head: &str, ) -> Result, Box> { - let comparison = octocrab.commits(owner, repo).compare(base, head).send().await?; - + let comparison = octocrab + .commits(owner, repo) + .compare(base, head) + .send() + .await?; + let mut changelogs = Vec::new(); for commit in comparison.commits { @@ -155,7 +172,7 @@ pub fn generate_changelog(changelogs: Vec) -> String { let mut breaking_chores = Vec::new(); let mut migration_notes = Vec::new(); let mut summary_set: HashSet = HashSet::new(); - + for changelog in &changelogs { if changelog.is_breaking { match changelog.pr_type.as_str() { @@ -178,7 +195,7 @@ pub fn generate_changelog(changelogs: Vec) -> String { summary_set.insert(format!("{}", changelog.release_notes.clone())); } } - + if !summary_set.is_empty() { content.push_str("# Summary\n\nIn this release, we:\n"); let mut summary_lines: Vec = summary_set.into_iter().collect(); @@ -188,7 +205,7 @@ pub fn generate_changelog(changelogs: Vec) -> String { } content.push_str("\n"); } - + // Generate the breaking changes section if !breaking_features.is_empty() || !breaking_fixes.is_empty() || !breaking_chores.is_empty() { content.push_str("# Breaking\n\n"); diff --git a/scripts/change-log/src/get_latest_release.rs b/scripts/change-log/src/get_latest_release.rs index 1926579fe..1f1edc717 100644 --- a/scripts/change-log/src/get_latest_release.rs +++ b/scripts/change-log/src/get_latest_release.rs @@ -1,5 +1,5 @@ -use octocrab::Octocrab; use dotenv::dotenv; +use octocrab::Octocrab; pub async fn get_latest_release_tag() -> Result> { dotenv().ok(); diff --git a/scripts/change-log/src/main.rs b/scripts/change-log/src/main.rs index e1c0760e8..d8c50a705 100644 --- a/scripts/change-log/src/main.rs +++ b/scripts/change-log/src/main.rs @@ -1,22 +1,30 @@ mod get_full_changelog; mod get_latest_release; -use get_full_changelog::{get_changelogs, generate_changelog, write_changelog_to_file}; +use get_full_changelog::{generate_changelog, get_changelogs, write_changelog_to_file}; use get_latest_release::get_latest_release_tag; use octocrab::Octocrab; #[tokio::main] async fn main() -> Result<(), Box> { dotenv::dotenv().ok(); - let github_token = std::env::var("GITHUB_TOKEN").expect("GITHUB_TOKEN is not set in the environment"); + let github_token = + std::env::var("GITHUB_TOKEN").expect("GITHUB_TOKEN is not set in the environment"); let repo_owner = std::env::var("GITHUB_REPOSITORY_OWNER").expect("Repository owner not found"); let repo_name = std::env::var("GITHUB_REPOSITORY_NAME").expect("Repository name not found"); - + let octocrab = Octocrab::builder().personal_token(github_token).build()?; let latest_release_tag = get_latest_release_tag().await?; - let changelogs = get_changelogs(&octocrab, &repo_owner, &repo_name, &latest_release_tag, "master").await?; + let changelogs = get_changelogs( + &octocrab, + &repo_owner, + &repo_name, + &latest_release_tag, + "master", + ) + .await?; let full_changelog = generate_changelog(changelogs); From 248747c04e6c8f1729cb1fd16258a797b96db4f1 Mon Sep 17 00:00:00 2001 From: segfault-magnet Date: Fri, 6 Sep 2024 17:57:23 +0200 Subject: [PATCH 3/4] clippy --- scripts/change-log/src/get_full_changelog.rs | 74 +++++++++----------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/scripts/change-log/src/get_full_changelog.rs b/scripts/change-log/src/get_full_changelog.rs index ff0bf9383..a7c96a66a 100644 --- a/scripts/change-log/src/get_full_changelog.rs +++ b/scripts/change-log/src/get_full_changelog.rs @@ -11,10 +11,10 @@ pub struct ChangelogInfo { pub bullet_point: String, pub migration_note: String, pub release_notes: String, - pub pr_number: u64, - pub pr_title: String, - pub pr_author: String, - pub pr_url: String, + pub _pr_number: u64, + pub _pr_title: String, + pub _pr_author: String, + pub _pr_url: String, } pub fn capitalize(s: &str) -> String { @@ -53,7 +53,7 @@ pub async fn get_changelog_info( .as_ref() .map_or("misc", |title| title.split(':').next().unwrap_or("misc")) .to_string(); - let is_breaking = pr.title.as_ref().map_or(false, |title| title.contains("!")); + let is_breaking = pr.title.as_ref().map_or(false, |title| title.contains('!')); let title_description = pr .title @@ -77,37 +77,31 @@ pub async fn get_changelog_info( let breaking_changes_regex = Regex::new(r"(?s)# Breaking Changes\s*(.*)").unwrap(); let breaking_changes = breaking_changes_regex - .captures(&pr.body.as_ref().unwrap_or(&String::new())) - .map_or_else( - || String::new(), - |cap| { - cap.get(1).map_or(String::new(), |m| { - m.as_str() - .split("\n# ") - .next() - .unwrap_or("") - .trim() - .to_string() - }) - }, - ); + .captures(pr.body.as_ref().unwrap_or(&String::new())) + .map_or_else(String::new, |cap| { + cap.get(1).map_or(String::new(), |m| { + m.as_str() + .split("\n# ") + .next() + .unwrap_or("") + .trim() + .to_string() + }) + }); let release_notes_regex = Regex::new(r"(?s)In this release, we:\s*(.*)").unwrap(); let release_notes = release_notes_regex - .captures(&pr.body.as_ref().unwrap_or(&String::new())) - .map_or_else( - || String::new(), - |cap| { - cap.get(1).map_or(String::new(), |m| { - m.as_str() - .split("\n# ") - .next() - .unwrap_or("") - .trim() - .to_string() - }) - }, - ); + .captures(pr.body.as_ref().unwrap_or(&String::new())) + .map_or_else(String::new, |cap| { + cap.get(1).map_or(String::new(), |m| { + m.as_str() + .split("\n# ") + .next() + .unwrap_or("") + .trim() + .to_string() + }) + }); let migration_note = format!( "### [{} - {}]({})\n\n{}", @@ -123,10 +117,10 @@ pub async fn get_changelog_info( bullet_point, migration_note, release_notes, - pr_number, - pr_title, - pr_author, - pr_url, + _pr_number: pr_number, + _pr_title: pr_title, + _pr_author: pr_author, + _pr_url: pr_url, }) } @@ -146,7 +140,7 @@ pub async fn get_changelogs( let mut changelogs = Vec::new(); for commit in comparison.commits { - match get_changelog_info(&octocrab, owner, repo, &commit.sha).await { + match get_changelog_info(octocrab, owner, repo, &commit.sha).await { Ok(info) => changelogs.push(info), Err(e) => { println!("Error retrieving PR for commit {}: {}", commit.sha, e); @@ -192,7 +186,7 @@ pub fn generate_changelog(changelogs: Vec) -> String { } if !changelog.release_notes.is_empty() && !summary_set.contains(&changelog.release_notes) { - summary_set.insert(format!("{}", changelog.release_notes.clone())); + summary_set.insert(changelog.release_notes.clone().to_string()); } } @@ -203,7 +197,7 @@ pub fn generate_changelog(changelogs: Vec) -> String { for line in summary_lines { content.push_str(&format!("{}\n", line)); } - content.push_str("\n"); + content.push('\n'); } // Generate the breaking changes section From 8ab29735dbed32d6bf140ed54fe36da928aa1726 Mon Sep 17 00:00:00 2001 From: segfault-magnet Date: Fri, 6 Sep 2024 18:04:08 +0200 Subject: [PATCH 4/4] update cargo toml --- Cargo.toml | 2 ++ scripts/change-log/Cargo.toml | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 843846295..6ec0d19e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,6 +81,8 @@ trybuild = "1.0.85" uint = { version = "0.9.5", default-features = false } which = { version = "6.0.0", default-features = false } zeroize = "1.7.0" +octocrab = { version = "0.39", default-features = false } +dotenv = { version = "0.15", default-features = false } # Dependencies from the `fuel-core` repository: fuel-core = { version = "0.35.0", default-features = false, features = [ diff --git a/scripts/change-log/Cargo.toml b/scripts/change-log/Cargo.toml index 6eef06e95..f00bb874e 100644 --- a/scripts/change-log/Cargo.toml +++ b/scripts/change-log/Cargo.toml @@ -1,10 +1,16 @@ [package] name = "change-log" -version = "0.1.0" -edition = "2021" +version = { workspace = true } +authors = { workspace = true } +edition = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } +publish = false +repository = { workspace = true } +rust-version = { workspace = true } [dependencies] regex = { workspace = true } -dotenv = "0.15" -tokio = { workspace = true, features = ["full"] } -octocrab = "0.39" +dotenv = { workspace = true } +tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } +octocrab = { workspace = true, features = ["default"] }