Add support for merge request events

This commit is contained in:
2023-09-15 12:21:02 -07:00
parent b1337d3599
commit 8edb8a6a4a
3 changed files with 293 additions and 7 deletions

View File

@ -6,17 +6,18 @@ pub trait GitlabEventExt {
fn title(&self) -> String;
}
#[derive(Debug, Deserialize)]
pub struct User {
name: String,
}
#[derive(Debug, Deserialize)]
pub struct Project {
pub id: u64,
pub name: String,
pub description: String,
pub web_url: String,
pub avatar_url: Option<String>,
pub git_ssh_url: String,
pub git_http_url: String,
pub namespace: String,
pub visibility_level: u32,
pub path_with_namespace: String,
pub default_branch: String,
pub homepage: String,
@ -31,9 +32,6 @@ pub struct Repository {
pub url: String,
pub description: String,
pub homepage: String,
pub git_http_url: String,
pub git_ssh_url: String,
pub visibility_level: u32,
}
#[derive(Debug, Deserialize)]
@ -43,6 +41,52 @@ pub struct Commit {
pub url: String,
}
#[derive(PartialEq, Debug, Deserialize)]
pub enum MergeRequestAction {
#[serde(rename = "open")]
Opened,
#[serde(rename = "close")]
Closed,
#[serde(rename = "reopen")]
Reopened,
#[serde(rename = "update")]
Updated,
#[serde(rename = "approved")]
Approved,
#[serde(rename = "unapproved")]
Unapproved,
#[serde(rename = "merge")]
Merged,
#[serde(other)]
Other,
}
impl MergeRequestAction {
pub fn as_str(&self) -> &str {
match self {
MergeRequestAction::Opened => "opened",
MergeRequestAction::Closed => "closed",
MergeRequestAction::Reopened => "reopened",
MergeRequestAction::Updated => "updated",
MergeRequestAction::Approved => "approved",
MergeRequestAction::Unapproved => "unapproved",
MergeRequestAction::Merged => "merged",
MergeRequestAction::Other => "other",
}
}
}
#[derive(Debug, Deserialize)]
pub struct ObjectAttributes {
pub target_branch: String,
pub source_branch: String,
pub title: String,
pub url: String,
pub source: Project,
pub target: Project,
pub action: MergeRequestAction,
}
#[derive(Debug, Deserialize)]
#[serde(tag = "object_kind")]
pub enum GitlabEvent {
@ -82,6 +126,13 @@ pub enum GitlabEvent {
commits: Vec<Commit>,
total_commits_count: u64,
},
#[serde(rename = "merge_request")]
MergeRequest {
user: User,
project: Project,
repository: Repository,
object_attributes: ObjectAttributes,
},
}
impl GitlabEventExt for GitlabEvent {
@ -89,6 +140,7 @@ impl GitlabEventExt for GitlabEvent {
match self {
GitlabEvent::Push { project, .. } => &project,
GitlabEvent::TagPush { project, .. } => &project,
GitlabEvent::MergeRequest { project, .. } => &project,
}
}
@ -96,6 +148,7 @@ impl GitlabEventExt for GitlabEvent {
match self {
GitlabEvent::Push { r#ref, .. } => &r#ref,
GitlabEvent::TagPush { r#ref, .. } => &r#ref,
GitlabEvent::MergeRequest { object_attributes, .. } => &object_attributes.target_branch,
}
}
@ -103,6 +156,7 @@ impl GitlabEventExt for GitlabEvent {
match self {
GitlabEvent::Push { user_name, .. } => &user_name,
GitlabEvent::TagPush { user_name, .. } => &user_name,
GitlabEvent::MergeRequest { user, .. } => &user.name,
}
}
@ -118,6 +172,7 @@ impl GitlabEventExt for GitlabEvent {
let refname = r#ref.split('/').into_iter().last().unwrap_or(checkout_sha);
format!("{}/-/tags/{}", project.web_url, refname)
}
GitlabEvent::MergeRequest { object_attributes, .. } => object_attributes.url.clone(),
};
url.replace("http://", "https://").to_string()
@ -137,6 +192,9 @@ impl GitlabEventExt for GitlabEvent {
} => find_commit(commits, &checkout_sha)
.map(|commit| commit.title.clone())
.unwrap_or_else(|| "New tag pushed".to_string()),
GitlabEvent::MergeRequest { object_attributes, .. } => {
format!("MR {}: {}", object_attributes.action.as_str(), object_attributes.title)
}
}
}
}
@ -221,4 +279,24 @@ mod test {
Ok(())
}
#[test]
pub fn parse_merge_request_event() -> anyhow::Result<()> {
let event = load_test_data("merge-request-event")?;
match event {
GitlabEvent::MergeRequest {
user,
object_attributes,
..
} => {
assert_eq!(user.name, "Administrator");
assert_eq!(object_attributes.action, MergeRequestAction::Opened);
assert_eq!(object_attributes.title, "MS-Viewport");
}
_ => panic!("not a merge request event"),
};
Ok(())
}
}

View File

@ -22,6 +22,8 @@ use matrix_sdk::{
};
use warp::Filter;
use crate::event::MergeRequestAction;
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 {
@ -125,6 +127,12 @@ async fn handle_gitlab_event(
room_id: &OwnedRoomOrAliasId,
matrix_client: &Client,
) -> anyhow::Result<()> {
if let GitlabEvent::MergeRequest { object_attributes, .. } = &event {
if object_attributes.action == MergeRequestAction::Other {
return Ok(());
}
}
let room = ensure_matrix_room_joined(matrix_client, room_id).await?;
let msg = build_gitlab_message(&event);
debug!("Sending message to {}: {}", room_id, msg);