Skip to content

Commit

Permalink
feat(core): add remote to commit and deprecate fields (#822)
Browse files Browse the repository at this point in the history
* feat(core): add `remote` field to commit

* refactor

* fmt

* deprecate old fields

* serialize remote field

* fix

* fix typo

* allow deprecated

* move warn

* move filename

* docs

* docs

* fix

* fmt

* chore(fixtures): use remote instead of platform name for template variables

* remove useless method

* update docs

* use contains_variable

* fix no-default-features

* chore(remote): update template varibles for platforms

* set commit.remote

* chore(release): update unit tests for new remote field

---------

Co-authored-by: Orhun Parmaksız <[email protected]>
  • Loading branch information
marcoieni and orhun authored Sep 15, 2024
1 parent fe37d14 commit 87e2c1d
Show file tree
Hide file tree
Showing 22 changed files with 251 additions and 79 deletions.
4 changes: 2 additions & 2 deletions .github/fixtures/test-bitbucket-integration/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ body = """
{%- if version %} in {{ version }}{%- endif -%}
{% for commit in commits %}
* {{ commit.message | split(pat="\n") | first | trim }}\
{% if commit.bitbucket.username %} by @{{ commit.bitbucket.username }}{%- endif -%}
{% if commit.bitbucket.pr_number %} in #{{ commit.bitbucket.pr_number }}{%- endif %}
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
{% if commit.remote.pr_number %} in #{{ commit.remote.pr_number }}{%- endif %}
{%- endfor -%}
{% if bitbucket.contributors | filter(attribute="is_first_time", value=true) | length != 0 %}
Expand Down
4 changes: 2 additions & 2 deletions .github/fixtures/test-gitea-integration/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ body = """
{%- if version %} in {{ version }}{%- endif -%}
{% for commit in commits %}
* {{ commit.message | split(pat="\n") | first | trim }}\
{% if commit.gitea.username %} by @{{ commit.gitea.username }}{%- endif -%}
{% if commit.gitea.pr_number %} in #{{ commit.gitea.pr_number }}{%- endif %}
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
{% if commit.remote.pr_number %} in #{{ commit.remote.pr_number }}{%- endif %}
{%- endfor -%}
{% if gitea.contributors | filter(attribute="is_first_time", value=true) | length != 0 %}
Expand Down
4 changes: 2 additions & 2 deletions .github/fixtures/test-github-integration/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ body = """
{%- if version %} in {{ version }}{%- endif -%}
{% for commit in commits %}
* {{ commit.message | split(pat="\n") | first | trim }}\
{% if commit.github.username %} by @{{ commit.github.username }}{%- endif -%}
{% if commit.github.pr_number %} in #{{ commit.github.pr_number }}{%- endif %}
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
{% if commit.remote.pr_number %} in #{{ commit.remote.pr_number }}{%- endif %}
{%- endfor -%}
{% if github.contributors | filter(attribute="is_first_time", value=true) | length != 0 %}
Expand Down
4 changes: 2 additions & 2 deletions .github/fixtures/test-gitlab-integration/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ body = """
{%- if version %} in {{ version }}{%- endif -%}
{% for commit in commits %}
* {{ commit.message | split(pat="\n") | first | trim }}\
{% if commit.gitlab.username %} by @{{ commit.gitlab.username }}{%- endif -%}
{% if commit.gitlab.pr_number %} in #{{ commit.gitlab.pr_number }}{%- endif %}
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
{% if commit.remote.pr_number %} in #{{ commit.remote.pr_number }}{%- endif %}
{%- endfor -%}
{% if gitlab.contributors | filter(attribute="is_first_time", value=true) | length != 0 %}
Expand Down
6 changes: 3 additions & 3 deletions examples/github-keepachangelog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ body = """
### {{ group | upper_first }}
{%- for commit in commits %}
- {{ commit.message | split(pat="\n") | first | upper_first | trim }}\
{% if commit.github.username %} by @{{ commit.github.username }}{%- endif -%}
{% if commit.github.pr_number %} in \
[#{{ commit.github.pr_number }}]({{ self::remote_url() }}/pull/{{ commit.github.pr_number }}) \
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
{% if commit.remote.pr_number %} in \
[#{{ commit.remote.pr_number }}]({{ self::remote_url() }}/pull/{{ commit.remote.pr_number }}) \
{%- endif -%}
{% endfor %}
{% endfor %}
Expand Down
10 changes: 5 additions & 5 deletions examples/github.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ body = """
{%- if version %} in {{ version }}{%- endif -%}
{% for commit in commits %}
{% if commit.github.pr_title -%}
{%- set commit_message = commit.github.pr_title -%}
{% if commit.remote.pr_title -%}
{%- set commit_message = commit.remote.pr_title -%}
{%- else -%}
{%- set commit_message = commit.message -%}
{%- endif -%}
* {{ commit_message | split(pat="\n") | first | trim }}\
{% if commit.github.username %} by @{{ commit.github.username }}{%- endif -%}
{% if commit.github.pr_number %} in \
[#{{ commit.github.pr_number }}]({{ self::remote_url() }}/pull/{{ commit.github.pr_number }}) \
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
{% if commit.remote.pr_number %} in \
[#{{ commit.remote.pr_number }}]({{ self::remote_url() }}/pull/{{ commit.remote.pr_number }}) \
{%- endif %}
{%- endfor -%}
Expand Down
33 changes: 24 additions & 9 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,7 @@ impl<'a> Changelog<'a> {
Some(header) => Some(Template::new(header.to_string(), trim)?),
None => None,
},
body_template: Template::new(
config
.changelog
.body
.as_deref()
.unwrap_or_default()
.to_string(),
trim,
)?,
body_template: get_body_template(config, trim)?,
footer_template: match &config.changelog.footer {
Some(footer) => Some(Template::new(footer.to_string(), trim)?),
None => None,
Expand Down Expand Up @@ -612,6 +604,29 @@ impl<'a> Changelog<'a> {
}
}

fn get_body_template(config: &Config, trim: bool) -> Result<Template> {
let template_str = config
.changelog
.body
.as_deref()
.unwrap_or_default()
.to_string();
let template = Template::new(template_str, trim)?;
let deprecated_vars = [
"commit.github",
"commit.gitea",
"commit.gitlab",
"commit.bitbucket",
];
if template.contains_variable(&deprecated_vars) {
warn!(
"Variables {deprecated_vars:?} are deprecated and will be removed in \
the future. Use `commit.remote` instead."
);
}
Ok(template)
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
18 changes: 14 additions & 4 deletions git-cliff-core/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,24 @@ pub struct Commit<'a> {
pub merge_commit: bool,
/// Arbitrary data to be used with the `--from-context` CLI option.
pub extra: Option<Value>,
/// Remote metadata of the commit.
pub remote: Option<crate::contributor::RemoteContributor>,
/// GitHub metadata of the commit.
#[cfg(feature = "github")]
pub github: crate::remote::RemoteContributor,
#[deprecated(note = "Use `remote` field instead")]
pub github: crate::contributor::RemoteContributor,
/// GitLab metadata of the commit.
#[cfg(feature = "gitlab")]
pub gitlab: crate::remote::RemoteContributor,
#[deprecated(note = "Use `remote` field instead")]
pub gitlab: crate::contributor::RemoteContributor,
/// Gitea metadata of the commit.
#[cfg(feature = "gitea")]
pub gitea: crate::remote::RemoteContributor,
#[deprecated(note = "Use `remote` field instead")]
pub gitea: crate::contributor::RemoteContributor,
/// Bitbucket metadata of the commit.
#[cfg(feature = "bitbucket")]
pub bitbucket: crate::remote::RemoteContributor,
#[deprecated(note = "Use `remote` field instead")]
pub bitbucket: crate::contributor::RemoteContributor,
}

impl<'a> From<String> for Commit<'a> {
Expand Down Expand Up @@ -398,6 +404,7 @@ impl Commit<'_> {
}

impl Serialize for Commit<'_> {
#[allow(deprecated)]
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down Expand Up @@ -466,6 +473,9 @@ impl Serialize for Commit<'_> {
commit.serialize_field("gitea", &self.gitea)?;
#[cfg(feature = "bitbucket")]
commit.serialize_field("bitbucket", &self.bitbucket)?;
if let Some(remote) = &self.remote {
commit.serialize_field("remote", remote)?;
}
commit.end()
}
}
Expand Down
29 changes: 29 additions & 0 deletions git-cliff-core/src/contributor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use serde::{
Deserialize,
Serialize,
};
use std::hash::{
Hash,
Hasher,
};

/// Representation of a remote contributor.
#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub struct RemoteContributor {
/// Username.
pub username: Option<String>,
/// Title of the pull request.
pub pr_title: Option<String>,
/// The pull request that the user created.
pub pr_number: Option<i64>,
/// Labels of the pull request.
pub pr_labels: Vec<String>,
/// Whether if the user contributed for the first time.
pub is_first_time: bool,
}

impl Hash for RemoteContributor {
fn hash<H: Hasher>(&self, state: &mut H) {
self.username.hash(state);
}
}
2 changes: 2 additions & 0 deletions git-cliff-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub mod command;
pub mod commit;
/// Config file parser.
pub mod config;
/// Remote contributor.
pub mod contributor;
/// Embedded file handler.
pub mod embed;
/// Error handling.
Expand Down
Loading

0 comments on commit 87e2c1d

Please sign in to comment.