Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codeberg): add Gitea support #680

Merged
merged 9 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/integration.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Integration ⚙️
description: Report a bug or request a feature about an integration (e.g GitHub/GitLab/Bitbucket)
description: Report a bug or request a feature about an integration (e.g GitHub/GitLab/Gitea/Bitbucket)
labels: ["integration"]
assignees:
- orhun
Expand Down
46 changes: 46 additions & 0 deletions .github/fixtures/test-gitea-integration/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[remote.gitea]
owner = "ThetaDev"
repo = "git-cliff-readme-example"

[changelog]
# template for the changelog body
# https://keats.github.io/tera/docs/#introduction
body = """
## What's Changed
{%- 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 %}
{%- endfor -%}

{% if gitea.contributors | filter(attribute="is_first_time", value=true) | length != 0 %}
{% raw %}\n{% endraw -%}
## New Contributors
{%- endif %}\
{% for contributor in gitea.contributors | filter(attribute="is_first_time", value=true) %}
* @{{ contributor.username }} made their first contribution
{%- if contributor.pr_number %} in \
[#{{ contributor.pr_number }}]({{ self::remote_url() }}/pull/{{ contributor.pr_number }}) \
{%- endif %}
{%- endfor -%}
{% raw %}\n\n{% endraw -%}
"""
# remove the leading and trailing whitespace from the template
trim = true
# changelog footer
footer = """
<!-- generated by -cliff -->
"""

[git]
# parse the commits based on https://www.conventionalcommits.org
conventional_commits = false
# 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 preprocessing the commit messages
commit_preprocessors = [{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "" }]
# protect breaking changes from being skipped due to matching a skipping commit_parser
protect_breaking_commits = false
6 changes: 6 additions & 0 deletions .github/fixtures/test-gitea-integration/commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -e

git remote add origin https://codeberg.org/ThetaDev/git-cliff-readme-example.git
git pull origin master
git fetch --tags
15 changes: 15 additions & 0 deletions .github/fixtures/test-gitea-integration/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## What's Changed
* Initial commit by @ThetaDev
* docs(project): add README.md by @ThetaDev
* feat(parser): add ability to parse arrays by @ThetaDev
* fix(args): rename help argument due to conflict by @ThetaDev
* docs(example)!: add tested usage example by @ThetaDev
* refactor(parser): expose string functions by @ThetaDev
* chore(release): add release script by @ThetaDev
* feat(config): support multiple file formats by @ThetaDev
* feat(cache): use cache while fetching pages by @ThetaDev

## New Contributors
* @ThetaDev made their first contribution

