Skip to content

Commit

Permalink
feat: Improved changelog display
Browse files Browse the repository at this point in the history
  • Loading branch information
MrExplode committed Nov 1, 2023
1 parent 88f891e commit 02fd170
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 33 deletions.
83 changes: 50 additions & 33 deletions src/jira.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ use actix_web::{
use chrono::Utc;
use serde::Deserialize;
use serde_json::json;
use webhook::models::Message;
use webhook::models::{Embed, Message};

use crate::{types::JiraData, Clients};
use crate::{
types::{JiraData, JiraIssue},
Clients,
};

const ENV_KEY: &str = "JIRA_TOKEN";

Expand Down Expand Up @@ -68,41 +71,55 @@ fn extract_event_name(event: &str) -> String {
fn create_message(data: JiraData) -> Message {
let root_url = root_url(&data.user.self_url);
let event_name = extract_event_name(&data.webhook_event);

let mut msg: Message = Message::new();
msg.username("Jira");
msg.embed(|embed| {
embed
.author(
&data.user.display_name,
None,
data.user.avatar_urls.get("48x48").cloned(),
)
.title(&event_name)
.url(&root_url);

if data.issue.is_some() {
let i = data.issue.as_ref().unwrap();
let f = &i.fields;
embed
.thumbnail(&f.issue_type.icon_url)
.description(
format!(
"[`{}`]({}) **{}**\n```\n{}\n```",
i.key, root_url, f.summary, f.description
)
.as_str(),
)

let mut embed = Embed::new();
embed
.author(
&data.user.display_name,
None,
data.user.avatar_urls.get("48x48").cloned(),
)
.title(&event_name)
.url(&root_url);

if data.issue.is_some() {
decorate_issue_embed(&mut embed, data, root_url);
}
msg.embeds.push(embed);
msg
}

fn decorate_issue_embed(e: &mut Embed, data: JiraData, root: String) {
let i = data.issue.unwrap();
let f = &i.fields;
e.footer(&f.project.name, f.project.avatar_urls.get("48x48").cloned());

match data.webhook_event.as_str() {
"jira:issue_created" => {
e.thumbnail(&f.issue_type.icon_url)
.description(format!("[`{}`]({}) **{}**\n```\n{}\n```", i.key, root, f.summary, f.description).as_str())
.field("Type", &f.issue_type.name, false)
.field("Priority", &f.priority.name, false)
.footer(&f.project.name, f.project.avatar_urls.get("48x48").cloned());
.color(ISSUE_CREATED);
}

match data.webhook_event.as_str() {
"jira:issue_created" => embed.color(ISSUE_CREATED),
"jira:issue_updated" => embed.color(ISSUE_UPDATED),
"jira:issue_deleted" => embed.color(ISSUE_DELETED),
_ => embed.color(UNSPECIFIED_EVENT),
"jira:issue_updated" => {
e.description(format!("[`{}`]({}) **{}**\n", i.key, root, f.summary).as_str())
.color(ISSUE_UPDATED);

for item in data.changelog.items {
e.field(&item.field, "", false)
.field("From", &item.from_string, true)
.field("To", &item.to_string, true);
}
}
});
msg
"jira:issue_deleted" => {
e.thumbnail(&f.issue_type.icon_url)
.description(format!("[`{}`]({}) **{}**\n", i.key, root, f.summary).as_str())
.color(ISSUE_DELETED);
}
_ => {}
}
}
16 changes: 16 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct JiraData {
pub user: JiraUser,
pub issue: Option<JiraIssue>,
pub issue_event_type_name: Option<String>,
pub changelog: JiraChangelog,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -74,3 +75,18 @@ pub struct JiraProject {
pub name: String,
pub avatar_urls: HashMap<String, String>,
}

#[derive(Deserialize)]
pub struct JiraChangelog {
pub id: String,
pub items: Vec<JiraChangelogItem>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JiraChangelogItem {
pub field: String,
pub field_id: String,
pub from_string: String,
pub to_string: String,
}

0 comments on commit 02fd170

Please sign in to comment.