forked from kemitix/git-next
163 lines
5.1 KiB
Rust
163 lines
5.1 KiB
Rust
pub mod branch;
|
|
mod file;
|
|
|
|
use std::time::Duration;
|
|
|
|
use actix::prelude::*;
|
|
|
|
use git::OpenRepository;
|
|
use git_next_config::{BranchName, GitDir, RepoConfig};
|
|
use git_next_git::{self as git, GitRef, RepoDetails, Repository};
|
|
use kxio::network::{self, Network};
|
|
use tracing::{error, info, warn};
|
|
|
|
use crate::{
|
|
actors::repo::{RepoActor, StartMonitoring, ValidateRepo},
|
|
gitforge,
|
|
};
|
|
|
|
struct ForgeJo;
|
|
#[derive(Clone, Debug)]
|
|
pub struct ForgeJoEnv {
|
|
repo_details: RepoDetails,
|
|
net: Network,
|
|
repo: Repository,
|
|
}
|
|
impl ForgeJoEnv {
|
|
pub(super) const fn new(repo_details: RepoDetails, net: Network, repo: Repository) -> Self {
|
|
Self {
|
|
repo_details,
|
|
net,
|
|
repo,
|
|
}
|
|
}
|
|
}
|
|
#[async_trait::async_trait]
|
|
impl super::ForgeLike for ForgeJoEnv {
|
|
fn name(&self) -> String {
|
|
"forgejo".to_string()
|
|
}
|
|
|
|
async fn branches_get_all(&self) -> Result<Vec<BranchName>, gitforge::ForgeBranchError> {
|
|
branch::get_all(&self.repo_details, &self.net).await
|
|
}
|
|
|
|
async fn file_contents_get(
|
|
&self,
|
|
branch: &BranchName,
|
|
file_path: &str,
|
|
) -> Result<String, gitforge::ForgeFileError> {
|
|
file::contents_get(&self.repo_details, &self.net, branch, file_path).await
|
|
}
|
|
|
|
async fn branches_validate_positions(
|
|
&self,
|
|
repository: git::OpenRepository,
|
|
repo_config: RepoConfig,
|
|
addr: Addr<RepoActor>,
|
|
message_token: gitforge::MessageToken,
|
|
) {
|
|
match branch::validate_positions(self, &repository, repo_config).await {
|
|
Ok(branch::ValidatedPositions {
|
|
main,
|
|
next,
|
|
dev,
|
|
dev_commit_history,
|
|
}) => {
|
|
addr.do_send(StartMonitoring::new(main, next, dev, dev_commit_history));
|
|
}
|
|
Err(err) => {
|
|
warn!("{:?}", err);
|
|
tokio::time::sleep(Duration::from_secs(10)).await;
|
|
addr.do_send(ValidateRepo::new(message_token));
|
|
}
|
|
}
|
|
}
|
|
|
|
fn branch_reset(
|
|
&self,
|
|
repository: &git::OpenRepository,
|
|
branch_name: BranchName,
|
|
to_commit: GitRef,
|
|
force: git::push::Force,
|
|
) -> git::push::Result {
|
|
repository.fetch()?;
|
|
repository.push(&self.repo_details, branch_name, to_commit, force)
|
|
}
|
|
|
|
async fn commit_status(&self, commit: &git::Commit) -> gitforge::CommitStatus {
|
|
let repo_details = &self.repo_details;
|
|
let hostname = &repo_details.forge.hostname();
|
|
let repo_path = &repo_details.repo_path;
|
|
let api_token = &repo_details.forge.token();
|
|
use secrecy::ExposeSecret;
|
|
let token = api_token.expose_secret();
|
|
let url = network::NetUrl::new(format!(
|
|
"https://{hostname}/api/v1/repos/{repo_path}/commits/{commit}/status?token={token}"
|
|
));
|
|
|
|
let request = network::NetRequest::new(
|
|
network::RequestMethod::Get,
|
|
url,
|
|
network::NetRequestHeaders::new(),
|
|
network::RequestBody::None,
|
|
network::ResponseType::Json,
|
|
None,
|
|
network::NetRequestLogging::None,
|
|
);
|
|
let result = self.net.get::<CombinedStatus>(request).await;
|
|
match result {
|
|
Ok(response) => {
|
|
match response.response_body() {
|
|
Some(status) => match status.state {
|
|
CommitStatusState::Success => gitforge::CommitStatus::Pass,
|
|
CommitStatusState::Pending => gitforge::CommitStatus::Pending,
|
|
CommitStatusState::Failure => gitforge::CommitStatus::Fail,
|
|
CommitStatusState::Error => gitforge::CommitStatus::Fail,
|
|
CommitStatusState::Blank => gitforge::CommitStatus::Pending,
|
|
},
|
|
None => {
|
|
warn!("No status found for commit");
|
|
gitforge::CommitStatus::Pending // assume issue is transient and allow retry
|
|
}
|
|
}
|
|
}
|
|
Err(e) => {
|
|
error!(?e, "Failed to get commit status");
|
|
gitforge::CommitStatus::Pending // assume issue is transient and allow retry
|
|
}
|
|
}
|
|
}
|
|
|
|
fn repo_clone(&self, gitdir: GitDir) -> Result<OpenRepository, git::repository::Error> {
|
|
let repository = if !gitdir.exists() {
|
|
info!("Local copy not found - cloning...");
|
|
self.repo.git_clone(&self.repo_details)?
|
|
} else {
|
|
self.repo.open(&gitdir)?
|
|
};
|
|
info!("Validating...");
|
|
git::validate(&repository, &self.repo_details)
|
|
.map_err(|e| git::repository::Error::Validation(e.to_string()))?;
|
|
Ok(repository)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
pub struct CombinedStatus {
|
|
pub state: CommitStatusState,
|
|
}
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
pub enum CommitStatusState {
|
|
#[serde(rename = "success")]
|
|
Success,
|
|
#[serde(rename = "pending")]
|
|
Pending,
|
|
#[serde(rename = "failure")]
|
|
Failure,
|
|
#[serde(rename = "error")]
|
|
Error,
|
|
#[serde(rename = "")]
|
|
Blank,
|
|
}
|