Skip to content

Commit 403d3dc

Browse files
Theta-Devorhun
andauthored
feat(codeberg): add Gitea support (#680)
* feat: add gitea integration * fix: several fixes to get gitea working, add test fixture * docs: update docs * fix: cargo fmt * fix: remove dbg trait * fix: tests * chore: remove vscode settings * refactor: cleanup implementation --------- Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
1 parent 5b58a37 commit 403d3dc

File tree

25 files changed

+944
-49
lines changed

25 files changed

+944
-49
lines changed

.github/ISSUE_TEMPLATE/integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Integration ⚙️
2-
description: Report a bug or request a feature about an integration (e.g GitHub/GitLab/Bitbucket)
2+
description: Report a bug or request a feature about an integration (e.g GitHub/GitLab/Gitea/Bitbucket)
33
labels: ["integration"]
44
assignees:
55
- orhun
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[remote.gitea]
2+
owner = "ThetaDev"
3+
repo = "git-cliff-readme-example"
4+
5+
[changelog]
6+
# template for the changelog body
7+
# https://keats.github.io/tera/docs/#introduction
8+
body = """
9+
## What's Changed
10+
{%- if version %} in {{ version }}{%- endif -%}
11+
{% for commit in commits %}
12+
* {{ 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 %}
15+
{%- endfor -%}
16+
17+
{% if gitea.contributors | filter(attribute="is_first_time", value=true) | length != 0 %}
18+
{% raw %}\n{% endraw -%}
19+
## New Contributors
20+
{%- endif %}\
21+
{% for contributor in gitea.contributors | filter(attribute="is_first_time", value=true) %}
22+
* @{{ contributor.username }} made their first contribution
23+
{%- if contributor.pr_number %} in \
24+
[#{{ contributor.pr_number }}]({{ self::remote_url() }}/pull/{{ contributor.pr_number }}) \
25+
{%- endif %}
26+
{%- endfor -%}
27+
{% raw %}\n\n{% endraw -%}
28+
"""
29+
# remove the leading and trailing whitespace from the template
30+
trim = true
31+
# changelog footer
32+
footer = """
33+
<!-- generated by -cliff -->
34+
"""
35+
36+
[git]
37+
# parse the commits based on https://www.conventionalcommits.org
38+
conventional_commits = false
39+
# filter out the commits that are not conventional
40+
filter_unconventional = true
41+
# process each line of a commit as an individual commit
42+
split_commits = false
43+
# regex for preprocessing the commit messages
44+
commit_preprocessors = [{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "" }]
45+
# protect breaking changes from being skipped due to matching a skipping commit_parser
46+
protect_breaking_commits = false
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
git remote add origin https://codeberg.org/ThetaDev/git-cliff-readme-example.git
5+
git pull origin master
6+
git fetch --tags
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## What's Changed
2+
* Initial commit by @ThetaDev
3+
* docs(project): add README.md by @ThetaDev
4+
* feat(parser): add ability to parse arrays by @ThetaDev
5+
* fix(args): rename help argument due to conflict by @ThetaDev
6+
* docs(example)!: add tested usage example by @ThetaDev
7+
* refactor(parser): expose string functions by @ThetaDev
8+
* chore(release): add release script by @ThetaDev
9+
* feat(config): support multiple file formats by @ThetaDev
10+
* feat(cache): use cache while fetching pages by @ThetaDev
11+
12+
## New Contributors
13+
* @ThetaDev made their first contribution
14+
15+
<!-- generated by -cliff -->

.github/workflows/test-fixtures.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ jobs:
1818
include:
1919
- fixtures-name: new-fixture-template
2020
- fixtures-name: test-github-integration
21+
- fixtures-name: test-gitlab-integration
22+
- fixtures-name: test-gitea-integration
23+
- fixtures-name: test-bitbucket-integration
2124
- fixtures-name: test-ignore-tags
2225
- fixtures-name: test-topo-order
2326
command: --latest

git-cliff-core/Cargo.toml

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,30 @@ default = ["repo"]
1717
## You can turn this off if you already have the commits to put in the
1818
## changelog and you don't need `git-cliff` to parse them.
1919
repo = ["dep:git2", "dep:glob", "dep:indexmap"]
20-
## Enable integration with GitHub.
21-
## You can turn this off if you don't use GitHub and don't want
22-
## to make network requests to the GitHub API.
23-
github = [
20+
# Enable integration with remote repositories.
21+
remote = [
2422
"dep:reqwest",
2523
"dep:http-cache-reqwest",
2624
"dep:reqwest-middleware",
2725
"dep:tokio",
2826
"dep:futures",
2927
]
28+
## Enable integration with GitHub.
29+
## You can turn this off if you don't use GitHub and don't want
30+
## to make network requests to the GitHub API.
31+
github = ["remote"]
3032
## Enable integration with GitLab.
3133
## You can turn this off if you don't use GitLab and don't want
3234
## to make network requests to the GitLab API.
33-
gitlab = [
34-
"dep:reqwest",
35-
"dep:http-cache-reqwest",
36-
"dep:reqwest-middleware",
37-
"dep:tokio",
38-
"dep:futures",
39-
]
35+
gitlab = ["remote"]
4036
## Enable integration with Bitbucket.
4137
## You can turn this off if you don't use Bitbucket and don't want
4238
## to make network requests to the Bitbucket API.
43-
bitbucket = [
44-
"dep:reqwest",
45-
"dep:http-cache-reqwest",
46-
"dep:reqwest-middleware",
47-
"dep:tokio",
48-
"dep:futures",
49-
]
39+
bitbucket = ["remote"]
40+
## Enable integration with Gitea.
41+
## You can turn this off if you don't use Gitea and don't want
42+
## to make network requests to the Gitea API.
43+
gitea = ["remote"]
5044

5145
[dependencies]
5246
glob = { workspace = true, optional = true }

git-cliff-core/src/changelog.rs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use crate::release::{
1010
};
1111
#[cfg(feature = "bitbucket")]
1212
use crate::remote::bitbucket::BitbucketClient;
13+
#[cfg(feature = "gitea")]
14+
use crate::remote::gitea::GiteaClient;
1315
#[cfg(feature = "github")]
1416
use crate::remote::github::GitHubClient;
1517
#[cfg(feature = "gitlab")]
@@ -224,7 +226,10 @@ impl<'a> Changelog<'a> {
224226
github_client.get_pull_requests(),
225227
)?;
226228
debug!("Number of GitHub commits: {}", commits.len());
227-
debug!("Number of GitHub pull requests: {}", commits.len());
229+
debug!(
230+
"Number of GitHub pull requests: {}",
231+
pull_requests.len()
232+
);
228233
Ok((commits, pull_requests))
229234
});
230235
info!("{}", github::FINISHED_FETCHING_MSG);
@@ -300,6 +305,57 @@ impl<'a> Changelog<'a> {
300305
}
301306
}
302307

