Skip to content

Commit 09906b8

Browse files
committed
Lintcheck: Linkify counts in CI summery
1 parent 057c4ae commit 09906b8

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

lintcheck/src/json.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ fn print_warnings(title: &str, warnings: &[LintJson]) {
5151
return;
5252
}
5353

54-
println!("### {title}");
54+
//We have to use HTML here to be able to manually add an id.
55+
println!(r#"<h3 id="{title}">{title}</h3>"#);
56+
println!();
5557
for warning in warnings {
5658
println!("{title} `{}` at {}", warning.lint, warning.file_link);
59+
println!();
5760
println!("```");
5861
print!("{}", warning.rendered);
5962
println!("```");
63+
println!();
6064
}
6165
}
6266

@@ -65,9 +69,12 @@ fn print_changed_diff(changed: &[(LintJson, LintJson)]) {
6569
return;
6670
}
6771

68-
println!("### Changed");
72+
//We have to use HTML here to be able to manually add an id.
73+
println!(r#"<h3 id="changed">Changed</h3>"#);
74+
println!();
6975
for (old, new) in changed {
7076
println!("Changed `{}` at {}", new.lint, new.file_link);
77+
println!();
7178
println!("```diff");
7279
for change in diff::lines(&old.rendered, &new.rendered) {
7380
use diff::Result::{Both, Left, Right};
@@ -109,13 +116,30 @@ pub(crate) fn diff(old_path: &Path, new_path: &Path) {
109116
}
110117

111118
print!(
112-
"{} added, {} removed, {} changed\n\n",
113-
added.len(),
114-
removed.len(),
115-
changed.len()
119+
r##"{}, {}, {}"##,
120+
count_string("added", added.len()),
121+
count_string("removed", removed.len()),
122+
count_string("changed", changed.len()),
116123
);
124+
println!();
125+
println!();
117126

118127
print_warnings("Added", &added);
119128
print_warnings("Removed", &removed);
120129
print_changed_diff(&changed);
121130
}
131+
132+
/// This generates the `x added` string for the start of the job summery.
133+
/// It linkifies them if possible to jump to the respective heading.
134+
fn count_string(label: &str, count: usize) -> String {
135+
// Headlines are only added, if anything will be displayed under the headline.
136+
// We therefore only want to add links to them if they exist
137+
if count == 0 {
138+
format!("0 {label}")
139+
} else {
140+
// GitHub's job summaries don't add HTML ids to headings. That's why we
141+
// manually have to add them. GitHub prefixes these manual ids with
142+
// `user-content-` and that's how we end up with these awesome links :D
143+
format!("[{count} {label}](#user-content-{label})")
144+
}
145+
}

0 commit comments

Comments
 (0)