Format TUI commit activity previews
This commit is contained in:
@@ -1163,12 +1163,7 @@ impl App {
|
||||
.as_ref()
|
||||
.and_then(|repo| repo.full_name.as_deref())
|
||||
.unwrap_or("repository");
|
||||
let detail = activity
|
||||
.comment
|
||||
.as_ref()
|
||||
.and_then(|comment| comment.body.clone())
|
||||
.or(activity.content.clone())
|
||||
.unwrap_or_else(|| "Server activity".into());
|
||||
let detail = activity_detail(&activity);
|
||||
let target = match gotcha_gitea::activity::target(&activity) {
|
||||
Some(gotcha_gitea::activity::Target::Repository { owner, repository }) => {
|
||||
Target::Repository(
|
||||
@@ -1869,6 +1864,49 @@ fn short_sha(sha: &str) -> &str {
|
||||
sha.get(..8).unwrap_or(sha)
|
||||
}
|
||||
|
||||
fn activity_detail(activity: &models::Activity) -> String {
|
||||
if let Some(body) = activity
|
||||
.comment
|
||||
.as_ref()
|
||||
.and_then(|comment| comment.body.as_deref())
|
||||
{
|
||||
return body.to_string();
|
||||
}
|
||||
if let Some(payload) = gotcha_gitea::activity::commit_activity(activity) {
|
||||
let noun = if payload.count == 1 {
|
||||
"commit"
|
||||
} else {
|
||||
"commits"
|
||||
};
|
||||
let mut detail = format!("**{} {noun}**", payload.count);
|
||||
for commit in payload.commits {
|
||||
let message = commit.message.lines().next().unwrap_or("Commit");
|
||||
detail.push_str("\n\n- ");
|
||||
if !commit.sha.is_empty() {
|
||||
detail.push('`');
|
||||
detail.push_str(short_sha(&commit.sha));
|
||||
detail.push_str("` ");
|
||||
}
|
||||
detail.push_str(message);
|
||||
let metadata = [commit.author.as_str(), commit.timestamp.as_str()]
|
||||
.into_iter()
|
||||
.filter(|value| !value.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" · ");
|
||||
if !metadata.is_empty() {
|
||||
detail.push_str(" \n ");
|
||||
detail.push_str(&metadata);
|
||||
}
|
||||
}
|
||||
return detail;
|
||||
}
|
||||
activity
|
||||
.content
|
||||
.clone()
|
||||
.filter(|content| !content.is_empty())
|
||||
.unwrap_or_else(|| "Server activity".into())
|
||||
}
|
||||
|
||||
fn err(error: gotcha_gitea::Error) -> String {
|
||||
error.to_string()
|
||||
}
|
||||
@@ -1950,4 +1988,23 @@ mod tests {
|
||||
assert_eq!(app.history.len(), 1);
|
||||
assert!(app.confirm.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commit_activity_preview_formats_the_payload() {
|
||||
let activity = models::Activity {
|
||||
content: Some(
|
||||
r#"{"Commits":[{"Sha1":"0123456789abcdef","Message":"Fix preview\n\nDetails","AuthorName":"Octo Cat","Timestamp":"2026-08-04T19:04:30+02:00"}],"Len":1}"#
|
||||
.into(),
|
||||
),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let detail = activity_detail(&activity);
|
||||
|
||||
assert_eq!(
|
||||
detail,
|
||||
"**1 commit**\n\n- `01234567` Fix preview \n Octo Cat · 2026-08-04T19:04:30+02:00"
|
||||
);
|
||||
assert!(!detail.contains("\"Commits\""));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user