Add support for pipeline events

This commit is contained in:
2023-09-15 12:51:33 -07:00
parent 8edb8a6a4a
commit 3a2da3fba8
3 changed files with 342 additions and 9 deletions

View File

@ -20,10 +20,6 @@ pub struct Project {
pub namespace: String,
pub path_with_namespace: String,
pub default_branch: String,
pub homepage: String,
pub url: String,
pub ssh_url: String,
pub http_url: String,
}
#[derive(Debug, Deserialize)]
@ -31,7 +27,6 @@ pub struct Repository {
pub name: String,
pub url: String,
pub description: String,
pub homepage: String,
}
#[derive(Debug, Deserialize)]
@ -77,7 +72,7 @@ impl MergeRequestAction {
}
#[derive(Debug, Deserialize)]
pub struct ObjectAttributes {
pub struct MergeRequestObjectAttributes {
pub target_branch: String,
pub source_branch: String,
pub title: String,
@ -87,6 +82,36 @@ pub struct ObjectAttributes {
pub action: MergeRequestAction,
}
#[derive(PartialEq, Debug, Deserialize)]
pub enum PipelineStatus {
#[serde(rename = "failed")]
Failed,
#[serde(other)]
Other,
}
impl PipelineStatus {
pub fn as_str(&self) -> &str {
match self {
PipelineStatus::Failed => "failed",
PipelineStatus::Other => "other",
}
}
}
#[derive(Debug, Deserialize)]
pub struct PipelineObjectAttributes {
pub name: Option<String>,
pub r#ref: String,
pub status: PipelineStatus,
pub url: String,
}
#[derive(Debug, Deserialize)]
pub struct PipelineMergeRequest {
pub title: String,
}
#[derive(Debug, Deserialize)]
#[serde(tag = "object_kind")]
pub enum GitlabEvent {
@ -131,7 +156,14 @@ pub enum GitlabEvent {
user: User,
project: Project,
repository: Repository,
object_attributes: ObjectAttributes,
object_attributes: MergeRequestObjectAttributes,
},
#[serde(rename = "pipeline")]
Pipeline {
object_attributes: PipelineObjectAttributes,
merge_request: PipelineMergeRequest,
user: User,
project: Project,
},
}
@ -141,6 +173,7 @@ impl GitlabEventExt for GitlabEvent {
GitlabEvent::Push { project, .. } => &project,
GitlabEvent::TagPush { project, .. } => &project,
GitlabEvent::MergeRequest { project, .. } => &project,
GitlabEvent::Pipeline { project, .. } => &project,
}
}
@ -149,6 +182,7 @@ impl GitlabEventExt for GitlabEvent {
GitlabEvent::Push { r#ref, .. } => &r#ref,
GitlabEvent::TagPush { r#ref, .. } => &r#ref,
GitlabEvent::MergeRequest { object_attributes, .. } => &object_attributes.target_branch,
GitlabEvent::Pipeline { object_attributes, .. } => &object_attributes.r#ref,
}
}
@ -157,6 +191,7 @@ impl GitlabEventExt for GitlabEvent {
GitlabEvent::Push { user_name, .. } => &user_name,
GitlabEvent::TagPush { user_name, .. } => &user_name,
GitlabEvent::MergeRequest { user, .. } => &user.name,
GitlabEvent::Pipeline { user, .. } => &user.name,
}
}
@ -173,6 +208,7 @@ impl GitlabEventExt for GitlabEvent {
format!("{}/-/tags/{}", project.web_url, refname)
}
GitlabEvent::MergeRequest { object_attributes, .. } => object_attributes.url.clone(),
GitlabEvent::Pipeline { object_attributes, .. } => object_attributes.url.clone(),
};
url.replace("http://", "https://").to_string()
@ -195,6 +231,14 @@ impl GitlabEventExt for GitlabEvent {
GitlabEvent::MergeRequest { object_attributes, .. } => {
format!("MR {}: {}", object_attributes.action.as_str(), object_attributes.title)
}
GitlabEvent::Pipeline {
object_attributes,
merge_request,
..
} => {
let title = object_attributes.name.as_ref().unwrap_or(&merge_request.title);
format!("Pipeline {}: {}", object_attributes.status.as_str(), title)
}
}
}
}
@ -299,4 +343,27 @@ mod test {
Ok(())
}
#[test]
pub fn parse_pipeline_event() -> anyhow::Result<()> {
let event = load_test_data("pipeline-event")?;
match event {
GitlabEvent::Pipeline {
object_attributes,
merge_request,
user,
..
} => {
assert_eq!(object_attributes.name, Some("Pipeline for branch: master".to_string()));
assert_eq!(object_attributes.r#ref, "master");
assert_eq!(object_attributes.status, PipelineStatus::Failed);
assert_eq!(merge_request.title, "Test");
assert_eq!(user.name, "Administrator");
}
_ => panic!("not a pipeline event"),
};
Ok(())
}
}

View File

@ -22,7 +22,7 @@ use matrix_sdk::{
};
use warp::Filter;
use crate::event::MergeRequestAction;
use crate::event::{MergeRequestAction, PipelineStatus};
async fn build_sync_settings(matrix_client: &Client) -> SyncSettings {
let mut settings = SyncSettings::default().timeout(Duration::from_secs(30));
@ -131,6 +131,10 @@ async fn handle_gitlab_event(
if object_attributes.action == MergeRequestAction::Other {
return Ok(());
}
} else if let GitlabEvent::Pipeline { object_attributes, .. } = &event {
if object_attributes.status == PipelineStatus::Other {
return Ok(());
}
}
let room = ensure_matrix_room_joined(matrix_client, room_id).await?;
@ -168,7 +172,7 @@ async fn run() -> anyhow::Result<()> {
async move {
let project = event.project();
let config_key = project.homepage.replace("http://", "").replace("https://", "");
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);