Skip to content

Commit dda767a

Browse files
committed
Use askama template for PR check comment rendering
1 parent 24f3a9a commit dda767a

File tree

6 files changed

+63
-59
lines changed

6 files changed

+63
-59
lines changed

ci/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ci/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ resolver = "2"
77

88
[workspace.dependencies]
99
anyhow = "1.0"
10+
askama = "0.12"
1011
chrono = { version = "0.4", features = ["serde"] }
1112
pico-args = "0.5"
1213
serde = { version = "1.0", features = ["derive"] }

ci/pr-check/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ too_many_lines = "allow"
3535
[dependencies]
3636
render = { path = "../render" }
3737
anyhow = { workspace = true }
38+
askama = { workspace = true }
3839
chrono = { workspace = true }
3940
pico-args = { workspace = true }
4041
serde = { workspace = true }

ci/pr-check/src/main.rs

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! PR_NUMBER - the pull request number
2020
2121
use anyhow::{Context, Result, bail};
22+
use askama::Template;
2223
use chrono::{DateTime, Duration, Utc};
2324
use render::types::ParsedEntry;
2425
use serde::Deserialize;
@@ -98,6 +99,18 @@ impl ToolReport {
9899
fn any_fail(&self) -> bool {
99100
self.stars.is_fail() || self.contributors.is_fail() || self.age.is_fail()
100101
}
102+
103+
fn status(&self) -> &'static str {
104+
if self.any_fail() { "FAIL" } else { "PASS" }
105+
}
106+
}
107+
108+
#[derive(Template)]
109+
#[template(path = "comment.md")]
110+
struct CommentTemplate<'a> {
111+
marker: &'a str,
112+
reports: &'a [ToolReport],
113+
any_failures: bool,
101114
}
102115

103116
struct GithubClient {
@@ -357,63 +370,19 @@ async fn check_tool(client: &GithubClient, tool: &ParsedEntry) -> Result<ToolRep
357370
}
358371

359372
/// Renders all tool reports into a Markdown comment body.
360-
fn render_comment(reports: &[ToolReport]) -> String {
373+
///
374+
/// # Errors
375+
///
376+
/// Returns an error if the template fails to render.
377+
fn render_comment(reports: &[ToolReport]) -> Result<String> {
361378
let any_failures = reports.iter().any(|r| r.any_fail());
362-
363-
let mut out = String::new();
364-
365-
out.push_str(COMMENT_MARKER);
366-
out.push('\n');
367-
out.push_str("## Contributing criteria check\n\n");
368-
369-
if reports.is_empty() {
370-
out.push_str("No new tool files detected in `data/tools/`. Nothing to check.\n");
371-
return out;
372-
}
373-
374-
for report in reports {
375-
let status = if report.any_fail() { "FAIL" } else { "PASS" };
376-
out.push_str(&format!("### [{}] `{}`\n\n", status, report.name));
377-
378-
if let Some(src) = &report.source {
379-
out.push_str(&format!("Source: {src}\n\n"));
380-
}
381-
382-
if let Some(note) = &report.note {
383-
out.push_str(&format!("> **Note:** {note}\n\n"));
384-
}
385-
386-
out.push_str("| Criterion | Result |\n");
387-
out.push_str("|---|---|\n");
388-
389-
for (label, check) in [
390-
("Stars (min 20)", &report.stars),
391-
("Contributors (min 2)", &report.contributors),
392-
("Age (min 3 months)", &report.age),
393-
] {
394-
out.push_str(&format!(
395-
"| {} | {} {} |\n",
396-
label,
397-
check.symbol(),
398-
check.message()
399-
));
400-
}
401-
402-
out.push('\n');
403-
}
404-
405-
if any_failures {
406-
out.push_str("---\n\n");
407-
out.push_str(
408-
"One or more tools do not meet the [contributing criteria](CONTRIBUTING.md) yet. \
409-
We will keep this PR open. Feel free to update it once the thresholds are met.\n",
410-
);
411-
} else {
412-
out.push_str("---\n\n");
413-
out.push_str("All criteria passed. Thank you for your contribution.\n");
379+
CommentTemplate {
380+
marker: COMMENT_MARKER,
381+
reports,
382+
any_failures,
414383
}
415-
416-
out
384+
.render()
385+
.context("Failed to render comment template")
417386
}
418387

419388
/// Posts or updates the bot comment on the PR.
@@ -472,7 +441,7 @@ async fn main() -> Result<()> {
472441
reports.push(report);
473442
}
474443

475-
let comment_body = render_comment(&reports);
444+
let comment_body = render_comment(&reports)?;
476445

477446
upsert_comment(&client, &gh_repo, pr_number, &comment_body).await?;
478447

@@ -521,13 +490,13 @@ mod tests {
521490

522491
#[test]
523492
fn render_comment_no_files() {
524-
let comment = render_comment(&[]);
493+
let comment = render_comment(&[]).unwrap();
525494
assert!(comment.contains("No new tool files detected"));
526495
}
527496

528497
#[test]
529498
fn render_comment_contains_marker() {
530-
let comment = render_comment(&[]);
499+
let comment = render_comment(&[]).unwrap();
531500
assert!(comment.contains(COMMENT_MARKER));
532501
}
533502
}

ci/pr-check/templates/comment.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{ marker }}
2+
## Contributing criteria check
3+
4+
{% if reports.is_empty() %}
5+
No new tool files detected in `data/tools/`. Nothing to check.
6+
{% else %}
7+
{% for report in reports %}
8+
### [{{ report.status() }}] `{{ report.name }}`
9+
10+
{% if let Some(src) = report.source.as_ref() %}
11+
Source: {{ src }}
12+
13+
{% endif %}
14+
{% if let Some(note) = report.note.as_ref() %}
15+
> **Note:** {{ note }}
16+
17+
{% endif %}
18+
| Criterion | Result |
19+
|---|---|
20+
| Stars (min 20) | {{ report.stars.symbol() }} {{ report.stars.message() }} |
21+
| Contributors (min 2) | {{ report.contributors.symbol() }} {{ report.contributors.message() }} |
22+
| Age (min 3 months) | {{ report.age.symbol() }} {{ report.age.message() }} |
23+
24+
{% endfor %}
25+
---
26+
27+
{% if any_failures %}
28+
One or more tools do not meet the [contributing criteria](CONTRIBUTING.md) yet. We will keep this PR open. Feel free to update it once the thresholds are met.
29+
{% else %}
30+
All criteria passed. Thank you for your contribution.
31+
{% endif %}
32+
{% endif %}

ci/render/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ serde_derive = { workspace = true }
5353
serde_json = { workspace = true }
5454
serde_yaml = { workspace = true }
5555
tokio = { workspace = true }
56-
askama = "0.12.1"
56+
askama = { workspace = true }
5757
# Switch back to crates as soon as a new release with tokio 1.x support is
5858
# released. See https://github.com/softprops/hubcaps/pull/285
5959
hubcaps = { git="https://github.com/softprops/hubcaps" }

0 commit comments

Comments
 (0)