From eea2e14fb074e26b9cb9a11bfebd3226d6632691 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 17 Sep 2024 19:42:05 +0100 Subject: [PATCH] feat: collect env into Config and note planned operations --- Cargo.toml | 1 + src/main.rs | 66 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5faa4ba..844f56c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" [dependencies] anyhow = "1.0" regex = "1.10" +ureq = "2.10" diff --git a/src/main.rs b/src/main.rs index 6a3d21c..c5a2ca7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,28 +1,62 @@ +use std::path::PathBuf; + use anyhow::Context; +use regex::Regex; + +struct Config { + workdir: PathBuf, + repo: String, + server: String, + auth_token: Option, + prefix_pattern: Regex, + issue_pattern: Regex, +} fn main() -> anyhow::Result<()> { println!("Forgejo TODO Checker!"); - let ci_repo = std::env::var("GITHUB_REPOSITORY").context("GITHUB_REPOSITORY")?; - let ci_server_url = std::env::var("GITHUB_SERVER_URL").context("GITHUB_SERVER_URL")?; - let ci_repo_url = format!("{ci_server_url}/{ci_repo}"); + let config = Config { + workdir: std::env::var("GITHUB_WORKSPACE") + .context("GITHUB_WORKSPACE")? + .into(), + repo: std::env::var("GITHUB_REPOSITORY").context("GITHUB_REPOSITORY")?, + server: std::env::var("GITHUB_SERVER_URL").context("GITHUB_SERVER_URL")?, + auth_token: std::env::var("REPO_TOKEN").ok(), + prefix_pattern: regex::Regex::new(r"(#|//)\s*(TODO|FIXME)").context("prefix regex")?, + issue_pattern: regex::Regex::new(r"( |)(\(|\(#)(?P\d+)(\))") + .context("issue regex")?, + }; - // optional arg for private repos - let repo_token = std::env::var("REPO_TOKEN").ok(); + println!("Repo: {}", config.repo); + println!("Prefix: {}", config.prefix_pattern); + println!("Issues: {}", config.issue_pattern); - // Regex to match a todo or fixme comment - let prefix = regex::Regex::new(r"(#|//)\s*(TODO|FIXME)").context("prefix regex")?; - // Regex to match the rest of the line to find an issue number - let issue = - regex::Regex::new(r"( |)(\(|\(#)(?P\d+)(\))").context("issue regex")?; - - println!("[ci_repo:{ci_repo}][ci_repo_url:{ci_repo_url}][repo_token:{repo_token:?}][prefix:{prefix}][issue:{issue}]"); + // TODO: scan files in workdir + // TODO: ignore files listed in .rgignore .ignore or .gitignore + // TODO: scan for malformed TODO and FIXME comments (e.g. no issue number) - add to error list + // TODO: build list of expected open issues with file locations where found // list files in current directory to get a feel for what we have access to - std::fs::read_dir(".")? - .filter_map(Result::ok) - .map(|e| e.path()) - .for_each(|e| println!("{e:?}")); + // std::fs::read_dir(workdir)? + // .filter_map(Result::ok) + // .map(|e| e.path()) + // .for_each(|e| println!("{e:?}")); + + // Collect open issues: + // let limit = 10; + // let page = 0; + // let api = format!( + // "{ci_server_url}/api/v1/repos/{ci_repo}/issues?state=open&limit=${limit}&page=${page}" + // ); + // TODO: add authentication when provided + // let issues = ureq::get(&api).call()?.into_string()?; + // TODO: parse issues to get list of open issue numbers + + // TODO: loop over list of expected issues and drop any where they do exist and are open + // TODO: if remaining list is not empty - add all to error list + // + // TODO: if error list is empty - exit okay + // TODO: if error list is not empty - log erros and exit not okay Ok(()) }