use async_stream::stream as async_stream;
use futures::{Stream, StreamExt, stream};
use reqwest_middleware::ClientWithMiddleware;
use serde::{Deserialize, Serialize};
use super::*;
use crate::config::Remote;
use crate::error::*;
pub const START_FETCHING_MSG: &str = "Retrieving data from GitLab...";
pub const FINISHED_FETCHING_MSG: &str = "Done fetching GitLab data.";
pub(crate) const TEMPLATE_VARIABLES: &[&str] = &["gitlab", "commit.gitlab", "commit.remote"];
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GitLabProject {
pub id: i64,
pub description: Option<String>,
pub name: String,
pub name_with_namespace: String,
pub path_with_namespace: String,
pub created_at: String,
pub default_branch: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GitLabCommit {
pub id: String,
pub short_id: String,
pub title: String,
pub author_name: String,
pub author_email: String,
pub authored_date: String,
pub committer_name: String,
pub committer_email: String,
pub committed_date: String,
pub created_at: String,
pub message: String,
pub parent_ids: Vec<String>,
pub web_url: String,
}
impl RemoteCommit for GitLabCommit {
fn id(&self) -> String {
self.id.clone()
}
fn username(&self) -> Option<String> {
Some(self.author_name.clone())
}
fn timestamp(&self) -> Option<i64> {
Some(self.convert_to_unix_timestamp(self.committed_date.clone().as_str()))
}
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GitLabMergeRequest {
pub id: i64,
pub iid: i64,
pub project_id: i64,
pub title: String,
pub description: String,
pub state: String,
pub created_at: String,
pub author: GitLabUser,
pub sha: String,
pub merge_commit_sha: Option<String>,
pub squash_commit_sha: Option<String>,
pub web_url: String,
pub labels: Vec<String>,
}
impl RemotePullRequest for GitLabMergeRequest {
fn number(&self) -> i64 {
self.iid
}
fn title(&self) -> Option<String> {
Some(self.title.clone())
}
fn labels(&self) -> Vec<String> {
self.labels.clone()
}
fn merge_commit(&self) -> Option<String> {
self.merge_commit_sha.clone().or_else(|| {
self.squash_commit_sha
.clone()
.or_else(|| Some(self.sha.clone()))
})
}
}
#[derive(Debug, Default, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)]
pub struct GitLabUser {
pub id: i64,
pub name: String,
pub username: String,
pub state: String,
pub avatar_url: Option<String>,
pub web_url: String,
}
#[derive(Debug, Default, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)]
pub struct GitLabReference {
pub short: String,
pub relative: String,
pub full: String,
}
#[derive(Debug, Clone)]
pub struct GitLabClient {
remote: Remote,
client: ClientWithMiddleware,
}
impl TryFrom<Remote> for GitLabClient {
type Error = Error;
fn try_from(remote: Remote) -> Result<Self> {
Ok(Self {
client: remote.create_client("application/json")?,
remote,
})
}
}
impl RemoteClient for GitLabClient {
const API_URL: &'static str = "https://gitlab.com/api/v4";
const API_URL_ENV: &'static str = "GITLAB_API_URL";
fn remote(&self) -> Remote {
self.remote.clone()
}
fn client(&self) -> ClientWithMiddleware {
self.client.clone()
}
}
impl GitLabClient {
fn project_url(api_url: &str, remote: &Remote) -> String {
format!(
"{}/projects/{}%2F{}",
api_url,
urlencoding::encode(remote.owner.as_str()),
remote.repo
)
}
fn commits_url(project_id: i64, api_url: &str, ref_name: Option<&str>, page: i32) -> String {
let mut url = format!(
"{api_url}/projects/{project_id}/repository/commits?per_page={MAX_PAGE_SIZE}&\
page={page}"
);
if let Some(ref_name) = ref_name {
url.push_str(&format!("&ref_name={ref_name}"));
}
url
}
fn pull_requests_url(project_id: i64, api_url: &str, page: i32) -> String {
format!(
"{api_url}/projects/{project_id}/merge_requests?per_page={MAX_PAGE_SIZE}&page={page}&\
state=merged"
)
}
pub async fn get_project(&self) -> Result<GitLabProject> {
let url = Self::project_url(&self.api_url(), &self.remote());
self.get_json::<GitLabProject>(&url).await
}
pub async fn get_commits(
&self,
project_id: i64,
ref_name: Option<&str>,
) -> Result<Vec<Box<dyn RemoteCommit>>> {
use futures::TryStreamExt;
self.get_commit_stream(project_id, ref_name)
.try_collect()
.await
}
pub async fn get_pull_requests(
&self,
project_id: i64,
) -> Result<Vec<Box<dyn RemotePullRequest>>> {
use futures::TryStreamExt;
self.get_pull_request_stream(project_id).try_collect().await
}
fn get_commit_stream<'a>(
&'a self,
project_id: i64,
ref_name: Option<&str>,
) -> impl Stream<Item = Result<Box<dyn RemoteCommit>>> + 'a {
let ref_name = ref_name.map(ToString::to_string);
async_stream! {
let page_stream = stream::iter(1..)
.map(move |page| {
let ref_name = ref_name.clone();
async move {
let url = Self::commits_url(project_id, &self.api_url(), ref_name.as_deref(), page);
self.get_json::<Vec<GitLabCommit>>(&url).await
}
})
.buffered(10);
let mut page_stream = Box::pin(page_stream);
while let Some(page_result) = page_stream.next().await {
match page_result {
Ok(commits) => {
if commits.is_empty() {
break;
}
for commit in commits {
yield Ok(Box::new(commit) as Box<dyn RemoteCommit>);
}
}
Err(e) => {
yield Err(e);
break;
}
}
}
}
}
fn get_pull_request_stream<'a>(
&'a self,
project_id: i64,
) -> impl Stream<Item = Result<Box<dyn RemotePullRequest>>> + 'a {
async_stream! {
let page_stream = stream::iter(1..)
.map(move |page| async move {
let url = Self::pull_requests_url(project_id, &self.api_url(), page);
self.get_json::<Vec<GitLabMergeRequest>>(&url).await
})
.buffered(5);
let mut page_stream = Box::pin(page_stream);
while let Some(page_result) = page_stream.next().await {
match page_result {
Ok(mrs) => {
if mrs.is_empty() {
break;
}
for mr in mrs {
yield Ok(Box::new(mr) as Box<dyn RemotePullRequest>);
}
}
Err(e) => {
yield Err(e);
break;
}
}
}
}
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn gitlab_project_url_encodes_owner() {
let remote = Remote {
owner: "abc/def".to_string(),
repo: "xyz1".to_string(),
..Default::default()
};
let url = GitLabClient::project_url("https://gitlab.test.com/api/v4", &remote);
assert_eq!(
"https://gitlab.test.com/api/v4/projects/abc%2Fdef%2Fxyz1",
url
);
}
#[test]
fn timestamp() {
let remote_commit = GitLabCommit {
id: String::from("1d244937ee6ceb8e0314a4a201ba93a7a61f2071"),
author_name: String::from("orhun"),
committed_date: String::from("2021-07-18T15:14:39+03:00"),
..Default::default()
};
assert_eq!(Some(1_626_610_479), remote_commit.timestamp());
}
#[test]
fn pull_request_no_merge_commit() {
let mr = GitLabMergeRequest {
sha: String::from("1d244937ee6ceb8e0314a4a201ba93a7a61f2071"),
..Default::default()
};
assert!(mr.merge_commit().is_some());
}
#[test]
fn pull_request_squash_commit() {
let mr = GitLabMergeRequest {
squash_commit_sha: Some(String::from("1d244937ee6ceb8e0314a4a201ba93a7a61f2071")),
..Default::default()
};
assert!(mr.merge_commit().is_some());
}
}