Compare commits

...

2 commits

Author SHA1 Message Date
0ad5b604c3 WIP: add github crate
All checks were successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
2024-05-26 18:22:17 +01:00
f259179274 refactor: move git::read_file implementation to git crate
All checks were successful
Rust / build (push) Successful in 2m19s
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
ci/woodpecker/cron/cron-docker-builder Pipeline was successful
ci/woodpecker/cron/push-next Pipeline was successful
ci/woodpecker/cron/tag-created Pipeline was successful
2024-05-26 18:22:15 +01:00
23 changed files with 260 additions and 185 deletions

View file

@ -7,6 +7,7 @@ members = [
"crates/git",
"crates/forge",
"crates/forge-forgejo",
"crates/forge-github",
"crates/repo-actor",
]
@ -26,6 +27,7 @@ git-next-config = { path = "crates/config" }
git-next-git = { path = "crates/git" }
git-next-forge = { path = "crates/forge" }
git-next-forge-forgejo = { path = "crates/forge-forgejo" }
git-next-forge-github = { path = "crates/forge-github" }
git-next-repo-actor = { path = "crates/repo-actor" }
# CLI parsing
@ -42,6 +44,7 @@ base64 = "0.22"
# git
# gix = "0.62"
gix = { version = "0.63", features = [
"dirwalk",
"blocking-http-transport-reqwest-rust-tls",
] }
async-trait = "0.1"

View file

@ -4,7 +4,7 @@ version = { workspace = true }
edition = { workspace = true }
[features]
default = ["forgejo"]
default = ["forgejo", "github"]
forgejo = []
github = []

View file

@ -1,87 +0,0 @@
use git_next_config as config;
use git_next_git as git;
use kxio::network::{self, Network};
use tracing::{error, warn};
pub async fn contents_get(
repo_details: &git::RepoDetails,
net: &Network,
branch: &config::BranchName,
file_path: &str,
) -> Result<String, git::file::Error> {
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}/contents/{file_path}?ref={branch}&token={token}"
));
// info!("Loading config");
let request = network::NetRequest::new(
network::RequestMethod::Get,
url,
network::NetRequestHeaders::new(),
network::RequestBody::None,
network::ResponseType::Json,
None,
network::NetRequestLogging::None,
);
let result = net.get::<ForgeContentsResponse>(request).await;
let response = result.map_err(|e| {
warn!(?e, "");
git::file::Error::NotFound(file_path.to_string())
})?;
let status = response.status_code();
let contents = match response.response_body() {
Some(body) => {
// we need to decode (see encoding field) the value of 'content' from the response
match body.content_type {
ForgeContentsType::File => decode_config(body),
_ => Err(git::file::Error::NotFile(file_path.to_string())),
}
}
None => {
error!(%status, "Failed to fetch repo config file");
Err(git::file::Error::Unknown(status.to_string()))
}
}?;
Ok(contents)
}
fn decode_config(body: ForgeContentsResponse) -> Result<String, git::file::Error> {
use base64::Engine;
match body.encoding.as_str() {
"base64" => {
let decoded = base64::engine::general_purpose::STANDARD
.decode(body.content)
.map_err(|_| git::file::Error::DecodeFromBase64)?;
let decoded =
String::from_utf8(decoded).map_err(|_| git::file::Error::DecodeFromUtf8)?;
Ok(decoded)
}
encoding => Err(git::file::Error::UnknownEncoding(encoding.to_string())),
}
}
#[derive(Clone, Debug, serde::Deserialize)]
struct ForgeContentsResponse {
#[serde(rename = "type")]
pub content_type: ForgeContentsType,
pub content: String,
pub encoding: String,
}
#[derive(Clone, Debug, serde::Deserialize)]
enum ForgeContentsType {
#[serde(rename = "file")]
File,
#[serde(rename = "dir")]
Dir,
#[serde(rename = "symlink")]
Symlink,
#[serde(rename = "submodule")]
Submodule,
}

View file

