|
| 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