-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(config): improve support for backwards compatability
- Loading branch information
Showing
15 changed files
with
495 additions
and
314 deletions.
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 |
---|---|---|
@@ -1,54 +1,74 @@ | ||
name: Run a fixtures test | ||
description: Run a fixtures test | ||
|
||
inputs: | ||
fixtures-dir: | ||
description: Path to the fixtures directory | ||
required: true | ||
command: | ||
description: The git-cliff command to run | ||
required: false | ||
default: "" | ||
date-format: | ||
description: The date format to use | ||
required: false | ||
default: "%Y-%m-%d" | ||
|
||
runs: | ||
using: composite | ||
steps: | ||
- name: Install toolchain | ||
uses: dtolnay/rust-toolchain@nightly | ||
|
||
- name: Install git-cliff | ||
run: cargo install --path git-cliff/ | ||
shell: bash | ||
|
||
- name: Set git config | ||
run: | | ||
git config --global user.name "github-actions[bot]" | ||
git config --global user.email "github-actions[bot]@users.noreply.github.com" | ||
shell: bash | ||
|
||
- name: Create commits and tags | ||
working-directory: ${{ inputs.fixtures-dir }} | ||
run: | | ||
git init | ||
./commit.sh | ||
shell: bash | ||
|
||
- name: Generate a changelog | ||
working-directory: ${{ inputs.fixtures-dir }} | ||
run: git cliff --config cliff.toml ${{ inputs.command }} > output.md | ||
shell: bash | ||
|
||
- name: Compare the output with the expected output | ||
working-directory: ${{ inputs.fixtures-dir }} | ||
env: | ||
IN_DATE_FORMAT: ${{ inputs.date-format }} | ||
run: | | ||
cat output.md | ||
current_date=$(date +"$IN_DATE_FORMAT") | ||
sed -i "s/<<DATE>>/$current_date/g" expected.md | ||
diff --strip-trailing-cr output.md expected.md | ||
shell: bash | ||
name: Run a fixtures test | ||
description: Run a fixtures test | ||
|
||
inputs: | ||
fixtures-dir: | ||
description: Path to the fixtures directory | ||
required: true | ||
command: | ||
description: The git-cliff command to run | ||
required: false | ||
default: "" | ||
date-format: | ||
description: The date format to use | ||
required: false | ||
default: "%Y-%m-%d" | ||
|
||
runs: | ||
using: composite | ||
steps: | ||
- name: Install toolchain | ||
uses: dtolnay/rust-toolchain@nightly | ||
|
||
- name: Install git-cliff | ||
run: cargo install --path git-cliff/ | ||
shell: bash | ||
|
||
- name: Set git config | ||
run: | | ||
git config --global user.name "github-actions[bot]" | ||
git config --global user.email "github-actions[bot]@users.noreply.github.com" | ||
shell: bash | ||
|
||
- name: Create commits and tags | ||
working-directory: ${{ inputs.fixtures-dir }} | ||
run: | | ||
git init | ||
./commit.sh | ||
shell: bash | ||
|
||
- name: Generate a changelog | ||
working-directory: ${{ inputs.fixtures-dir }} | ||
run: git cliff --config cliff.toml ${{ inputs.command }} > output.md | ||
shell: bash | ||
|
||
- name: Compare the output with the expected output | ||
working-directory: ${{ inputs.fixtures-dir }} | ||
env: | ||
IN_DATE_FORMAT: ${{ inputs.date-format }} | ||
run: | | ||
cat output.md | ||
current_date=$(date +"$IN_DATE_FORMAT") | ||
sed -i "s/<<DATE>>/$current_date/g" expected.md | ||
diff --strip-trailing-cr output.md expected.md | ||
shell: bash | ||
|
||
# test backwards compatibility using v1 confiuration | ||
|
||
- name: Generate a chanlog using v1 configuration | ||
if: ${{ hashFiles(format('{0}/cliff.v1.toml', inputs.fixtures-dir)) }} | ||
working-directory: ${{ inputs.fixtures-dir }} | ||
run: git-cliff --config cliff.v1.toml --config-version 1 ${{ inputs.command }} > output.md | ||
shell: bash | ||
|
||
- name: Compare the v1 configuration output with the expected output | ||
if: ${{ hashFiles(format('{0}/cliff.v1.toml', inputs.fixtures-dir)) }} | ||
working-directory: ${{ inputs.fixtures-dir }} | ||
env: | ||
IN_DATE_FORMAT: ${{ inputs.date-format }} | ||
run: | | ||
cat output.md | ||
current_date=$(date +"$IN_DATE_FORMAT") | ||
sed -i "s/<<DATE>>/$current_date/g" expected.md | ||
diff --strip-trailing-cr output.md expected.md | ||
shell: bash |
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,27 @@ | ||
[changelog] | ||
# changelog header | ||
header = """ | ||
# Changelog\n | ||
All notable changes to this project will be documented in this file.\n | ||
""" | ||
# template for the changelog body | ||
# https://keats.github.io/tera/docs/#introduction | ||
body = """ | ||
{% if version %}\ | ||
## [{{ version | trim_start_matches(pat="v") }}] | ||
{% else %}\ | ||
## [unreleased] | ||
{% endif %}\ | ||
{% for group, commits in commits | group_by(attribute="group") %} | ||
### {{ group | upper_first }} | ||
{% for commit in commits %} | ||
- {% if commit.breaking %}[**breaking**] {% endif %}{{ commit.message | upper_first }}\ | ||
{% endfor %} | ||
{% endfor %}\n | ||
""" | ||
# template for the changelog footer | ||
footer = """ | ||
<!-- generated by git-cliff --> | ||
""" | ||
# remove the leading and trailing whitespace from the templates | ||
trim = true |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,103 @@ | ||
use super::DEFAULT_CONFIG_FILENAME; | ||
use crate::error::{ | ||
Error, | ||
Result, | ||
}; | ||
use regex::{ | ||
Regex, | ||
RegexBuilder, | ||
}; | ||
use rust_embed::RustEmbed; | ||
use std::fs; | ||
use std::{ | ||
path::PathBuf, | ||
str, | ||
}; | ||
|
||
/// Manifest file information and regex for matching contents. | ||
#[derive(Debug)] | ||
struct ManifestInfo { | ||
/// Path of the manifest. | ||
path: PathBuf, | ||
/// Regular expression for matching metadata in the manifest. | ||
regex: Regex, | ||
} | ||
|
||
lazy_static::lazy_static! { | ||
/// Array containing manifest information for Rust and Python projects. | ||
static ref MANIFEST_INFO: Vec<ManifestInfo> = vec![ | ||
ManifestInfo { | ||
path: PathBuf::from("Cargo.toml"), | ||
regex: RegexBuilder::new( | ||
r"^\[(?:workspace|package)\.metadata\.git\-cliff\.", | ||
) | ||
.multi_line(true) | ||
.build() | ||
.expect("failed to build regex"), | ||
}, | ||
ManifestInfo { | ||
path: PathBuf::from("pyproject.toml"), | ||
regex: RegexBuilder::new(r"^\[(?:tool)\.git\-cliff\.") | ||
.multi_line(true) | ||
.build() | ||
.expect("failed to build regex"), | ||
}, | ||
]; | ||
|
||
} | ||
|
||
/// Reads the config file contents from project manifest (e.g. Cargo.toml, | ||
/// pyproject.toml) | ||
pub fn read_from_manifest() -> Result<Option<String>> { | ||
for info in (*MANIFEST_INFO).iter() { | ||
if info.path.exists() { | ||
let contents = fs::read_to_string(&info.path)?; | ||
if info.regex.is_match(&contents) { | ||
return Ok(Some(info.regex.replace_all(&contents, "[").to_string())); | ||
} | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
/// Default configuration file embedder/extractor. | ||
/// | ||
/// Embeds `config/`[`DEFAULT_CONFIG`] into the binary. | ||
/// | ||
/// [`DEFAULT_CONFIG`]: crate::DEFAULT_CONFIG | ||
#[derive(Debug, RustEmbed)] | ||
#[folder = "../config/"] | ||
pub struct EmbeddedConfig; | ||
|
||
impl EmbeddedConfig { | ||
/// Extracts the embedded content. | ||
pub fn get_config_str() -> Result<String> { | ||
match Self::get(DEFAULT_CONFIG_FILENAME) { | ||
Some(v) => Ok(str::from_utf8(&v.data)?.to_string()), | ||
None => Err(Error::EmbeddedError(String::from( | ||
"Embedded config not found", | ||
))), | ||
} | ||
} | ||
} | ||
|
||
/// Built-in configuration file embedder/extractor. | ||
/// | ||
/// Embeds the files under `/examples/` into the binary. | ||
#[derive(RustEmbed)] | ||
#[folder = "../examples/"] | ||
pub struct BuiltinConfig; | ||
|
||
impl BuiltinConfig { | ||
/// Extracts the embedded content. | ||
pub fn get_config_str(mut name: String) -> Result<(String, String)> { | ||
if !name.ends_with(".toml") { | ||
name = format!("{name}.toml"); | ||
} | ||
let contents = match Self::get(&name) { | ||
Some(v) => Ok(str::from_utf8(&v.data)?.to_string()), | ||
None => Err(Error::EmbeddedError(format!("config {} not found", name,))), | ||
}?; | ||
Ok((contents, name)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use serde::{ | ||
Deserialize, | ||
Serialize, | ||
}; | ||
|
||
/// Meta section of the configuration file. | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct MetaConfig { | ||
/// The version of the config schema. | ||
pub version: Option<i64>, | ||
} |
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
Oops, something went wrong.