Compare commits
2 commits
3ce69efeba
...
baf959b7a4
Author | SHA1 | Date | |
---|---|---|---|
baf959b7a4 | |||
f92398a2f0 |
7 changed files with 208 additions and 5 deletions
|
@ -1,5 +1,9 @@
|
||||||
pub mod branch;
|
pub mod branch;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
|
use git_next_config as config;
|
||||||
use git_next_git as git;
|
use git_next_git as git;
|
||||||
|
|
||||||
use kxio::network::{self, Network};
|
use kxio::network::{self, Network};
|
||||||
|
@ -21,6 +25,10 @@ impl git::ForgeLike for ForgeJo {
|
||||||
"forgejo".to_string()
|
"forgejo".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn branches_get_all(&self) -> Result<Vec<config::BranchName>, git::branch::Error> {
|
||||||
|
branch::get_all(&self.repo_details, &self.net).await
|
||||||
|
}
|
||||||
|
|
||||||
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status {
|
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status {
|
||||||
let repo_details = &self.repo_details;
|
let repo_details = &self.repo_details;
|
||||||
let hostname = &repo_details.forge.hostname();
|
let hostname = &repo_details.forge.hostname();
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"commit": {
|
||||||
|
"added": [
|
||||||
|
"string"
|
||||||
|
],
|
||||||
|
"author": {
|
||||||
|
"email": "user@example.com",
|
||||||
|
"name": "string",
|
||||||
|
"username": "string"
|
||||||
|
},
|
||||||
|
"committer": {
|
||||||
|
"email": "user@example.com",
|
||||||
|
"name": "string",
|
||||||
|
"username": "string"
|
||||||
|
},
|
||||||
|
"id": "string",
|
||||||
|
"message": "string",
|
||||||
|
"modified": [
|
||||||
|
"string"
|
||||||
|
],
|
||||||
|
"removed": [
|
||||||
|
"string"
|
||||||
|
],
|
||||||
|
"timestamp": "2024-04-16T21:35:56.331Z",
|
||||||
|
"url": "string",
|
||||||
|
"verification": {
|
||||||
|
"payload": "string",
|
||||||
|
"reason": "string",
|
||||||
|
"signature": "string",
|
||||||
|
"signer": {
|
||||||
|
"email": "user@example.com",
|
||||||
|
"name": "string",
|
||||||
|
"username": "string"
|
||||||
|
},
|
||||||
|
"verified": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"effective_branch_protection_name": "string",
|
||||||
|
"enable_status_check": true,
|
||||||
|
"name": "string",
|
||||||
|
"protected": true,
|
||||||
|
"required_approvals": 0,
|
||||||
|
"status_check_contexts": [
|
||||||
|
"string"
|
||||||
|
],
|
||||||
|
"user_can_merge": true,
|
||||||
|
"user_can_push": true
|
||||||
|
}
|
||||||
|
]
|
102
crates/forge-forgejo/src/tests/mod.rs
Normal file
102
crates/forge-forgejo/src/tests/mod.rs
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
//
|
||||||
|
use assert2::let_assert;
|
||||||
|
use kxio::network::{MockNetwork, Network, StatusCode};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use crate as forgejo;
|
||||||
|
use git::ForgeLike as _;
|
||||||
|
use git_next_config as config;
|
||||||
|
use git_next_git as git;
|
||||||
|
|
||||||
|
mod branch {
|
||||||
|
use super::*;
|
||||||
|
mod get_all {
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_branches_get() {
|
||||||
|
let_assert!(Ok(fs) = kxio::fs::temp());
|
||||||
|
let mut net = MockNetwork::new();
|
||||||
|
//given
|
||||||
|
given_forgejo_has_branches(&mut net, 1);
|
||||||
|
let repo_details = given_repo_details(fs.base(), 1);
|
||||||
|
|
||||||
|
let net = Network::from(net);
|
||||||
|
let forge = forgejo::ForgeJo::new(repo_details, net.clone());
|
||||||
|
|
||||||
|
//when
|
||||||
|
let_assert!(Ok(branches) = forge.branches_get_all().await);
|
||||||
|
|
||||||
|
//then
|
||||||
|
let_assert!(Some(requests) = net.mocked_requests());
|
||||||
|
assert_eq!(requests.len(), 1);
|
||||||
|
assert_eq!(branches, vec![config::BranchName::new("string")]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// mod validate_positions {
|
||||||
|
//
|
||||||
|
// use git::ForgeLike;
|
||||||
|
//
|
||||||
|
// use super::*;
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn should_ok_all_branches_on_same_commit() {
|
||||||
|
// let_assert!(Ok(fs) = kxio::fs::temp());
|
||||||
|
// let mut net = MockNetwork::new();
|
||||||
|
// //given
|
||||||
|
// let repo_details = given_repo_details(fs.base(), 1);
|
||||||
|
// let net = Network::from(net);
|
||||||
|
// let (repo, _reality) = git::repository::mock();
|
||||||
|
// let forge = given_a_forge(repo_details, &net, repo);
|
||||||
|
// let open_repository = given_an_open_repository();
|
||||||
|
// let repo_config = given_a_repo_config();
|
||||||
|
//
|
||||||
|
// let forge = forgejo::ForgeJo::new(repo_details, net.clone(), repo);
|
||||||
|
//
|
||||||
|
// let_assert!(
|
||||||
|
// Ok(positions) = forge
|
||||||
|
// .branches_validate_positions(open_repository, repo_config)
|
||||||
|
// .await
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fn given_a_forge(
|
||||||
|
// repo_details: git::RepoDetails,
|
||||||
|
// net: &Network,
|
||||||
|
// repo: git::Repository,
|
||||||
|
// ) -> forgejo::ForgeJo {
|
||||||
|
// forgejo::ForgeJo::new(repo_details, net.clone(), repo)
|
||||||
|
// }
|
||||||
|
// fn given_an_open_repository() -> git::OpenRepository {
|
||||||
|
// todo!()
|
||||||
|
// }
|
||||||
|
// fn given_a_repo_config() -> config::RepoConfig {
|
||||||
|
// todo!()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn given_forgejo_has_branches(net: &mut MockNetwork, i: u32) {
|
||||||
|
let hostname = config::common::hostname(i);
|
||||||
|
let path = config::common::repo_path(i);
|
||||||
|
let api_token = config::common::api_token(i);
|
||||||
|
use secrecy::ExposeSecret;
|
||||||
|
let token = api_token.expose_secret();
|
||||||
|
let url = format!("https://{hostname}/api/v1/repos/{path}/branches?token={token}");
|
||||||
|
let body = include_str!("./data-forgejo-branches-get.json");
|
||||||
|
net.add_get_response(&url, StatusCode::OK, body);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn given_repo_details(path: &Path, i: u32) -> git::RepoDetails {
|
||||||
|
git::common::repo_details(
|
||||||
|
i,
|
||||||
|
git::Generation::new(),
|
||||||
|
config::common::forge_details(i, config::ForgeType::ForgeJo),
|
||||||
|
Some(config::common::repo_config(
|
||||||
|
i,
|
||||||
|
config::RepoConfigSource::Repo,
|
||||||
|
)),
|
||||||
|
config::GitDir::new(path),
|
||||||
|
)
|
||||||
|
}
|
|
@ -14,6 +14,10 @@ impl git::ForgeLike for MockForge {
|
||||||
"mock".to_string()
|
"mock".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn branches_get_all(&self) -> Result<Vec<config::BranchName>, git::branch::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
async fn commit_status(&self, _commit: &git::Commit) -> git::commit::Status {
|
async fn commit_status(&self, _commit: &git::Commit) -> git::commit::Status {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
use crate as git;
|
use crate as git;
|
||||||
|
use git_next_config as config;
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait ForgeLike {
|
pub trait ForgeLike {
|
||||||
fn name(&self) -> String;
|
fn name(&self) -> String;
|
||||||
|
|
||||||
|
/// Returns a list of all branches in the repo.
|
||||||
|
async fn branches_get_all(&self) -> Result<Vec<config::BranchName>, git::branch::Error>;
|
||||||
|
|
||||||
/// Checks the results of any (e.g. CI) status checks for the commit.
|
/// Checks the results of any (e.g. CI) status checks for the commit.
|
||||||
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status;
|
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status;
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,11 +121,12 @@ impl Handler<LoadConfigFromRepo> for RepoActor {
|
||||||
fn handle(&mut self, _msg: LoadConfigFromRepo, ctx: &mut Self::Context) -> Self::Result {
|
fn handle(&mut self, _msg: LoadConfigFromRepo, ctx: &mut Self::Context) -> Self::Result {
|
||||||
let details = self.repo_details.clone();
|
let details = self.repo_details.clone();
|
||||||
let addr = ctx.address();
|
let addr = ctx.address();
|
||||||
|
let forge = self.forge.clone();
|
||||||
let Some(open_repository) = self.open_repository.clone() else {
|
let Some(open_repository) = self.open_repository.clone() else {
|
||||||
warn!("missing open repository - can't load configuration");
|
warn!("missing open repository - can't load configuration");
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
repo_actor::load::load_file(details, addr, open_repository)
|
repo_actor::load::load_file(details, addr, forge, open_repository)
|
||||||
.in_current_span()
|
.in_current_span()
|
||||||
.into_actor(self)
|
.into_actor(self)
|
||||||
.wait(ctx);
|
.wait(ctx);
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
//
|
//
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
|
|
||||||
use tracing::{error, info};
|
use tracing::{error, info, warn};
|
||||||
|
|
||||||
use git_next_config as config;
|
use git_next_config as config;
|
||||||
|
use git_next_forge as forge;
|
||||||
use git_next_git as git;
|
use git_next_git as git;
|
||||||
|
|
||||||
use super::{LoadedConfig, RepoActor};
|
use super::{LoadedConfig, RepoActor};
|
||||||
|
@ -13,10 +14,11 @@ use super::{LoadedConfig, RepoActor};
|
||||||
pub async fn load_file(
|
pub async fn load_file(
|
||||||
repo_details: git::RepoDetails,
|
repo_details: git::RepoDetails,
|
||||||
addr: Addr<RepoActor>,
|
addr: Addr<RepoActor>,
|
||||||
|
forge: forge::Forge,
|
||||||
open_repository: git::OpenRepository,
|
open_repository: git::OpenRepository,
|
||||||
) {
|
) {
|
||||||
info!("Loading .git-next.toml from repo");
|
info!("Loading .git-next.toml from repo");
|
||||||
let repo_config = match load(&repo_details, &open_repository).await {
|
let repo_config = match load(&repo_details, &forge, &open_repository).await {
|
||||||
Ok(repo_config) => repo_config,
|
Ok(repo_config) => repo_config,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!(?err, "Failed to load config");
|
error!(?err, "Failed to load config");
|
||||||
|
@ -29,11 +31,12 @@ pub async fn load_file(
|
||||||
|
|
||||||
async fn load(
|
async fn load(
|
||||||
details: &git::RepoDetails,
|
details: &git::RepoDetails,
|
||||||
|
forge: &forge::Forge,
|
||||||
open_repository: &git::OpenRepository,
|
open_repository: &git::OpenRepository,
|
||||||
) -> Result<config::RepoConfig, Error> {
|
) -> Result<config::RepoConfig, Error> {
|
||||||
let contents = open_repository.read_file(&details.branch, ".git-next.toml")?;
|
let contents = open_repository.read_file(&details.branch, ".git-next.toml")?;
|
||||||
let config = config::RepoConfig::load(&contents)?;
|
let config = config::RepoConfig::load(&contents)?;
|
||||||
let config = validate(config, open_repository).await?;
|
let config = validate(config, forge, open_repository).await?;
|
||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,9 +51,40 @@ pub enum Error {
|
||||||
|
|
||||||
pub async fn validate(
|
pub async fn validate(
|
||||||
config: config::RepoConfig,
|
config: config::RepoConfig,
|
||||||
|
forge: &forge::Forge,
|
||||||
open_repository: &git::OpenRepository,
|
open_repository: &git::OpenRepository,
|
||||||
) -> Result<config::RepoConfig, Error> {
|
) -> Result<config::RepoConfig, Error> {
|
||||||
let branches = open_repository.remote_branches()?;
|
let new = open_repository.remote_branches();
|
||||||
|
let branches = forge.branches_get_all().await.map_err(|e| {
|
||||||
|
error!(?e, "Failed to list branches");
|
||||||
|
Error::Branch(e)
|
||||||
|
});
|
||||||
|
let branches = match (branches, new) {
|
||||||
|
(Ok(old), Ok(new)) => {
|
||||||
|
if old == new {
|
||||||
|
info!("remote_branches old and new match");
|
||||||
|
} else {
|
||||||
|
warn!("remote_branches old and new differ:");
|
||||||
|
warn!("old: {old:?}");
|
||||||
|
warn!("new: {new:?}");
|
||||||
|
}
|
||||||
|
Ok(old)
|
||||||
|
}
|
||||||
|
(Ok(old), Err(new)) => {
|
||||||
|
warn!("remote_branches old okay, new error: {new:?}");
|
||||||
|
Ok(old)
|
||||||
|
}
|
||||||
|
(Err(old), Ok(_new)) => {
|
||||||
|
warn!("remote_branches new okay, old error: {old:?}");
|
||||||
|
Err(old)
|
||||||
|
}
|
||||||
|
(Err(old), Err(new)) => {
|
||||||
|
warn!("remote_branches both error:");
|
||||||
|
warn!("old: {old:?}");
|
||||||
|
warn!("new: {new:?}");
|
||||||
|
Err(old)
|
||||||
|
}
|
||||||
|
}?;
|
||||||
if !branches
|
if !branches
|
||||||
.iter()
|
.iter()
|
||||||
.any(|branch| branch == &config.branches().main())
|
.any(|branch| branch == &config.branches().main())
|
||||||
|
|
Loading…
Reference in a new issue