git_cliff_core/remote/
github.rsuse crate::config::Remote;
use crate::error::*;
use reqwest_middleware::ClientWithMiddleware;
use serde::{
Deserialize,
Serialize,
};
use std::env;
use super::*;
const GITHUB_API_URL: &str = "https://api.github.com";
const GITHUB_API_URL_ENV: &str = "GITHUB_API_URL";
pub const START_FETCHING_MSG: &str = "Retrieving data from GitHub...";
pub const FINISHED_FETCHING_MSG: &str = "Done fetching GitHub data.";
pub(crate) const TEMPLATE_VARIABLES: &[&str] =
&["github", "commit.github", "commit.remote"];
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GitHubCommit {
pub sha: String,
pub author: Option<GitHubCommitAuthor>,
}
impl RemoteCommit for GitHubCommit {
fn id(&self) -> String {
self.sha.clone()
}
fn username(&self) -> Option<String> {
self.author.clone().and_then(|v| v.login)
}
}
impl RemoteEntry for GitHubCommit {
fn url(_id: i64, api_url: &str, remote: &Remote, page: i32) -> String {
format!(
"{}/repos/{}/{}/commits?per_page={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 GitHubCommitAuthor {
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 GitHubPullRequest {
pub number: i64,
pub title: Option<String>,
pub merge_commit_sha: Option<String>,
pub labels: Vec<PullRequestLabel>,
}
impl RemotePullRequest for GitHubPullRequest {
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 GitHubPullRequest {
fn url(_id: i64, api_url: &str, remote: &Remote, page: i32) -> String {
format!(
"{}/repos/{}/{}/pulls?per_page={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 GitHubClient {
remote: Remote,
client: ClientWithMiddleware,
}
impl TryFrom<Remote> for GitHubClient {
type Error = Error;
fn try_from(remote: Remote) -> Result<Self> {
Ok(Self {
client: create_remote_client(&remote, "application/vnd.github+json")?,
remote,
})
}
}
impl RemoteClient for GitHubClient {
fn api_url() -> String {
env::var(GITHUB_API_URL_ENV)
.ok()
.unwrap_or_else(|| GITHUB_API_URL.to_string())
}
fn remote(&self) -> Remote {
self.remote.clone()
}
fn client(&self) -> ClientWithMiddleware {
self.client.clone()
}
}
impl GitHubClient {
pub async fn get_commits(&self) -> Result<Vec<Box<dyn RemoteCommit>>> {
Ok(self
.fetch::<GitHubCommit>(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::<GitHubPullRequest>(0)
.await?
.into_iter()
.map(|v| Box::new(v) as Box<dyn RemotePullRequest>)
.collect())
}
}