<!-- generated by -cliff -->
3 changes: 3 additions & 0 deletions .github/workflows/test-fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:
include:
- fixtures-name: new-fixture-template
- fixtures-name: test-github-integration
- fixtures-name: test-gitlab-integration
- fixtures-name: test-gitea-integration
- fixtures-name: test-bitbucket-integration
- fixtures-name: test-ignore-tags
- fixtures-name: test-topo-order
command: --latest
Expand Down
30 changes: 12 additions & 18 deletions git-cliff-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,30 @@ default = ["repo"]
## You can turn this off if you already have the commits to put in the
## changelog and you don't need `git-cliff` to parse them.
repo = ["dep:git2", "dep:glob", "dep:indexmap"]
## Enable integration with GitHub.
## You can turn this off if you don't use GitHub and don't want
## to make network requests to the GitHub API.
github = [
# Enable integration with remote repositories.
remote = [
"dep:reqwest",
"dep:http-cache-reqwest",
"dep:reqwest-middleware",
"dep:tokio",
"dep:futures",
]
## Enable integration with GitHub.
## You can turn this off if you don't use GitHub and don't want
## to make network requests to the GitHub API.
github = ["remote"]
## Enable integration with GitLab.
## You can turn this off if you don't use GitLab and don't want
## to make network requests to the GitLab API.
gitlab = [
"dep:reqwest",
"dep:http-cache-reqwest",
"dep:reqwest-middleware",
"dep:tokio",
"dep:futures",
]
gitlab = ["remote"]
## Enable integration with Bitbucket.
## You can turn this off if you don't use Bitbucket and don't want
## to make network requests to the Bitbucket API.
bitbucket = [
"dep:reqwest",
"dep:http-cache-reqwest",
"dep:reqwest-middleware",
"dep:tokio",
"dep:futures",
]
bitbucket = ["remote"]
## Enable integration with Gitea.
## You can turn this off if you don't use Gitea and don't want
## to make network requests to the Gitea API.
gitea = ["remote"]

[dependencies]
glob = { workspace = true, optional = true }
Expand Down
83 changes: 82 additions & 1 deletion git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::release::{
};
#[cfg(feature = "bitbucket")]
use crate::remote::bitbucket::BitbucketClient;
#[cfg(feature = "gitea")]
use crate::remote::gitea::GiteaClient;
#[cfg(feature = "github")]
use crate::remote::github::GitHubClient;
#[cfg(feature = "gitlab")]
Expand Down Expand Up @@ -224,7 +226,10 @@ impl<'a> Changelog<'a> {
github_client.get_pull_requests(),
)?;
debug!("Number of GitHub commits: {}", commits.len());
debug!("Number of GitHub pull requests: {}", commits.len());
debug!(
"Number of GitHub pull requests: {}",
pull_requests.len()
);
Ok((commits, pull_requests))
});
info!("{}", github::FINISHED_FETCHING_MSG);
Expand Down Expand Up @@ -300,6 +305,57 @@ impl<'a> Changelog<'a> {
}
}

/// Returns the Gitea metadata needed for the changelog.
///
/// This function creates a multithread async runtime for handling the
/// requests. The following are fetched from the GitHub REST API:
///
/// - Commits
/// - Pull requests
///
/// Each of these are paginated requests so they are being run in parallel
/// for speedup.
///
/// If no Gitea related variable is used in the template then this function
/// returns empty vectors.
#[cfg(feature = "gitea")]
fn get_gitea_metadata(&self) -> Result<crate::remote::RemoteMetadata> {
use crate::remote::gitea;
if self
.body_template
.contains_variable(gitea::TEMPLATE_VARIABLES) ||
self.footer_template
.as_ref()
.map(|v| v.contains_variable(gitea::TEMPLATE_VARIABLES))
.unwrap_or(false)
{
warn!("You are using an experimental feature! Please report bugs at <https://git-cliff.org/issues>");
let gitea_client =
GiteaClient::try_from(self.config.remote.gitea.clone())?;
info!(
"{} ({})",
gitea::START_FETCHING_MSG,
self.config.remote.gitea
);
let data = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?
.block_on(async {
let (commits, pull_requests) = tokio::try_join!(
gitea_client.get_commits(),
gitea_client.get_pull_requests(),
)?;
debug!("Number of Gitea commits: {}", commits.len());
debug!("Number of Gitea pull requests: {}", pull_requests.len());
Ok((commits, pull_requests))
});
info!("{}", gitea::FINISHED_FETCHING_MSG);
data
} else {
Ok((vec![], vec![]))
}
}

