2024-09-18 11:55:33 +01:00
|
|
|
//
|
|
|
|
use std::path::Path;
|
2024-09-17 19:42:05 +01:00
|
|
|
|
2024-09-18 07:44:34 +01:00
|
|
|
use anyhow::{Context, Result};
|
2024-09-18 11:55:33 +01:00
|
|
|
use model::{Config, FoundMarkers, Line, Marker, MarkerKind};
|
2024-09-18 15:28:17 +01:00
|
|
|
use patterns::{issue_pattern, marker_pattern};
|
2024-09-18 11:55:33 +01:00
|
|
|
|
|
|
|
mod model;
|
2024-09-18 15:28:17 +01:00
|
|
|
mod patterns;
|
2024-09-17 19:42:05 +01:00
|
|
|
|
2024-09-18 07:44:34 +01:00
|
|
|
fn main() -> Result<()> {
|
2024-09-17 16:48:09 +01:00
|
|
|
println!("Forgejo TODO Checker!");
|
|
|
|
|
2024-09-18 11:55:33 +01:00
|
|
|
let config = Config::builder()
|
|
|
|
.fs(kxio::fs::new(
|
2024-09-18 07:44:34 +01:00
|
|
|
std::env::var("GITHUB_WORKSPACE")
|
|
|
|
.context("GITHUB_WORKSPACE")?
|
|
|
|
.into(),
|
2024-09-18 11:55:33 +01:00
|
|
|
))
|
|
|
|
.repo(std::env::var("GITHUB_REPOSITORY").context("GITHUB_REPOSITORY")?)
|
|
|
|
.server(std::env::var("GITHUB_SERVER_URL").context("GITHUB_SERVER_URL")?)
|
2024-09-18 15:28:17 +01:00
|
|
|
.prefix_pattern(marker_pattern()?)
|
|
|
|
.issue_pattern(issue_pattern()?)
|
2024-09-18 11:55:33 +01:00
|
|
|
.maybe_auth_token(std::env::var("REPO_TOKEN").ok())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
println!("Repo: {}", config.repo());
|
|
|
|
println!("Prefix: {}", config.prefix_pattern());
|
|
|
|
println!("Issues: {}", config.issue_pattern());
|
2024-09-17 18:03:50 +01:00
|
|
|
|
2024-09-18 07:44:34 +01:00
|
|
|
let mut found_markers = FoundMarkers::default();
|
|
|
|
|
2024-09-18 11:55:33 +01:00
|
|
|
for file in ignore::Walk::new(config.fs().base()).flatten() {
|
2024-09-18 11:23:11 +01:00
|
|
|
let path = file.path();
|
2024-09-18 11:55:33 +01:00
|
|
|
if config.fs().path_is_file(path)? {
|
2024-09-18 11:23:11 +01:00
|
|
|
scan_file(path, &config, &mut found_markers)?;
|
|
|
|
}
|
|
|
|
}
|
2024-09-18 07:44:34 +01:00
|
|
|
|
|
|
|
println!("{found_markers:?}");
|
2024-09-17 18:03:50 +01:00
|
|
|
|
2024-09-17 19:42:05 +01:00
|
|
|
// 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
|
2024-09-17 16:48:09 +01:00
|
|
|
|
|
|
|
Ok(())
|
2024-09-17 16:26:32 +01:00
|
|
|
}
|
2024-09-18 07:44:34 +01:00
|
|
|
|
2024-09-18 11:23:11 +01:00
|
|
|
fn scan_file(file: &Path, config: &Config, found_markers: &mut FoundMarkers) -> Result<()> {
|
|
|
|
println!("file: {}", file.to_string_lossy());
|
2024-09-18 07:44:34 +01:00
|
|
|
config
|
2024-09-18 11:55:33 +01:00
|
|
|
.fs()
|
2024-09-18 11:23:11 +01:00
|
|
|
.file_read_to_string(file)?
|
2024-09-18 07:44:34 +01:00
|
|
|
.lines()
|
|
|
|
.enumerate()
|
2024-09-18 11:23:11 +01:00
|
|
|
.filter_map(|(n, line)| prefix_match(n, line, file, config))
|
2024-09-18 07:44:34 +01:00
|
|
|
.for_each(|marker| {
|
2024-09-18 11:55:33 +01:00
|
|
|
println!("- {}", marker.line().value());
|
2024-09-18 07:44:34 +01:00
|
|
|
if let Some(issue) = config
|
2024-09-18 11:55:33 +01:00
|
|
|
.issue_pattern()
|
|
|
|
.find(marker.line().value())
|
2024-09-18 07:44:34 +01:00
|
|
|
.map(|issue| issue.as_str())
|
|
|
|
.and_then(|issue| issue.parse::<usize>().ok())
|
|
|
|
{
|
|
|
|
found_markers.add_issue_marker(marker.into_issue_marker(issue));
|
|
|
|
} else {
|
|
|
|
found_markers.add_marker(marker);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prefix_match(num: usize, line: &str, path: &Path, config: &Config) -> Option<Marker> {
|
2024-09-18 11:55:33 +01:00
|
|
|
let find = config.prefix_pattern().find(line)?;
|
|
|
|
let kind = match find.as_str() {
|
|
|
|
"TODO" => Some(MarkerKind::Todo),
|
|
|
|
"FIXME" => Some(MarkerKind::Fixme),
|
2024-09-18 07:44:34 +01:00
|
|
|
_ => None,
|
|
|
|
}?;
|
2024-09-18 11:55:33 +01:00
|
|
|
Some(
|
|
|
|
Marker::builder()
|
|
|
|
.kind(kind)
|
|
|
|
.file(path.to_path_buf())
|
|
|
|
.line(Line::builder().num(num).value(line.to_owned()).build())
|
|
|
|
.build(),
|
|
|
|
)
|
2024-09-18 07:44:34 +01:00
|
|
|
}
|