git_cliff_core/remote/
gitea.rsuse crate::config::Remote;
use crate::error::*;
use reqwest_middleware::ClientWithMiddleware;
use serde::{
Deserialize,
Serialize,
};
use std::env;
use super::*;
const GITEA_API_URL: &str = "https://codeberg.org";
const GITEA_API_URL_ENV: &str = "GITEA_API_URL";
pub const START_FETCHING_MSG: &str = "Retrieving data from Gitea...";
pub const FINISHED_FETCHING_MSG: &str = "Done fetching Gitea data.";
pub(crate) const TEMPLATE_VARIABLES: &[&str] =
&["gitea", "commit.gitea", "commit.remote"];
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GiteaCommit {
pub sha: String,
pub author: Option<GiteaCommitAuthor>,
}
impl RemoteCommit for GiteaCommit {
fn id(&self) -> String {
self.sha.clone()
}
fn username(&self) -> Option<String> {
self.author.clone().and_then(|v| v.login)
}
}
impl RemoteEntry for GiteaCommit {
fn url(_id: i64, api_url: &str, remote: &Remote, page: i32) -> String {
format!(
"{}/api/v1/repos/{}/{}/commits?limit={MAX_PAGE_SIZE}&page={page}",
api_url, remote.owner, remote.repo
)
}
fn buffer_size() -> usize {
10
}
fn early_exit(&self) -> bool {
false
}
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GiteaCommitAuthor {
pub login: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PullRequestLabel {
pub name: String,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GiteaPullRequest {
pub number: i64,
pub title: Option<String>,
pub merge_commit_sha: Option<String>,
pub labels: Vec<PullRequestLabel>,
}
impl RemotePullRequest for GiteaPullRequest {
fn number(&self) -> i64 {
self.number
}
fn title(&self) -> Option<String> {
self.title.clone()
}
fn labels(&self) -> Vec<String> {
self.labels.iter().map(|v| v.name.clone()).collect()
}
fn merge_commit(&self) -> Option<String> {
self.merge_commit_sha.clone()
}
}
impl RemoteEntry for GiteaPullRequest {
fn url(_id: i64, api_url: &str, remote: &Remote, page: i32) -> String {
format!(
"{}/api/v1/repos/{}/{}/pulls?limit={MAX_PAGE_SIZE}&page={page}&\
state=closed",
api_url, remote.owner, remote.repo
)
}
fn buffer_size() -> usize {
5
}
fn early_exit(&self) -> bool {
false
}
}
#[derive(Debug, Clone)]
pub struct GiteaClient {
remote: Remote,
client: ClientWithMiddleware,
}
impl TryFrom<Remote> for GiteaClient {
type Error = Error;
fn try_from(remote: Remote) -> Result<Self> {
Ok(Self {
client: create_remote_client(&remote, "application/json")?,
remote,
})
}
}
impl RemoteClient for GiteaClient {
fn api_url() -> String {
env::var(GITEA_API_URL_ENV)
.ok()
.unwrap_or_else(|| GITEA_API_URL.to_string())
}
fn remote(&self) -> Remote {
self.remote.clone()
}
fn client(&self) -> ClientWithMiddleware {
self.client.clone()
}
}
impl GiteaClient {
pub async fn get_commits(&self) -> Result<Vec<Box<dyn RemoteCommit>>> {
Ok(self
.fetch::<GiteaCommit>(0)
.await?
.into_iter()
.map(|v| Box::new(v) as Box<dyn RemoteCommit>)
.collect())
}
pub async fn get_pull_requests(
&self,
) -> Result<Vec<Box<dyn RemotePullRequest>>> {
Ok(self
.fetch::<GiteaPullRequest>(0)
.await?
.into_iter()
.map(|v| Box::new(v) as Box<dyn RemotePullRequest>)
.collect())
}
}