|
- use crate::gitea::{get_releases, get_issue_by_index};
- use crate::discord::{Interaction, InteractionResponse, InteractionApplicationCommandCallbackData, Embed, EmbedFooter, EmbedAuthor, EmbedField, CommandValue};
-
- pub fn gitea_release(interaction: &Interaction) -> InteractionResponse {
- let cmd = interaction.cmd().unwrap();
- let mut username = String::new();
- let mut repo_name = String::new();
- for opt in &cmd.data.options.unwrap() {
- match &opt.name as &str {
- "username" => username = opt.value.clone().unwrap().to_string(),
- "repo" => repo_name = opt.value.clone().unwrap().to_string(),
- _ => {}
- }
- }
- let res = get_releases(&username, &repo_name);
- if let Ok(resp) = res {
- if resp.is_empty() {
- // no release in this repo
- return InteractionResponse::ChannelMessage {
- data: Some(InteractionApplicationCommandCallbackData {
- tts: false,
- content: format!("No releases found for {}'s {} repository", &username, &repo_name),
- embeds: None,
- allowed_mentions: None,
- flags: None,
- })
- }
- }
- // releases found, use most recent (0th index)
- let release = &resp[0];
- // build downloads field
- let mut asset_str = String::new();
- if let Some(assets) = &release.assets {
- for f in assets {
- asset_str += &format!("[{}]({})\n", f.name, f.browser_download_url);
- }
- }
- // limit description to 2000 characters
- let mut desc = release.body.clone();
- if desc.len() > 2000 {
- desc = desc[..2000].to_string() + "...";
- }
- // assemble embed
- let embed = Embed {
- title: Some(format!("{} {}", &repo_name, &release.tag_name)),
- type_: None,
- description: Some(desc),
- url: None,
- timestamp: None,
- color: Some(0x00C800), // Colour::from_rgb(0, 200, 0)
- footer: Some(EmbedFooter {
- text: release.author.login.clone(),
- icon_url: Some(release.author.avatar_url.clone()),
- proxy_icon_url: None
- }),
- image: None,
- thumbnail: None,
- video: None,
- provider: None,
- author: Some(EmbedAuthor {
- name: Some(release.name.clone()),
- url: Some(format!("https://git.exmods.org/{}/{}", &username, &repo_name)),
- icon_url: None,
- proxy_icon_url: None
- }),
- fields: Some(vec![
- EmbedField {
- name: "Download".to_string(),
- value: asset_str,
- inline: Some(true)
- }
- ])
- };
- return InteractionResponse::ChannelMessageWithSource {
- data: Some(InteractionApplicationCommandCallbackData {
- tts: false,
- content: "".to_string(),
- embeds: Some(vec![embed]),
- allowed_mentions: None,
- flags: None,
- })
- }
- } else {
- return InteractionResponse::ChannelMessageWithSource {
- data: Some(InteractionApplicationCommandCallbackData {
- tts: false,
- content: format!("Gitea error: `{}`", res.err().unwrap()),
- embeds: None,
- allowed_mentions: None,
- flags: None,
- })
- }
- }
- }
-
- pub fn gitea_issue(interaction: &Interaction) -> InteractionResponse {
- let cmd = interaction.cmd().unwrap();
- // these should always be populated, but Rust doesn't know that
- let mut username = String::new();
- let mut repo_name = String::new();
- let mut index = 0;
- for opt in &cmd.data.options.unwrap() {
- match &opt.name as &str {
- "username" => {
- if let CommandValue::StrVal(v) = opt.value.clone().unwrap() {
- username = v;
- }
- },
- "repo" => {
- if let CommandValue::StrVal(v) = opt.value.clone().unwrap() {
- repo_name = v;
- }
- },
- "issue" => {
- if let CommandValue::IntVal(v) = opt.value.clone().unwrap() {
- index = v;
- }
- }
- _ => {}
- }
- }
- let res = get_issue_by_index(&username, &repo_name, index);
- if let Ok(resp) = res {
- // limit description to 2000 characters
- let mut desc = resp.body.clone();
- if desc.len() > 2000 {
- desc = desc[..2000].to_string() + "...";
- }
- // build tags field
- let mut tags = Vec::new();
- for t in resp.labels {
- tags.push(format!("{}", &t.name));
- }
- let tag_str = if tags.is_empty() {
- "<none>".to_string()
- } else {
- tags.join(", ")
- };
- // build embed
- let embed = Embed {
- title: Some(format!("Issue #{}", index)),
- type_: None,
- description: Some(desc),
- url: None,
- timestamp: None,
- color: Some(0x00C800), // Colour::from_rgb(0, 200, 0)
- footer: Some(EmbedFooter {
- text: resp.user.login.clone(),
- icon_url: Some(resp.user.avatar_url.clone()),
- proxy_icon_url: None
- }),
- image: None,
- thumbnail: None,
- video: None,
- provider: None,
- author: Some(EmbedAuthor {
- name: Some(resp.title.clone()),
- url: Some(resp.html_url),
- icon_url: None,
- proxy_icon_url: None
- }),
- //fields: None,
- fields: Some(vec![
- EmbedField {
- name: "Labels".to_string(),
- value: tag_str,
- inline: Some(true)
- },
- EmbedField {
- name: "Repository".to_string(),
- value: format!("[{}](https://git.exmods.org/{}/{})", resp.repository.name, &username, &repo_name),
- inline: Some(true)
- },
- ])
- };
- return InteractionResponse::ChannelMessageWithSource {
- data: Some(InteractionApplicationCommandCallbackData {
- tts: false,
- content: "".to_string(),
- embeds: Some(vec![embed]),
- allowed_mentions: None,
- flags: None,
- })
- }
- } else {
- return InteractionResponse::ChannelMessageWithSource {
- data: Some(InteractionApplicationCommandCallbackData {
- tts: false,
- content: format!("Gitea error: `{}`", res.err().unwrap()),
- embeds: None,
- allowed_mentions: None,
- flags: None,
- })
- }
- }
- }
|