forge_notify: generic type label helper, compose kind from label+state
This commit is contained in:
parent
13b7a94257
commit
8cc6306728
1 changed files with 18 additions and 13 deletions
|
|
@ -105,6 +105,18 @@ async fn fetch_json(
|
||||||
resp.json().await.ok()
|
resp.json().await.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Map a Forgejo notification `subject.type` to a human-readable label.
|
||||||
|
/// Known values: "Pull", "Issue", "Commit", "Repository". Any unknown
|
||||||
|
/// type is passed through as-is so new Forgejo types degrade gracefully
|
||||||
|
/// rather than silently collapsing into a generic label.
|
||||||
|
fn notif_type_label(t: &str) -> &str {
|
||||||
|
match t {
|
||||||
|
"Pull" => "PR",
|
||||||
|
"Issue" => "issue",
|
||||||
|
other => other,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Truncate a string to `max` bytes at a char boundary, appending `…` if cut.
|
/// Truncate a string to `max` bytes at a char boundary, appending `…` if cut.
|
||||||
fn truncate(s: &str, max: usize) -> String {
|
fn truncate(s: &str, max: usize) -> String {
|
||||||
if s.len() <= max {
|
if s.len() <= max {
|
||||||
|
|
@ -158,12 +170,7 @@ async fn format_notification(
|
||||||
.unwrap_or("")
|
.unwrap_or("")
|
||||||
.trim();
|
.trim();
|
||||||
|
|
||||||
// Forgejo API returns "Pull" / "Issue", never "Pull Request".
|
let kind = format!("comment on {}", notif_type_label(notif_type));
|
||||||
let kind = match notif_type {
|
|
||||||
"Pull" => "comment on PR",
|
|
||||||
"Issue" => "comment on issue",
|
|
||||||
_ => "comment",
|
|
||||||
};
|
|
||||||
let mut out = format!(
|
let mut out = format!(
|
||||||
"[{kind}] {title}\nrepo: {repo}\nurl: {}\n\n{author}: {}\n",
|
"[{kind}] {title}\nrepo: {repo}\nurl: {}\n\n{author}: {}\n",
|
||||||
if comment_html_url.is_empty() { html_url } else { comment_html_url },
|
if comment_html_url.is_empty() { html_url } else { comment_html_url },
|
||||||
|
|
@ -184,13 +191,11 @@ async fn format_notification(
|
||||||
// - Forgejo API type is "Pull" / "Issue", never "Pull Request".
|
// - Forgejo API type is "Pull" / "Issue", never "Pull Request".
|
||||||
let notif_state = notif["subject"]["state"].as_str().unwrap_or("");
|
let notif_state = notif["subject"]["state"].as_str().unwrap_or("");
|
||||||
|
|
||||||
let kind = match (notif_type, notif_state) {
|
let label = notif_type_label(notif_type);
|
||||||
("Pull", "merged") => "PR merged".to_owned(),
|
let kind = match notif_state {
|
||||||
("Pull", "closed") => "PR closed".to_owned(),
|
"merged" => format!("{label} merged"),
|
||||||
("Pull", _) => "new PR".to_owned(),
|
"closed" => format!("{label} closed"),
|
||||||
("Issue", "closed") => "issue closed".to_owned(),
|
_ => format!("new {label}"),
|
||||||
("Issue", _) => "new issue".to_owned(),
|
|
||||||
_ => format!("new {notif_type}"),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fetch subject only for body/author on new (open) items — not
|
// Fetch subject only for body/author on new (open) items — not
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue