Skip to content

Commit 28c9e4e

Browse files
committed
Shorter branch check message with github information
1 parent a0ecaae commit 28c9e4e

3 files changed

Lines changed: 54 additions & 8 deletions

File tree

src/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,16 @@ async fn branch_check(
990990
.clone()
991991
};
992992
let result = chat::branch_check(&resources, &repo_resources, &branch).await?;
993-
let reply = branch_check_message(&repo, &branch, &branch_settings, &result);
993+
let reply = {
994+
let settings = repo_resources.settings.read().await;
995+
branch_check_message(
996+
&repo,
997+
&branch,
998+
&branch_settings,
999+
&result,
1000+
settings.github_info.as_ref(),
1001+
)
1002+
};
9941003

9951004
let mut send = reply_to_msg(&bot, &msg, reply).parse_mode(ParseMode::MarkdownV2);
9961005
send = try_attach_subscribe_button_markup(msg.chat.id, send, "b", &repo, &branch);

src/message.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
},
1010
condition::Action,
1111
error::Error,
12+
github::GitHubInfo,
1213
repo::{pr_issue_url, resources::RepoResources},
1314
utils::empty_or_start_new_line,
1415
};
@@ -118,19 +119,22 @@ pub fn branch_check_message(
118119
branch: &str,
119120
settings: &BranchSettings,
120121
result: &BranchCheckResult,
122+
github_info: Option<&GitHubInfo>,
121123
) -> String {
122124
let status = if result.old == result.new {
123125
format!(
124126
"{}
125127
\\(not changed\\)",
126-
markdown_optional_commit(result.new.as_deref())
128+
markdown_optional_commit(result.new.as_deref(), github_info)
127129
)
130+
} else if let (Some(info), Some(old), Some(new)) = (github_info, &result.old, &result.new) {
131+
github_commit_diff(info, old, new)
128132
} else {
129133
format!(
130134
"{old} \u{2192}
131135
{new}",
132-
old = markdown_optional_commit(result.old.as_deref()),
133-
new = markdown_optional_commit(result.new.as_deref()),
136+
old = markdown_optional_commit(result.old.as_deref(), github_info),
137+
new = markdown_optional_commit(result.new.as_deref(), github_info),
134138
)
135139
};
136140
format!(
@@ -143,10 +147,33 @@ pub fn branch_check_message(
143147
)
144148
}
145149

146-
pub fn markdown_optional_commit(commit: Option<&str>) -> String {
150+
const SHORT_COMMIT_LENGTH: usize = 11;
151+
152+
pub fn short_commit(commit: &str) -> &str {
153+
&commit[..SHORT_COMMIT_LENGTH.min(commit.len())]
154+
}
155+
156+
pub fn github_commit_diff(github_info: &GitHubInfo, old: &str, new: &str) -> String {
157+
let GitHubInfo { owner, repo, .. } = github_info;
158+
let old_short = short_commit(old);
159+
let new_short = short_commit(new);
160+
let url = format!("https://github.com/{owner}/{repo}/compare/{old}...{new}");
161+
let text = format!("{old_short}...{new_short}");
162+
markdown::link(&url, &markdown::escape(&text))
163+
}
164+
165+
pub fn markdown_optional_commit(commit: Option<&str>, github_info: Option<&GitHubInfo>) -> String {
147166
match &commit {
148167
None => "\\(nothing\\)".to_owned(),
149-
Some(c) => markdown::code_inline(&markdown::escape(c)),
168+
Some(commit) => match github_info {
169+
Some(info) => {
170+
let GitHubInfo { owner, repo, .. } = info;
171+
let short = short_commit(commit);
172+
let url = format!("https://github.com/{owner}/{repo}/commit/{short}");
173+
markdown::link(&url, &markdown::escape(short))
174+
}
175+
None => markdown::code_inline(&markdown::escape(commit)),
176+
},
150177
}
151178
}
152179

src/update.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,20 @@ async fn update_chat_repo_branch(
218218
let result = chat::branch_check(resources, repo_resources, branch).await?;
219219
log::info!("finished branch check ({chat}, {repo}, {branch})");
220220
if result.new != result.old {
221-
let message = branch_check_message(repo, branch, settings, &result);
221+
let message = {
222+
let repo_settings = repo_resources.settings.read().await;
223+
branch_check_message(
224+
repo,
225+
branch,
226+
settings,
227+
&result,
228+
repo_settings.github_info.as_ref(),
229+
)
230+
};
222231
let mut send = bot
223232
.send_message(chat, message)
224-
.parse_mode(ParseMode::MarkdownV2);
233+
.parse_mode(ParseMode::MarkdownV2)
234+
.disable_link_preview(true);
225235
send = try_attach_subscribe_button_markup(chat, send, "b", repo, branch);
226236
send.await?;
227237
}

0 commit comments

Comments
 (0)