WIP: gitea

This commit is contained in:
2022-11-24 22:10:34 +01:00
parent 0f8db6be08
commit 1a3084e651
9 changed files with 155 additions and 13 deletions

View File

@@ -1,24 +1,24 @@
use std::{path::PathBuf, sync::Arc};
use git2::{Cred, RemoteCallbacks, Repository, Signature};
use git2::{Cred, PushOptions, RemoteCallbacks, Repository};
use tokio::sync::Mutex;
use crate::{schema::models::GitPushBranch, storage::DynStorageEngine};
use super::GitProvider;
pub struct GitHubGitProvider {
pub struct LocalGitProvider {
storage_engine: DynStorageEngine,
}
impl GitHubGitProvider {
impl LocalGitProvider {
pub fn new(storage_engine: DynStorageEngine) -> Self {
Self { storage_engine }
}
}
#[async_trait::async_trait]
impl GitProvider for GitHubGitProvider {
impl GitProvider for LocalGitProvider {
async fn clone_from_url(&self, url: String) -> eyre::Result<(PathBuf, Repository)> {
tracing::debug!(url, "allocating dir");
let dir = self.storage_engine.allocate_dir().await?;
@@ -86,12 +86,13 @@ impl GitProvider for GitHubGitProvider {
) -> eyre::Result<()> {
let repo = repo.lock().await;
tracing::trace!("pulling signature from local git");
let signature = repo.signature()?;
tracing::trace!("fetching index and adding changed files to working tree");
let mut index = repo.index()?;
index.add_path(PathBuf::from(".").as_path())?;
index.add_all(&["."], git2::IndexAddOption::DEFAULT, None)?;
index.write()?;
let tree = index.write_tree()?;
let tree = repo.find_tree(tree)?;
@@ -101,6 +102,7 @@ impl GitProvider for GitHubGitProvider {
.map(|t| repo.find_commit(t))
})???;
tracing::trace!("writing commit object");
repo.commit(
None,
&signature,
@@ -110,6 +112,24 @@ impl GitProvider for GitHubGitProvider {
&[&parents],
)?;
let mut remote = repo.find_remote("origin")?;
let head = repo.head()?;
let refspec = &[head
.name()
.ok_or(eyre::anyhow!("could not find head.name"))?];
let mut remote_callbacks = RemoteCallbacks::new();
remote_callbacks.credentials(|url, username_from_url, _allowed_types| {
tracing::debug!(username_from_url, url, "pulling key from ssh-agent");
Cred::ssh_key_from_agent(username_from_url.unwrap())
});
let mut push_options = PushOptions::new();
push_options.remote_callbacks(remote_callbacks);
tracing::trace!("pushing to remote");
remote.push(refspec, Some(&mut push_options))?;
Ok(())
}
}

View File

@@ -0,0 +1,7 @@
pub struct DefaultGiteaClient {}
impl DefaultGiteaClient {
pub fn new() -> Self {
Self {}
}
}

View File

@@ -0,0 +1,17 @@
pub mod client;
pub mod provider;
use std::sync::Arc;
use async_trait::async_trait;
pub trait GiteaClient {}
pub type DynGiteaClient = Arc<dyn GiteaClient + Send + Sync>;
#[async_trait]
pub trait GiteaProvider {
async fn clone_from_qualified(repo: String) -> eyre::Result<String>;
}
pub type DynGiteaProvider = Arc<dyn GiteaProvider + Send + Sync>;

View File

@@ -0,0 +1,32 @@
use async_trait::async_trait;
use crate::{git::DynGitProvider, storage::DynStorageEngine};
use super::{DynGiteaClient, GiteaProvider};
pub struct DefaultGiteaProvider {
git_provider: DynGitProvider,
storage_engine: DynStorageEngine,
gitea_client: DynGiteaClient,
}
impl DefaultGiteaProvider {
pub fn new(
git_provider: DynGitProvider,
storage_engine: DynStorageEngine,
gitea_client: DynGiteaClient,
) -> Self {
Self {
git_provider,
storage_engine,
gitea_client,
}
}
}
#[async_trait]
impl GiteaProvider for DefaultGiteaProvider {
async fn clone_from_qualified(repo: String) -> eyre::Result<String> {
todo!()
}
}

View File

@@ -6,7 +6,8 @@ use tokio::sync::Mutex;
use crate::schema::models::GitPushBranch;
pub mod github;
pub mod git;
pub mod gitea;
#[async_trait]
pub trait GitProvider {

View File

@@ -7,6 +7,11 @@ pub struct GitPushBranch {
pub name: String,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct GitPushPullRequest {
pub name: String,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct GitPush {
pub branch: GitPushBranch,
@@ -18,9 +23,33 @@ pub struct Git {
pub repositories: Vec<Repository>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct GitHubPush {
pub branch: GitPushPullRequest,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct GiteaPush {
pub branch: GitPushPullRequest,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct GitHub {
pub push: Option<GitHubPush>,
pub repositories: Vec<Repository>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Gitea {
pub push: Option<GiteaPush>,
pub repositories: Vec<Repository>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct SelectAction {
pub git: Option<Git>,
pub github: Option<GitHub>,
pub gitea: Option<Gitea>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]