Skip to content

Commit 87e2c1d

Browse files
marcoieniorhun
andauthored
feat(core): add remote to commit and deprecate fields (#822)
* 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]>
1 parent fe37d14 commit 87e2c1d

File tree

22 files changed

+251
-79
lines changed

22 files changed

+251
-79
lines changed

.github/fixtures/test-bitbucket-integration/cliff.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ body = """
1010
{%- if version %} in {{ version }}{%- endif -%}
1111
{% for commit in commits %}
1212
* {{ commit.message | split(pat="\n") | first | trim }}\
13-
{% if commit.bitbucket.username %} by @{{ commit.bitbucket.username }}{%- endif -%}
14-
{% if commit.bitbucket.pr_number %} in #{{ commit.bitbucket.pr_number }}{%- endif %}
13+
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
14+
{% if commit.remote.pr_number %} in #{{ commit.remote.pr_number }}{%- endif %}
1515
{%- endfor -%}
1616
1717
{% if bitbucket.contributors | filter(attribute="is_first_time", value=true) | length != 0 %}

.github/fixtures/test-gitea-integration/cliff.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ body = """
1010
{%- if version %} in {{ version }}{%- endif -%}
1111
{% for commit in commits %}
1212
* {{ commit.message | split(pat="\n") | first | trim }}\
13-
{% if commit.gitea.username %} by @{{ commit.gitea.username }}{%- endif -%}
14-
{% if commit.gitea.pr_number %} in #{{ commit.gitea.pr_number }}{%- endif %}
13+
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
14+
{% if commit.remote.pr_number %} in #{{ commit.remote.pr_number }}{%- endif %}
1515
{%- endfor -%}
1616
1717
{% if gitea.contributors | filter(attribute="is_first_time", value=true) | length != 0 %}

.github/fixtures/test-github-integration/cliff.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ body = """
1111
{%- if version %} in {{ version }}{%- endif -%}
1212
{% for commit in commits %}
1313
* {{ commit.message | split(pat="\n") | first | trim }}\
14-
{% if commit.github.username %} by @{{ commit.github.username }}{%- endif -%}
15-
{% if commit.github.pr_number %} in #{{ commit.github.pr_number }}{%- endif %}
14+
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
15+
{% if commit.remote.pr_number %} in #{{ commit.remote.pr_number }}{%- endif %}
1616
{%- endfor -%}
1717
1818
{% if github.contributors | filter(attribute="is_first_time", value=true) | length != 0 %}

.github/fixtures/test-gitlab-integration/cliff.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ body = """
1111
{%- if version %} in {{ version }}{%- endif -%}
1212
{% for commit in commits %}
1313
* {{ commit.message | split(pat="\n") | first | trim }}\
14-
{% if commit.gitlab.username %} by @{{ commit.gitlab.username }}{%- endif -%}
15-
{% if commit.gitlab.pr_number %} in #{{ commit.gitlab.pr_number }}{%- endif %}
14+
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
15+
{% if commit.remote.pr_number %} in #{{ commit.remote.pr_number }}{%- endif %}
1616
{%- endfor -%}
1717
1818
{% if gitlab.contributors | filter(attribute="is_first_time", value=true) | length != 0 %}

