use std::borrow::Cow;
use std::fmt::Write as _;
use crate::json_paths::uri_to_path;
use crate::markdown_prose::markdown_table_prose;
use crate::review_envelopes::{default_marker_regex, default_marker_regex_flags};
use crate::{
CodeClimateIssue, CodeClimateSeverity, DiffIndex, GitHubReviewComment, GitHubReviewSide,
GitLabReviewComment, GitLabReviewPosition, GitLabReviewPositionType, ReviewCheckConclusion,
ReviewComment, ReviewEnvelopeEvent, ReviewEnvelopeMeta, ReviewEnvelopeOutput,
ReviewEnvelopeSchema, ReviewEnvelopeSummary, ReviewId, ReviewProvider, review_id_marker,
};
use serde_json::Value;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CiProvider {
Github,
Gitlab,
}
impl CiProvider {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Github => "GitHub",
Self::Gitlab => "GitLab",
}
}
}
#[must_use]
pub fn apply_path_prefix(prefix: &str, path: &str) -> String {
if prefix.is_empty() {
return path.to_owned();
}
let mut segments: Vec<&str> = prefix.split('/').filter(|s| !s.is_empty()).collect();
let mut rest = path;
while let Some(after) = rest.strip_prefix("../") {
if segments.pop().is_none() {
return format!("{prefix}/{path}");
}
rest = after;
}
segments.push(rest);
segments.join("/")
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CiIssue {
pub rule_id: String,
pub description: String,
pub severity: String,
pub path: String,
pub line: u64,
pub end_line: Option<u64>,
pub other_locations: Vec<CiLocation>,
pub fingerprint: String,
pub legacy_fingerprint: Option<String>,
pub audit: Option<crate::AuditReviewContext>,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CiLocation {
pub path: String,
pub line: u64,
pub end_line: u64,
}
pub struct PrCommentRenderInput<'a> {
pub command: &'a str,
pub provider: CiProvider,
pub issues: &'a [CiIssue],
pub marker_id: String,
pub max_comments: usize,
pub category_for_rule: &'a dyn Fn(&str) -> &'static str,
pub docs_url_for_rule: &'a dyn Fn(&str) -> Option<String>,
pub trailing_section: Option<&'a str>,
pub status_note: Option<&'a str>,
pub layout: crate::PrCommentLayout,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReviewGitlabDiffRefs {
pub base_sha: String,
pub start_sha: String,
pub head_sha: String,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ReviewEnvelopeTruncation {
pub body: bool,
pub comment_limit: bool,
}
#[derive(Debug)]
pub struct ReviewEnvelopeRenderResult {
pub envelope: ReviewEnvelopeOutput,
pub truncation: ReviewEnvelopeTruncation,
}
pub struct ReviewEnvelopeRenderInput<'a> {
pub command: &'a str,
pub provider: CiProvider,
pub issues: &'a [CiIssue],
pub diff_index: Option<&'a DiffIndex>,
pub path_prefix: &'a str,
pub max_comments: usize,
pub gitlab_diff_refs: Option<&'a ReviewGitlabDiffRefs>,
pub include_guidance: bool,
pub suggestion_block: &'a dyn Fn(CiProvider, &CiIssue) -> Option<String>,
pub guidance_block: &'a dyn Fn(&CiIssue) -> Option<String>,
pub docs_url_for_rule: &'a dyn Fn(&str) -> Option<String>,
pub trailing_section: Option<&'a str>,
}
pub const MARKER_PREFIX_V3: &str = "<!-- fallow-fingerprint:v3: ";
pub const MARKER_SUFFIX_V3: &str = " -->";
pub const MAX_COMMENT_BODY_BYTES: usize = 65_536;
const TRUNCATION_SUFFIX: &str = "\n\n<!-- fallow-truncated -->\n> Body truncated by fallow.";
#[must_use]
pub fn issues_from_codeclimate(value: &Value) -> Vec<CiIssue> {
let mut issues = value
.as_array()
.into_iter()
.flatten()
.filter_map(issue_from_codeclimate)
.collect::<Vec<_>>();
sort_ci_issues(&mut issues);
issues
}
#[must_use]
pub fn issues_from_codeclimate_issues(issues: &[CodeClimateIssue]) -> Vec<CiIssue> {
let mut issues = issues
.iter()
.map(issue_from_codeclimate_issue)
.collect::<Vec<_>>();
sort_ci_issues(&mut issues);
issues
}
fn issue_from_codeclimate(value: &Value) -> Option<CiIssue> {
let path = uri_to_path(value.pointer("/location/path")?.as_str()?);
let line = value
.pointer("/location/lines/begin")
.and_then(Value::as_u64)
.unwrap_or(1);
let end_line = value.pointer("/location/lines/end").and_then(Value::as_u64);
let mut other_locations = value
.get("other_locations")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|location| {
let line = location.pointer("/lines/begin")?.as_u64()?;
let end_line = location
.pointer("/lines/end")
.and_then(Value::as_u64)
.filter(|end| *end >= line)
.unwrap_or(line);
Some(CiLocation {
path: uri_to_path(location.get("path")?.as_str()?),
line,
end_line,
})
})
.collect::<Vec<_>>();
other_locations.sort();
Some(CiIssue {
rule_id: value
.get("check_name")
.and_then(Value::as_str)
.unwrap_or("fallow/finding")
.to_string(),
description: value
.get("description")
.and_then(Value::as_str)
.unwrap_or("Fallow finding")
.to_string(),
severity: value
.get("severity")
.and_then(Value::as_str)
.unwrap_or("minor")
.to_string(),
fingerprint: value
.get("fingerprint")
.and_then(Value::as_str)
.unwrap_or("")
.to_string(),
legacy_fingerprint: None,
audit: None,
path,
line,
end_line,
other_locations,
})
}
fn issue_from_codeclimate_issue(issue: &CodeClimateIssue) -> CiIssue {
let mut other_locations = issue
.other_locations
.iter()
.map(|location| CiLocation {
path: uri_to_path(&location.path),
line: u64::from(location.lines.begin),
end_line: u64::from(
location
.lines
.end
.filter(|end| *end >= location.lines.begin)
.unwrap_or(location.lines.begin),
),
})
.collect::<Vec<_>>();
other_locations.sort();
CiIssue {
rule_id: issue.check_name.clone(),
description: issue.description.clone(),
severity: codeclimate_severity_label(issue.severity).to_owned(),
path: uri_to_path(&issue.location.path),
line: u64::from(issue.location.lines.begin),
end_line: issue.location.lines.end.map(u64::from),
other_locations,
fingerprint: issue.fingerprint.clone(),
legacy_fingerprint: issue.legacy_fingerprint.clone(),
audit: issue.audit,
}
}
const fn codeclimate_severity_label(severity: CodeClimateSeverity) -> &'static str {
match severity {
CodeClimateSeverity::Info => "info",
CodeClimateSeverity::Minor => "minor",
CodeClimateSeverity::Major => "major",
CodeClimateSeverity::Critical => "critical",
CodeClimateSeverity::Blocker => "blocker",
}
}
fn sort_ci_issues(issues: &mut [CiIssue]) {
issues
.sort_by(|a, b| (&a.path, a.line, &a.fingerprint).cmp(&(&b.path, b.line, &b.fingerprint)));
}
fn fingerprint_hash(parts: &[&str]) -> String {
crate::codeclimate_fingerprint_hash(parts)
}
#[must_use]
pub fn render_pr_comment(input: &PrCommentRenderInput<'_>) -> String {
render_pr_comment_with_verdict(input, None)
}
#[must_use]
#[expect(clippy::expect_used, reason = "formatting into String is infallible")]
pub fn render_pr_comment_with_verdict(
input: &PrCommentRenderInput<'_>,
gate: Option<ReviewCheckConclusion>,
) -> String {
let marker = format!("<!-- fallow-id: {} -->", input.marker_id);
let title = command_title(input.command);
let verdict = most_severe_conclusion(github_check_conclusion(input.issues), gate);
let mut out = String::new();
out.push_str(&marker);
out.push('\n');
write!(&mut out, "### {}\n\n", pr_comment_verdict(verdict)).expect("write to string");
if input.issues.is_empty() {
write!(
&mut out,
"Fallow {title}, no findings for this {}.\n\n",
change_noun(input.provider)
)
.expect("write to string");
} else {
write!(
&mut out,
"Fallow {title}, {}.\n\n",
severity_count_line(input.issues)
)
.expect("write to string");
}
if let Some(note) = input
.status_note
.map(str::trim)
.filter(|note| !note.is_empty())
{
write!(&mut out, "> {note}\n\n").expect("write to string");
}
let groups = group_by_category(input.issues, input.category_for_rule);
match input.layout {
crate::PrCommentLayout::GateOnly => {}
crate::PrCommentLayout::Compact => render_category_table(&mut out, &groups),
crate::PrCommentLayout::Default | crate::PrCommentLayout::Details => {
let table = FindingsTable {
max: input.max_comments,
open: input.layout == crate::PrCommentLayout::Details,
docs_url_for_rule: input.docs_url_for_rule,
};
for (category, mut group_issues) in groups {
group_issues
.sort_by_key(|issue| review_label_from_codeclimate(&issue.severity) != "error");
let label = summary_label(category, group_issues.len(), input.max_comments);
render_findings_table(&mut out, &group_issues, &table, &label);
}
}
}
let trailing = input
.trailing_section
.map(str::trim)
.filter(|section| !section.is_empty());
if let Some(section) = trailing.filter(|_| input.layout != crate::PrCommentLayout::GateOnly) {
out.push_str(section);
out.push_str("\n\n");
}
out.push_str("Generated by fallow.");
out
}
fn severity_count_line(issues: &[CiIssue]) -> String {
format!(
"{}: {}",
count_noun(issues.len(), "finding", "findings"),
severity_counts(issues)
)
}
fn severity_counts(issues: &[CiIssue]) -> String {
let errors = issues
.iter()
.filter(|issue| review_label_from_codeclimate(&issue.severity) == "error")
.count();
let warnings = issues.len() - errors;
match (errors, warnings) {
(0, _) => count_noun(warnings, "warning", "warnings"),
(_, 0) => count_noun(errors, "error", "errors"),
_ => format!(
"{}, {}",
count_noun(errors, "error", "errors"),
count_noun(warnings, "warning", "warnings")
),
}
}
#[must_use]
pub fn pr_verdict_title(issues: &[CiIssue], gate: Option<ReviewCheckConclusion>) -> String {
let verdict = pr_comment_verdict(most_severe_conclusion(
github_check_conclusion(issues),
gate,
));
if issues.is_empty() {
verdict.to_owned()
} else {
format!("{verdict}: {}", severity_counts(issues))
}
}
fn count_noun(count: usize, singular: &str, plural: &str) -> String {
format!("{count} {}", if count == 1 { singular } else { plural })
}
#[expect(clippy::expect_used, reason = "formatting into String is infallible")]
fn render_category_table(out: &mut String, groups: &[(&'static str, Vec<&CiIssue>)]) {
if groups.is_empty() {
return;
}
out.push_str("| Section | Errors | Warnings |\n| --- | --: | --: |\n");
for (category, issues) in groups {
let errors = issues
.iter()
.filter(|issue| review_label_from_codeclimate(&issue.severity) == "error")
.count();
writeln!(out, "| {category} | {errors} | {} |", issues.len() - errors)
.expect("write to string");
}
out.push('\n');
}
const fn pr_comment_verdict(conclusion: ReviewCheckConclusion) -> &'static str {
match conclusion {
ReviewCheckConclusion::Failure => "Quality gate failed",
ReviewCheckConclusion::Neutral => "Review needed",
ReviewCheckConclusion::Success => "Quality gate passed",
}
}
const fn conclusion_rank(conclusion: ReviewCheckConclusion) -> u8 {
match conclusion {
ReviewCheckConclusion::Success => 0,
ReviewCheckConclusion::Neutral => 1,
ReviewCheckConclusion::Failure => 2,
}
}
const fn most_severe_conclusion(
derived: ReviewCheckConclusion,
gate: Option<ReviewCheckConclusion>,
) -> ReviewCheckConclusion {
match gate {
Some(gate) if conclusion_rank(gate) > conclusion_rank(derived) => gate,
_ => derived,
}
}
pub const PROJECT_LEVEL_RULE_IDS: &[&str] = &[
"fallow/unused-catalog-entry",
"fallow/empty-catalog-group",
"fallow/unresolved-catalog-reference",
"fallow/unused-dependency-override",
"fallow/misconfigured-dependency-override",
"fallow/unused-dependency",
"fallow/unused-dev-dependency",
"fallow/unused-optional-dependency",
"fallow/type-only-dependency",
"fallow/test-only-dependency",
"fallow/dev-dependency-in-production",
];
#[must_use]
pub fn is_project_level_rule(rule_id: &str) -> bool {
PROJECT_LEVEL_RULE_IDS.contains(&rule_id)
}
pub const FILE_SCOPED_RULE_IDS: &[&str] = &[
"fallow/unused-file",
"fallow/circular-dependency",
"fallow/re-export-cycle",
"fallow/package-cycle",
];
#[must_use]
pub fn is_file_scoped_rule(rule_id: &str) -> bool {
FILE_SCOPED_RULE_IDS.contains(&rule_id)
}
const CATEGORY_ORDER: [&str; 11] = [
"Dead code",
"Dependencies",
"Duplication",
"Health",
"Architecture",
"Policy",
"Security",
"Flags",
"Suppressions",
"Tests",
"Other",
];
fn group_by_category<'a>(
issues: &'a [CiIssue],
category_for_rule: &dyn Fn(&str) -> &'static str,
) -> Vec<(&'static str, Vec<&'a CiIssue>)> {
let mut buckets: std::collections::BTreeMap<&'static str, Vec<&CiIssue>> =
std::collections::BTreeMap::new();
for issue in issues {
let category = category_for_rule(&issue.rule_id);
buckets.entry(category).or_default().push(issue);
}
let mut ordered: Vec<(&'static str, Vec<&CiIssue>)> = Vec::with_capacity(buckets.len());
for category in CATEGORY_ORDER {
if let Some(items) = buckets.remove(category) {
ordered.push((category, items));
}
}
for (category, items) in buckets {
ordered.push((category, items));
}
ordered
}
#[must_use]
pub fn summary_label(category: &str, total: usize, max: usize) -> String {
if total > max {
format!("{category} ({total}, showing {max})")
} else {
format!("{category} ({total})")
}
}
struct FindingsTable<'a> {
max: usize,
open: bool,
docs_url_for_rule: &'a dyn Fn(&str) -> Option<String>,
}
#[expect(clippy::expect_used, reason = "formatting into String is infallible")]
fn render_findings_table(
out: &mut String,
issues: &[&CiIssue],
table: &FindingsTable<'_>,
summary: &str,
) {
let open = if table.open { " open" } else { "" };
writeln!(out, "<details{open}>\n<summary>{summary}</summary>\n").expect("write to string");
out.push_str("| Severity | Rule | Location | Description |\n");
out.push_str("| --- | --- | --- | --- |\n");
for issue in issues.iter().take(table.max) {
writeln!(
out,
"| {} | {} | {} | {} |",
review_label_from_codeclimate(&issue.severity),
rule_link(&issue.rule_id, table.docs_url_for_rule),
markdown_table_code_span(&format!("{}:{}", issue.path, issue.line)),
markdown_table_prose(&issue.description),
)
.expect("write to string");
}
if issues.len() > table.max {
writeln!(
out,
"\nThe table shows {} of {} findings. Run fallow locally to see each finding.",
table.max,
issues.len(),
)
.expect("write to string");
}
out.push_str("\n</details>\n\n");
}
fn rule_link(rule_id: &str, docs_url_for_rule: &dyn Fn(&str) -> Option<String>) -> String {
let span = markdown_table_code_span(rule_id);
match docs_url_for_rule(rule_id) {
Some(url) => format!("[{span}]({url})"),
None => span,
}
}
#[must_use]
pub fn command_title(command: &str) -> &'static str {
match command {
"dead-code" | "check" => "codebase report",
"architecture" => "architecture report",
"dupes" => "duplication report",
"health" => "health report",
"audit" => "audit report",
"security" => "security report",
"fix" => "fix report",
"" | "combined" => "combined report",
_ => "report",
}
}
const fn change_noun(provider: CiProvider) -> &'static str {
match provider {
CiProvider::Github => "pull request",
CiProvider::Gitlab => "merge request",
}
}
const fn changes_tab(provider: CiProvider) -> &'static str {
match provider {
CiProvider::Github => "Files changed",
CiProvider::Gitlab => "Changes",
}
}
#[must_use]
pub fn markdown_code_span(value: &str) -> String {
let longest_run = value
.split(|c| c != '`')
.map(str::len)
.max()
.unwrap_or_default();
let fence = "`".repeat(longest_run + 1);
let needs_padding = value.starts_with('`')
|| value.ends_with('`')
|| (value.starts_with(' ') && value.ends_with(' ') && !value.chars().all(|c| c == ' '));
if needs_padding {
format!("{fence} {value} {fence}")
} else {
format!("{fence}{value}{fence}")
}
}
#[must_use]
pub fn markdown_table_code_span(value: &str) -> String {
let collapsed = value.replace("\r\n", " ").replace(['\n', '\r'], " ");
markdown_code_span(&collapsed.replace('|', "\\|"))
}
#[must_use]
pub fn markdown_table_text(value: &str) -> String {
value
.replace("\r\n", " ")
.replace(['\n', '\r'], " ")
.replace('|', "\\|")
}
#[must_use]
pub fn render_review_envelope(
input: &ReviewEnvelopeRenderInput<'_>,
status_message: Option<&str>,
) -> ReviewEnvelopeRenderResult {
render_review_envelope_with_id(input, None, None, status_message)
}
#[must_use]
pub fn render_review_envelope_with_conclusion(
input: &ReviewEnvelopeRenderInput<'_>,
conclusion: ReviewCheckConclusion,
status_message: Option<&str>,
) -> ReviewEnvelopeRenderResult {
render_review_envelope_with_id(input, None, Some(conclusion), status_message)
}
#[must_use]
pub fn render_scoped_review_envelope(
input: &ReviewEnvelopeRenderInput<'_>,
review_id: &ReviewId,
status_message: Option<&str>,
) -> ReviewEnvelopeRenderResult {
render_review_envelope_with_id(input, Some(review_id), None, status_message)
}
#[must_use]
pub fn render_scoped_review_envelope_with_conclusion(
input: &ReviewEnvelopeRenderInput<'_>,
review_id: &ReviewId,
conclusion: ReviewCheckConclusion,
status_message: Option<&str>,
) -> ReviewEnvelopeRenderResult {
render_review_envelope_with_id(input, Some(review_id), Some(conclusion), status_message)
}
fn render_review_envelope_with_id(
input: &ReviewEnvelopeRenderInput<'_>,
review_id: Option<&ReviewId>,
conclusion: Option<ReviewCheckConclusion>,
status_message: Option<&str>,
) -> ReviewEnvelopeRenderResult {
let grouped = group_review_issues_by_path_line(input.issues, input.max_comments);
let comments: Vec<ReviewComment> = grouped
.groups
.iter()
.map(|group| {
render_review_comment_for_group_with_id(
&ReviewCommentRenderInput {
provider: input.provider,
group,
gitlab_diff_refs: input.gitlab_diff_refs,
diff_index: input.diff_index,
path_prefix: input.path_prefix,
include_guidance: input.include_guidance,
suggestion_block: input.suggestion_block,
guidance_block: input.guidance_block,
docs_url_for_rule: input.docs_url_for_rule,
},
review_id,
)
})
.collect();
let conclusion = conclusion.unwrap_or_else(|| github_check_conclusion(input.issues));
let summary_text = review_summary_text(
input.command,
input.provider,
comments.len(),
status_message,
input.trailing_section,
);
let summary_fp = summary_fingerprint(&summary_text);
let summary_marker = review_markers(&summary_fp, review_id);
let body = format!("{summary_text}{summary_marker}");
let summary = ReviewEnvelopeSummary {
body: body.clone(),
fingerprint: summary_fp,
};
let truncation = ReviewEnvelopeTruncation {
body: comments.iter().any(review_comment_truncated),
comment_limit: grouped.truncated,
};
ReviewEnvelopeRenderResult {
envelope: build_review_envelope_output(input.provider, body, summary, comments, conclusion),
truncation,
}
}
fn review_summary_text(
command: &str,
provider: CiProvider,
comment_count: usize,
status_message: Option<&str>,
trailing_section: Option<&str>,
) -> String {
let status = status_message.map_or_else(String::new, |message| format!("\n\n> {message}"));
let section = trailing_section
.map(str::trim)
.filter(|section| !section.is_empty())
.map_or_else(String::new, |section| format!("\n\n{section}"));
format!(
"### Fallow {}{}\n\n{}{}\n\n<!-- fallow-review -->",
command_title(command),
status,
inline_comment_line(provider, comment_count),
section,
)
}
fn inline_comment_line(provider: CiProvider, count: usize) -> String {
if count == 0 {
return format!(
"No findings anchored to the changed lines in this {}.",
change_noun(provider)
);
}
format!(
"{count} inline comment{} on the changed lines. Open the {} tab to review.",
if count == 1 { "" } else { "s" },
changes_tab(provider),
)
}
#[derive(Debug, PartialEq, Eq)]
pub struct GroupedReviewIssues<'a> {
pub groups: Vec<Vec<&'a CiIssue>>,
pub truncated: bool,
}
#[must_use]
pub fn group_review_issues_by_path_line(
issues: &[CiIssue],
max_groups: usize,
) -> GroupedReviewIssues<'_> {
if max_groups == 0 {
return GroupedReviewIssues {
groups: Vec::new(),
truncated: !issues.is_empty(),
};
}
let mut groups: Vec<Vec<&CiIssue>> = Vec::with_capacity(max_groups.min(issues.len()));
let mut current: Vec<&CiIssue> = Vec::new();
let mut current_key: Option<(&str, u64)> = None;
for issue in issues {
let key = (issue.path.as_str(), issue.line);
if Some(key) != current_key {
if !current.is_empty() {
groups.push(std::mem::take(&mut current));
if groups.len() == max_groups {
return GroupedReviewIssues {
groups,
truncated: true,
};
}
}
current_key = Some(key);
}
current.push(issue);
}
if !current.is_empty() && groups.len() < max_groups {
groups.push(current);
}
GroupedReviewIssues {
groups,
truncated: false,
}
}
fn review_comment_truncated(comment: &ReviewComment) -> bool {
match comment {
ReviewComment::GitHub(comment) => comment.truncated,
ReviewComment::GitLab(comment) => comment.truncated,
}
}
pub struct ReviewCommentRenderInput<'a, 'group> {
pub provider: CiProvider,
pub group: &'a [&'group CiIssue],
pub gitlab_diff_refs: Option<&'a ReviewGitlabDiffRefs>,
pub diff_index: Option<&'a DiffIndex>,
pub path_prefix: &'a str,
pub include_guidance: bool,
pub suggestion_block: &'a dyn Fn(CiProvider, &CiIssue) -> Option<String>,
pub guidance_block: &'a dyn Fn(&CiIssue) -> Option<String>,
pub docs_url_for_rule: &'a dyn Fn(&str) -> Option<String>,
}
#[must_use]
pub fn render_review_comment_for_group(input: &ReviewCommentRenderInput<'_, '_>) -> ReviewComment {
render_review_comment_for_group_with_id(input, None)
}
fn render_review_comment_for_group_with_id(
input: &ReviewCommentRenderInput<'_, '_>,
review_id: Option<&ReviewId>,
) -> ReviewComment {
assert!(
!input.group.is_empty(),
"group_review_issues_by_path_line never yields empty"
);
let representative = input.group[0];
let fingerprint = if input.group.len() == 1 {
representative.fingerprint.clone()
} else {
let constituents: Vec<&str> = input.group.iter().map(|i| i.fingerprint.as_str()).collect();
composite_fingerprint(&constituents)
};
let legacy_fingerprint = group_legacy_fingerprint(input.group);
let content = build_merged_comment_content(input);
let marker_line = review_markers(&fingerprint, review_id);
let (body, truncated) = cap_body_with_marker(&content, &marker_line);
build_review_comment(ReviewCommentInput {
provider: input.provider,
representative,
gitlab_diff_refs: input.gitlab_diff_refs,
diff_index: input.diff_index,
path_prefix: input.path_prefix,
body,
fingerprint,
legacy_fingerprint,
truncated,
})
}
fn group_legacy_fingerprint(group: &[&CiIssue]) -> Option<String> {
if group.iter().all(|issue| issue.legacy_fingerprint.is_none()) {
return None;
}
if let [issue] = group {
return issue.legacy_fingerprint.clone();
}
let constituents: Vec<&str> = group
.iter()
.map(|issue| {
issue
.legacy_fingerprint
.as_deref()
.unwrap_or(issue.fingerprint.as_str())
})
.collect();
Some(composite_fingerprint(&constituents))
}
#[expect(clippy::expect_used, reason = "formatting into String is infallible")]
fn build_merged_comment_content(input: &ReviewCommentRenderInput<'_, '_>) -> String {
let mut content = String::new();
for (index, issue) in input.group.iter().enumerate() {
let label = review_label_from_codeclimate(&issue.severity);
if index > 0 {
content.push_str("\n\n");
}
let audit_label = issue
.audit
.map(|audit| format!(" ({})", audit.label()))
.unwrap_or_default();
write!(
content,
"**{}**{} {}: {}",
label,
audit_label,
rule_link(&issue.rule_id, input.docs_url_for_rule),
markdown_table_prose(&issue.description)
)
.expect("write to String is infallible");
if !issue.other_locations.is_empty() {
content.push_str("\n\nAlso in: ");
let locations = issue
.other_locations
.iter()
.map(|location| {
let path = apply_path_prefix(input.path_prefix, &location.path);
markdown_code_span(&line_range(&path, location.line, location.end_line))
})
.collect::<Vec<_>>()
.join(", ");
content.push_str(&locations);
}
if let Some(suggestion) = (input.suggestion_block)(input.provider, issue) {
content.push_str(&suggestion);
}
if input.include_guidance
&& let Some(guidance) = (input.guidance_block)(issue)
{
content.push_str(&guidance);
}
}
content
}
struct ReviewCommentInput<'a> {
provider: CiProvider,
representative: &'a CiIssue,
gitlab_diff_refs: Option<&'a ReviewGitlabDiffRefs>,
diff_index: Option<&'a DiffIndex>,
path_prefix: &'a str,
body: String,
fingerprint: String,
legacy_fingerprint: Option<String>,
truncated: bool,
}
fn build_review_comment(input: ReviewCommentInput<'_>) -> ReviewComment {
let ReviewCommentInput {
provider,
representative,
gitlab_diff_refs,
diff_index,
path_prefix,
body,
fingerprint,
legacy_fingerprint,
truncated,
} = input;
match provider {
CiProvider::Github => ReviewComment::GitHub(GitHubReviewComment {
path: apply_path_prefix(path_prefix, &representative.path),
line: u32::try_from(representative.line).unwrap_or(u32::MAX),
side: GitHubReviewSide::Right,
body,
fingerprint,
legacy_fingerprint,
truncated,
}),
CiProvider::Gitlab => {
let old_rel = diff_index
.and_then(|di| di.old_path_for_root_relative(&representative.path))
.map_or_else(|| representative.path.clone(), Cow::into_owned);
let new_path = apply_path_prefix(path_prefix, &representative.path);
let old_path = apply_path_prefix(path_prefix, &old_rel);
let position = GitLabReviewPosition {
base_sha: gitlab_diff_refs.map(|r| r.base_sha.clone()),
start_sha: gitlab_diff_refs.map(|r| r.start_sha.clone()),
head_sha: gitlab_diff_refs.map(|r| r.head_sha.clone()),
position_type: GitLabReviewPositionType::Text,
old_path,
new_path,
new_line: u32::try_from(representative.line).unwrap_or(u32::MAX),
};
ReviewComment::GitLab(GitLabReviewComment {
body,
position,
fingerprint,
legacy_fingerprint,
truncated,
})
}
}
}
#[must_use]
pub fn cap_body_with_marker(content: &str, marker_line: &str) -> (String, bool) {
let intact_len = content.len() + marker_line.len();
if intact_len <= MAX_COMMENT_BODY_BYTES {
let mut out = String::with_capacity(intact_len);
out.push_str(content);
out.push_str(marker_line);
return (out, false);
}
let reserved = marker_line.len() + TRUNCATION_SUFFIX.len();
let budget = MAX_COMMENT_BODY_BYTES.saturating_sub(reserved);
let mut cut = budget.min(content.len());
while cut > 0 && !content.is_char_boundary(cut) {
cut -= 1;
}
let mut out = String::with_capacity(MAX_COMMENT_BODY_BYTES);
out.push_str(&content[..cut]);
out.push_str(TRUNCATION_SUFFIX);
out.push_str(marker_line);
(out, true)
}
fn line_range(path: &str, line: u64, end_line: u64) -> String {
if end_line > line {
format!("{path}:{line}-{end_line}")
} else {
format!("{path}:{line}")
}
}
#[must_use]
pub const fn review_label_from_codeclimate(severity_name: &str) -> &'static str {
match severity_name.as_bytes() {
b"major" | b"critical" | b"blocker" => "error",
_ => "warning",
}
}
#[must_use]
pub fn github_check_conclusion(issues: &[CiIssue]) -> ReviewCheckConclusion {
if issues
.iter()
.any(|issue| matches!(issue.severity.as_str(), "major" | "critical" | "blocker"))
{
ReviewCheckConclusion::Failure
} else if issues.is_empty() {
ReviewCheckConclusion::Success
} else {
ReviewCheckConclusion::Neutral
}
}
fn build_review_envelope_output(
provider: CiProvider,
body: String,
summary: ReviewEnvelopeSummary,
comments: Vec<ReviewComment>,
conclusion: ReviewCheckConclusion,
) -> ReviewEnvelopeOutput {
match provider {
CiProvider::Github => ReviewEnvelopeOutput {
event: Some(ReviewEnvelopeEvent::Comment),
body,
summary,
comments,
marker_regex: default_marker_regex(),
marker_regex_flags: default_marker_regex_flags(),
meta: ReviewEnvelopeMeta {
schema: ReviewEnvelopeSchema::V3,
provider: ReviewProvider::Github,
check_conclusion: Some(conclusion),
},
},
CiProvider::Gitlab => ReviewEnvelopeOutput {
event: None,
body,
summary,
comments,
marker_regex: default_marker_regex(),
marker_regex_flags: default_marker_regex_flags(),
meta: ReviewEnvelopeMeta {
schema: ReviewEnvelopeSchema::V3,
provider: ReviewProvider::Gitlab,
check_conclusion: None,
},
},
}
}
fn review_markers(fingerprint: &str, review_id: Option<&ReviewId>) -> String {
let fingerprint = format!("\n\n{MARKER_PREFIX_V3}{fingerprint}{MARKER_SUFFIX_V3}");
match review_id {
Some(review_id) => format!("{fingerprint}\n{}", review_id_marker(review_id)),
None => fingerprint,
}
}
#[must_use]
pub fn summary_fingerprint(body: &str) -> String {
fingerprint_hash(&[body])
}
#[must_use]
pub fn composite_fingerprint(constituents: &[&str]) -> String {
let mut sorted: Vec<&str> = constituents.to_vec();
sorted.sort_unstable();
let joined = sorted.join(":");
format!("merged:{}", fingerprint_hash(&[joined.as_str()]))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_parent_path_resolves_against_the_prefix() {
assert_eq!(
apply_path_prefix("packages/app", "src/a.ts"),
"packages/app/src/a.ts"
);
assert_eq!(
apply_path_prefix("packages/app", "../../.fallowrc.json"),
".fallowrc.json"
);
assert_eq!(
apply_path_prefix("packages/app", "../shared.json"),
"packages/shared.json"
);
assert_eq!(apply_path_prefix("", "../x.json"), "../x.json");
}
use crate::{CodeClimateIssueKind, CodeClimateLines, CodeClimateLocation};
fn category_for_rule(rule_id: &str) -> &'static str {
match rule_id {
"fallow/code-duplication" => "Duplication",
"fallow/high-complexity" => "Health",
"fallow/unused-dependency" => "Dependencies",
_ => "Dead code",
}
}
#[test]
fn command_title_names_each_analysis_command() {
assert_eq!(command_title("dead-code"), "codebase report");
assert_eq!(command_title("architecture"), "architecture report");
assert_eq!(command_title("dupes"), "duplication report");
}
#[test]
fn extracts_issues_from_codeclimate() {
let value = serde_json::json!([{
"check_name": "fallow/unused-export",
"description": "Export x is never imported",
"severity": "minor",
"fingerprint": "abc",
"location": { "path": "src/a.ts", "lines": { "begin": 7 } }
}]);
let issues = issues_from_codeclimate(&value);
assert_eq!(issues.len(), 1);
assert_eq!(issues[0].path, "src/a.ts");
assert_eq!(issues[0].line, 7);
}
#[test]
fn ci_issues_carry_the_filesystem_path_of_an_encoded_codeclimate_path() {
let typed = vec![CodeClimateIssue {
kind: CodeClimateIssueKind::Issue,
check_name: "fallow/high-complexity".to_owned(),
description: "Function is complex".to_owned(),
categories: vec!["Complexity".to_owned()],
severity: CodeClimateSeverity::Major,
fingerprint: "fp".to_owned(),
location: CodeClimateLocation {
path: "app/%5Bid%5D/page.tsx".to_owned(),
lines: CodeClimateLines {
begin: 7,
end: None,
},
},
other_locations: vec![CodeClimateLocation {
path: "app/%5Blang%5D/peer.tsx".to_owned(),
lines: CodeClimateLines {
begin: 2,
end: None,
},
}],
owner: None,
group: None,
legacy_fingerprint: None,
audit: None,
}];
let value = serde_json::to_value(&typed).expect("typed fixture serializes");
for issues in [
issues_from_codeclimate_issues(&typed),
issues_from_codeclimate(&value),
] {
assert_eq!(issues[0].path, "app/[id]/page.tsx");
assert_eq!(issues[0].other_locations[0].path, "app/[lang]/peer.tsx");
}
}
#[test]
fn typed_codeclimate_issues_extract_like_json_codeclimate() {
let severities = [
(CodeClimateSeverity::Info, "info"),
(CodeClimateSeverity::Minor, "minor"),
(CodeClimateSeverity::Major, "major"),
(CodeClimateSeverity::Critical, "critical"),
(CodeClimateSeverity::Blocker, "blocker"),
];
let typed = severities
.iter()
.enumerate()
.map(|(index, (severity, _))| CodeClimateIssue {
kind: CodeClimateIssueKind::Issue,
check_name: format!("fallow/rule-{index}"),
description: format!("Finding {index}"),
categories: vec!["Complexity".to_owned()],
severity: *severity,
fingerprint: format!("fp-{index}"),
location: CodeClimateLocation {
path: format!("src/{index}.ts"),
lines: CodeClimateLines {
begin: u32::try_from(index + 1).expect("small fixture index"),
end: Some(u32::try_from(index + 3).expect("small fixture index")),
},
},
other_locations: vec![CodeClimateLocation {
path: format!("src/peer-{index}.ts"),
lines: CodeClimateLines {
begin: 20,
end: Some(24),
},
}],
owner: None,
group: None,
legacy_fingerprint: None,
audit: None,
})
.collect::<Vec<_>>();
let value = serde_json::to_value(&typed).expect("typed fixture serializes");
assert_eq!(
issues_from_codeclimate_issues(&typed),
issues_from_codeclimate(&value)
);
let normalized = issues_from_codeclimate_issues(&typed);
assert_eq!(normalized[0].end_line, Some(3));
assert_eq!(normalized[0].other_locations[0].path, "src/peer-0.ts");
assert_eq!(normalized[0].other_locations[0].line, 20);
assert_eq!(normalized[0].other_locations[0].end_line, 24);
let typed_labels = issues_from_codeclimate_issues(&typed)
.into_iter()
.map(|issue| issue.severity)
.collect::<Vec<_>>();
let expected_labels = severities
.iter()
.map(|(_, label)| (*label).to_owned())
.collect::<Vec<_>>();
assert_eq!(typed_labels, expected_labels);
}
#[test]
fn review_comment_renders_repository_prefixed_peer_ranges() {
let issue = CiIssue {
rule_id: "fallow/code-duplication".to_owned(),
description: "11 lines repeat in 2 places (clone group dup:abcd1234)".to_owned(),
severity: "minor".to_owned(),
path: "src/a.ts".to_owned(),
line: 5,
end_line: Some(15),
other_locations: vec![CiLocation {
path: "src/b.ts".to_owned(),
line: 30,
end_line: 40,
}],
fingerprint: "instance-fingerprint".to_owned(),
legacy_fingerprint: None,
audit: None,
};
let comment = render_review_comment_for_group(&ReviewCommentRenderInput {
provider: CiProvider::Gitlab,
group: &[&issue],
gitlab_diff_refs: None,
diff_index: None,
path_prefix: "packages/app",
include_guidance: false,
suggestion_block: &|_, _| None,
guidance_block: &|_| None,
docs_url_for_rule: &|_| None,
});
let ReviewComment::GitLab(comment) = comment else {
panic!("expected GitLab comment");
};
assert_eq!(comment.position.new_path, "packages/app/src/a.ts");
assert!(
comment
.body
.contains("Also in: `packages/app/src/b.ts:30-40`")
);
}
#[test]
fn renders_default_empty_comment() {
let body = render_pr_comment(&PrCommentRenderInput {
command: "check",
provider: CiProvider::Github,
issues: &[],
marker_id: "fallow-results".to_owned(),
max_comments: 50,
category_for_rule: &category_for_rule,
trailing_section: None,
docs_url_for_rule: &|_| None,
status_note: None,
layout: crate::PrCommentLayout::Default,
});
assert!(body.contains("<!-- fallow-id: fallow-results"));
assert!(body.contains("### Quality gate passed"), "{body}");
assert!(
body.contains("Fallow codebase report, no findings for this pull request."),
"{body}"
);
}
fn pr_comment_issue(rule_id: &str, description: &str, severity: &str, path: &str) -> CiIssue {
CiIssue {
rule_id: rule_id.to_owned(),
description: description.to_owned(),
severity: severity.to_owned(),
path: path.to_owned(),
line: 3,
end_line: None,
other_locations: Vec::new(),
fingerprint: path.to_owned(),
legacy_fingerprint: None,
audit: None,
}
}
fn dead_code_issue(line: u64, fingerprint: &str, legacy: Option<&str>) -> CiIssue {
CiIssue {
rule_id: "fallow/unused-export".to_owned(),
description: "Export 'helper' is never imported by other modules".to_owned(),
severity: "major".to_owned(),
path: "src/lib.ts".to_owned(),
line,
end_line: None,
other_locations: Vec::new(),
fingerprint: fingerprint.to_owned(),
legacy_fingerprint: legacy.map(str::to_owned),
audit: None,
}
}
fn github_comment(group: &[&CiIssue]) -> GitHubReviewComment {
let comment = render_review_comment_for_group(&ReviewCommentRenderInput {
provider: CiProvider::Github,
group,
gitlab_diff_refs: None,
diff_index: None,
path_prefix: "",
include_guidance: false,
suggestion_block: &|_, _| None,
guidance_block: &|_| None,
docs_url_for_rule: &|_| None,
});
let ReviewComment::GitHub(comment) = comment else {
panic!("expected GitHub comment");
};
comment
}
#[test]
fn review_comment_writes_a_v3_marker_and_keeps_the_legacy_fingerprint() {
let issue = dead_code_issue(3, "0123456789abcdef", Some("fedcba9876543210"));
let comment = github_comment(&[&issue]);
assert!(
comment
.body
.ends_with("<!-- fallow-fingerprint:v3: 0123456789abcdef -->"),
"{}",
comment.body
);
assert!(!comment.body.contains("fallow-fingerprint:v2:"));
assert_eq!(comment.fingerprint, "0123456789abcdef");
assert_eq!(
comment.legacy_fingerprint.as_deref(),
Some("fedcba9876543210")
);
let value = serde_json::to_value(&comment).expect("comment serializes");
assert_eq!(value["legacy_fingerprint"], "fedcba9876543210");
}
#[test]
fn review_comment_omits_an_unchanged_legacy_fingerprint() {
let issue = dead_code_issue(3, "0123456789abcdef", None);
let comment = github_comment(&[&issue]);
assert_eq!(comment.legacy_fingerprint, None);
let value = serde_json::to_value(&comment).expect("comment serializes");
assert!(value.get("legacy_fingerprint").is_none());
}
#[test]
fn merged_review_comment_rebuilds_the_legacy_composite() {
let moved = dead_code_issue(3, "0123456789abcdef", Some("fedcba9876543210"));
let kept = dead_code_issue(3, "1111111111111111", None);
let comment = github_comment(&[&moved, &kept]);
assert_eq!(
comment.fingerprint,
composite_fingerprint(&["0123456789abcdef", "1111111111111111"])
);
assert_eq!(
comment.legacy_fingerprint,
Some(composite_fingerprint(&[
"fedcba9876543210",
"1111111111111111"
]))
);
}
#[test]
fn pr_comment_titles_by_content_and_names_its_only_category() {
let issues = vec![
pr_comment_issue(
"fallow/unresolved-import",
"Import './x' could not be resolved",
"major",
"src/a.ts",
),
pr_comment_issue(
"fallow/unresolved-import",
"Import './y' could not be resolved",
"major",
"src/b.ts",
),
];
let body = render_pr_comment(&PrCommentRenderInput {
command: "dead-code",
provider: CiProvider::Github,
issues: &issues,
marker_id: "fallow-results".to_owned(),
max_comments: 50,
category_for_rule: &category_for_rule,
trailing_section: None,
docs_url_for_rule: &|_| None,
status_note: None,
layout: crate::PrCommentLayout::Default,
});
assert!(body.contains("### Quality gate failed\n"), "{body}");
assert!(
body.contains("Fallow codebase report, 2 findings: 2 errors."),
"{body}"
);
assert!(body.contains("<summary>Dead code (2)</summary>"), "{body}");
}
#[test]
fn pr_comment_counts_by_severity_and_opens_a_section_per_category() {
let issues = vec![
pr_comment_issue(
"fallow/unresolved-import",
"Import './x' could not be resolved",
"major",
"src/a.ts",
),
pr_comment_issue(
"fallow/unused-dependency",
"Package 'lodash' is never imported",
"minor",
"package.json",
),
];
let body = render_pr_comment(&PrCommentRenderInput {
command: "check",
provider: CiProvider::Github,
issues: &issues,
marker_id: "fallow-results".to_owned(),
max_comments: 50,
category_for_rule: &category_for_rule,
trailing_section: None,
docs_url_for_rule: &|_| None,
status_note: None,
layout: crate::PrCommentLayout::Default,
});
assert!(body.contains("2 findings: 1 error, 1 warning."), "{body}");
assert!(body.contains("<summary>Dead code (1)</summary>"), "{body}");
assert!(
body.contains("<summary>Dependencies (1)</summary>"),
"{body}"
);
}
fn layout_body(layout: crate::PrCommentLayout, note: Option<&str>) -> String {
let issues = vec![
pr_comment_issue(
"fallow/unresolved-import",
"Import './x' could not be resolved",
"major",
"src/a.ts",
),
pr_comment_issue(
"fallow/unused-dependency",
"Package 'lodash' is never imported",
"minor",
"package.json",
),
];
render_pr_comment(&PrCommentRenderInput {
command: "check",
provider: CiProvider::Github,
issues: &issues,
marker_id: "fallow-results".to_owned(),
max_comments: 50,
category_for_rule: &category_for_rule,
trailing_section: Some("#### Mutation testing"),
docs_url_for_rule: &|rule| {
(rule == "fallow/unused-dependency")
.then(|| "https://fallow.tools/docs/deps/".to_owned())
},
status_note: note,
layout,
})
}
#[test]
fn default_layout_links_rules_and_quotes_names_as_code() {
let body = layout_body(crate::PrCommentLayout::Default, None);
assert!(
body.contains(
"| warning | [`fallow/unused-dependency`](https://fallow.tools/docs/deps/) | `package.json:3` | Package `lodash` is never imported |"
),
"{body}"
);
assert!(
body.contains("| error | `fallow/unresolved-import` | `src/a.ts:3` | Import `./x` could not be resolved |"),
"{body}"
);
assert!(body.contains("<details>\n"), "{body}");
}
#[test]
fn the_status_note_sits_under_the_verdict_and_before_the_sections() {
let body = layout_body(crate::PrCommentLayout::Default, Some("Gates: failed x."));
let note = body.find("> Gates: failed x.").expect("note");
let first_section = body.find("<details>").expect("section");
let footer = body.find("Generated by fallow.").expect("footer");
assert!(note < first_section && first_section < footer, "{body}");
assert!(body.ends_with("Generated by fallow."), "{body}");
}
#[test]
fn each_layout_renders_its_own_parts() {
let details = layout_body(crate::PrCommentLayout::Details, None);
assert!(details.contains("<details open>"), "{details}");
let compact = layout_body(crate::PrCommentLayout::Compact, None);
assert!(!compact.contains("<details"), "{compact}");
assert!(
compact.contains("| Section | Errors | Warnings |"),
"{compact}"
);
assert!(compact.contains("| Dead code | 1 | 0 |"), "{compact}");
assert!(compact.contains("#### Mutation testing"), "{compact}");
let gate_only = layout_body(crate::PrCommentLayout::GateOnly, Some("Gates: failed x."));
assert!(!gate_only.contains("<details"), "{gate_only}");
assert!(!gate_only.contains("| Section"), "{gate_only}");
assert!(!gate_only.contains("#### Mutation testing"), "{gate_only}");
assert!(gate_only.contains("> Gates: failed x."), "{gate_only}");
assert!(
gate_only.contains("2 findings: 1 error, 1 warning."),
"{gate_only}"
);
}
#[test]
fn the_check_run_title_repeats_the_comment_verdict() {
let issues = vec![pr_comment_issue("fallow/a", "x", "minor", "src/a.ts")];
assert_eq!(pr_verdict_title(&issues, None), "Review needed: 1 warning");
assert_eq!(pr_verdict_title(&[], None), "Quality gate passed");
assert_eq!(
pr_verdict_title(&[], Some(ReviewCheckConclusion::Failure)),
"Quality gate failed"
);
}
#[test]
fn a_capped_table_shows_errors_before_warnings_and_reports_the_total() {
let issues = [
pr_comment_issue("fallow/a", "first warning", "minor", "src/a.ts"),
pr_comment_issue("fallow/a", "second warning", "minor", "src/b.ts"),
pr_comment_issue("fallow/a", "last error", "major", "src/c.ts"),
];
let body = render_pr_comment(&PrCommentRenderInput {
command: "check",
provider: CiProvider::Github,
issues: &issues,
marker_id: "fallow-results".to_owned(),
max_comments: 2,
category_for_rule: &category_for_rule,
trailing_section: None,
docs_url_for_rule: &|_| None,
status_note: None,
layout: crate::PrCommentLayout::Default,
});
assert!(body.contains("last error"), "{body}");
assert!(body.contains("first warning"), "{body}");
assert!(!body.contains("second warning"), "{body}");
assert!(
body.contains(
"The table shows 2 of 3 findings. Run fallow locally to see each finding."
),
"{body}"
);
}
#[test]
fn a_one_line_range_names_the_line_once() {
assert_eq!(line_range("a.ts", 3, 3), "a.ts:3");
assert_eq!(line_range("a.ts", 3, 9), "a.ts:3-9");
}
#[test]
fn the_tests_section_comes_after_the_registered_sections_and_before_other() {
let categories = |rule_id: &str| -> &'static str {
match rule_id {
"fallow/mutation-survived" => "Tests",
"fallow/unregistered" => "Other",
_ => "Suppressions",
}
};
let issues = vec![
pr_comment_issue("fallow/unregistered", "x", "minor", "src/c.ts"),
pr_comment_issue("fallow/mutation-survived", "y", "minor", "src/b.ts"),
pr_comment_issue("fallow/stale-suppression", "z", "minor", "src/a.ts"),
];
let order: Vec<&str> = group_by_category(&issues, &categories)
.into_iter()
.map(|(category, _)| category)
.collect();
assert_eq!(order, ["Suppressions", "Tests", "Other"]);
}
#[test]
fn pr_comment_empty_state_speaks_the_provider_language() {
let github = render_pr_comment(&PrCommentRenderInput {
command: "dead-code",
provider: CiProvider::Github,
issues: &[],
marker_id: "fallow-results".to_owned(),
max_comments: 50,
category_for_rule: &category_for_rule,
trailing_section: None,
docs_url_for_rule: &|_| None,
status_note: None,
layout: crate::PrCommentLayout::Default,
});
let gitlab = render_pr_comment(&PrCommentRenderInput {
command: "dead-code",
provider: CiProvider::Gitlab,
issues: &[],
marker_id: "fallow-results".to_owned(),
max_comments: 50,
category_for_rule: &category_for_rule,
trailing_section: None,
docs_url_for_rule: &|_| None,
status_note: None,
layout: crate::PrCommentLayout::Default,
});
assert!(
github.contains("no findings for this pull request."),
"{github}"
);
assert!(
gitlab.contains("no findings for this merge request."),
"{gitlab}"
);
assert!(
github.starts_with("<!-- fallow-id: fallow-results -->\n"),
"{github}"
);
assert!(github.contains("Generated by fallow."), "{github}");
}
#[test]
fn summary_label_foreshadows_truncation() {
assert_eq!(
summary_label("Duplication", 160, 50),
"Duplication (160, showing 50)"
);
assert_eq!(summary_label("Health", 12, 50), "Health (12)");
assert_eq!(summary_label("Dependencies", 50, 50), "Dependencies (50)");
}
#[test]
fn markdown_code_span_grows_fence_past_inner_backticks() {
assert_eq!(markdown_code_span("plain"), "`plain`");
assert_eq!(markdown_code_span("has`tick"), "``has`tick``");
assert_eq!(markdown_code_span("`leading"), "`` `leading ``");
}
#[test]
fn markdown_table_code_span_escapes_pipes() {
assert_eq!(markdown_table_code_span("a|b"), "`a\\|b`");
assert_eq!(markdown_table_code_span("x`|y"), "``x`\\|y``");
}
#[test]
fn markdown_table_code_span_collapses_line_endings() {
assert_eq!(markdown_table_code_span("a\r\nb\rc\nd"), "`a b c d`");
}
#[test]
fn markdown_table_text_neutralizes_pipes_and_line_endings() {
assert_eq!(markdown_table_text("a|b"), "a\\|b");
assert_eq!(markdown_table_text("a\r\nb\rc\nd"), "a b c d");
}
#[test]
fn is_project_level_rule_covers_config_anchored_dependency_findings() {
for rule_id in PROJECT_LEVEL_RULE_IDS {
assert!(
is_project_level_rule(rule_id),
"{rule_id} must be project-level"
);
}
for rule_id in [
"fallow/unused-file",
"fallow/unused-export",
"fallow/unused-type",
"fallow/unused-enum-member",
"fallow/unused-class-member",
"fallow/unused-store-member",
"fallow/unused-namespace-member",
"fallow/unused-protected-class-member",
"fallow/namespace-escape",
"fallow/unresolved-import",
"fallow/unlisted-dependency",
"fallow/unlisted-binary",
"fallow/duplicate-export",
"fallow/circular-dependency",
"fallow/re-export-cycle",
"fallow/package-cycle",
"fallow/boundary-violation",
"fallow/stale-suppression",
"fallow/private-type-leak",
"fallow/high-complexity",
"fallow/high-crap-score",
] {
assert!(
!is_project_level_rule(rule_id),
"{rule_id} must NOT be project-level"
);
}
}
#[test]
fn pr_comment_trailing_section_renders_before_the_footer() {
let body = render_pr_comment(&PrCommentRenderInput {
command: "check",
provider: CiProvider::Github,
issues: &[],
marker_id: "fallow-results".to_owned(),
max_comments: 50,
category_for_rule: &category_for_rule,
trailing_section: Some("\n## Unmatched config patterns\n\n- `@typo/*`"),
docs_url_for_rule: &|_| None,
status_note: None,
layout: crate::PrCommentLayout::Default,
});
assert!(
body.ends_with(
"\n\n## Unmatched config patterns\n\n- `@typo/*`\n\nGenerated by fallow."
),
"{body}"
);
}
}