Find the workflow files.

This commit is contained in:
Tom Alexander
2024-09-28 18:34:16 -04:00
parent 66228f83f2
commit 5bd4862973
5 changed files with 107 additions and 19 deletions

View File

@@ -1,14 +1,17 @@
use std::path::Path;
use std::path::PathBuf;
use crate::gitea_client::GiteaClient;
use crate::gitea_client::Tree;
use crate::gitea_client::TreeFileReference;
use crate::in_repo_config::InRepoConfig;
use regex::Regex;
use tracing::debug;
pub(crate) async fn discover_webhook_bridge_config<O: AsRef<str>, R: AsRef<str>, C: AsRef<str>>(
pub(crate) async fn discover_webhook_bridge_config(
gitea: &GiteaClient,
owner: O,
repo: R,
commit: C,
repo_tree: &Tree,
) -> Result<InRepoConfig, Box<dyn std::error::Error>> {
let repo_tree = gitea.get_tree(owner, repo, commit).await?;
let in_repo_config_reference = repo_tree
.files
.iter()
@@ -22,3 +25,54 @@ pub(crate) async fn discover_webhook_bridge_config<O: AsRef<str>, R: AsRef<str>,
Ok(parsed_in_repo_config)
}
pub(crate) async fn discover_matching_push_triggers<RE: AsRef<str>>(
gitea: &GiteaClient,
repo_tree: &Tree,
git_ref: RE,
in_repo_config: &InRepoConfig,
) -> Result<(), Box<dyn std::error::Error>> {
let ref_to_branch_regex = Regex::new(r"refs/heads/(?P<branch>.+)")?;
let captures = ref_to_branch_regex
.captures(git_ref.as_ref())
.ok_or("Could not find branch name.")?;
let branch = &captures["branch"];
debug!("Detected branch from push as {:?}", branch);
let push_triggers = in_repo_config.get_push_triggers_for_branch(branch)?;
for trigger in push_triggers {
let path_to_source = normalize_path(Path::new(".webhook_bridge").join(&trigger.source));
debug!("Loading path {}", path_to_source.display());
let pipeline_template = repo_tree
.files
.iter()
.filter(|file_reference| Path::new(&file_reference.path) == path_to_source.as_path())
.next()
.ok_or("Trigger source not found in remote repo.")?;
let pipeline_contents = String::from_utf8(gitea.read_file(pipeline_template).await?)?;
debug!("Pipeline contents: {}", pipeline_contents);
}
Ok(())
}
fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
let mut ret = PathBuf::new();
for component in path.as_ref().components() {
match component {
// Prefix does not happen on unix-based systems.
std::path::Component::Prefix(_)
| std::path::Component::RootDir
| std::path::Component::Normal(_) => {
ret.push(component);
}
std::path::Component::CurDir => {}
std::path::Component::ParentDir => {
ret.pop();
}
}
}
ret
}