chore: update everything to work with utoipa

This commit is contained in:
Zomatree
2025-11-13 23:06:41 +00:00
parent 27ea7345ea
commit ac60b2c795
148 changed files with 1200 additions and 1117 deletions

View File

@@ -1,5 +1,4 @@
use rocket::Route;
use revolt_rocket_okapi::revolt_okapi::openapi3::OpenApi;
mod webhook_delete;
mod webhook_delete_token;
@@ -10,8 +9,23 @@ mod webhook_fetch_token;
mod webhook_fetch;
mod webhook_execute_github;
pub fn routes() -> (Vec<Route>, OpenApi) {
openapi_get_routes_spec![
#[derive(OpenApi)]
#[openapi(
paths(
webhook_delete_token::webhook_delete_token,
webhook_delete::webhook_delete,
webhook_edit_token::webhook_edit_token,
webhook_edit::webhook_edit,
webhook_execute_github::webhook_execute_github,
webhook_execute::webhook_execute,
webhook_fetch_token::webhook_fetch_token,
webhook_fetch::webhook_fetch,
)
)]
pub struct ApiDoc;
pub fn routes() -> Vec<Route> {
routes![
webhook_delete_token::webhook_delete_token,
webhook_delete::webhook_delete,
webhook_edit_token::webhook_edit_token,

View File

@@ -10,7 +10,7 @@ use rocket_empty::EmptyResponse;
/// # Deletes a webhook
///
/// Deletes a webhook
#[openapi(tag = "Webhooks")]
#[utoipa::path(tag = "Webhooks")]
#[delete("/<webhook_id>")]
pub async fn webhook_delete(
db: &State<Database>,

View File

@@ -6,7 +6,7 @@ use rocket_empty::EmptyResponse;
/// # Deletes a webhook
///
/// Deletes a webhook with a token
#[openapi(tag = "Webhooks")]
#[utoipa::path(tag = "Webhooks")]
#[delete("/<webhook_id>/<token>")]
pub async fn webhook_delete_token(
db: &State<Database>,

View File

@@ -11,7 +11,7 @@ use validator::Validate;
/// # Edits a webhook
///
/// Edits a webhook
#[openapi(tag = "Webhooks")]
#[utoipa::path(tag = "Webhooks")]
#[patch("/<webhook_id>", data = "<data>")]
pub async fn webhook_edit(
db: &State<Database>,

View File

@@ -8,7 +8,7 @@ use rocket::{serde::json::Json, State};
/// # Edits a webhook
///
/// Edits a webhook with a token
#[openapi(tag = "Webhooks")]
#[utoipa::path(tag = "Webhooks")]
#[patch("/<webhook_id>/<token>", data = "<data>")]
pub async fn webhook_edit_token(
db: &State<Database>,

View File

@@ -13,7 +13,7 @@ use validator::Validate;
/// # Executes a webhook
///
/// Executes a webhook and sends a message
#[openapi(tag = "Webhooks")]
#[utoipa::path(tag = "Webhooks")]
#[post("/<webhook_id>/<token>", data = "<data>")]
pub async fn webhook_execute(
db: &State<Database>,

View File

@@ -1,19 +1,13 @@
use revolt_database::{util::reference::Reference, Database, Message, AMQP};
use revolt_models::v0::{MessageAuthor, SendableEmbed, Webhook};
use revolt_result::{create_error, Error, Result};
use revolt_rocket_okapi::{
gen::OpenApiGenerator,
request::{OpenApiFromRequest, RequestHeaderInput},
revolt_okapi::openapi3::{MediaType, Parameter, ParameterValue},
};
use rocket::{http::Status, request::FromRequest, Request, State};
use schemars::schema::SchemaObject;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use ulid::Ulid;
use validator::Validate;
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubUser {
name: Option<String>,
email: Option<String>,
@@ -35,19 +29,19 @@ pub struct GithubUser {
received_events_url: String,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubRepositorySecurityAndAnalysisStatus {
status: String,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubRepositorySecurityAndAnalysis {
advanced_security: GithubRepositorySecurityAndAnalysisStatus,
secret_scanning: GithubRepositorySecurityAndAnalysisStatus,
secret_scanning_push_protection: GithubRepositorySecurityAndAnalysisStatus,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubRepositoryLicense {
key: Option<String>,
name: Option<String>,
@@ -56,7 +50,7 @@ pub struct GithubRepositoryLicense {
node_id: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubRepositoryCodeOfConduct {
key: String,
name: String,
@@ -65,7 +59,7 @@ pub struct GithubRepositoryCodeOfConduct {
html_url: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubRepositoryPermissions {
admin: Option<bool>,
maintain: Option<bool>,
@@ -74,7 +68,7 @@ pub struct GithubRepositoryPermissions {
pull: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubRepository {
id: u32,
node_id: String,
@@ -165,7 +159,7 @@ pub struct GithubRepository {
security_and_analysis: Option<GithubRepositorySecurityAndAnalysis>,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct CommitAuthor {
date: Option<String>,
email: Option<String>,
@@ -173,7 +167,7 @@ pub struct CommitAuthor {
username: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubCommit {
added: Option<Vec<String>>,
author: CommitAuthor,
@@ -187,7 +181,7 @@ pub struct GithubCommit {
tree_id: String,
url: String,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubReactions {
#[serde(rename = "+1")]
plus_one: u32,
@@ -203,7 +197,7 @@ pub struct GithubReactions {
url: String,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubComment {
author_association: String,
body: String,
@@ -221,7 +215,7 @@ pub struct GithubComment {
user: GithubUser,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubDiscussionComment {
#[serde(flatten)]
comment: GithubComment,
@@ -230,7 +224,7 @@ pub struct GithubDiscussionComment {
repository_url: String,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubDiscussionCategory {
id: u32,
node_id: String,
@@ -244,7 +238,7 @@ pub struct GithubDiscussionCategory {
is_answerable: bool,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubDiscussion {
repository_url: String,
category: GithubDiscussionCategory,
@@ -269,7 +263,7 @@ pub struct GithubDiscussion {
timeline_url: String,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
#[serde(rename_all = "snake_case", tag = "action")]
#[allow(clippy::large_enum_variant)]
pub enum GithubDiscussionEvent {
@@ -282,7 +276,7 @@ pub enum GithubDiscussionEvent {
},
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
#[serde(rename_all = "snake_case", tag = "action")]
#[allow(clippy::large_enum_variant)]
pub enum DiscussionCommentEvent {
@@ -294,7 +288,7 @@ pub enum DiscussionCommentEvent {
Edited {},
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubMilestone {
url: String,
html_url: String,
@@ -314,7 +308,7 @@ pub struct GithubMilestone {
due_on: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubLinkedPullRequest {
merged_at: Option<Value>,
diff_url: Option<String>,
@@ -322,7 +316,7 @@ pub struct GithubLinkedPullRequest {
patch_url: Option<String>,
url: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubAppPermissions {
issues: Option<String>,
checks: Option<String>,
@@ -331,7 +325,7 @@ pub struct GithubAppPermissions {
deployments: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubApp {
id: u32,
slug: String,
@@ -352,7 +346,7 @@ pub struct GithubApp {
pem: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct GithubIssue {
id: u32,
node_id: String,
@@ -388,7 +382,7 @@ pub struct GithubIssue {
title: String,
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
#[serde(rename_all = "snake_case", tag = "action")]
#[allow(clippy::large_enum_variant)]
pub enum IssueCommentEvent {
@@ -400,7 +394,7 @@ pub enum IssueCommentEvent {
Edited {},
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
#[serde(rename_all = "snake_case", tag = "action")]
#[allow(clippy::large_enum_variant)]
pub enum IssuesEvent {
@@ -618,8 +612,12 @@ pub struct Event {
repository: GithubRepository,
}
#[derive(Debug, JsonSchema)]
pub struct EventHeader<'r>(pub &'r str);
#[derive(Debug, IntoParams)]
#[into_params(names("X-Github-Event"), parameter_in=Header)]
pub struct EventHeader<'r>(
/// The name of the github event
pub &'r str,
);
impl std::ops::Deref for EventHeader<'_> {
type Target = str;
@@ -646,40 +644,6 @@ impl<'r> FromRequest<'r> for EventHeader<'r> {
}
}
impl<'r> OpenApiFromRequest<'r> for EventHeader<'r> {
fn from_request_input(
_gen: &mut OpenApiGenerator,
_name: String,
_required: bool,
) -> revolt_rocket_okapi::Result<RequestHeaderInput> {
let mut content = schemars::Map::new();
content.insert(
"X-Github-Event".to_string(),
MediaType {
schema: Some(SchemaObject {
string: Some(Box::default()),
..Default::default()
}),
example: None,
examples: None,
encoding: schemars::Map::new(),
extensions: schemars::Map::new(),
},
);
Ok(RequestHeaderInput::Parameter(Parameter {
name: "X-Github-Event".to_string(),
location: "header".to_string(),
required: true,
description: Some("The name of the github event".to_string()),
deprecated: false,
allow_empty_value: false,
value: ParameterValue::Content { content },
extensions: schemars::Map::new(),
}))
}
}
const GREEN: &str = "#0f890f";
const RED: &str = "#e73e3e";
const GREY: &str = "#202224";
@@ -747,7 +711,7 @@ fn convert_event(data: &str, event_name: &str) -> Result<Event> {
/// # Executes a webhook specific to github
///
/// Executes a webhook specific to github and sends a message containing the relevant info about the event
#[openapi(tag = "Webhooks")]
#[utoipa::path(tag = "Webhooks", params(EventHeader))]
#[post("/<webhook_id>/<token>/github", data = "<data>")]
pub async fn webhook_execute_github(
db: &State<Database>,

View File

@@ -10,7 +10,7 @@ use rocket::{serde::json::Json, State};
/// # Gets a webhook
///
/// Gets a webhook
#[openapi(tag = "Webhooks")]
#[utoipa::path(tag = "Webhooks")]
#[get("/<webhook_id>")]
pub async fn webhook_fetch(
db: &State<Database>,

View File

@@ -6,7 +6,7 @@ use rocket::{serde::json::Json, State};
/// # Gets a webhook
///
/// Gets a webhook with a token
#[openapi(tag = "Webhooks")]
#[utoipa::path(tag = "Webhooks")]
#[get("/<webhook_id>/<token>")]
pub async fn webhook_fetch_token(
db: &State<Database>,