Compare commits
10 Commits
dcf7ef34ab
...
v0.2.3
Author | SHA1 | Date | |
---|---|---|---|
4b5d9e2cde | |||
0f8f580050 | |||
0df350dae6 | |||
52a89428f3 | |||
903577aba7 | |||
a7629a650a | |||
629e13710d | |||
2cc3b87a35 | |||
fa67902c22 | |||
2032b97f7c |
3356
Cargo.lock
generated
3356
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
14
Cargo.toml
14
Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "bebot"
|
||||
version = "0.2.0"
|
||||
version = "0.2.3"
|
||||
description = "Gitlab webhook bot that publishes events to Matrix"
|
||||
license = "GPL-3.0"
|
||||
authors = [
|
||||
@@ -25,16 +25,16 @@ exclude = [
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
constant_time_eq = "0.3"
|
||||
constant_time_eq = "0.4"
|
||||
dateparser = "0.2"
|
||||
env_logger = "0.10"
|
||||
env_logger = "0.11"
|
||||
futures = "0.3"
|
||||
http = "0.2"
|
||||
http = "1.3"
|
||||
log = { version = "0.4", features = ["std"] }
|
||||
matrix-sdk = { version = "0.6", features = ["anyhow", "markdown", "rustls-tls"], default-features = false }
|
||||
quick-xml = { version = "0.30", features = ["serialize"] }
|
||||
matrix-sdk = { version = "0.13", features = ["anyhow", "markdown", "rustls-tls"], default-features = false }
|
||||
quick-xml = { version = "0.38", features = ["serialize"] }
|
||||
regex = "1"
|
||||
reqwest = { version = "0.11", default-features = false, features = ["tokio-rustls", "rustls-tls-native-roots", "gzip"] }
|
||||
reqwest = { version = "0.12", default-features = false, features = ["charset", "http2", "gzip", "rustls-tls-native-roots", "system-proxy"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
serde_regex = "1"
|
||||
|
@@ -7,7 +7,7 @@ user_id: "@mybebot:example.com"
|
||||
# Password for Matrix user.
|
||||
password: "secret-matrix-account-password"
|
||||
# All Gitlab-specific settings are under here.
|
||||
gitlab:
|
||||
gitlab_webhook:
|
||||
# Optional prefix to serve the webhook path under (default is empty string).
|
||||
url_prefix: "/bebot"
|
||||
# Default Matrix room to publish Gitlab events to.
|
||||
@@ -58,3 +58,28 @@ gitlab:
|
||||
"gitlab.example.com/myuser/some-other-less-cool-app":
|
||||
token: "kljaslkdjaklsdjalksd"
|
||||
# This repo uses the default events and room.
|
||||
# The mail_archive configuration section allows you to set up bebot to publish
|
||||
# messages based on RSS feeds from mail-archive.com.
|
||||
mail_archive:
|
||||
# List of rooms that will be published to by default, unless overridden by
|
||||
# a per-list config.
|
||||
default_rooms:
|
||||
- "#some-room:example.com"
|
||||
- "#some-other-room:example.com"
|
||||
# How often bebot will fetch the RSS feed to check for updates, in seconds.
|
||||
update_interval: 60
|
||||
# A directory where bebot can store state, such as the data of the last
|
||||
# entry in the RSS feed it has seen.
|
||||
state_dir: "/var/lib/bebot/mail-archive-state"
|
||||
# A list of mailing lists.
|
||||
lists:
|
||||
# This is the list name as is displayed in mail-archive.com URLS.
|
||||
- name: "my-list@example.com"
|
||||
# Disable publishing a matrix message for replies sent to the list
|
||||
# (default true). This isn't perfect, and can only guess if a message
|
||||
# is a reply based on the subject line.
|
||||
publish_on_replies: false
|
||||
# An optional list of rooms to publish to. If not specified, the
|
||||
# default_rooms setting above will be used.
|
||||
rooms:
|
||||
- "#yet-some-other-room:example.com"
|
||||
|
@@ -65,6 +65,8 @@ pub struct GitlabWebhookConfig {
|
||||
#[derive(Clone, Deserialize)]
|
||||
pub struct MailListConfig {
|
||||
pub name: String,
|
||||
#[serde(default = "default_true")]
|
||||
pub publish_on_replies: bool,
|
||||
#[serde(default)]
|
||||
pub rooms: Vec<OwnedRoomOrAliasId>,
|
||||
}
|
||||
@@ -89,6 +91,10 @@ pub struct Config {
|
||||
pub mail_archive: Option<MailArchiveConfig>,
|
||||
}
|
||||
|
||||
fn default_true() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn load_blocking(path: &String) -> anyhow::Result<Config> {
|
||||
let f = File::open(path)?;
|
||||
let r = BufReader::new(f);
|
||||
@@ -99,7 +105,7 @@ fn load_blocking(path: &String) -> anyhow::Result<Config> {
|
||||
pub async fn load<S: AsRef<str>>(path: S) -> anyhow::Result<Config> {
|
||||
let p = String::from(path.as_ref());
|
||||
let config = tokio::task::spawn_blocking(move || {
|
||||
load_blocking(&p).with_context(|| format!("Failed to load config from {}", p))
|
||||
load_blocking(&p).with_context(|| format!("Failed to load config from {p}"))
|
||||
})
|
||||
.await??;
|
||||
Ok(config)
|
||||
|
@@ -14,6 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#![allow(unused)]
|
||||
|
||||
use core::fmt;
|
||||
|
||||
use crate::config::PublishEvent;
|
||||
@@ -281,7 +283,7 @@ impl GitlabEventExt for GitlabEvent {
|
||||
false
|
||||
}
|
||||
}
|
||||
GitlabEvent::TagPush { .. } => find_publish_event!(publish_events, PublishEvent::TagPush { .. }).is_some(),
|
||||
GitlabEvent::TagPush { .. } => find_publish_event!(publish_events, PublishEvent::TagPush).is_some(),
|
||||
GitlabEvent::Issue { object_attributes, .. } => {
|
||||
if let Some(PublishEvent::Issues { actions }) =
|
||||
find_publish_event!(publish_events, PublishEvent::Issues { .. })
|
||||
@@ -437,7 +439,7 @@ impl GitlabEventExt for GitlabEvent {
|
||||
.or(merge_request.as_ref().map(|mr| mr.title.clone()))
|
||||
.iter()
|
||||
.fold(format!("Pipeline **{}**", object_attributes.status), |accum, title| {
|
||||
format!("{}: {}", accum, title)
|
||||
format!("{accum}: {title}")
|
||||
});
|
||||
vec![markdown_link(&title, &object_attributes.url)]
|
||||
} else {
|
||||
@@ -451,7 +453,7 @@ impl GitlabEventExt for GitlabEvent {
|
||||
|
||||
#[inline]
|
||||
fn markdown_link(title: &String, url: &String) -> String {
|
||||
format!("[{}]({})", title, url)
|
||||
format!("[{title}]({url})")
|
||||
}
|
||||
|
||||
pub fn parse_ref(r#ref: &str) -> String {
|
||||
|
@@ -17,13 +17,12 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use constant_time_eq::constant_time_eq;
|
||||
use http::StatusCode;
|
||||
use matrix_sdk::{
|
||||
ruma::{events::room::message::RoomMessageEventContent, OwnedRoomOrAliasId},
|
||||
Client,
|
||||
};
|
||||
use tokio::sync::mpsc;
|
||||
use warp::{filters::BoxedFilter, reply::Reply, Filter};
|
||||
use warp::{filters::BoxedFilter, http::StatusCode, reply::Reply, Filter};
|
||||
|
||||
use crate::{
|
||||
config::GitlabWebhookConfig,
|
||||
@@ -41,10 +40,7 @@ pub fn build_gitlab_messages(event: &GitlabEvent) -> Vec<String> {
|
||||
format!(
|
||||
"\\[{}\\] {}*{}* {}",
|
||||
project.path_with_namespace,
|
||||
refname
|
||||
.as_ref()
|
||||
.map(|rn| format!("`{}` ", rn))
|
||||
.unwrap_or_else(|| "".to_string()),
|
||||
refname.as_ref().map(|rn| format!("`{rn}` ")).unwrap_or_default(),
|
||||
event.user(),
|
||||
title,
|
||||
)
|
||||
@@ -59,9 +55,9 @@ pub async fn handle_gitlab_event(
|
||||
) -> anyhow::Result<()> {
|
||||
let room = matrix::ensure_room_joined(matrix_client, room_id).await?;
|
||||
for msg in build_gitlab_messages(&event) {
|
||||
debug!("Sending message to {}: {}", room_id, msg);
|
||||
debug!("Sending message to {room_id}: {msg}");
|
||||
let msg_content = RoomMessageEventContent::text_markdown(&msg);
|
||||
room.send(msg_content, None).await?;
|
||||
room.send(msg_content).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -71,7 +67,7 @@ pub fn build_route(config: GitlabWebhookConfig, matrix_client: Client) -> anyhow
|
||||
tokio::spawn(async move {
|
||||
while let Some((event, room)) = event_rx.recv().await {
|
||||
if let Err(err) = handle_gitlab_event(event, &room, &matrix_client).await {
|
||||
warn!("Failed to handle payload: {}", err);
|
||||
warn!("Failed to handle payload: {err}");
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -108,10 +104,10 @@ pub fn build_route(config: GitlabWebhookConfig, matrix_client: Client) -> anyhow
|
||||
let config_key = project.web_url.replace("http://", "").replace("https://", "");
|
||||
if let Some(repo_config) = config.repo_configs.get(&config_key) {
|
||||
if !constant_time_eq(token.as_bytes(), repo_config.token.as_bytes()) {
|
||||
warn!("Invalid token for repo '{}'", config_key);
|
||||
warn!("Invalid token for repo '{config_key}'");
|
||||
warp::reply::with_status("Invalid token", StatusCode::FORBIDDEN)
|
||||
} else {
|
||||
debug!("payload: {:?}", event);
|
||||
debug!("payload: {event:?}");
|
||||
if let Some(room) = &repo_config.room.as_ref().or(config.default_room.as_ref()) {
|
||||
let publish_events = repo_config
|
||||
.publish_events
|
||||
@@ -119,12 +115,12 @@ pub fn build_route(config: GitlabWebhookConfig, matrix_client: Client) -> anyhow
|
||||
.or(config.default_publish_events.as_ref());
|
||||
if publish_events.map(|ecs| event.should_publish(ecs)).unwrap_or(true) {
|
||||
if let Err(err) = event_tx.send((event, (*room).clone())).await {
|
||||
warn!("Failed to enqueue payload: {}", err);
|
||||
warn!("Failed to enqueue payload: {err}");
|
||||
}
|
||||
}
|
||||
warp::reply::with_status("OK", StatusCode::OK)
|
||||
} else {
|
||||
info!("Channel not configured for repo '{}'", config_key);
|
||||
info!("Channel not configured for repo '{config_key}'");
|
||||
warp::reply::with_status(
|
||||
"Matrix room not configured for repo",
|
||||
StatusCode::NOT_FOUND,
|
||||
@@ -132,7 +128,7 @@ pub fn build_route(config: GitlabWebhookConfig, matrix_client: Client) -> anyhow
|
||||
}
|
||||
}
|
||||
} else {
|
||||
info!("Repo '{}' unconfigured", config_key);
|
||||
info!("Repo '{config_key}' unconfigured");
|
||||
warp::reply::with_status("Repo not configured", StatusCode::NOT_FOUND)
|
||||
}
|
||||
}
|
||||
|
@@ -105,20 +105,21 @@ async fn handle_list(
|
||||
http_client: &reqwest::Client,
|
||||
url: &String,
|
||||
matrix_client: &Client,
|
||||
publish_on_replies: bool,
|
||||
room_ids: &[OwnedRoomOrAliasId],
|
||||
) -> anyhow::Result<()> {
|
||||
let list_state = load_list_state(state_file).await?;
|
||||
|
||||
let rooms_f = room_ids.iter().map(|room_id| {
|
||||
matrix::ensure_room_joined(matrix_client, room_id)
|
||||
.map(move |res| res.with_context(|| format!("Failed to join Matrix room '{}'", room_id)))
|
||||
.map(move |res| res.with_context(|| format!("Failed to join Matrix room '{room_id}'")))
|
||||
});
|
||||
let rooms = join_all(rooms_f)
|
||||
.await
|
||||
.into_iter()
|
||||
.flat_map(|room_res| match room_res {
|
||||
Err(err) => {
|
||||
warn!("{:#}", err);
|
||||
warn!("{err:#}");
|
||||
vec![]
|
||||
}
|
||||
Ok(room) => vec![room],
|
||||
@@ -132,7 +133,7 @@ async fn handle_list(
|
||||
.get(url)
|
||||
.send()
|
||||
.await
|
||||
.with_context(|| format!("Failed to fetch mail RSS feed from '{}'", url))
|
||||
.with_context(|| format!("Failed to fetch mail RSS feed from '{url}'"))
|
||||
.and_then(|response| {
|
||||
if !response.status().is_success() {
|
||||
Err(anyhow!(
|
||||
@@ -147,10 +148,10 @@ async fn handle_list(
|
||||
let body = response
|
||||
.text()
|
||||
.await
|
||||
.with_context(|| format!("Failed to decode RSS response body for '{}'", url))?;
|
||||
.with_context(|| format!("Failed to decode RSS response body for '{url}'"))?;
|
||||
let mail_rss = tokio::task::spawn_blocking(move || quick_xml::de::from_str::<MailRss>(&body))
|
||||
.await?
|
||||
.with_context(|| format!("Failed to parse RSS feed for '{}'", url))?;
|
||||
.with_context(|| format!("Failed to parse RSS feed for '{url}'"))?;
|
||||
let items = mail_rss
|
||||
.channel
|
||||
.items
|
||||
@@ -161,11 +162,15 @@ async fn handle_list(
|
||||
|
||||
for room in rooms {
|
||||
for item in &items {
|
||||
let msg =
|
||||
RoomMessageEventContent::text_markdown(format!("\\[{}\\] [{}]({}]", list.name, item.title, item.link));
|
||||
room.send(msg, None)
|
||||
if publish_on_replies || !item.title.starts_with("Re: ") {
|
||||
let msg = RoomMessageEventContent::text_markdown(format!(
|
||||
"\\[{}\\] [{}]({})",
|
||||
list.name, item.title, item.link
|
||||
));
|
||||
room.send(msg)
|
||||
.await
|
||||
.with_context(|| format!("Failed to send message to room '{}'", room.room_id()))?;
|
||||
}
|
||||
save_list_state(
|
||||
ListState {
|
||||
last_pub_date: item.pub_date.value,
|
||||
@@ -207,10 +212,18 @@ pub fn start_polling(config: MailArchiveConfig, matrix_client: Client) -> anyhow
|
||||
tokio::spawn(async move {
|
||||
if !room_ids.is_empty() {
|
||||
loop {
|
||||
if let Err(err) =
|
||||
handle_list(&list, &state_file, &http_client, &url, &matrix_client, &room_ids).await
|
||||
if let Err(err) = handle_list(
|
||||
&list,
|
||||
&state_file,
|
||||
&http_client,
|
||||
&url,
|
||||
&matrix_client,
|
||||
list.publish_on_replies,
|
||||
&room_ids,
|
||||
)
|
||||
.await
|
||||
{
|
||||
warn!("{:#}", err);
|
||||
warn!("{err:#}");
|
||||
}
|
||||
|
||||
sleep(update_interval).await;
|
||||
@@ -256,7 +269,7 @@ mod test {
|
||||
let f = File::open(format!("{}/test-data/maillist.xml", env!("CARGO_MANIFEST_DIR")))?;
|
||||
let r = BufReader::new(f);
|
||||
let mail_rss = quick_xml::de::from_reader::<_, MailRss>(r)?;
|
||||
println!("{:#?}", mail_rss);
|
||||
println!("{mail_rss:#?}");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@@ -76,7 +76,7 @@ async fn main() {
|
||||
env_logger::init_from_env(lenv);
|
||||
|
||||
if let Err(err) = run().await {
|
||||
error!("{:#}", err);
|
||||
error!("{err:#}");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@ use std::{fmt, process::exit, time::Duration};
|
||||
|
||||
use matrix_sdk::{
|
||||
config::SyncSettings,
|
||||
room::Joined,
|
||||
room::Room,
|
||||
ruma::{OwnedRoomOrAliasId, OwnedUserId, RoomOrAliasId, UserId},
|
||||
BaseRoom, Client,
|
||||
};
|
||||
@@ -26,12 +26,8 @@ use serde::de;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
async fn build_sync_settings(matrix_client: &Client) -> SyncSettings {
|
||||
let mut settings = SyncSettings::default().timeout(Duration::from_secs(30));
|
||||
if let Some(token) = matrix_client.sync_token().await {
|
||||
settings = settings.token(token);
|
||||
}
|
||||
settings
|
||||
async fn build_sync_settings() -> SyncSettings {
|
||||
SyncSettings::default().timeout(Duration::from_secs(30))
|
||||
}
|
||||
|
||||
pub async fn connect(config: &Config) -> anyhow::Result<Client> {
|
||||
@@ -41,21 +37,21 @@ pub async fn connect(config: &Config) -> anyhow::Result<Client> {
|
||||
.build()
|
||||
.await?;
|
||||
client
|
||||
.matrix_auth()
|
||||
.login_username(&config.user_id, &config.password)
|
||||
.initial_device_display_name("Bebot")
|
||||
.send()
|
||||
.await?;
|
||||
info!("Connected to matrix as {}; waiting for first sync", config.user_id);
|
||||
|
||||
let settings = build_sync_settings(&client).await;
|
||||
let settings = build_sync_settings().await;
|
||||
client.sync_once(settings).await?;
|
||||
info!("First matrix sync complete");
|
||||
|
||||
let sync_client = client.clone();
|
||||
tokio::spawn(async move {
|
||||
let settings = build_sync_settings(&sync_client).await;
|
||||
let settings = build_sync_settings().await;
|
||||
if let Err(err) = sync_client.sync(settings).await {
|
||||
error!("Matrix sync failed: {}", err);
|
||||
error!("Matrix sync failed: {err}");
|
||||
exit(1);
|
||||
}
|
||||
});
|
||||
@@ -63,7 +59,7 @@ pub async fn connect(config: &Config) -> anyhow::Result<Client> {
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
pub async fn ensure_room_joined(matrix_client: &Client, room_id: &OwnedRoomOrAliasId) -> anyhow::Result<Joined> {
|
||||
pub async fn ensure_room_joined(matrix_client: &Client, room_id: &OwnedRoomOrAliasId) -> anyhow::Result<Room> {
|
||||
fn room_matches(a_room: &BaseRoom, our_room: &OwnedRoomOrAliasId) -> bool {
|
||||
let our_room_str = our_room.as_str();
|
||||
a_room.room_id().as_str() == our_room_str
|
||||
@@ -85,11 +81,11 @@ pub async fn ensure_room_joined(matrix_client: &Client, room_id: &OwnedRoomOrAli
|
||||
.iter()
|
||||
.find(|a_room| room_matches(a_room, room_id))
|
||||
{
|
||||
invited.accept_invitation().await?;
|
||||
invited.join().await?;
|
||||
} else {
|
||||
matrix_client.join_room_by_id_or_alias(room_id, &[]).await?;
|
||||
}
|
||||
let settings = build_sync_settings(matrix_client).await;
|
||||
let settings = build_sync_settings().await;
|
||||
matrix_client.sync_once(settings).await?;
|
||||
room = matrix_client
|
||||
.joined_rooms()
|
||||
|
Reference in New Issue
Block a user