Compare commits

...

No commits in common. "30cb80559364784834b3cf770b42c167c201f892" and "b57720a2f875ea8766fbcbd555a5c31ca91c0247" have entirely different histories.

View File

@ -15,6 +15,7 @@ use event::{GitlabEvent, GitlabEventExt};
use http::StatusCode; use http::StatusCode;
use matrix_sdk::{ use matrix_sdk::{
config::SyncSettings, config::SyncSettings,
room::Joined,
ruma::{events::room::message::RoomMessageEventContent, OwnedRoomOrAliasId}, ruma::{events::room::message::RoomMessageEventContent, OwnedRoomOrAliasId},
BaseRoom, Client, BaseRoom, Client,
}; };
@ -54,29 +55,7 @@ async fn matrix_connect(config: &config::Config) -> anyhow::Result<Client> {
Ok(client) Ok(client)
} }
fn build_gitlab_message(event: &GitlabEvent) -> String { async fn ensure_matrix_room_joined(matrix_client: &Client, room_id: &OwnedRoomOrAliasId) -> anyhow::Result<Joined> {
let project = event.project();
let refname = event
.r#ref()
.split('/')
.last()
.unwrap_or_else(|| event.r#ref())
.to_string();
format!(
"*{}* {} **{}** [{}]({})",
project.path_with_namespace,
refname,
event.user(),
event.title(),
event.url()
)
}
async fn handle_gitlab_event(
event: GitlabEvent,
room_id: &OwnedRoomOrAliasId,
matrix_client: &Client,
) -> anyhow::Result<()> {
fn room_matches(a_room: &BaseRoom, our_room: &OwnedRoomOrAliasId) -> bool { fn room_matches(a_room: &BaseRoom, our_room: &OwnedRoomOrAliasId) -> bool {
let our_room_str = our_room.as_str(); let our_room_str = our_room.as_str();
a_room.room_id().as_str() == our_room_str a_room.room_id().as_str() == our_room_str
@ -116,15 +95,38 @@ async fn handle_gitlab_event(
.map(|room| room.clone()); .map(|room| room.clone());
} }
if let Some(room) = room { room.ok_or_else(|| anyhow!("Unable to join room {}", room_id))
let msg = build_gitlab_message(&event); }
debug!("Sending message to {}: {}", room_id, msg);
let msg_content = RoomMessageEventContent::text_markdown(&msg); fn build_gitlab_message(event: &GitlabEvent) -> String {
room.send(msg_content, None).await?; let project = event.project();
Ok(()) let refname = event
} else { .r#ref()
Err(anyhow!("Unable to join room {}", room_id)) .split('/')
} .last()
.unwrap_or_else(|| event.r#ref())
.to_string();
format!(
"*{}* {} **{}** [{}]({})",
project.path_with_namespace,
refname,
event.user(),
event.title(),
event.url()
)
}
async fn handle_gitlab_event(
event: GitlabEvent,
room_id: &OwnedRoomOrAliasId,
matrix_client: &Client,
) -> anyhow::Result<()> {
let room = ensure_matrix_room_joined(matrix_client, room_id).await?;
let msg = build_gitlab_message(&event);
debug!("Sending message to {}: {}", room_id, msg);
let msg_content = RoomMessageEventContent::text_markdown(&msg);
room.send(msg_content, None).await?;
Ok(())
} }
#[tokio::main] #[tokio::main]