From a506f49812642918b49d30b1e3079c9e7743b973 Mon Sep 17 00:00:00 2001 From: RakuJa Date: Mon, 27 May 2024 13:42:55 +0200 Subject: [PATCH 01/10] feat: add pf version filter, remove alignment from trait list (#56) --- Cargo.toml | 2 +- build/creature_core_db_init.rs | 3 +- src/db/cr_core_initializer.rs | 26 ++++++- src/db/data_providers/fetcher.rs | 73 +++++-------------- src/db/data_providers/raw_query_builder.rs | 28 ++++--- src/db/proxy.rs | 3 +- src/models/creature.rs | 15 +++- .../creature_component/creature_core.rs | 5 +- .../creature_component/filter_struct.rs | 2 + src/models/creature_filter_enum.rs | 11 ++- .../creature_metadata/alignment_enum.rs | 60 ++++----------- src/models/encounter_structs.rs | 2 + src/models/mod.rs | 1 + src/models/pf_version_enum.rs | 10 +++ src/models/routers_validator_structs.rs | 2 + src/routes/bestiary.rs | 21 +++--- src/services/encounter_service.rs | 18 ++++- 17 files changed, 149 insertions(+), 133 deletions(-) create mode 100644 src/models/pf_version_enum.rs diff --git a/Cargo.toml b/Cargo.toml index e097691..c4aeeb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ build = "build/main.rs" unsafe_code = "forbid" [dependencies] -actix-web = "4.5.1" +actix-web = "4.6.0" actix-cors = "0.7.0" actix-web-validator = "5.0.1" # Cannot be updated until actix-web updates validator dependency diff --git a/build/creature_core_db_init.rs b/build/creature_core_db_init.rs index 700588d..5329bb2 100644 --- a/build/creature_core_db_init.rs +++ b/build/creature_core_db_init.rs @@ -22,7 +22,8 @@ pub async fn create_creature_core_table(conn: &Pool) -> Result<()> { family TEXT NOT NULL DEFAULT '-', license TEXT NOT NULL DEFAULT '', source TEXT NOT NULL DEFAULT '', - remaster BOOL NOT NULL DEFAULT 0 + remaster BOOL NOT NULL DEFAULT 0, + alignment TEXT NOT NULL DEFAULT NO )" ) .execute(conn) diff --git a/src/db/cr_core_initializer.rs b/src/db/cr_core_initializer.rs index 495d911..5852e3e 100644 --- a/src/db/cr_core_initializer.rs +++ b/src/db/cr_core_initializer.rs @@ -1,8 +1,9 @@ use crate::db::data_providers::fetcher::{ fetch_creature_combat_data, fetch_creature_extra_data, fetch_creature_scales, - fetch_creature_spell_caster_data, + fetch_creature_spell_caster_data, fetch_creature_traits, }; use crate::models::creature_component::creature_core::EssentialData; +use crate::models::creature_metadata::alignment_enum::AlignmentEnum; use crate::models::creature_metadata::creature_role::CreatureRoleEnum; use crate::models::creature_metadata::rarity_enum::RarityEnum; use crate::models::creature_metadata::size_enum::SizeEnum; @@ -21,6 +22,9 @@ pub async fn update_creature_core_table(conn: &Pool) -> Result<()> { }; let scales = fetch_creature_scales(conn).await?; for cr in get_creatures_raw_essential_data(conn, &pagination).await? { + let traits = fetch_creature_traits(conn, cr.id).await?; + let alignment = AlignmentEnum::from((&traits, cr.remaster)); + update_alignment_column_value(conn, alignment.to_string(), cr.id).await?; let essential_data = EssentialData { id: cr.id, aon_id: cr.aon_id, @@ -34,6 +38,7 @@ pub async fn update_creature_core_table(conn: &Pool) -> Result<()> { remaster: cr.remaster, source: cr.source, cr_type: CreatureTypeEnum::from(cr.cr_type), + alignment, }; let extra_data = fetch_creature_extra_data(conn, essential_data.id).await?; let combat_data = fetch_creature_combat_data(conn, essential_data.id).await?; @@ -45,6 +50,7 @@ pub async fn update_creature_core_table(conn: &Pool) -> Result<()> { &spell_caster_data, &scales, ); + for (curr_role, curr_percentage) in roles { update_role_column_value(conn, curr_role, curr_percentage, essential_data.id).await?; } @@ -117,6 +123,24 @@ async fn update_role_column_value( Ok(()) } +async fn update_alignment_column_value( + conn: &Pool, + alignment: String, + creature_id: i64, +) -> Result<()> { + let x = sqlx::query!( + "UPDATE CREATURE_CORE SET alignment = ? WHERE id = ?", + alignment, + creature_id + ) + .execute(conn) + .await?; + if x.rows_affected() < 1 { + bail!("Error encountered with creature id: {creature_id}. Could not update alignment: {alignment}") + } + Ok(()) +} + async fn get_creatures_raw_essential_data( conn: &Pool, paginated_request: &PaginatedRequest, diff --git a/src/db/data_providers/fetcher.rs b/src/db/data_providers/fetcher.rs index 1905cbd..b25159e 100644 --- a/src/db/data_providers/fetcher.rs +++ b/src/db/data_providers/fetcher.rs @@ -1,14 +1,12 @@ use crate::db::data_providers::raw_query_builder::prepare_filtered_get_creatures_core; use crate::models::creature::Creature; use crate::models::creature_component::creature_combat::{CreatureCombatData, SavingThrows}; -use crate::models::creature_component::creature_core::{ - CreatureCoreData, DerivedData, EssentialData, -}; +use crate::models::creature_component::creature_core::CreatureCoreData; use crate::models::creature_component::creature_extra::{AbilityScores, CreatureExtraData}; use crate::models::creature_component::creature_spell_caster::CreatureSpellCasterData; use crate::models::creature_component::creature_variant::CreatureVariantData; use crate::models::creature_filter_enum::CreatureFilter; -use crate::models::creature_metadata::alignment_enum::{AlignmentEnum, ALIGNMENT_TRAITS}; +use crate::models::creature_metadata::alignment_enum::ALIGNMENT_TRAITS; use crate::models::creature_metadata::variant_enum::CreatureVariant; use crate::models::db::raw_immunity::RawImmunity; use crate::models::db::raw_language::RawLanguage; @@ -193,12 +191,12 @@ async fn fetch_creature_perception_detail( ) } -async fn fetch_creature_traits(conn: &Pool, creature_id: i64) -> Result> { +pub async fn fetch_creature_traits(conn: &Pool, creature_id: i64) -> Result> { Ok(sqlx::query_as!( RawTrait, "SELECT * FROM TRAIT_TABLE INTERSECT SELECT trait_id FROM TRAIT_CREATURE_ASSOCIATION_TABLE WHERE creature_id == ($1)", creature_id - ).fetch_all(conn).await?) + ).fetch_all(conn).await?.iter().map(|x| x.name.clone()).collect()) } async fn fetch_creature_weapons(conn: &Pool, creature_id: i64) -> Result> { @@ -256,47 +254,19 @@ async fn fetch_creature_core_data( conn: &Pool, creature_id: i64, ) -> Result { - let essential = fetch_creature_essential_data(conn, creature_id).await?; - let derived = fetch_creature_derived_data(conn, creature_id).await?; - let traits = fetch_creature_traits(conn, creature_id) + let mut cr_core: CreatureCoreData = + sqlx::query_as("SELECT * FROM CREATURE_CORE WHERE id = ? ORDER BY name LIMIT 1") + .bind(creature_id) + .fetch_one(conn) + .await?; + cr_core.traits = fetch_creature_traits(conn, creature_id) .await - .unwrap_or_default(); - let is_remaster = essential.remaster; - Ok(CreatureCoreData { - essential, - derived, - traits: traits.iter().map(|x| x.name.clone()).collect(), - alignment: AlignmentEnum::from((&traits, is_remaster)), - }) -} - -async fn fetch_creature_essential_data( - conn: &Pool, - creature_id: i64, -) -> Result { - Ok(sqlx::query_as!( - EssentialData, - "SELECT id, aon_id, name, hp, level, size, family, rarity, - license, remaster, source, cr_type - FROM CREATURE_CORE WHERE id = ? ORDER BY name LIMIT 1", - creature_id, - ) - .fetch_one(conn) - .await?) -} - -async fn fetch_creature_derived_data(conn: &Pool, creature_id: i64) -> Result { - Ok(sqlx::query_as!( - DerivedData, - "SELECT - archive_link, is_melee, is_ranged, is_spell_caster, brute_percentage, - magical_striker_percentage, skill_paragon_percentage, skirmisher_percentage, - sniper_percentage, soldier_percentage, spell_caster_percentage - FROM CREATURE_CORE WHERE id = ? ORDER BY name LIMIT 1", - creature_id, - ) - .fetch_one(conn) - .await?) + .unwrap_or_default() + .iter() + .filter(|x| !ALIGNMENT_TRAITS.contains(&&*x.as_str().to_uppercase())) + .cloned() + .collect(); + Ok(cr_core) } async fn update_creatures_core_with_traits( @@ -304,16 +274,13 @@ async fn update_creatures_core_with_traits( mut creature_core_data: Vec, ) -> Vec { for core in &mut creature_core_data { - let traits = fetch_creature_traits(conn, core.essential.id) + core.traits = fetch_creature_traits(conn, core.essential.id) .await - .unwrap_or_default(); - let is_remaster = core.essential.remaster; - core.traits = traits + .unwrap_or_default() .iter() - .filter(|x| !ALIGNMENT_TRAITS.contains(&&*x.name.as_str().to_uppercase())) - .map(|x| x.name.clone()) + .filter(|x| !ALIGNMENT_TRAITS.contains(&&*x.as_str().to_uppercase())) + .cloned() .collect(); - core.alignment = AlignmentEnum::from((&traits, is_remaster)); } creature_core_data } diff --git a/src/db/data_providers/raw_query_builder.rs b/src/db/data_providers/raw_query_builder.rs index ccf5ae0..e2415a3 100644 --- a/src/db/data_providers/raw_query_builder.rs +++ b/src/db/data_providers/raw_query_builder.rs @@ -11,6 +11,7 @@ pub fn prepare_filtered_get_creatures_core( for (key, value) in key_value_filters { match key { CreatureFilter::Level + | CreatureFilter::PathfinderVersion | CreatureFilter::Melee | CreatureFilter::Ranged | CreatureFilter::SpellCaster => { @@ -22,6 +23,7 @@ pub fn prepare_filtered_get_creatures_core( ) } CreatureFilter::Family + | CreatureFilter::Alignment | CreatureFilter::Size | CreatureFilter::Rarity | CreatureFilter::CreatureTypes => { @@ -33,18 +35,23 @@ pub fn prepare_filtered_get_creatures_core( ) } CreatureFilter::Traits => trait_query.push_str(prepare_trait_filter(value).as_str()), - CreatureFilter::CreatureRoles => simple_core_query - .push_str(prepare_bounded_check(value, 0, ACCURACY_THRESHOLD).as_str()), + CreatureFilter::CreatureRoles => { + if !simple_core_query.is_empty() { + simple_core_query.push_str(" AND ") + } + simple_core_query + .push_str(prepare_bounded_check(value, ACCURACY_THRESHOLD, 100).as_str()) + } } } let mut where_query = simple_core_query.to_string(); if !trait_query.is_empty() { - where_query.push_str(format!("AND id IN ({trait_query})").as_str()); - }; + where_query.push_str(format!(" AND id IN ({trait_query}) GROUP BY cc.id").as_str()) + } if !where_query.is_empty() { where_query = format!("WHERE {where_query}"); } - let query = format!("SELECT * FROM CREATURE_CORE {where_query} ORDER BY RANDOM() LIMIT 20"); + let query = format!("SELECT * FROM CREATURE_CORE cc {where_query} ORDER BY RANDOM() LIMIT 20"); debug!("{}", query); query } @@ -57,12 +64,9 @@ fn prepare_bounded_check( upper_bound: i64, ) -> String { let mut bounded_query = String::new(); - if column_names.is_empty() { - return bounded_query; - } for column in column_names { if !bounded_query.is_empty() { - bounded_query.push_str(" AND "); + bounded_query.push_str(" OR "); } bounded_query.push_str( format!("({column} >= {lower_bound} AND {column} <= {upper_bound})").as_str(), @@ -84,13 +88,15 @@ fn prepare_trait_filter(column_values: &HashSet) -> String { if !in_string.is_empty() { let select_query = "SELECT tcat.creature_id FROM TRAIT_CREATURE_ASSOCIATION_TABLE"; let inner_query = format!("SELECT * FROM TRAIT_TABLE tt WHERE {in_string}"); - return format!("{select_query} tcat RIGHT JOIN ({inner_query}) jt ON tcat.trait_id = jt.name GROUP BY tcat.creature_id"); + return format!( + "{select_query} tcat RIGHT JOIN ({inner_query}) jt ON tcat.trait_id = jt.name" + ); } in_string } /// Prepares an 'in' statement in the following format. Assuming a string value -/// "field in ('el1', 'el2', 'el3')" +/// "UPPER(field) in (UPPER('el1'), UPPER('el2'), UPPER('el3'))" fn prepare_in_statement_for_string_type( column_name: &str, column_values: &HashSet, diff --git a/src/db/proxy.rs b/src/db/proxy.rs index 35689a8..221566a 100644 --- a/src/db/proxy.rs +++ b/src/db/proxy.rs @@ -75,10 +75,9 @@ pub async fn get_creatures_passing_all_filters( fetch_elite: bool, ) -> Result> { let mut creature_vec = Vec::new(); - let empty_set = HashSet::new(); let level_vec = key_value_filters .get(&CreatureFilter::Level) - .unwrap_or(&empty_set) + .unwrap_or(&HashSet::new()) .clone(); let modified_filters = prepare_filters_for_db_communication(key_value_filters, fetch_weak, fetch_elite); diff --git a/src/models/creature.rs b/src/models/creature.rs index 17a7fbb..b3291b4 100644 --- a/src/models/creature.rs +++ b/src/models/creature.rs @@ -5,6 +5,7 @@ use crate::models::creature_component::creature_spell_caster::CreatureSpellCaste use crate::models::creature_component::creature_variant::CreatureVariantData; use crate::models::creature_metadata::creature_role::CreatureRoleEnum; use crate::models::creature_metadata::variant_enum::CreatureVariant; +use crate::models::pf_version_enum::PathfinderVersionEnum; use crate::models::routers_validator_structs::FieldFilters; use serde::{Deserialize, Serialize}; @@ -101,10 +102,9 @@ impl Creature { .size_filter .as_ref() .map_or(true, |size| self.core_data.essential.size == *size); - let alignment_pass = filters - .alignment_filter - .as_ref() - .map_or(true, |alignment| self.core_data.alignment == *alignment); + let alignment_pass = filters.alignment_filter.as_ref().map_or(true, |alignment| { + self.core_data.essential.alignment == *alignment + }); let is_melee_pass = filters .is_melee_filter .map_or(true, |is_melee| self.core_data.derived.is_melee == is_melee); @@ -143,6 +143,12 @@ impl Creature { } }); + let version_pass = match filters.pathfinder_version.clone().unwrap_or_default() { + PathfinderVersionEnum::Legacy => !self.core_data.essential.remaster, + PathfinderVersionEnum::Remaster => self.core_data.essential.remaster, + PathfinderVersionEnum::Any => true, + }; + rarity_pass && size_pass && alignment_pass @@ -151,6 +157,7 @@ impl Creature { && is_spell_caster_pass && type_pass && role_pass + && version_pass } fn check_creature_pass_string_filters(&self, filters: &FieldFilters) -> bool { diff --git a/src/models/creature_component/creature_core.rs b/src/models/creature_component/creature_core.rs index ee54d8a..3b4e6b6 100644 --- a/src/models/creature_component/creature_core.rs +++ b/src/models/creature_component/creature_core.rs @@ -12,7 +12,6 @@ pub struct CreatureCoreData { pub essential: EssentialData, pub derived: DerivedData, pub traits: Vec, - pub alignment: AlignmentEnum, } #[derive(Serialize, Deserialize, Clone, ToSchema, Eq, Hash, PartialEq, FromRow)] pub struct EssentialData { @@ -28,6 +27,7 @@ pub struct EssentialData { pub remaster: bool, pub source: String, pub cr_type: CreatureTypeEnum, + pub alignment: AlignmentEnum, } #[derive(Serialize, Deserialize, Clone, ToSchema, Eq, Hash, PartialEq, FromRow)] @@ -51,6 +51,7 @@ impl<'r> FromRow<'r, SqliteRow> for CreatureCoreData { fn from_row(row: &'r SqliteRow) -> Result { let rarity: String = row.try_get("rarity")?; let size: String = row.try_get("size")?; + let alignment: String = row.try_get("alignment")?; Ok(CreatureCoreData { essential: EssentialData { id: row.try_get("id")?, @@ -65,6 +66,7 @@ impl<'r> FromRow<'r, SqliteRow> for CreatureCoreData { remaster: row.try_get("remaster")?, source: row.try_get("source")?, cr_type: CreatureTypeEnum::from(row.try_get("cr_type").ok()), + alignment: AlignmentEnum::from(alignment), }, derived: DerivedData { archive_link: row.try_get("archive_link").ok(), @@ -80,7 +82,6 @@ impl<'r> FromRow<'r, SqliteRow> for CreatureCoreData { spell_caster_percentage: row.try_get("spell_caster_percentage")?, }, traits: vec![], - alignment: Default::default(), }) } } diff --git a/src/models/creature_component/filter_struct.rs b/src/models/creature_component/filter_struct.rs index 9a9e8b5..1855f53 100644 --- a/src/models/creature_component/filter_struct.rs +++ b/src/models/creature_component/filter_struct.rs @@ -3,6 +3,7 @@ use crate::models::creature_metadata::creature_role::CreatureRoleEnum; use crate::models::creature_metadata::rarity_enum::RarityEnum; use crate::models::creature_metadata::size_enum::SizeEnum; use crate::models::creature_metadata::type_enum::CreatureTypeEnum; +use crate::models::pf_version_enum::PathfinderVersionEnum; use std::collections::HashSet; pub struct FilterStruct { @@ -14,4 +15,5 @@ pub struct FilterStruct { pub creature_types: Option>, pub creature_roles: Option>, pub lvl_combinations: HashSet, + pub pathfinder_version: PathfinderVersionEnum, } diff --git a/src/models/creature_filter_enum.rs b/src/models/creature_filter_enum.rs index 9370348..8627843 100644 --- a/src/models/creature_filter_enum.rs +++ b/src/models/creature_filter_enum.rs @@ -11,8 +11,11 @@ pub enum CreatureFilter { Ranged, SpellCaster, Traits, + Alignment, CreatureTypes, + #[serde(alias = "creature_roles")] CreatureRoles, + PathfinderVersion, } impl fmt::Display for CreatureFilter { @@ -46,7 +49,13 @@ impl fmt::Display for CreatureFilter { write!(f, "cr_type") } CreatureFilter::CreatureRoles => { - write!(f, "CREATUREROLES") + write!(f, "creature_roles") + } + CreatureFilter::Alignment => { + write!(f, "alignment") + } + CreatureFilter::PathfinderVersion => { + write!(f, "remaster") } } } diff --git a/src/models/creature_metadata/alignment_enum.rs b/src/models/creature_metadata/alignment_enum.rs index 8495492..ac44b66 100644 --- a/src/models/creature_metadata/alignment_enum.rs +++ b/src/models/creature_metadata/alignment_enum.rs @@ -1,4 +1,3 @@ -use crate::models::db::raw_trait::RawTrait; use serde::{Deserialize, Serialize}; use std::str::FromStr; use strum::{Display, EnumIter}; @@ -56,17 +55,13 @@ pub enum AlignmentEnum { pub const ALIGNMENT_TRAITS: [&str; 4] = ["GOOD", "EVIL", "CHAOTIC", "LAWFUL"]; -impl From<(&Vec, bool)> for AlignmentEnum { - fn from(tuple: (&Vec, bool)) -> AlignmentEnum { +impl From<(&Vec, bool)> for AlignmentEnum { + fn from(tuple: (&Vec, bool)) -> AlignmentEnum { // If remaster then it's always no alignment if tuple.1 { return AlignmentEnum::No; } - let string_traits: Vec = tuple - .0 - .iter() - .map(|x| x.name.clone().to_uppercase()) - .collect(); + let string_traits: Vec = tuple.0.iter().map(|x| x.to_uppercase()).collect(); let is_good = string_traits.contains(&"GOOD".to_string()); let is_evil = string_traits.contains(&"EVIL".to_string()); let is_chaos = string_traits.contains(&"CHAOTIC".to_string()); @@ -89,6 +84,12 @@ impl From<(&Vec, bool)> for AlignmentEnum { } return AlignmentEnum::Ne; } + if is_chaos { + return AlignmentEnum::Cn; + } + if is_lawful { + return AlignmentEnum::Ln; + } AlignmentEnum::N } } @@ -99,6 +100,12 @@ impl From for AlignmentEnum { } } +impl From<&String> for AlignmentEnum { + fn from(value: &String) -> Self { + AlignmentEnum::from_str(value.as_str()).unwrap_or_default() + } +} + impl FromStr for AlignmentEnum { type Err = (); fn from_str(s: &str) -> Result { @@ -118,43 +125,6 @@ impl FromStr for AlignmentEnum { } } -impl AlignmentEnum { - pub fn to_traits(&self) -> Vec { - match self { - AlignmentEnum::Ce => { - vec![String::from("CHAOTIC"), String::from("EVIL")] - } - AlignmentEnum::Cn => { - vec![String::from("CHAOTIC"), String::from("NEUTRAL")] - } - AlignmentEnum::Cg => { - vec![String::from("CHAOTIC"), String::from("GOOD")] - } - AlignmentEnum::Ne => { - vec![String::from("NEUTRAL"), String::from("EVIL")] - } - AlignmentEnum::N => { - vec![String::from("NEUTRAL")] - } - AlignmentEnum::Ng => { - vec![String::from("NEUTRAL"), String::from("GOOD")] - } - AlignmentEnum::Le => { - vec![String::from("LAWFUL"), String::from("EVIL")] - } - AlignmentEnum::Ln => { - vec![String::from("LAWFUL"), String::from("NEUTRAL")] - } - AlignmentEnum::Lg => { - vec![String::from("LAWFUL"), String::from("GOOD")] - } - AlignmentEnum::No | AlignmentEnum::Any => { - vec![] - } - } - } -} - impl Clone for AlignmentEnum { fn clone(&self) -> AlignmentEnum { match self { diff --git a/src/models/encounter_structs.rs b/src/models/encounter_structs.rs index 7bcb0ba..c34b4ee 100644 --- a/src/models/encounter_structs.rs +++ b/src/models/encounter_structs.rs @@ -3,6 +3,7 @@ use crate::models::creature_metadata::creature_role::CreatureRoleEnum; use crate::models::creature_metadata::rarity_enum::RarityEnum; use crate::models::creature_metadata::size_enum::SizeEnum; use crate::models::creature_metadata::type_enum::CreatureTypeEnum; +use crate::models::pf_version_enum::PathfinderVersionEnum; use rand::distributions::{Distribution, Standard}; use rand::Rng; use serde::{Deserialize, Serialize}; @@ -36,6 +37,7 @@ pub struct RandomEncounterData { pub allow_elite_variants: Option, pub allow_weak_variants: Option, pub is_pwl_on: bool, + pub pathfinder_versions: Option, } #[derive( diff --git a/src/models/mod.rs b/src/models/mod.rs index d14456b..efb2622 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -6,6 +6,7 @@ pub mod creature_metadata; pub mod db; pub mod encounter_structs; pub mod items; +pub mod pf_version_enum; pub mod response_data; pub mod routers_validator_structs; pub mod scales_struct; diff --git a/src/models/pf_version_enum.rs b/src/models/pf_version_enum.rs new file mode 100644 index 0000000..5dcbba6 --- /dev/null +++ b/src/models/pf_version_enum.rs @@ -0,0 +1,10 @@ +use serde::{Deserialize, Serialize}; +use utoipa::ToSchema; + +#[derive(Serialize, Deserialize, Eq, PartialEq, Hash, Default, ToSchema, Clone)] +pub enum PathfinderVersionEnum { + Legacy, + Remaster, + #[default] + Any, +} diff --git a/src/models/routers_validator_structs.rs b/src/models/routers_validator_structs.rs index c89712d..66e660e 100644 --- a/src/models/routers_validator_structs.rs +++ b/src/models/routers_validator_structs.rs @@ -3,6 +3,7 @@ use crate::models::creature_metadata::creature_role::CreatureRoleEnum; use crate::models::creature_metadata::rarity_enum::RarityEnum; use crate::models::creature_metadata::size_enum::SizeEnum; use crate::models::creature_metadata::type_enum::CreatureTypeEnum; +use crate::models::pf_version_enum::PathfinderVersionEnum; use serde::{Deserialize, Serialize}; use utoipa::IntoParams; use validator::Validate; @@ -24,6 +25,7 @@ pub struct FieldFilters { pub is_melee_filter: Option, pub is_ranged_filter: Option, pub is_spell_caster_filter: Option, + pub pathfinder_version: Option, } #[derive(Serialize, Deserialize, IntoParams, Validate, Eq, PartialEq, Hash)] diff --git a/src/routes/bestiary.rs b/src/routes/bestiary.rs index bf40d19..e84fef8 100644 --- a/src/routes/bestiary.rs +++ b/src/routes/bestiary.rs @@ -1,12 +1,3 @@ -use crate::models::creature_metadata::alignment_enum::AlignmentEnum; -use crate::models::creature_metadata::creature_role::CreatureRoleEnum; -use crate::models::creature_metadata::rarity_enum::RarityEnum; -use crate::models::creature_metadata::size_enum::SizeEnum; -use crate::models::creature_metadata::type_enum::CreatureTypeEnum; -use crate::models::creature_metadata::variant_enum::CreatureVariant; -use crate::models::response_data::OptionalData; -use crate::models::response_data::ResponseCreature; - use crate::models::creature_component::creature_combat::CreatureCombatData; use crate::models::creature_component::creature_combat::SavingThrows; use crate::models::creature_component::creature_core::CreatureCoreData; @@ -16,6 +7,15 @@ use crate::models::creature_component::creature_extra::AbilityScores; use crate::models::creature_component::creature_extra::CreatureExtraData; use crate::models::creature_component::creature_spell_caster::CreatureSpellCasterData; use crate::models::creature_component::creature_variant::CreatureVariantData; +use crate::models::creature_metadata::alignment_enum::AlignmentEnum; +use crate::models::creature_metadata::creature_role::CreatureRoleEnum; +use crate::models::creature_metadata::rarity_enum::RarityEnum; +use crate::models::creature_metadata::size_enum::SizeEnum; +use crate::models::creature_metadata::type_enum::CreatureTypeEnum; +use crate::models::creature_metadata::variant_enum::CreatureVariant; +use crate::models::pf_version_enum::PathfinderVersionEnum; +use crate::models::response_data::OptionalData; +use crate::models::response_data::ResponseCreature; use crate::models::items::action::Action; use crate::models::items::skill::Skill; @@ -88,7 +88,8 @@ pub fn init_docs(doc: &mut utoipa::openapi::OpenApi) { Action, Skill, CreatureRoleEnum, - SpellCasterEntry + SpellCasterEntry, + PathfinderVersionEnum )) )] struct ApiDoc; diff --git a/src/services/encounter_service.rs b/src/services/encounter_service.rs index bdda2c7..7c3d3b9 100644 --- a/src/services/encounter_service.rs +++ b/src/services/encounter_service.rs @@ -5,6 +5,7 @@ use crate::models::creature_filter_enum::CreatureFilter; use crate::models::encounter_structs::{ EncounterChallengeEnum, EncounterParams, RandomEncounterData, }; +use crate::models::pf_version_enum::PathfinderVersionEnum; use crate::models::response_data::ResponseCreature; use crate::services::encounter_handler::encounter_calculator; use crate::services::encounter_handler::encounter_calculator::calculate_encounter_scaling_difficulty; @@ -110,6 +111,7 @@ async fn calculate_random_encounter( creature_types: enc_data.creature_types, creature_roles: enc_data.creature_roles, lvl_combinations: unique_levels, + pathfinder_version: enc_data.pathfinder_versions.unwrap_or_default(), }); let filtered_creatures = get_filtered_creatures( @@ -245,8 +247,8 @@ fn build_filter_map(filter_enum: FilterStruct) -> HashMap HashMap vec![String::from("FALSE")].into_iter(), + PathfinderVersionEnum::Remaster => vec![String::from("TRUE")].into_iter(), + PathfinderVersionEnum::Any => { + vec![String::from("TRUE"), String::from("FALSE")].into_iter() + } + }), + ); filter_map } From 631f564762ffc0f562d7b02a2462278770184018 Mon Sep 17 00:00:00 2001 From: rakuja Date: Mon, 27 May 2024 14:39:56 +0200 Subject: [PATCH 02/10] doc: add changelog --- .github/workflows/changelog.yml | 76 ++++++++++++++++++++ .github/workflows/clippy.yml | 5 +- .github/workflows/fmt.yml | 4 +- .github/workflows/push_on_main.yml | 16 +++++ CHANGELOG.md | 109 +++++++++++++++++++++++++++++ cliff.toml | 78 +++++++++++++++++++++ 6 files changed, 283 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/changelog.yml create mode 100644 .github/workflows/push_on_main.yml create mode 100644 CHANGELOG.md create mode 100644 cliff.toml diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml new file mode 100644 index 0000000..3377fb5 --- /dev/null +++ b/.github/workflows/changelog.yml @@ -0,0 +1,76 @@ +on: workflow_call +jobs: + + push-changelog: + name: Generate changelog + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Generate a changelog + uses: orhun/git-cliff-action@v3 + with: + config: cliff.toml + args: --verbose + env: + OUTPUT: CHANGELOG.md + GITHUB_REPO: ${{ github.repository }} + + - name: Commit + run: | + git config user.name 'github-actions[bot]' + git config user.email 'github-actions[bot]@users.noreply.github.com' + set +e + git add CHANGELOG.md + git commit -m "Update changelog" + git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git add_changelog + + + + + changelog: + name: Generate changelog + runs-on: ubuntu-latest + outputs: + release_body: ${{ steps.git-cliff.outputs.content }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Generate a changelog + uses: orhun/git-cliff-action@v3 + id: git-cliff + with: + config: cliff.toml + args: -vv --latest --strip header + env: + OUTPUT: CHANGES.md + GITHUB_REPO: ${{ github.repository }} + + # use release body in the same job + - name: Upload the binary releases + uses: svenstaro/upload-release-action@v2 + with: + file: binary_release.zip + repo_token: ${{ secrets.GITHUB_TOKEN }} + tag: ${{ github.ref }} + body: ${{ steps.git-cliff.outputs.content }} + + # use release body in another job + upload: + name: Upload the release + needs: changelog + runs-on: ubuntu-latest + steps: + - name: Upload the binary releases + uses: svenstaro/upload-release-action@v2 + with: + file: binary_release.zip + repo_token: ${{ secrets.GITHUB_TOKEN }} + tag: ${{ github.ref }} + body: ${{ needs.changelog.outputs.release_body }} diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index 9cbf9bd..ed328e9 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -1,6 +1,5 @@ -on: push -name: Clippy check - +on: + workflow_call: # Make sure CI fails on all warnings, including Clippy lints env: RUSTFLAGS: "-Dwarnings" diff --git a/.github/workflows/fmt.yml b/.github/workflows/fmt.yml index d727d69..cdf47d9 100644 --- a/.github/workflows/fmt.yml +++ b/.github/workflows/fmt.yml @@ -1,4 +1,4 @@ -on: pull_request +on: [workflow_call] name: Rustfmt @@ -15,4 +15,4 @@ jobs: - uses: mbrobbel/rustfmt-check@master with: token: ${{ secrets.GITHUB_TOKEN }} - mode: review \ No newline at end of file + mode: review diff --git a/.github/workflows/push_on_main.yml b/.github/workflows/push_on_main.yml new file mode 100644 index 0000000..a2fb8ce --- /dev/null +++ b/.github/workflows/push_on_main.yml @@ -0,0 +1,16 @@ +on: push +name: Push on main + +jobs: + check: + uses: ./.github/workflows/clippy.yml + with: + job: clippy_check + format: + uses: ./.github/workflows/fmt.yml + with: + job: format + changelog: + uses: ./.github/workflows/changelog.yml + with: + job: push-changelog diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5b04490 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,109 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [1.3.0] - 2024-05-19 + +### Feature + +- Introduce pwl, remove lazy static where used. (#47) +- Refactor Creature response according to new db and remastered (#48) +- Introduce Creature Role (#49) +- Refactor DB communication, remove moka cache (#51) + +## [1.2.0] - 2024-01-17 + +### Feature + +- Allow variants on encounter generation (#37) + +### Fixed + +- Change default from any to no alignment (#39) +- Any not converted correctly from db string (#40) + +## [1.0.0] - 2023-11-16 + +### Added + +- Add gunicorn support, expand config usage, fix docker image +- Add encounter info: encounter difficulty, exp and difficulty thresholds +- Add endpoint to fetch creature fields (family, rarity, size, alignment), page size must be greater or equal than 1, generalize fetch_keys redis_handler method +- Add method to procedurally create endpoints ending with or without / +- Add weak and elite endpoints +- Add melee, ranged, spell caster and sources fields. add melee, ranged and spell caster filters (#14) +- Add health controller +- Add get filter routers, use dbcache filters field, separate a little bit the cache logic and router initialization +- Add elite and weak modifier. Complete bestiary +- Add swagger at swagger-ui endpoint +- Add get_encounter_info logic + +### Changed + +- Flake8 refactor +- Introduce list validation, change endpoint name to a more Rest like naming scheme +- Expand Creature object +- Bump version +- Refactor following ruff guidelines (#13) +- Refactor from rocket to actix-web +- Run clippy linter, add pre-commit, add Dockerfile +- Update bestiary swagger, refactor id endpoint +- Allow cors, optimize dockerignore, fix Dockerfile +- Rename difficulty to challenge, return metadata enums in UPPERCASE +- Return enum fields in caps and null as vec result when enc gen failed with error. Remove various debug +- Rename encounter_challenge to challenge, format any +- Refactor get single creature to not call redis each time +- Update readme +- Add valid rust actions +- 1.0.0 release (#32) + +### Feature + +- Backend is now deployment ready using docker and fly.io +- Implement random encounter generator, redis_handler can now fetch ids that pass filters +- Allow encounter generation with no filter, difficulty is randomized and the rest ignored +- Introduce get creature API, add filter name to list API +- Add sort and label filters (#6) +- Add level and hp (min, max) filters (#9) +- Refactor using rust +- Implement cache (that needs to be refactored, it's ported 1:1 from python code) +- Implement full feature routing for bestiary list, with filters and default values. +- Allow request without pagination +- Implement encounter generation in rust (#16) +- Refactor cache to make it more rusty. This has the problem that it will have a slow first call every 24hours for one unlucky user +- Parse sources and return them correctly +- Parse complex sources strings +- Implement traits (#19) +- Implement creature types (#21) +- Implement multiple filter values (#23) +- Implement cache control (#25) +- Implement SQLite db (#28) +- Implement source endpoint (#30) +- Implement generator filtering by number of creatures (#31) + +### Fixed + +- Now get_bestiary returns json instead of string +- Handle difference between negative levels when calculating encounter info +- Next element wrong address +- Allow CORS calls +- Simplify calculate_encounter_difficulty, fix logical bug with trivial difficulty +- Populate new_ids_dict correctly (#7) +- Side effect on creature cache (#11) +- Refactor to handle correctly json body on post +- Use of scan instead of keys, that raised an error when the number of keys is high +- Creature_id endpoint overriding rarities, families, size, alignment etc +- Encounter info returning impossible on trivial encounters +- Pagination taking more element than requested +- Ln string not in caps +- Encounter info broken with negative enemies +- Set filtering was doing union operation instead of intersection +- Filter correctly vectors containing integer with value < -1 + +[1.3.0]: https://github.com///compare/v1.2.0..v1.3.0 +[1.2.0]: https://github.com///compare/v1.0.0..v1.2.0 + + diff --git a/cliff.toml b/cliff.toml new file mode 100644 index 0000000..51916d0 --- /dev/null +++ b/cliff.toml @@ -0,0 +1,78 @@ +# git-cliff ~ configuration file +# https://git-cliff.org/docs/configuration + +[changelog] +# changelog header +header = """ +# Changelog\n +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n +""" +body = """ +{% if version -%} + ## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }} +{% else -%} + ## [Unreleased] +{% endif -%} +{% for group, commits in commits | group_by(attribute="group") %} + ### {{ group | upper_first }} + {% for commit in commits %} + - {{ commit.message | upper_first }}\ + {% endfor %} +{% endfor %}\n +""" +# template for the changelog footer +footer = """ +{% for release in releases -%} + {% if release.version -%} + {% if release.previous.version -%} + [{{ release.version | trim_start_matches(pat="v") }}]: \ + https://github.com/{{ remote.github.owner }}/{{ remote.github.repo }}\ + /compare/{{ release.previous.version }}..{{ release.version }} + {% endif -%} + {% else -%} + [unreleased]: https://github.com/{{ remote.github.owner }}/{{ remote.github.repo }}\ + /compare/{{ release.previous.version }}..HEAD + {% endif -%} +{% endfor %} + +""" +# remove the leading and trailing whitespace from the templates +trim = true + +[git] +# parse the commits based on https://www.conventionalcommits.org +conventional_commits = true +# filter out the commits that are not conventional +filter_unconventional = true +# process each line of a commit as an individual commit +split_commits = false +# regex for parsing and grouping commits +commit_parsers = [ + { message = "^.*: add", group = "Added" }, + { message = "^.*: support", group = "Added" }, + { message = "^.*: remove", group = "Removed" }, + { message = "^.*: delete", group = "Removed" }, + { message = "^feat", group = "Feature"}, + { message = "^doc", group = "Documentation"}, + { message = "^test", group = "Fixed" }, + { message = "^fix", group = "Fixed" }, + { message = "^.*: fix", group = "Fixed" }, + { message = "^.*", group = "Changed" }, +] +# protect breaking changes from being skipped due to matching a skipping commit_parser +protect_breaking_commits = false +# filter out the commits that are not matched by commit parsers +filter_commits = true +# regex for matching git tags +tag_pattern = "v[0-9].*" +# regex for skipping tags +skip_tags = "v0.1.0-beta.1" +# regex for ignoring tags +ignore_tags = "" +# sort the tags topologically +topo_order = false +# sort the commits inside sections by oldest/newest order +sort_commits = "oldest" From 293e2b717841d6fad3617ee6a407be3730d523e5 Mon Sep 17 00:00:00 2001 From: rakuja Date: Mon, 27 May 2024 14:43:23 +0200 Subject: [PATCH 03/10] doc: update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1862894..da97e7b 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ - Browse and filter a list of all creatures. - Balance encounters based on your party size and level. - Generate random encounters based on your requirements. +- Support for both remaster and legacy content. - More to come... ## Requirements @@ -28,7 +29,7 @@ Built using: ## Installation guide - Local 1. Install [Rust](https://www.rust-lang.org/tools/install) on your machine. -2. Populate the SQLite database. +2. Populate the SQLite database (public release date TBA). 3. Clone this repository: ``` From b8c68874455d8cd5e232ab96c4564c1eae91e873 Mon Sep 17 00:00:00 2001 From: rakuja Date: Thu, 30 May 2024 21:24:29 +0200 Subject: [PATCH 04/10] chore: CI refactor --- .github/workflows/clippy.yml | 13 ----- .github/workflows/fmt.yml | 18 ------- .github/workflows/main_push_or_pr.yml | 51 +++++++++++++++++++ .github/workflows/push_on_main.yml | 16 ------ .../workflows/{changelog.yml => release.yml} | 33 +----------- 5 files changed, 52 insertions(+), 79 deletions(-) delete mode 100644 .github/workflows/clippy.yml delete mode 100644 .github/workflows/fmt.yml create mode 100644 .github/workflows/main_push_or_pr.yml delete mode 100644 .github/workflows/push_on_main.yml rename .github/workflows/{changelog.yml => release.yml} (61%) diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml deleted file mode 100644 index ed328e9..0000000 --- a/.github/workflows/clippy.yml +++ /dev/null @@ -1,13 +0,0 @@ -on: - workflow_call: -# Make sure CI fails on all warnings, including Clippy lints -env: - RUSTFLAGS: "-Dwarnings" - -jobs: - clippy_check: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Run Clippy - run: cargo clippy --all-targets --all-features diff --git a/.github/workflows/fmt.yml b/.github/workflows/fmt.yml deleted file mode 100644 index cdf47d9..0000000 --- a/.github/workflows/fmt.yml +++ /dev/null @@ -1,18 +0,0 @@ -on: [workflow_call] - -name: Rustfmt - -jobs: - format: - runs-on: ubuntu-latest - permissions: - pull-requests: write - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@nightly - with: - components: rustfmt - - uses: mbrobbel/rustfmt-check@master - with: - token: ${{ secrets.GITHUB_TOKEN }} - mode: review diff --git a/.github/workflows/main_push_or_pr.yml b/.github/workflows/main_push_or_pr.yml new file mode 100644 index 0000000..8fee37d --- /dev/null +++ b/.github/workflows/main_push_or_pr.yml @@ -0,0 +1,51 @@ +on: [push, pull_request] +name: Push on main or PR +env: + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} + +jobs: + clippy_check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run Clippy + run: cargo clippy --all-targets --all-features + format: + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly + with: + components: rustfmt + - uses: mbrobbel/rustfmt-check@master + with: + token: ${{ secrets.GITHUB_TOKEN }} + mode: review + changelog: + name: Generate changelog + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Generate a changelog + uses: orhun/git-cliff-action@v3 + with: + config: cliff.toml + args: --verbose + env: + OUTPUT: CHANGELOG.md + GITHUB_REPO: ${{ github.repository }} + + - name: Commit + run: | + git config user.name 'github-actions[bot]' + git config user.email 'github-actions[bot]@users.noreply.github.com' + set +e + git add CHANGELOG.md + git commit --no-verify -m "Update changelog" + git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git ${{ env.BRANCH_NAME }} diff --git a/.github/workflows/push_on_main.yml b/.github/workflows/push_on_main.yml deleted file mode 100644 index a2fb8ce..0000000 --- a/.github/workflows/push_on_main.yml +++ /dev/null @@ -1,16 +0,0 @@ -on: push -name: Push on main - -jobs: - check: - uses: ./.github/workflows/clippy.yml - with: - job: clippy_check - format: - uses: ./.github/workflows/fmt.yml - with: - job: format - changelog: - uses: ./.github/workflows/changelog.yml - with: - job: push-changelog diff --git a/.github/workflows/changelog.yml b/.github/workflows/release.yml similarity index 61% rename from .github/workflows/changelog.yml rename to .github/workflows/release.yml index 3377fb5..f9e19f8 100644 --- a/.github/workflows/changelog.yml +++ b/.github/workflows/release.yml @@ -1,36 +1,5 @@ -on: workflow_call +on: [release] jobs: - - push-changelog: - name: Generate changelog - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Generate a changelog - uses: orhun/git-cliff-action@v3 - with: - config: cliff.toml - args: --verbose - env: - OUTPUT: CHANGELOG.md - GITHUB_REPO: ${{ github.repository }} - - - name: Commit - run: | - git config user.name 'github-actions[bot]' - git config user.email 'github-actions[bot]@users.noreply.github.com' - set +e - git add CHANGELOG.md - git commit -m "Update changelog" - git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git add_changelog - - - - changelog: name: Generate changelog runs-on: ubuntu-latest From b7c21aa889d7cfc96b4765df3257bce7fe856bec Mon Sep 17 00:00:00 2001 From: rakuja Date: Thu, 30 May 2024 21:32:47 +0200 Subject: [PATCH 05/10] chore: divide actions --- .github/workflows/pr.yml | 25 +++++++++++++++++++ .../{main_push_or_pr.yml => push.yml} | 9 +++---- 2 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/pr.yml rename .github/workflows/{main_push_or_pr.yml => push.yml} (88%) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 0000000..2207478 --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,25 @@ +on: [pull_request] +name: PR +env: + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} + +jobs: + clippy_check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run Clippy + run: cargo clippy --all-targets --all-features + format: + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly + with: + components: rustfmt + - uses: mbrobbel/rustfmt-check@master + with: + token: ${{ secrets.GITHUB_TOKEN }} + mode: review diff --git a/.github/workflows/main_push_or_pr.yml b/.github/workflows/push.yml similarity index 88% rename from .github/workflows/main_push_or_pr.yml rename to .github/workflows/push.yml index 8fee37d..65c5a8d 100644 --- a/.github/workflows/main_push_or_pr.yml +++ b/.github/workflows/push.yml @@ -1,5 +1,5 @@ -on: [push, pull_request] -name: Push on main or PR +on: [push] +name: Push env: BRANCH_NAME: ${{ github.head_ref || github.ref_name }} @@ -12,17 +12,14 @@ jobs: run: cargo clippy --all-targets --all-features format: runs-on: ubuntu-latest - permissions: - pull-requests: write steps: - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@nightly + - uses: dtolnay/rust-toolchain@stable with: components: rustfmt - uses: mbrobbel/rustfmt-check@master with: token: ${{ secrets.GITHUB_TOKEN }} - mode: review changelog: name: Generate changelog runs-on: ubuntu-latest From 96c90dc4a54bae2943642322d983bc40e3defc0d Mon Sep 17 00:00:00 2001 From: rakuja Date: Thu, 30 May 2024 21:54:43 +0200 Subject: [PATCH 06/10] chore: update actions --- .github/workflows/pr.yml | 4 +++- .github/workflows/push.yml | 33 ++++----------------------- .github/workflows/push_on_main.yml | 36 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 30 deletions(-) create mode 100644 .github/workflows/push_on_main.yml diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 2207478..40bed02 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -1,4 +1,6 @@ -on: [pull_request] +on: + pull_request: + types: [opened, synchronize, edited, review_requested, ready_for_review] name: PR env: BRANCH_NAME: ${{ github.head_ref || github.ref_name }} diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 65c5a8d..23e2e7c 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -1,7 +1,8 @@ -on: [push] +on: + push: + branches: + - "*" name: Push -env: - BRANCH_NAME: ${{ github.head_ref || github.ref_name }} jobs: clippy_check: @@ -20,29 +21,3 @@ jobs: - uses: mbrobbel/rustfmt-check@master with: token: ${{ secrets.GITHUB_TOKEN }} - changelog: - name: Generate changelog - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Generate a changelog - uses: orhun/git-cliff-action@v3 - with: - config: cliff.toml - args: --verbose - env: - OUTPUT: CHANGELOG.md - GITHUB_REPO: ${{ github.repository }} - - - name: Commit - run: | - git config user.name 'github-actions[bot]' - git config user.email 'github-actions[bot]@users.noreply.github.com' - set +e - git add CHANGELOG.md - git commit --no-verify -m "Update changelog" - git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git ${{ env.BRANCH_NAME }} diff --git a/.github/workflows/push_on_main.yml b/.github/workflows/push_on_main.yml new file mode 100644 index 0000000..41b3020 --- /dev/null +++ b/.github/workflows/push_on_main.yml @@ -0,0 +1,36 @@ +name: Push on main +on: + push: + branches: + - 'master' + +env: + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} + +jobs: + changelog: + name: Generate changelog + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Generate a changelog + uses: orhun/git-cliff-action@v3 + with: + config: cliff.toml + args: --verbose + env: + OUTPUT: CHANGELOG.md + GITHUB_REPO: ${{ github.repository }} + + - name: Commit + run: | + git config user.name 'github-actions[bot]' + git config user.email 'github-actions[bot]@users.noreply.github.com' + set +e + git add CHANGELOG.md + git commit --no-verify -m "Update changelog" + git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git ${{ env.BRANCH_NAME }} From 51e3806f1df5099d7ac43e7141a13bb51cb57a47 Mon Sep 17 00:00:00 2001 From: rakuja Date: Thu, 30 May 2024 21:55:50 +0200 Subject: [PATCH 07/10] chore: update actions --- .github/workflows/pr.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 40bed02..9a7e560 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -6,12 +6,6 @@ env: BRANCH_NAME: ${{ github.head_ref || github.ref_name }} jobs: - clippy_check: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Run Clippy - run: cargo clippy --all-targets --all-features format: runs-on: ubuntu-latest permissions: From 8b2aa2a29043bb34a98358c6efcdc7a02fb7565a Mon Sep 17 00:00:00 2001 From: rakuja Date: Thu, 30 May 2024 22:05:38 +0200 Subject: [PATCH 08/10] chore: update actions --- .github/workflows/push_on_main.yml | 36 --------------------------- .github/workflows/release.yml | 40 ++++++++++-------------------- 2 files changed, 13 insertions(+), 63 deletions(-) delete mode 100644 .github/workflows/push_on_main.yml diff --git a/.github/workflows/push_on_main.yml b/.github/workflows/push_on_main.yml deleted file mode 100644 index 41b3020..0000000 --- a/.github/workflows/push_on_main.yml +++ /dev/null @@ -1,36 +0,0 @@ -name: Push on main -on: - push: - branches: - - 'master' - -env: - BRANCH_NAME: ${{ github.head_ref || github.ref_name }} - -jobs: - changelog: - name: Generate changelog - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Generate a changelog - uses: orhun/git-cliff-action@v3 - with: - config: cliff.toml - args: --verbose - env: - OUTPUT: CHANGELOG.md - GITHUB_REPO: ${{ github.repository }} - - - name: Commit - run: | - git config user.name 'github-actions[bot]' - git config user.email 'github-actions[bot]@users.noreply.github.com' - set +e - git add CHANGELOG.md - git commit --no-verify -m "Update changelog" - git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git ${{ env.BRANCH_NAME }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f9e19f8..4bca111 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,10 +1,11 @@ on: [release] +env: + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} + jobs: changelog: name: Generate changelog runs-on: ubuntu-latest - outputs: - release_body: ${{ steps.git-cliff.outputs.content }} steps: - name: Checkout uses: actions/checkout@v4 @@ -13,33 +14,18 @@ jobs: - name: Generate a changelog uses: orhun/git-cliff-action@v3 - id: git-cliff with: config: cliff.toml - args: -vv --latest --strip header + args: --verbose env: - OUTPUT: CHANGES.md + OUTPUT: CHANGELOG.md GITHUB_REPO: ${{ github.repository }} - # use release body in the same job - - name: Upload the binary releases - uses: svenstaro/upload-release-action@v2 - with: - file: binary_release.zip - repo_token: ${{ secrets.GITHUB_TOKEN }} - tag: ${{ github.ref }} - body: ${{ steps.git-cliff.outputs.content }} - - # use release body in another job - upload: - name: Upload the release - needs: changelog - runs-on: ubuntu-latest - steps: - - name: Upload the binary releases - uses: svenstaro/upload-release-action@v2 - with: - file: binary_release.zip - repo_token: ${{ secrets.GITHUB_TOKEN }} - tag: ${{ github.ref }} - body: ${{ needs.changelog.outputs.release_body }} + - name: Commit + run: | + git config user.name 'github-actions[bot]' + git config user.email 'github-actions[bot]@users.noreply.github.com' + set +e + git add CHANGELOG.md + git commit --no-verify -m "Update changelog" + git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git ${{ env.BRANCH_NAME }} From 11a1da251d6945b7aaed1a200d129fd9905ca769 Mon Sep 17 00:00:00 2001 From: rakuja Date: Thu, 30 May 2024 22:09:36 +0200 Subject: [PATCH 09/10] chore: update actions --- .github/workflows/release.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4bca111..dfa8dfa 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,6 +1,4 @@ on: [release] -env: - BRANCH_NAME: ${{ github.head_ref || github.ref_name }} jobs: changelog: @@ -28,4 +26,4 @@ jobs: set +e git add CHANGELOG.md git commit --no-verify -m "Update changelog" - git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git ${{ env.BRANCH_NAME }} + git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git master From 7d05fc56fee625ecdf567e840489f4c11315c6d7 Mon Sep 17 00:00:00 2001 From: rakuja Date: Thu, 30 May 2024 23:45:20 +0200 Subject: [PATCH 10/10] chore: refactor actions --- .github/workflows/pr.yml | 5 +++-- .github/workflows/push.yml | 3 +++ .github/workflows/release.yml | 36 +++++++++++++++++++++++++++++++---- cliff.toml | 3 +-- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 9a7e560..8f9f3be 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -2,8 +2,9 @@ on: pull_request: types: [opened, synchronize, edited, review_requested, ready_for_review] name: PR -env: - BRANCH_NAME: ${{ github.head_ref || github.ref_name }} + +permissions: + contents: read jobs: format: diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 23e2e7c..5f914e0 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -4,6 +4,9 @@ on: - "*" name: Push +permissions: + contents: read + jobs: clippy_check: runs-on: ubuntu-latest diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dfa8dfa..cc5163e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,7 @@ -on: [release] +on: + release: + types: + - published jobs: changelog: @@ -19,11 +22,36 @@ jobs: OUTPUT: CHANGELOG.md GITHUB_REPO: ${{ github.repository }} - - name: Commit + - name: Create Changelog Branch run: | git config user.name 'github-actions[bot]' git config user.email 'github-actions[bot]@users.noreply.github.com' set +e + git switch -c update_changelog git add CHANGELOG.md - git commit --no-verify -m "Update changelog" - git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git master + git commit --no-verify -m "Update changelog after release" + git push -f https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git update_changelog + + + - name: Create Changelog Pull Request + id: cpr + uses: peter-evans/create-pull-request@v6 + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: Update changelog after release + committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> + author: ${{ github.actor }} <${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com> + signoff: false + base: master + branch: update_changelog + delete-branch: true + title: 'Update changelog after release' + body: | + Automatic changelog update using git cliff + - Auto-generated after latest release + :artificial_satellite: :rocket: :newspaper: + labels: | + documentation + assignees: RakuJa + reviewers: RakuJa + draft: false diff --git a/cliff.toml b/cliff.toml index 51916d0..8decf21 100644 --- a/cliff.toml +++ b/cliff.toml @@ -60,7 +60,6 @@ commit_parsers = [ { message = "^test", group = "Fixed" }, { message = "^fix", group = "Fixed" }, { message = "^.*: fix", group = "Fixed" }, - { message = "^.*", group = "Changed" }, ] # protect breaking changes from being skipped due to matching a skipping commit_parser protect_breaking_commits = false @@ -69,7 +68,7 @@ filter_commits = true # regex for matching git tags tag_pattern = "v[0-9].*" # regex for skipping tags -skip_tags = "v0.1.0-beta.1" +skip_tags = "" # regex for ignoring tags ignore_tags = "" # sort the tags topologically