Skip to content

Commit

Permalink
Detect the private ticket footnote; #24
Browse files Browse the repository at this point in the history
  • Loading branch information
msuchane committed Oct 27, 2023
1 parent 2720cc2 commit fe56670
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 4 deletions.
70 changes: 70 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ counter = "^0.5"
regex = "1.10"
once_cell = "1.18"
include_dir = "0.7"
ignore = "0.4"

[build-dependencies]
bpaf = { version = "0.9", features = ["derive", "docgen"]}
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use std::sync::Arc;
use color_eyre::eyre::{eyre, Result, WrapErr};
use serde::Deserialize;

use crate::footnote;

/// The name of this program, as specified in Cargo.toml. Used later to access configuration files.
const PROGRAM_NAME: &str = env!("CARGO_PKG_NAME");

Expand Down Expand Up @@ -422,6 +424,7 @@ pub struct Project {
pub tickets: Vec<Arc<TicketQuery>>,
pub trackers: tracker::Config,
pub templates: Template,
pub private_footnote: bool,
}

impl Project {
Expand Down Expand Up @@ -452,12 +455,15 @@ impl Project {
let trackers = parse_trackers(&trackers_path)?;
let templates = parse_templates(&templates_path)?;

let private_footnote = footnote::is_footnote_defined(&abs_path)?;

Ok(Self {
base_dir: abs_path,
generated_dir,
tickets,
trackers,
templates,
private_footnote,
})
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ use regex::Regex;
use serde::Deserialize;

use crate::config::{tracker::Service, KeyOrSearch};

/// A shared error message that displays if the static regular expressions
/// are invalid, and the regex library can't parse them.
const REGEX_ERROR: &str = "Invalid built-in regular expression.";
use crate::REGEX_ERROR;

/// A regular expression that matches the Bugzilla ID format in CoRN 3.
static BZ_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^BZ#(\d+)$").expect(REGEX_ERROR));
Expand Down
59 changes: 59 additions & 0 deletions src/footnote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::fs;
use std::path::Path;

use color_eyre::{Result, eyre::WrapErr};
use ignore::Walk;
use once_cell::sync::Lazy;
use regex::Regex;

use crate::REGEX_ERROR;

/// This regex looks for a footnote definition with the `PrivateTicketFootnote` ID.
static FOOTNOTE_ATTR_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"footnoteref:\[PrivateTicketFootnote,.+\]").expect(REGEX_ERROR));


pub fn is_footnote_defined(project: &Path) -> Result<bool> {
for result in Walk::new(project) {
// Each item yielded by the iterator is either a directory entry or an error.
let dir_entry = result?;

let file_path = dir_entry.path();

if is_file_adoc(file_path) && file_contains_footnote(file_path)? {
log::info!("The private ticket footnote is defined.");
return Ok(true);
}
}

log::info!("The private ticket footnote is not defined.");
Ok(false)
}

fn is_file_adoc(path: &Path) -> bool {
let adoc_extensions = ["adoc", "asciidoc"];

let file_ext = path.extension().and_then(|ext| ext.to_str());

if let Some(ext) = file_ext {
if adoc_extensions.contains(&ext) {
return true;
}
}

false
}

fn file_contains_footnote(path: &Path) -> Result<bool> {
let text = fs::read_to_string(path)
.wrap_err("Cannot read AsciiDoc file in the project repository.")?;

let found_attr = text.lines().any(|line| {
// Detect and reject basic line comments.
!line.starts_with("//") &&
// If any line contains the footnote attribute definition, return `true`.
FOOTNOTE_ATTR_REGEX.is_match(line)
});

Ok(found_attr)
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod cli;
mod config;
mod convert;
mod extra_fields;
mod footnote;
mod init;
mod logging;
mod note;
Expand All @@ -55,6 +56,10 @@ use templating::{DocumentVariant, Module};
use crate::config::Project;
pub use crate::ticket_abstraction::AbstractTicket;

/// A shared error message that displays if the static regular expressions
/// are invalid, and the regex library can't parse them.
pub const REGEX_ERROR: &str = "Invalid built-in regular expression.";

/// Run the subcommand that the user picked on the command line.
pub fn run(cli: &Cli) -> Result<()> {
// Initialize the logging system based on the set verbosity
Expand Down

0 comments on commit fe56670

Please sign in to comment.