examples/github-keepachangelog.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ body = """
2727
### {{ group | upper_first }}
2828
{%- for commit in commits %}
2929
- {{ commit.message | split(pat="\n") | first | upper_first | trim }}\
30-
{% if commit.github.username %} by @{{ commit.github.username }}{%- endif -%}
31-
{% if commit.github.pr_number %} in \
32-
[#{{ commit.github.pr_number }}]({{ self::remote_url() }}/pull/{{ commit.github.pr_number }}) \
30+
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
31+
{% if commit.remote.pr_number %} in \
32+
[#{{ commit.remote.pr_number }}]({{ self::remote_url() }}/pull/{{ commit.remote.pr_number }}) \
3333
{%- endif -%}
3434
{% endfor %}
3535
{% endfor %}

examples/github.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ body = """
1414
1515
{%- if version %} in {{ version }}{%- endif -%}
1616
{% for commit in commits %}
17-
{% if commit.github.pr_title -%}
18-
{%- set commit_message = commit.github.pr_title -%}
17+
{% if commit.remote.pr_title -%}
18+
{%- set commit_message = commit.remote.pr_title -%}
1919
{%- else -%}
2020
{%- set commit_message = commit.message -%}
2121
{%- endif -%}
2222
* {{ commit_message | split(pat="\n") | first | trim }}\
23-
{% if commit.github.username %} by @{{ commit.github.username }}{%- endif -%}
24-
{% if commit.github.pr_number %} in \
25-
[#{{ commit.github.pr_number }}]({{ self::remote_url() }}/pull/{{ commit.github.pr_number }}) \
23+
{% if commit.remote.username %} by @{{ commit.remote.username }}{%- endif -%}
24+
{% if commit.remote.pr_number %} in \
25+
[#{{ commit.remote.pr_number }}]({{ self::remote_url() }}/pull/{{ commit.remote.pr_number }}) \
2626
{%- endif %}
2727
{%- endfor -%}
2828

git-cliff-core/src/changelog.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,7 @@ impl<'a> Changelog<'a> {
5858
Some(header) => Some(Template::new(header.to_string(), trim)?),
5959
None => None,
6060
},
61-
body_template: Template::new(
62-
config
63-
.changelog
64-
.body
65-
.as_deref()
66-
.unwrap_or_default()
67-
.to_string(),
68-
trim,
69-
)?,
61+
body_template: get_body_template(config, trim)?,
7062
footer_template: match &config.changelog.footer {
7163
Some(footer) => Some(Template::new(footer.to_string(), trim)?),
7264
None => None,
@@ -612,6 +604,29 @@ impl<'a> Changelog<'a> {
612604
}
613605
}
614606

607+
fn get_body_template(config: &Config, trim: bool) -> Result<Template> {
608+
let template_str = config
609+
.changelog
610+
.body
611+
.as_deref()
612+
.unwrap_or_default()
613+
.to_string();
614+
let template = Template::new(template_str, trim)?;
615+
let deprecated_vars = [
616+
"commit.github",
617+
"commit.gitea",
618+
"commit.gitlab",
619+
"commit.bitbucket",
620+
];
621+
if template.contains_variable(&deprecated_vars) {
622+
warn!(
623+
"Variables {deprecated_vars:?} are deprecated and will be removed in \
624+
the future. Use `commit.remote` instead."
625+
);
626+
}
627+
Ok(template)
628+
}
629+
615630
#[cfg(test)]
616631
mod test {
617632
use super::*;

git-cliff-core/src/commit.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,24 @@ pub struct Commit<'a> {
125125
pub merge_commit: bool,
126126
/// Arbitrary data to be used with the `--from-context` CLI option.
127127
pub extra: Option<Value>,
128+
/// Remote metadata of the commit.
129+
pub remote: Option<crate::contributor::RemoteContributor>,
128130
/// GitHub metadata of the commit.
129131
#[cfg(feature = "github")]
130-
pub github: crate::remote::RemoteContributor,
132+
#[deprecated(note = "Use `remote` field instead")]
133+
pub github: crate::contributor::RemoteContributor,
131134
/// GitLab metadata of the commit.
132135
#[cfg(feature = "gitlab")]
133-
pub gitlab: crate::remote::RemoteContributor,
136+
#[deprecated(note = "Use `remote` field instead")]
137+
pub gitlab: crate::contributor::RemoteContributor,
134138
/// Gitea metadata of the commit.
135139
#[cfg(feature = "gitea")]
136-
pub gitea: crate::remote::RemoteContributor,
140+
#[deprecated(note = "Use `remote` field instead")]
141+
pub gitea: crate::contributor::RemoteContributor,
137142
/// Bitbucket metadata of the commit.
138143
#[cfg(feature = "bitbucket")]
139-
pub bitbucket: crate::remote::RemoteContributor,
144+
#[deprecated(note = "Use `remote` field instead")]
145+
pub bitbucket: crate::contributor::RemoteContributor,
140146
}
141147

142148
impl<'a> From<String> for Commit<'a> {
@@ -398,6 +404,7 @@ impl Commit<'_> {
398404
}
399405

400406
impl Serialize for Commit<'_> {
407+
#[allow(deprecated)]
401408
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
402409
where
403410
S: Serializer,
@@ -466,6 +473,9 @@ impl Serialize for Commit<'_> {
466473
commit.serialize_field("gitea", &self.gitea)?;
467474
#[cfg(feature = "bitbucket")]
468475
commit.serialize_field("bitbucket", &self.bitbucket)?;
476+
if let Some(remote) = &self.remote {
477+
commit.serialize_field("remote", remote)?;
478+
}
469479
commit.end()
470480
}
471481
}

git-cliff-core/src/contributor.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use serde::{
2+
Deserialize,
3+
Serialize,
4+
};
5+
use std::hash::{
6+
Hash,
7+
Hasher,
8+
};
9+
10+
/// Representation of a remote contributor.
11+
#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize, Serialize)]
12+
pub struct RemoteContributor {
13+
/// Username.
14+
pub username: Option<String>,
15+
/// Title of the pull request.
16+
pub pr_title: Option<String>,
17+
/// The pull request that the user created.
18+
pub pr_number: Option<i64>,
19+
/// Labels of the pull request.
20+
pub pr_labels: Vec<String>,
21+
/// Whether if the user contributed for the first time.
22+
pub is_first_time: bool,
23+
}
24+
25+
impl Hash for RemoteContributor {
26+
fn hash<H: Hasher>(&self, state: &mut H) {
27+
self.username.hash(state);
28+
}
29+
}

git-cliff-core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub mod command;
2020
pub mod commit;
2121
/// Config file parser.
2222
pub mod config;
23+
/// Remote contributor.
24+
pub mod contributor;
2325
/// Embedded file handler.
2426
pub mod embed;
2527
/// Error handling.

0 commit comments

Comments
 (0)