/// Returns the Bitbucket metadata needed for the changelog.
///
/// This function creates a multithread async runtime for handling the
Expand Down Expand Up @@ -379,6 +435,13 @@ impl<'a> Changelog<'a> {
} else {
(vec![], vec![])
};
#[cfg(feature = "gitea")]
let (gitea_commits, gitea_merge_request) = if self.config.remote.gitea.is_set() {
self.get_gitea_metadata()
.expect("Could not get gitea metadata")
} else {
(vec![], vec![])
};
#[cfg(feature = "bitbucket")]
let (bitbucket_commits, bitbucket_pull_request) =
if self.config.remote.bitbucket.is_set() {
Expand All @@ -398,6 +461,11 @@ impl<'a> Changelog<'a> {
gitlab_commits.clone(),
gitlab_merge_request.clone(),
)?;
#[cfg(feature = "gitea")]
release.update_gitea_metadata(
gitea_commits.clone(),
gitea_merge_request.clone(),
)?;
#[cfg(feature = "bitbucket")]
release.update_bitbucket_metadata(
bitbucket_commits.clone(),
Expand Down Expand Up @@ -692,6 +760,11 @@ mod test {
repo: String::from("awesome"),
token: None,
},
gitea: Remote {
owner: String::from("coolguy"),
repo: String::from("awesome"),
token: None,
},
bitbucket: Remote {
owner: String::from("coolguy"),
repo: String::from("awesome"),
Expand Down Expand Up @@ -771,6 +844,10 @@ mod test {
gitlab: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
},
#[cfg(feature = "gitea")]
gitea: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
},
#[cfg(feature = "bitbucket")]
bitbucket: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
Expand Down Expand Up @@ -826,6 +903,10 @@ mod test {
gitlab: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
},
#[cfg(feature = "gitea")]
gitea: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
},
#[cfg(feature = "bitbucket")]
bitbucket: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
Expand Down
5 changes: 5 additions & 0 deletions git-cliff-core/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ pub struct Commit<'a> {
/// GitLab metadata of the commit.
#[cfg(feature = "gitlab")]
pub gitlab: crate::remote::RemoteContributor,
/// Gitea metadata of the commit.
#[cfg(feature = "gitea")]
pub gitea: crate::remote::RemoteContributor,
/// Bitbucket metadata of the commit.
#[cfg(feature = "bitbucket")]
pub bitbucket: crate::remote::RemoteContributor,
Expand Down Expand Up @@ -446,6 +449,8 @@ impl Serialize for Commit<'_> {
commit.serialize_field("github", &self.github)?;
#[cfg(feature = "gitlab")]
commit.serialize_field("gitlab", &self.gitlab)?;
#[cfg(feature = "gitea")]
commit.serialize_field("gitea", &self.gitea)?;
#[cfg(feature = "bitbucket")]
commit.serialize_field("bitbucket", &self.bitbucket)?;
commit.end()
Expand Down
3 changes: 3 additions & 0 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ pub struct RemoteConfig {
/// GitLab remote.
#[serde(default)]
pub gitlab: Remote,
/// Gitea remote.
#[serde(default)]
pub gitea: Remote,
/// Bitbucket remote.
#[serde(default)]
pub bitbucket: Remote,
Expand Down
6 changes: 3 additions & 3 deletions git-cliff-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ pub enum Error {
SemverError(#[from] semver::Error),
/// The errors that may occur when processing a HTTP request.
#[error("HTTP client error: `{0}`")]
#[cfg(any(feature = "github", feature = "gitlab", feature = "bitbucket"))]
#[cfg(feature = "remote")]
HttpClientError(#[from] reqwest::Error),
/// The errors that may occur while constructing the HTTP client with
/// middleware.
#[error("HTTP client with middleware error: `{0}`")]
#[cfg(any(feature = "github", feature = "gitlab", feature = "bitbucket"))]
#[cfg(feature = "remote")]
HttpClientMiddlewareError(#[from] reqwest_middleware::Error),
/// A possible error when converting a HeaderValue from a string or byte
/// slice.
#[error("HTTP header error: `{0}`")]
#[cfg(any(feature = "github", feature = "gitlab", feature = "bitbucket"))]
#[cfg(feature = "remote")]
HttpHeaderError(#[from] reqwest::header::InvalidHeaderValue),
/// Error that may occur during handling pages.
#[error("Pagination error: `{0}`")]
Expand Down
2 changes: 1 addition & 1 deletion git-cliff-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub mod error;
/// Common release type.
pub mod release;
/// Remote handler.
#[cfg(any(feature = "github", feature = "gitlab", feature = "bitbucket"))]
#[cfg(feature = "remote")]
#[allow(async_fn_in_trait)]
pub mod remote;
/// Git repository.
Expand Down
Loading
Loading