periphery get repo status

This commit is contained in:
mbecker20
2024-05-11 18:42:21 -07:00
parent c0b010d5ce
commit 8b2c4d604a
3 changed files with 55 additions and 1 deletions

View File

@@ -1,9 +1,30 @@
use anyhow::anyhow;
use monitor_client::entities::{to_monitor_name, update::Log};
use periphery_client::api::git::{CloneRepo, DeleteRepo, PullRepo};
use periphery_client::api::git::{
CloneRepo, DeleteRepo, GetLatestCommit, GetLatestCommitResponse,
PullRepo,
};
use resolver_api::Resolve;
use crate::{config::periphery_config, helpers::git, State};
#[async_trait::async_trait]
impl Resolve<GetLatestCommit, ()> for State {
async fn resolve(
&self,
GetLatestCommit { name }: GetLatestCommit,
_: (),
) -> anyhow::Result<GetLatestCommitResponse> {
let repo_path = periphery_config().repo_dir.join(name);
if !repo_path.is_dir() {
return Err(anyhow!(
"repo path is not directory. is it cloned?"
));
}
git::get_commit_hash_info(&repo_path).await
}
}
#[async_trait::async_trait]
impl Resolve<CloneRepo> for State {
#[instrument(name = "CloneRepo", skip(self))]

View File

@@ -6,6 +6,7 @@ use monitor_client::entities::{
monitor_timestamp, to_monitor_name, update::Log, CloneArgs,
SystemCommand,
};
use periphery_client::api::git::GetLatestCommitResponse;
use run_command::async_run_command;
use crate::config::periphery_config;
@@ -181,6 +182,26 @@ async fn clone_inner(
}
}
pub async fn get_commit_hash_info(
repo_dir: &Path,
) -> anyhow::Result<GetLatestCommitResponse> {
let command = format!("cd {} && git rev-parse --short HEAD && git rev-parse HEAD && git log -1 --pretty=%B", repo_dir.display());
let output = async_run_command(&command).await;
let mut split = output.stdout.split('\n');
let (hash, _, message) = (
split
.next()
.context("failed to get short commit hash")?
.to_string(),
split.next().context("failed to get long commit hash")?,
split
.next()
.context("failed to get commit message")?
.to_string(),
);
Ok(GetLatestCommitResponse { hash, message })
}
#[instrument]
async fn get_commit_hash_log(repo_dir: &Path) -> anyhow::Result<Log> {
let start_ts = monitor_timestamp();

View File

@@ -4,6 +4,18 @@ use monitor_client::entities::{
use resolver_api::derive::Request;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, Request)]
#[response(GetLatestCommitResponse)]
pub struct GetLatestCommit {
pub name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetLatestCommitResponse {
pub hash: String,
pub message: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, Request)]
#[response(Vec<Log>)]
pub struct CloneRepo {