create branch

This commit is contained in:
2022-11-23 22:17:06 +01:00
parent 18ec10914b
commit 6e13263009
9 changed files with 126 additions and 37 deletions

View File

@@ -1,8 +1,9 @@
use std::path::PathBuf;
use std::{path::PathBuf, sync::Arc};
use git2::{Cred, RemoteCallbacks};
use git2::{Cred, RemoteCallbacks, Repository};
use tokio::sync::Mutex;
use crate::storage::DynStorageEngine;
use crate::{schema::models::GitPushBranch, storage::DynStorageEngine};
use super::GitProvider;
@@ -18,12 +19,12 @@ impl GitHubGitProvider {
#[async_trait::async_trait]
impl GitProvider for GitHubGitProvider {
async fn clone_from_url(&self, url: String) -> eyre::Result<PathBuf> {
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?;
let dirpath = dir.clone().path();
let _ = tokio::task::spawn_blocking(move || {
let repo = tokio::task::spawn_blocking(move || {
let mut callbacks = RemoteCallbacks::new();
callbacks.credentials(|url, username_from_url, _allowed_types| {
tracing::debug!(username_from_url, url, "pulling key from ssh-agent");
@@ -47,6 +48,43 @@ impl GitProvider for GitHubGitProvider {
tracing::debug!("done pulling repo");
Ok(dir.path())
Ok((dir.path(), repo))
}
async fn create_branch(
&self,
repo: Arc<Mutex<Repository>>,
branch: &GitPushBranch,
) -> eyre::Result<()> {
let repo = repo.lock().await;
let head_commit_oid = repo
.head()?
.target()
.ok_or(eyre::anyhow!("could not get access to target commit"))?;
let head_commit = repo.find_commit(head_commit_oid)?;
let newbranch = repo.branch(
&branch.name.to_lowercase().replace(" ", "-"),
&head_commit,
true,
)?;
repo.set_head(
newbranch
.into_reference()
.name()
.ok_or(eyre::anyhow!("could not get name of reference"))?,
)?;
Ok(())
}
async fn push_branch(
&self,
repo: Arc<Mutex<Repository>>,
branch: &GitPushBranch,
) -> eyre::Result<()> {
let repo = repo.lock().await;
Ok(())
}
}

View File

@@ -1,12 +1,26 @@
use std::{path::PathBuf, sync::Arc};
use async_trait::async_trait;
use git2::Repository;
use tokio::sync::Mutex;
use crate::schema::models::GitPushBranch;
pub mod github;
#[async_trait]
pub trait GitProvider {
async fn clone_from_url(&self, url: String) -> eyre::Result<PathBuf>;
async fn clone_from_url(&self, url: String) -> eyre::Result<(PathBuf, Repository)>;
async fn create_branch(
&self,
repo: Arc<Mutex<Repository>>,
branch: &GitPushBranch,
) -> eyre::Result<()>;
async fn push_branch(
&self,
repo: Arc<Mutex<Repository>>,
branch: &GitPushBranch,
) -> eyre::Result<()>;
}
pub type DynGitProvider = Arc<dyn GitProvider + Send + Sync>;

View File

@@ -3,10 +3,26 @@ use serde::{Deserialize, Serialize};
pub type Repository = String;
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct SelectAction {
pub struct GitPushBranch {
pub name: String,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct GitPush {
pub branch: GitPushBranch,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Git {
pub push: Option<GitPush>,
pub repositories: Vec<Repository>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct SelectAction {
pub git: Option<Git>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum Action {
@@ -21,6 +37,6 @@ pub enum Schema {
Action {
name: String,
select: SelectAction,
actions: Vec<Action>,
action: Action,
},
}

View File

@@ -60,7 +60,7 @@ actions:
select: SelectAction {
repositories: vec!["git@git.front.kjuulh.io:kjuulh/octopush-test.git".into()]
},
actions: vec![Action::Go {
action: vec![Action::Go {
entry: "main.go".into()
}]
}

View File

@@ -43,7 +43,7 @@ pub async fn execute_shell(cmd: String, path: Option<PathBuf>) -> eyre::Result<(
});
while let Some(line) = reader.next_line().await? {
tracing::trace!("{}", line)
tracing::trace!("something: {}", line)
}
Ok(())