@ -1,5 +1,4 @@
pub mod branch;
mod file;
#[cfg(test)]
mod tests;
@ -30,14 +29,6 @@ impl git::ForgeLike for ForgeJo {
branch::get_all(&self.repo_details, &self.net).await
}
async fn file_contents_get(
&self,
branch: &config::BranchName,
file_path: &str,
) -> Result<String, git::file::Error> {
file::contents_get(&self.repo_details, &self.net, branch, file_path).await
}
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status {
let repo_details = &self.repo_details;
let hostname = &repo_details.forge.hostname();

View file

@ -0,0 +1,59 @@
[package]
name = "git-next-forge-github"
version = { workspace = true }
edition = { workspace = true }
[dependencies]
git-next-config = { workspace = true }
git-next-git = { workspace = true }
# logging
console-subscriber = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
# base64 decoding
base64 = { workspace = true }
# git
async-trait = { workspace = true }
# fs/network
kxio = { workspace = true }
# TOML parsing
serde = { workspace = true }
serde_json = { workspace = true }
toml = { workspace = true }
# Secrets and Password
secrecy = { workspace = true }
# Conventional Commit check
git-conventional = { workspace = true }
# Webhooks
bytes = { workspace = true }
ulid = { workspace = true }
warp = { workspace = true }
# boilerplate
derive_more = { workspace = true }
# file watcher
inotify = { workspace = true }
# # Actors
# actix = { workspace = true }
# actix-rt = { workspace = true }
tokio = { workspace = true }
[dev-dependencies]
# Testing
assert2 = { workspace = true }
[lints.clippy]
nursery = { level = "warn", priority = -1 }
# pedantic = "warn"
unwrap_used = "warn"
expect_used = "warn"

View file

@ -0,0 +1,39 @@
//
#![allow(dead_code)]
use derive_more::Constructor;
use git_next_config as config;
use git_next_git as git;
use kxio::network::Network;
#[cfg(test)]
mod tests;
#[derive(Clone, Debug, Constructor)]
pub struct Github {
net: Network,
}
#[async_trait::async_trait]
impl git_next_git::ForgeLike for Github {
fn name(&self) -> String {
"github".to_string()
}
async fn branches_get_all(&self) -> Result<Vec<config::BranchName>, git::branch::Error> {
todo!();
}
/// Returns the contents of the file.
async fn file_contents_get(
&self,
_branch_name: &config::BranchName,
_file_path: &str,
) -> Result<String, git::file::Error> {
todo!();
}
/// Checks the results of any (e.g. CI) status checks for the commit.
async fn commit_status(&self, _commit: &git::Commit) -> git::commit::Status {
todo!();
}
}

View file

@ -0,0 +1,8 @@
use git_next_git::ForgeLike as _;
#[test]
fn test_name() {
let net = kxio::network::Network::new_mock();
let forge = crate::Github::new(net);
assert_eq!(forge.name(), "github");
}

View file

@ -4,14 +4,15 @@ version = { workspace = true }
edition = { workspace = true }
[features]
default = ["forgejo"]
default = ["forgejo", "github"]
forgejo = ["git-next-forge-forgejo"]
github = []
github = ["git-next-forge-github"]
[dependencies]
git-next-config = { workspace = true }
git-next-git = { workspace = true }
git-next-forge-forgejo = { workspace = true, optional = true }
git-next-forge-github = { workspace = true, optional = true }
# logging
console-subscriber = { workspace = true }

View file

@ -1,21 +0,0 @@
use crate::network::Network;
struct Github;
pub(super) struct GithubEnv {
net: Network,
}
impl GithubEnv {
pub(crate) const fn new(net: Network) -> GithubEnv {
Self { net }
}
}
#[async_trait::async_trait]
impl super::ForgeLike for GithubEnv {
fn name(&self) -> String {
"github".to_string()
}
async fn branches_get_all(&self) -> Vec<super::Branch> {
todo!()
}
}

View file

@ -1,34 +1,34 @@
#![allow(dead_code)]
//
use git_next_forge_forgejo as forgejo;
use git_next_forge_github as github;
use git_next_git as git;
use kxio::network::Network;
#[cfg(feature = "github")]
mod github;
mod mock_forge;
#[derive(Clone, Debug)]
pub enum Forge {
Mock(mock_forge::MockForgeEnv),
#[allow(clippy::enum_variant_names)]
Mock(mock_forge::MockForge),
#[cfg(feature = "forgejo")]
ForgeJo(forgejo::ForgeJo),
ForgeJo(git_next_forge_forgejo::ForgeJo),
#[cfg(feature = "github")]
Github(github::GithubEnv),
Github(git_next_forge_github::Github),
}
impl Forge {
pub const fn new_mock() -> Self {
Self::Mock(mock_forge::MockForgeEnv::new())
Self::Mock(mock_forge::MockForge::new())
}
#[cfg(feature = "forgejo")]
pub const fn new_forgejo(repo_details: git::RepoDetails, net: Network) -> Self {
Self::ForgeJo(forgejo::ForgeJo::new(repo_details, net))
}
#[cfg(feature = "github")]
pub const fn new_github(net: Network) -> Self {
Self::Github(github::GithubEnv::new(net))
Self::Github(github::Github::new(net))
}
}
impl std::ops::Deref for Forge {
@ -39,7 +39,7 @@ impl std::ops::Deref for Forge {
#[cfg(feature = "forgejo")]
Self::ForgeJo(env) => env,
#[cfg(feature = "github")]
Forge::Github(env) => env,
Self::Github(env) => env,
}
}
}

