Skip to content

Commit e1d09fe

Browse files
committed
Fix username escape
1 parent ceee879 commit e1d09fe

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "commit-notifier"
33
authors = [ "Lin Yinfeng <[email protected]>" ]
4-
version = "0.2.1"
4+
version = "0.2.2"
55
edition = "2024"
66
description = """
77
A simple bot tracking git commits/PRs/issues/branches

src/chat/settings.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl TryFrom<SubscriberCompat> for Subscriber {
8888
markdown_mention: mention.clone(),
8989
}),
9090
(_, Some(username)) => Ok(Subscriber::Telegram {
91-
markdown_mention: format!("@{username}"),
91+
markdown_mention: format!("@{}", markdown::escape(username)),
9292
}),
9393
(_, _) => Err(Error::InvalidSubscriber(compat)),
9494
},
@@ -105,9 +105,14 @@ pub enum SubscriberCompat {
105105
}
106106

107107
impl Subscriber {
108-
pub fn from_tg_user(u: &User) -> Self {
108+
pub fn from_tg_user(user: &User) -> Self {
109+
// `markdown::user_mention_or_link` does not escape `user.mention()`
110+
let mention = match user.mention() {
111+
Some(mention) => markdown::escape(&mention),
112+
None => markdown::link(user.url().as_str(), &markdown::escape(&user.full_name())),
113+
};
109114
Self::Telegram {
110-
markdown_mention: markdown::user_mention_or_link(u),
115+
markdown_mention: mention,
111116
}
112117
}
113118

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod condition;
44
mod error;
55
mod github;
66
mod message;
7+
mod migration;
78
mod options;
89
mod repo;
910
mod resources;
@@ -167,6 +168,9 @@ async fn version_check() -> Result<(), Error> {
167168
version_info.version,
168169
));
169170
}
171+
if old_version == version_compare::Version::from("0.2.1").unwrap() {
172+
migration::from_0_2_1().await?;
173+
}
170174
} else {
171175
// do nothing, start from an empty configuration
172176
}

src/migration.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use std::{collections::BTreeSet, mem};
2+
3+
use teloxide::utils::markdown;
4+
5+
use crate::{
6+
chat::{
7+
self,
8+
settings::{NotifySettings, Subscriber},
9+
},
10+
error::Error,
11+
};
12+
13+
pub async fn from_0_2_1() -> Result<(), Error> {
14+
fn migrate_notify_settings(settings: &mut NotifySettings) {
15+
let mut subscribers = BTreeSet::new();
16+
mem::swap(&mut subscribers, &mut settings.subscribers);
17+
for subscriber in subscribers {
18+
match subscriber {
19+
Subscriber::Telegram { markdown_mention } => {
20+
if markdown_mention.starts_with("@") {
21+
let new_mention = markdown::escape(&markdown_mention);
22+
if new_mention != markdown_mention {
23+
log::info!("escape username '{markdown_mention}' to '{new_mention}'");
24+
}
25+
settings.subscribers.insert(Subscriber::Telegram {
26+
markdown_mention: new_mention,
27+
});
28+
} else {
29+
settings
30+
.subscribers
31+
.insert(Subscriber::Telegram { markdown_mention });
32+
}
33+
}
34+
}
35+
}
36+
}
37+
38+
log::info!("migration from version 0.2.1");
39+
let chats = chat::chats().await?;
40+
for chat in chats.iter().cloned() {
41+
log::info!("migrating chat {chat}...");
42+
let repos = chat::repos(chat).await?;
43+
for repo in repos {
44+
log::info!("migrating repo {chat}/{repo}...");
45+
let resources = chat::resources_chat_repo(chat, repo.to_string()).await?;
46+
let mut settings = resources.settings.write().await;
47+
for (_, settings) in settings.branches.iter_mut() {
48+
migrate_notify_settings(&mut settings.notify)
49+
}
50+
for (_, settings) in settings.commits.iter_mut() {
51+
migrate_notify_settings(&mut settings.notify)
52+
}
53+
for (_, settings) in settings.pr_issues.iter_mut() {
54+
migrate_notify_settings(&mut settings.notify)
55+
}
56+
}
57+
}
58+
59+
// no errors, save all settings at once
60+
for chat in chats {
61+
let repos = chat::repos(chat).await?;
62+
for repo in repos {
63+
let resources = chat::resources_chat_repo(chat, repo.to_string()).await?;
64+
resources.save_settings().await?;
65+
}
66+
}
67+
Ok(())
68+
}

0 commit comments

Comments
 (0)