-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5193 from systeminit/chore(sdf)-Migrate-module-bu…
…iltin-promotion-and-rejection-to-v2-endpoints chore(sdf): Migrate module builtin promotion and rejection to v2 endpoints
- Loading branch information
Showing
6 changed files
with
144 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
use axum::{ | ||
extract::{Host, OriginalUri, Path}, | ||
Json, | ||
}; | ||
use dal::{module::ModuleId, ChangeSetId, HistoryActor, User, WorkspacePk}; | ||
use module_index_client::ModuleIndexClient; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
extract::{AccessBuilder, HandlerContext, PosthogClient, RawAccessToken}, | ||
track, | ||
}; | ||
|
||
use super::{ModuleAPIResult, ModulesAPIError}; | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct PromoteToBuiltinModuleResponse { | ||
pub success: bool, | ||
} | ||
|
||
pub async fn promote( | ||
HandlerContext(builder): HandlerContext, | ||
AccessBuilder(request_ctx): AccessBuilder, | ||
RawAccessToken(raw_access_token): RawAccessToken, | ||
PosthogClient(posthog_client): PosthogClient, | ||
OriginalUri(original_uri): OriginalUri, | ||
Host(host_name): Host, | ||
Path((_workspace_pk, change_set_id, module_id)): Path<(WorkspacePk, ChangeSetId, ModuleId)>, | ||
) -> ModuleAPIResult<Json<PromoteToBuiltinModuleResponse>> { | ||
let ctx = builder | ||
.build(request_ctx.build(change_set_id.into())) | ||
.await?; | ||
|
||
let module_index_url = match ctx.module_index_url() { | ||
Some(url) => url, | ||
None => return Err(ModulesAPIError::ModuleIndexNotConfigured), | ||
}; | ||
|
||
let user = match ctx.history_actor() { | ||
HistoryActor::User(user_pk) => User::get_by_pk(&ctx, *user_pk).await?, | ||
_ => None, | ||
}; | ||
|
||
let (_, created_by_email) = user | ||
.map(|user| (user.name().to_owned(), user.email().to_owned())) | ||
.unwrap_or(( | ||
"unauthenticated user name".into(), | ||
"unauthenticated user email".into(), | ||
)); | ||
|
||
let module_index_client = | ||
ModuleIndexClient::new(module_index_url.try_into()?, &raw_access_token); | ||
|
||
module_index_client | ||
.promote_to_builtin(module_id.into(), created_by_email.clone()) | ||
.await?; | ||
|
||
track( | ||
&posthog_client, | ||
&ctx, | ||
&original_uri, | ||
&host_name, | ||
"promote_to_builtin", | ||
serde_json::json!({ | ||
"pkg_id": module_id, | ||
"pkg_promoted_to_builtin_by": created_by_email, | ||
}), | ||
); | ||
|
||
ctx.commit().await?; | ||
|
||
Ok(Json(PromoteToBuiltinModuleResponse { success: true })) | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct RejectModuleResponse { | ||
pub success: bool, | ||
} | ||
|
||
pub async fn reject( | ||
HandlerContext(builder): HandlerContext, | ||
AccessBuilder(request_ctx): AccessBuilder, | ||
RawAccessToken(raw_access_token): RawAccessToken, | ||
PosthogClient(posthog_client): PosthogClient, | ||
OriginalUri(original_uri): OriginalUri, | ||
Host(host_name): Host, | ||
Path((_workspace_pk, change_set_id, module_id)): Path<(WorkspacePk, ChangeSetId, ModuleId)>, | ||
) -> ModuleAPIResult<Json<RejectModuleResponse>> { | ||
let ctx = builder | ||
.build(request_ctx.build(change_set_id.into())) | ||
.await?; | ||
|
||
let module_index_url = match ctx.module_index_url() { | ||
Some(url) => url, | ||
None => return Err(ModulesAPIError::ModuleIndexNotConfigured), | ||
}; | ||
|
||
let user = match ctx.history_actor() { | ||
HistoryActor::User(user_pk) => User::get_by_pk(&ctx, *user_pk).await?, | ||
_ => None, | ||
}; | ||
|
||
let (_, created_by_email) = user | ||
.map(|user| (user.name().to_owned(), user.email().to_owned())) | ||
.unwrap_or(( | ||
"unauthenticated user name".into(), | ||
"unauthenticated user email".into(), | ||
)); | ||
|
||
let module_index_client = | ||
ModuleIndexClient::new(module_index_url.try_into()?, &raw_access_token); | ||
|
||
module_index_client | ||
.reject_module(module_id.into(), created_by_email.clone()) | ||
.await?; | ||
|
||
track( | ||
&posthog_client, | ||
&ctx, | ||
&original_uri, | ||
&host_name, | ||
"reject_pkg", | ||
serde_json::json!({ | ||
"pkg_id": module_id, | ||
"pkg_rejected_by": created_by_email, | ||
}), | ||
); | ||
|
||
ctx.commit().await?; | ||
|
||
Ok(Json(RejectModuleResponse { success: true })) | ||
} |