308+
/// Returns the Gitea metadata needed for the changelog.
309+
///
310+
/// This function creates a multithread async runtime for handling the
311+
/// requests. The following are fetched from the GitHub REST API:
312+
///
313+
/// - Commits
314+
/// - Pull requests
315+
///
316+
/// Each of these are paginated requests so they are being run in parallel
317+
/// for speedup.
318+
///
319+
/// If no Gitea related variable is used in the template then this function
320+
/// returns empty vectors.
321+
#[cfg(feature = "gitea")]
322+
fn get_gitea_metadata(&self) -> Result<crate::remote::RemoteMetadata> {
323+
use crate::remote::gitea;
324+
if self
325+
.body_template
326+
.contains_variable(gitea::TEMPLATE_VARIABLES) ||
327+
self.footer_template
328+
.as_ref()
329+
.map(|v| v.contains_variable(gitea::TEMPLATE_VARIABLES))
330+
.unwrap_or(false)
331+
{
332+
warn!("You are using an experimental feature! Please report bugs at <https://git-cliff.org/issues>");
333+
let gitea_client =
334+
GiteaClient::try_from(self.config.remote.gitea.clone())?;
335+
info!(
336+
"{} ({})",
337+
gitea::START_FETCHING_MSG,
338+
self.config.remote.gitea
339+
);
340+
let data = tokio::runtime::Builder::new_multi_thread()
341+
.enable_all()
342+
.build()?
343+
.block_on(async {
344+
let (commits, pull_requests) = tokio::try_join!(
345+
gitea_client.get_commits(),
346+
gitea_client.get_pull_requests(),
347+
)?;
348+
debug!("Number of Gitea commits: {}", commits.len());
349+
debug!("Number of Gitea pull requests: {}", pull_requests.len());
350+
Ok((commits, pull_requests))
351+
});
352+
info!("{}", gitea::FINISHED_FETCHING_MSG);
353+
data
354+
} else {
355+
Ok((vec![], vec![]))
356+
}
357+
}
358+
303359
/// Returns the Bitbucket metadata needed for the changelog.
304360
///
305361
/// This function creates a multithread async runtime for handling the
@@ -379,6 +435,13 @@ impl<'a> Changelog<'a> {
379435
} else {
380436
(vec![], vec![])
381437
};
438+
#[cfg(feature = "gitea")]
439+
let (gitea_commits, gitea_merge_request) = if self.config.remote.gitea.is_set() {
440+
self.get_gitea_metadata()
441+
.expect("Could not get gitea metadata")
442+
} else {
443+
(vec![], vec![])
444+
};
382445
#[cfg(feature = "bitbucket")]
383446
let (bitbucket_commits, bitbucket_pull_request) =
384447
if self.config.remote.bitbucket.is_set() {
@@ -398,6 +461,11 @@ impl<'a> Changelog<'a> {
398461
gitlab_commits.clone(),
399462
gitlab_merge_request.clone(),
400463
)?;
464+
#[cfg(feature = "gitea")]
465+
release.update_gitea_metadata(
466+
gitea_commits.clone(),
467+
gitea_merge_request.clone(),
468+
)?;
401469
#[cfg(feature = "bitbucket")]
402470
release.update_bitbucket_metadata(
403471
bitbucket_commits.clone(),
@@ -692,6 +760,11 @@ mod test {
692760
repo: String::from("awesome"),
693761
token: None,
694762
},
763+
gitea: Remote {
764+
owner: String::from("coolguy"),
765+
repo: String::from("awesome"),
766+
token: None,
767+
},
695768
bitbucket: Remote {
696769
owner: String::from("coolguy"),
697770
repo: String::from("awesome"),
@@ -771,6 +844,10 @@ mod test {
771844
gitlab: crate::remote::RemoteReleaseMetadata {
772845
contributors: vec![],
773846
},
847+
#[cfg(feature = "gitea")]
848+
gitea: crate::remote::RemoteReleaseMetadata {
849+
contributors: vec![],
850+
},
774851
#[cfg(feature = "bitbucket")]
775852
bitbucket: crate::remote::RemoteReleaseMetadata {
776853
contributors: vec![],
@@ -826,6 +903,10 @@ mod test {
826903
gitlab: crate::remote::RemoteReleaseMetadata {
827904
contributors: vec![],
828905
},
906+
#[cfg(feature = "gitea")]
907+
gitea: crate::remote::RemoteReleaseMetadata {
908+
contributors: vec![],
909+
},
829910
#[cfg(feature = "bitbucket")]
830911
bitbucket: crate::remote::RemoteReleaseMetadata {
831912
contributors: vec![],

git-cliff-core/src/commit.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ pub struct Commit<'a> {
128128
/// GitLab metadata of the commit.
129129
#[cfg(feature = "gitlab")]
130130
pub gitlab: crate::remote::RemoteContributor,
131+
/// Gitea metadata of the commit.
132+
#[cfg(feature = "gitea")]
133+
pub gitea: crate::remote::RemoteContributor,
131134
/// Bitbucket metadata of the commit.
132135
#[cfg(feature = "bitbucket")]
133136
pub bitbucket: crate::remote::RemoteContributor,
@@ -446,6 +449,8 @@ impl Serialize for Commit<'_> {
446449
commit.serialize_field("github", &self.github)?;
447450
#[cfg(feature = "gitlab")]
448451
commit.serialize_field("gitlab", &self.gitlab)?;
452+
#[cfg(feature = "gitea")]
453+
commit.serialize_field("gitea", &self.gitea)?;
449454
#[cfg(feature = "bitbucket")]
450455
commit.serialize_field("bitbucket", &self.bitbucket)?;
451456
commit.end()

git-cliff-core/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ pub struct RemoteConfig {
126126
/// GitLab remote.
127127
#[serde(default)]
128128
pub gitlab: Remote,
129+
/// Gitea remote.
130+
#[serde(default)]
131+
pub gitea: Remote,
129132
/// Bitbucket remote.
130133
#[serde(default)]
131134
pub bitbucket: Remote,

git-cliff-core/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ pub enum Error {
7777
SemverError(#[from] semver::Error),
7878
/// The errors that may occur when processing a HTTP request.
7979
#[error("HTTP client error: `{0}`")]
80-
#[cfg(any(feature = "github", feature = "gitlab", feature = "bitbucket"))]
80+
#[cfg(feature = "remote")]
8181
HttpClientError(#[from] reqwest::Error),
8282
/// The errors that may occur while constructing the HTTP client with
8383
/// middleware.
8484
#[error("HTTP client with middleware error: `{0}`")]
85-
#[cfg(any(feature = "github", feature = "gitlab", feature = "bitbucket"))]
85+
#[cfg(feature = "remote")]
8686
HttpClientMiddlewareError(#[from] reqwest_middleware::Error),
8787
/// A possible error when converting a HeaderValue from a string or byte
8888
/// slice.
8989
#[error("HTTP header error: `{0}`")]
90-
#[cfg(any(feature = "github", feature = "gitlab", feature = "bitbucket"))]
90+
#[cfg(feature = "remote")]
9191
HttpHeaderError(#[from] reqwest::header::InvalidHeaderValue),
9292
/// Error that may occur during handling pages.
9393
#[error("Pagination error: `{0}`")]

0 commit comments

Comments
 (0)