View file

@ -1,19 +1,15 @@
//
#![cfg(not(tarpaulin_include))]
use derive_more::Constructor;
use git_next_config as config;
use git_next_git as git;
struct MockForge;
#[derive(Clone, Debug)]
pub struct MockForgeEnv;
impl MockForgeEnv {
pub(crate) const fn new() -> Self {
Self
}
}
#[derive(Clone, Debug, Constructor)]
pub struct MockForge;
#[async_trait::async_trait]
impl git::ForgeLike for MockForgeEnv {
impl git::ForgeLike for MockForge {
fn name(&self) -> String {
"mock".to_string()
}
@ -22,14 +18,6 @@ impl git::ForgeLike for MockForgeEnv {
todo!()
}
async fn file_contents_get(
&self,
_branch: &config::BranchName,
_file_path: &str,
) -> Result<String, git::file::Error> {
todo!()
}
async fn commit_status(&self, _commit: &git::Commit) -> git::commit::Status {
todo!()
}

View file

@ -1,8 +0,0 @@
use super::*;
#[test]
fn test_name() {
let net = Network::new_mock();
let forge = Forge::new_github(net);
assert_eq!(forge.name(), "github");
}

View file

@ -4,9 +4,6 @@ use super::*;
use git_next_config as config;
use git_next_git as git;
#[cfg(feature = "github")]
mod github;
#[test]
fn test_mock_name() {
let forge = Forge::new_mock();

View file

@ -1,18 +1,74 @@
//
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, derive_more::Display)]
pub enum Error {
Lock,
#[display("File not found: {}", 0)]
NotFound(String),
#[display("Unable to parse file contents")]
ParseContent,
#[display("Unable to decode from base64")]
DecodeFromBase64,
#[display("Unable to decoce from UTF-8")]
#[display("Unable to decode from UTF-8")]
DecodeFromUtf8,
#[display("Unknown file encoding: {}", 0)]
UnknownEncoding(String),
#[display("Not a file: {}", 0)]
NotFile(String),
#[display("Unknown error (status: {})", 0)]
Unknown(String),
CommitLog(crate::commit::log::Error),
CommitNotFound,
NoTreeInCommit(String),
NoGitNextToml,
FindReference(String),
FindObject(String),
NonUtf8Blob(String),
TryId,
}
impl std::error::Error for Error {}
impl From<crate::commit::log::Error> for Error {
fn from(value: crate::commit::log::Error) -> Self {
Self::CommitLog(value)
}
}
impl From<gix::reference::find::existing::Error> for Error {
fn from(value: gix::reference::find::existing::Error) -> Self {
Self::FindReference(value.to_string())
}
}
impl From<gix::object::commit::Error> for Error {
fn from(value: gix::object::commit::Error) -> Self {
Self::NoTreeInCommit(value.to_string())
}
}
impl From<gix::object::find::existing::Error> for Error {
fn from(value: gix::object::find::existing::Error) -> Self {
Self::FindObject(value.to_string())
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(value: std::string::FromUtf8Error) -> Self {
Self::NonUtf8Blob(value.to_string())
}
}

View file

@ -8,13 +8,6 @@ pub trait ForgeLike {
/// Returns a list of all branches in the repo.
async fn branches_get_all(&self) -> Result<Vec<config::BranchName>, git::branch::Error>;
/// Returns the contents of the file.
async fn file_contents_get(
&self,
branch: &config::BranchName,
file_path: &str,
) -> Result<String, git::file::Error>;
/// Checks the results of any (e.g. CI) status checks for the commit.
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status;
}

View file

@ -22,4 +22,4 @@ pub enum Error {
}
impl std::error::Error for Error {}
pub type Result = core::result::Result<(), Error>;
pub type Result<T> = core::result::Result<T, Error>;

View file

@ -143,6 +143,17 @@ impl OpenRepositoryLike for MockOpenRepository {
.map(|inner| inner.commit_log(branch_name, find_commits))
.unwrap()
}
fn read_file(
&self,
branch_name: &git_next_config::BranchName,
file_name: &str,
) -> git::file::Result<String> {
self.inner
.lock()
.map(|inner| inner.read_file(branch_name, file_name))
.unwrap()
}
}
impl OpenRepositoryLike for InnerMockOpenRepository {
fn find_default_remote(&self, direction: Direction) -> Option<GitRemote> {
@ -173,4 +184,12 @@ impl OpenRepositoryLike for InnerMockOpenRepository {
) -> core::result::Result<Vec<crate::Commit>, crate::commit::log::Error> {
todo!()
}
fn read_file(
&self,
_branch_name: &git_next_config::BranchName,
_file_name: &str,
) -> git::file::Result<String> {
todo!()
}
}

View file

@ -36,14 +36,23 @@ pub trait OpenRepositoryLike {
branch_name: config::BranchName,
to_commit: git::GitRef,
force: git::push::Force,
) -> Result<(), git::push::Error>;
) -> git::push::Result<()>;
/// List of commits in a branch, optionally up-to any specified commit.
fn commit_log(
&self,
branch_name: &config::BranchName,
find_commits: &[git::Commit],
) -> Result<Vec<git::Commit>, git::commit::log::Error>;
) -> git::commit::log::Result<Vec<git::Commit>>;
/// Read the contents of a file as a string.
///
/// Only handles files in the root of the repo.
fn read_file(
&self,
branch_name: &config::BranchName,
file_name: &str,
) -> git::file::Result<String>;
}
impl std::ops::Deref for OpenRepository {
type Target = dyn OpenRepositoryLike;

View file

@ -1,9 +1,8 @@
//
use crate as git;
use git_next_config as config;
use gix::bstr::BStr;
use gix::bstr::BStr;
use std::sync::{Arc, Mutex};
use tracing::{info, warn};
@ -152,6 +151,32 @@ impl super::OpenRepositoryLike for RealOpenRepository {
Ok(commits)
})?
}
#[tracing::instrument(skip_all, fields(%branch_name, %file_name))]
fn read_file(
&self,
branch_name: &config::BranchName,
file_name: &str,
) -> git::file::Result<String> {
self.0
.lock()
.map_err(|_| git::file::Error::Lock)
.and_then(|repo| {
let fref = repo.find_reference(format!("origin/{}", branch_name).as_str())?;
let id = fref.try_id().ok_or(git::file::Error::TryId)?;
let oid = id.detach();
let obj = repo.find_object(oid)?;
let commit = obj.into_commit();
let tree = commit.tree()?;
let ent = tree
.find_entry(".git-next.toml")
.ok_or(git::file::Error::NoGitNextToml)?;
let fobj = ent.object()?;
let blob = fobj.into_blob().take_data();
let content = String::from_utf8(blob)?;
Ok(content)
})
}
}
impl From<&gix::Url> for git::GitRemote {

View file

@ -4,7 +4,7 @@ version = { workspace = true }
edition = { workspace = true }
[features]
default = ["forgejo"]
default = ["forgejo", "github"]
forgejo = []
github = []

View file

@ -122,7 +122,11 @@ impl Handler<LoadConfigFromRepo> for RepoActor {
let details = self.repo_details.clone();
let addr = ctx.address();
let forge = self.forge.clone();
repo_actor::load::load_file(details, addr, forge)
let Some(open_repository) = self.open_repository.clone() else {
warn!("missing open repository - can't load configuration");
return;
};
repo_actor::load::load_file(details, addr, forge, open_repository)
.in_current_span()
.into_actor(self)
.wait(ctx);

View file

@ -11,9 +11,14 @@ use super::{LoadedConfig, RepoActor};
/// Loads the [RepoConfig] from the `.git-next.toml` file in the repository
#[tracing::instrument(skip_all, fields(branch = %repo_details.branch))]
pub async fn load_file(repo_details: git::RepoDetails, addr: Addr<RepoActor>, forge: forge::Forge) {
pub async fn load_file(
repo_details: git::RepoDetails,
addr: Addr<RepoActor>,
forge: forge::Forge,
open_repository: git::OpenRepository,
) {
info!("Loading .git-next.toml from repo");
let repo_config = match load(&repo_details, &forge).await {
let repo_config = match load(&repo_details, &forge, open_repository).await {
Ok(repo_config) => repo_config,
Err(err) => {
error!(?err, "Failed to load config");
@ -27,10 +32,9 @@ pub async fn load_file(repo_details: git::RepoDetails, addr: Addr<RepoActor>, fo
async fn load(
details: &git::RepoDetails,
forge: &forge::Forge,
open_repository: git::OpenRepository,
) -> Result<config::RepoConfig, Error> {
let contents = forge
.file_contents_get(&details.branch, ".git-next.toml")
.await?;
let contents = open_repository.read_file(&details.branch, ".git-next.toml")?;
let config = config::RepoConfig::load(&contents)?;
let config = validate(config, forge).await?;
Ok(config)

View file

@ -3,11 +3,6 @@ name = "git-next-server"
version = { workspace = true }
edition = { workspace = true }
[features]
default = ["forgejo"]
forgejo = []
github = []
[dependencies]
git-next-config = { workspace = true }
git-next-git = { workspace = true }