|
19 | 19 | //! PR_NUMBER - the pull request number |
20 | 20 |
|
21 | 21 | use anyhow::{Context, Result, bail}; |
| 22 | +use askama::Template; |
22 | 23 | use chrono::{DateTime, Duration, Utc}; |
23 | 24 | use render::types::ParsedEntry; |
24 | 25 | use serde::Deserialize; |
@@ -98,6 +99,18 @@ impl ToolReport { |
98 | 99 | fn any_fail(&self) -> bool { |
99 | 100 | self.stars.is_fail() || self.contributors.is_fail() || self.age.is_fail() |
100 | 101 | } |
| 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, |
101 | 114 | } |
102 | 115 |
|
103 | 116 | struct GithubClient { |
@@ -357,63 +370,19 @@ async fn check_tool(client: &GithubClient, tool: &ParsedEntry) -> Result<ToolRep |
357 | 370 | } |
358 | 371 |
|
359 | 372 | /// 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> { |
361 | 378 | 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, |
414 | 383 | } |
415 | | - |
416 | | - out |
| 384 | + .render() |
| 385 | + .context("Failed to render comment template") |
417 | 386 | } |
418 | 387 |
|
419 | 388 | /// Posts or updates the bot comment on the PR. |
@@ -472,7 +441,7 @@ async fn main() -> Result<()> { |
472 | 441 | reports.push(report); |
473 | 442 | } |
474 | 443 |
|
475 | | - let comment_body = render_comment(&reports); |
| 444 | + let comment_body = render_comment(&reports)?; |
476 | 445 |
|
477 | 446 | upsert_comment(&client, &gh_repo, pr_number, &comment_body).await?; |
478 | 447 |
|
@@ -521,13 +490,13 @@ mod tests { |
521 | 490 |
|
522 | 491 | #[test] |
523 | 492 | fn render_comment_no_files() { |
524 | | - let comment = render_comment(&[]); |
| 493 | + let comment = render_comment(&[]).unwrap(); |
525 | 494 | assert!(comment.contains("No new tool files detected")); |
526 | 495 | } |
527 | 496 |
|
528 | 497 | #[test] |
529 | 498 | fn render_comment_contains_marker() { |
530 | | - let comment = render_comment(&[]); |
| 499 | + let comment = render_comment(&[]).unwrap(); |
531 | 500 | assert!(comment.contains(COMMENT_MARKER)); |
532 | 501 | } |
533 | 502 | } |
0 commit comments